use of com.developmentontheedge.sql.model.SimpleNode 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.SimpleNode in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method applyPlaceHolder.
private void applyPlaceHolder(AstBePlaceHolder placeholderNode) {
String ph = placeholderNode.getPlaceHolder();
SimpleNode replacement;
if (placeholderNode instanceof AstBeListPlaceHolder) {
AstInValueList list = new AstInValueList(SqlParserTreeConstants.JJTINVALUELIST);
context.roles().map(AstStringConstant::new).forEach(list::addChild);
replacement = list;
} else {
String rawResult;
switch(ph) {
case "username":
rawResult = context.getUserName();
break;
case "timestamp":
rawResult = String.valueOf(System.currentTimeMillis());
break;
default:
throw new UnsupportedOperationException("Unsupported placeholder: " + ph);
}
replacement = new AstStringConstant(rawResult);
}
replacement.inheritFrom(placeholderNode);
placeholderNode.replaceWith(replacement);
}
use of com.developmentontheedge.sql.model.SimpleNode in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method applyVar.
private void applyVar(String key, AstBeSqlVar varNode, Function<String, String> varResolver) {
String value = varResolver.apply(context.getSubQueries().get(key).translateVar(varNode.getName()));
if (value == null)
value = varNode.getDefault();
SimpleNode constant;
if (varNode.jjtGetParent() instanceof AstBeSqlSubQuery && value != null && !"".equals(value.trim())) {
constant = SqlQuery.parse(value).getQuery();
} else {
if (value == null) {
value = "null";
}
constant = varNode.jjtGetParent() instanceof AstStringConstant ? new AstStringPart(value) : new AstIdentifierConstant(value);
}
varNode.replaceWith(constant);
}
use of com.developmentontheedge.sql.model.SimpleNode in project be5 by DevelopmentOnTheEdge.
the class DB2Transformer method transformDateAdd.
@Override
protected void transformDateAdd(AstFunNode node) {
Function opPlus = DefaultParserContext.FUNC_PLUS;
Function opTimes = DefaultParserContext.FUNC_TIMES;
SimpleNode date = node.child(0);
SimpleNode number = node.child(1);
String name = node.getFunction().getName();
String type = name.equalsIgnoreCase("add_months") ? "MONTHS" : name.equalsIgnoreCase("add_days") ? "DAYS" : "MICROSECONDS";
if (type.equals("MICROSECONDS"))
number = new AstParenthesis(opTimes.node(number, AstNumericConstant.of(1000)));
node.replaceWith(new AstParenthesis(opPlus.node(date, new AstFirstDayOf(number, type))));
}
use of com.developmentontheedge.sql.model.SimpleNode in project be5 by DevelopmentOnTheEdge.
the class DB2Transformer method transformSelect.
@Override
protected void transformSelect(AstSelect select) {
AstLimit limit = select.getLimit();
if (limit != null) {
if (limit.getOffset() == 0)
limit.setShape("FETCH FIRST", "ROWS ONLY");
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);
}
Aggregations