Search in sources :

Example 16 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant 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 17 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant 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)

Example 18 with AstIdentifierConstant

use of com.developmentontheedge.sql.model.AstIdentifierConstant 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);
    }
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstCast(com.developmentontheedge.sql.model.AstCast) AstPosition(com.developmentontheedge.sql.model.AstPosition) AstWith(com.developmentontheedge.sql.model.AstWith) AstExtract(com.developmentontheedge.sql.model.AstExtract) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstExcept(com.developmentontheedge.sql.model.AstExcept) AstInterval(com.developmentontheedge.sql.model.AstInterval) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 19 with AstIdentifierConstant

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

the class ContextApplier method applyParameters.

private SimpleNode applyParameters(AstBeParameterTag paramNode, String value, boolean tableRefAddend) {
    String defValue = paramNode.getDefValue() != null ? paramNode.getDefValue() : "";
    String prefix = paramNode.getPrefix() != null ? paramNode.getPrefix() : "";
    String postfix = paramNode.getPostfix() != null ? paramNode.getPostfix() : "";
    String regex = paramNode.getRegex();
    String repl = paramNode.getReplacement();
    String changeCase = paramNode.getCase();
    String type = paramNode.getType();
    boolean safeStr = "yes".equals(paramNode.getSafeStr()) && !tableRefAddend;
    if (value == null)
        value = "";
    if (value.equals(""))
        value = defValue;
    if (regex != null && repl != null)
        value = value.replaceAll(regex, repl);
    if ("upper".equals(changeCase))
        value = value.toUpperCase();
    if ("lower".equals(changeCase))
        value = value.toLowerCase();
    if ("capitalize".equals(changeCase)) {
        value = value.toLowerCase();
        if (value.length() > 0)
            value = value.substring(0, 1).toUpperCase() + value.substring(1);
    }
    value = prefix + value + postfix;
    SimpleNode constant;
    if (paramNode.jjtGetParent() instanceof AstStringConstant) {
        constant = new AstStringPart(value);
    } else if (type != null) {
        if (SqlTypeUtils.isNumber(type)) {
            if (!value.equals("")) {
                constant = AstNumericConstant.of((Number) SqlTypeUtils.parseValue(value, type));
            } else {
                constant = new AstIdentifierConstant(value);
            }
        } else {
            constant = new AstStringConstant(value);
        }
    } else if (!safeStr) {
        constant = new AstIdentifierConstant(value);
    } else if (paramNode.jjtGetParent() instanceof AstOrderingElement) {
        constant = new AstIdentifierConstant(value, false);
    } else {
        constant = new AstStringConstant(value);
    }
    return constant;
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

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