use of com.developmentontheedge.sql.model.SimpleNode 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.SimpleNode 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);
}
use of com.developmentontheedge.sql.model.SimpleNode in project be5 by DevelopmentOnTheEdge.
the class GenericDbmsTransformer method recursiveProcessing.
private void recursiveProcessing(SimpleNode node) {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
SimpleNode child = node.child(i);
if (child instanceof AstFunNode) {
transformFunction((AstFunNode) child);
}
if (child instanceof AstSelect) {
transformSelect((AstSelect) child);
}
if (child instanceof AstIdentifierConstant) {
transformIdentifier((AstIdentifierConstant) child);
}
if (child instanceof AstStringConstant) {
transformString((AstStringConstant) child);
}
if (child instanceof AstCast) {
transformCastExpression((AstCast) child);
}
if (child instanceof AstExtract) {
transformExtractExpression((AstExtract) child);
}
if (child instanceof AstPosition) {
transformPosition((AstPosition) child);
}
if (child instanceof AstInterval) {
transformInterval((AstInterval) child);
}
if (child instanceof AstWith) {
transformWith((AstWith) child);
}
if (child instanceof AstExcept) {
transformExcept((AstExcept) child);
}
recursiveProcessing(child);
}
}
use of com.developmentontheedge.sql.model.SimpleNode in project be5 by DevelopmentOnTheEdge.
the class GenericDbmsTransformer method expandDbArguments.
private void expandDbArguments(SimpleNode node, Dbms dbms) {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
SimpleNode child = node.child(i);
expandDbArguments(child, dbms);
if (child instanceof AstFunNode && DbSpecificFunction.needsTransformation(dbms).test(((AstFunNode) child).getFunction()))
expandDbFunction((AstFunNode) child, dbms);
}
}
use of com.developmentontheedge.sql.model.SimpleNode in project be5 by DevelopmentOnTheEdge.
the class GenericDbmsTransformer method transformCastExpression.
protected void transformCastExpression(AstCast cast) {
SimpleNode diff = cast.child(0);
// Detect date_trunc("MONTH", x) + (1 MONTH)::interval - (1 DAY)::interval and extract x
Predicate<SimpleNode> truncMonth = AstFunNode.isFunction("date_trunc", AstStringConstant.isString("MONTH"), x -> true);
Predicate<SimpleNode> truncMonthPlusMonth = AstFunNode.isFunction("+", truncMonth, AstInterval.isInterval("1 MONTH"));
Predicate<SimpleNode> truncMonthPlusMonthMinusDay = AstFunNode.isFunction("-", truncMonthPlusMonth, AstInterval.isInterval("1 DAY"));
if ("DATE".equals(cast.getDataType()) && truncMonthPlusMonthMinusDay.test(diff))
transformLastDayPostgres(cast, diff.child(0).child(0).child(1));
else
transformCast(cast);
}
Aggregations