use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class OracleTransformer method transformYearMonthDay.
@Override
protected void transformYearMonthDay(SimpleNode node) {
String dateField = "";
String name = node instanceof AstFunNode ? ((AstFunNode) node).getFunction().getName() : ((AstExtract) node).getDateField();
if ("year".equalsIgnoreCase(name))
dateField = "YYYY";
if ("month".equalsIgnoreCase(name))
dateField = "MM";
if ("day".equalsIgnoreCase(name))
dateField = "DD";
node.replaceWith(parserContext.getFunction("to_number").node(parserContext.getFunction("to_char").node(node.child(0), new AstStringConstant(dateField))));
}
use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class OracleTransformer method transformSelect.
@Override
protected void transformSelect(AstSelect select) {
super.transformSelect(select);
AstLimit limit = select.getLimit();
if (limit != null) {
select.dropLimit();
SimpleNode parent = select.jjtGetParent();
int idx = parent.indexOf(select);
AstTableRef tableRef = new AstTableRef(select);
AstFrom from = new AstFrom(tableRef);
AstWhere where = new AstWhere();
AstFunNode less = parserContext.getFunction("<=").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getLimit() + limit.getOffset()));
where.addChild(less);
if (limit.getOffset() == 0) {
AstSelect newSelect = new AstSelect(new AstSelectList(), from, where);
parent.jjtAddChild(newSelect, idx);
} else {
AstSelectList list = new AstSelectList();
list.addChild(new AstFieldReference(new AstIdentifierConstant("tmp"), new AstIdentifierConstant("*,")));
list.addChild(new AstIdentifierConstant("ROWNUM rn"));
tableRef.addChild(new AstIdentifierConstant("tmp"));
AstSelect innerSelect = new AstSelect(list, from, where);
AstWhere outerWhere = new AstWhere();
AstFunNode more = parserContext.getFunction(">").node(new AstIdentifierConstant("ROWNUM"), AstNumericConstant.of(limit.getOffset()));
outerWhere.addChild(more);
AstSelect outerSelect = new AstSelect(select.getSelectList(), new AstFrom(new AstTableRef(innerSelect)), outerWhere);
parent.jjtAddChild(outerSelect, idx);
}
}
}
use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class OracleTransformer method transformStringAgg.
@Override
protected void transformStringAgg(AstFunNode node) {
node.setFunction(LISTAGG);
if (node.isDistinct())
throw new IllegalStateException("DISTINCT clause is unsupported for " + node.getFunction().getName());
AstOrderBy orderBy;
if (node.child(node.jjtGetNumChildren() - 1) instanceof AstOrderBy) {
orderBy = (AstOrderBy) node.child(node.jjtGetNumChildren() - 1);
node.removeChild(node.jjtGetNumChildren() - 1);
} else {
orderBy = new AstOrderBy(new AstOrderingElement(node.child(0)));
}
node.replaceWith(new AstOrderedSetAggregate((AstFunNode) node.clone(), new AstWithinGroup(orderBy)));
}
use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class SqlServerTransformer method transformSelect.
@Override
protected void transformSelect(AstSelect select) {
AstLimit limit = select.getLimit();
if (limit != null) {
if (limit.getOffset() == 0) {
limit.setShape("TOP", null);
select.moveToFront(limit);
} else {
if (select.getOrderBy() == null)
throw new IllegalStateException("The ranking function \"ROW_NUMBER\" must have an ORDER BY clause");
select.dropLimit();
SimpleNode parent = select.jjtGetParent();
int idx = parent.indexOf(select);
AstIdentifierConstant tmp = new AstIdentifierConstant("tmp");
AstIdentifierConstant rn = new AstIdentifierConstant("rn");
AstSelectList list = select.getSelectList();
AstTableRef tableRef = new AstTableRef(select);
tableRef.setAsToken(true);
tableRef.addChild(tmp);
AstFrom from = new AstFrom(tableRef);
AstWhere where = new AstWhere();
AstBetweenPredicate between = new AstBetweenPredicate(new AstFieldReference(tmp, rn), AstNumericConstant.of(limit.getOffset()), AstNumericConstant.of(limit.getOffset() + limit.getLimit()));
where.addChild(between);
AstSelect newSelect = new AstSelect((AstSelectList) list.clone(), from, where);
AstFunNode func = parserContext.getFunction("row_number").node();
AstDerivedColumn derCol = new AstDerivedColumn(new AstWindowFunction(func, new AstWindowSpecification(select.getOrderBy())));
select.dropOrder();
derCol.setPrefixComma(true);
derCol.setAsToken(true);
derCol.addChild(rn);
if (list.isAllColumns()) {
for (AstTableRef tr : select.getFrom().tableRefs()) {
String tableName = tr.getAlias() == null ? tr.getTable() : tr.getAlias();
list.addChild(new AstFieldReference(new AstIdentifierConstant(tableName), new AstIdentifierConstant("*")));
}
}
list.addChild(derCol);
parent.jjtAddChild(newSelect, idx);
}
}
super.transformSelect(select);
}
use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class SqlServerTransformer method getDateTimeDiff.
@Override
protected SimpleNode getDateTimeDiff(SimpleNode startDate, SimpleNode endDate, String format) {
int dp;
switch(format) {
case "YEAR":
format = "yy";
dp = 5;
break;
case "MONTH":
format = "mm";
dp = 2;
break;
case "DAY":
return DATEDIFF.node(new AstIdentifierConstant("dd"), startDate, endDate);
case "HOUR":
format = "hh";
dp = 9;
break;
case "MINUTE":
format = "mi";
dp = 6;
break;
case "SECOND":
format = "ss";
dp = 3;
break;
default:
throw new IllegalStateException("Unsupported value for datepart in TIMESTAMPDIFF: " + format);
}
AstFunNode dateDiff = DATEDIFF.node(new AstIdentifierConstant(format), startDate, endDate);
AstFunNode cond = parserContext.getFunction("<=").node(getDatePart(startDate, dp, format), getDatePart(endDate, dp, format));
return new AstCase(new AstWhen(cond, dateDiff), new AstCaseElse(DefaultParserContext.FUNC_MINUS.node(dateDiff, AstNumericConstant.of(1))));
}
Aggregations