Search in sources :

Example 26 with SimpleNode

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);
}
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 27 with SimpleNode

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);
}
Also used : AstBeListPlaceHolder(com.developmentontheedge.sql.model.AstBeListPlaceHolder) AstInValueList(com.developmentontheedge.sql.model.AstInValueList) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 28 with SimpleNode

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);
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 29 with SimpleNode

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))));
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstFirstDayOf(com.developmentontheedge.sql.model.AstFirstDayOf)

Example 30 with SimpleNode

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);
}
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)

Aggregations

SimpleNode (com.developmentontheedge.sql.model.SimpleNode)39 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)12 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)11 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)10 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)10 Function (com.developmentontheedge.sql.model.Function)9 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)9 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)7 AstFrom (com.developmentontheedge.sql.model.AstFrom)5 AstInterval (com.developmentontheedge.sql.model.AstInterval)5 AstSelect (com.developmentontheedge.sql.model.AstSelect)5 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)5 AstCase (com.developmentontheedge.sql.model.AstCase)4 AstCaseElse (com.developmentontheedge.sql.model.AstCaseElse)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)4 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)4 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)4 AstCast (com.developmentontheedge.sql.model.AstCast)3 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)3 AstLimit (com.developmentontheedge.sql.model.AstLimit)3