use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class SqlServerTransformer method transformString.
@Override
protected void transformString(AstStringConstant string) {
for (AstStringPart child : string.children().select(AstStringPart.class)) {
String content = child.getContent().replace("\\'", "''");
child.setContent(string.isEscape() ? content.replaceAll("\\\\([^bfnrt])", "$1") : content, true);
}
if (string.isEscape()) {
string.setEscape(false);
AstFunNode concat = DefaultParserContext.FUNC_PLUS.node();
if (replacedEscapes(string, concat))
string.replaceWith(concat);
}
}
use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class DB2Transformer 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());
if (node.child(node.jjtGetNumChildren() - 1) instanceof AstOrderBy) {
AstOrderBy orderBy = (AstOrderBy) node.child(node.jjtGetNumChildren() - 1);
node.removeChild(node.jjtGetNumChildren() - 1);
node.replaceWith(new AstOrderedSetAggregate((AstFunNode) node.clone(), new AstWithinGroup(orderBy)));
}
}
use of com.developmentontheedge.sql.model.AstFunNode 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);
}
use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class FilterApplier method addWhere.
private void addWhere(AstWhere where, Map<ColumnRef, Object> conditions) {
if (where.jjtGetNumChildren() != 0) {
if (!AstFunNode.isFunction(DefaultParserContext.AND_LIT).test(where.child(0))) {
AstFunNode and = DefaultParserContext.FUNC_AND.node();
for (SimpleNode child : where.children()) and.addChild(child instanceof AstBooleanExpression ? new AstParenthesis(child) : child);
where.removeChildren();
where.addChild(and);
}
setConditions(where.child(0), conditions);
} else if (conditions.size() > 1) {
where.addChild(DefaultParserContext.FUNC_AND.node());
setConditions(where.child(0), conditions);
} else
setConditions(where, conditions);
}
use of com.developmentontheedge.sql.model.AstFunNode in project be5 by DevelopmentOnTheEdge.
the class GenericDbmsTransformer method transformExtractExpression.
protected void transformExtractExpression(AstExtract extract) {
String dateField = extract.getDateField().toUpperCase();
SimpleNode child = extract.child(0);
if (AstFunNode.isFunction("age").test(child)) {
SimpleNode parent = extract.jjtGetParent();
if ("YEAR".equals(dateField)) {
SimpleNode grandParent = parent.jjtGetParent();
SimpleNode toReplace = extract;
if (AstFunNode.isFunction("*").test(parent) && AstFunNode.isFunction("+").test(grandParent) && AstExtract.isExtract("MONTH").test(grandParent.other(parent))) {
dateField = "MONTH";
toReplace = parent.jjtGetParent();
}
toReplace.replaceWith(getDateTimeDiff(child.child(1), child.child(0), dateField));
} else if ("MONTH".equals(dateField) && AstFunNode.isFunction("+").test(parent) && AstFunNode.isFunction("*").test(parent.child(1)) && parent.child(1).children().anyMatch(AstExtract.isExtract("YEAR"))) {
parent.jjtGetParent().replaceWith(getDateTimeDiff(child.child(1), child.child(0), dateField));
}
} else if (child instanceof AstParenthesis && AstFunNode.isFunction("-").test(child.child(0)) && ("DAY".equals(dateField) || "EPOCH".equals(dateField))) {
AstFunNode date = (AstFunNode) child.child(0);
extract.replaceWith(getDateTimeDiff(date.child(1), date.child(0), "EPOCH".equals(dateField) ? "SECOND" : "DAY"));
} else
transformExtract(extract);
}
Aggregations