Search in sources :

Example 11 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant 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);
        }
    }
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 12 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant in project be5 by DevelopmentOnTheEdge.

the class OrderByFilter method apply.

public void apply(AstQuery query, Map<String, String> columns) {
    AstOrderBy orderBy = new AstOrderBy();
    apply(columns, query, orderBy);
    query.children().select(AstSelect.class).forEach(AstSelect::dropOrder);
    AstSelect select;
    if (query.jjtGetNumChildren() == 1)
        select = (AstSelect) query.child(0);
    else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        query.replaceWith(new AstQuery(select));
    }
    select.addChild(orderBy);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Example 13 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant in project be5 by DevelopmentOnTheEdge.

the class Simplifier method simplifyNot.

private static void simplifyNot(AstBooleanNot node) {
    node.children().toList().forEach(Simplifier::simplifyBoolean);
    if (node.jjtGetNumChildren() == 1) {
        SimpleNode child = node.child(0);
        String childStr = child.format().trim();
        if (childStr.equals("TRUE"))
            child.replaceWith(new AstIdentifierConstant("FALSE"));
        else if (childStr.equals("FALSE"))
            child.replaceWith(new AstIdentifierConstant("TRUE"));
    }
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 14 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant 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);
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstWindowSpecification(com.developmentontheedge.sql.model.AstWindowSpecification) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstFieldReference(com.developmentontheedge.sql.model.AstFieldReference) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstBetweenPredicate(com.developmentontheedge.sql.model.AstBetweenPredicate)

Example 15 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant 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))));
}
Also used : AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstWhen(com.developmentontheedge.sql.model.AstWhen) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstCase(com.developmentontheedge.sql.model.AstCase)

Aggregations

AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)19 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)11 AstSelect (com.developmentontheedge.sql.model.AstSelect)10 AstFrom (com.developmentontheedge.sql.model.AstFrom)8 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)8 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)8 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)6 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)5 AstQuery (com.developmentontheedge.sql.model.AstQuery)5 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)5 AstWhere (com.developmentontheedge.sql.model.AstWhere)5 AstLimit (com.developmentontheedge.sql.model.AstLimit)4 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)2 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)2 AstWindowSpecification (com.developmentontheedge.sql.model.AstWindowSpecification)2 Function (com.developmentontheedge.sql.model.Function)2 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)2