Search in sources :

Example 6 with SimpleNode

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

the class MySqlTransformer method transformDecode.

@Override
protected void transformDecode(AstFunNode node) {
    AstCase astCase = new AstCase();
    for (int i = 1; i < node.jjtGetNumChildren() - 1; i++) {
        SimpleNode cond;
        if (node.child(i) instanceof AstSpecialConstant) {
            cond = new AstNullPredicate(0);
            cond.addChild(node.child(0));
        } else {
            cond = DefaultParserContext.FUNC_EQ.node(node.child(0), node.child(i));
        }
        astCase.addChild(new AstWhen(cond, node.child(++i)));
        if (i == node.jjtGetNumChildren() - 2) {
            astCase.addChild(new AstCaseElse(node.child(++i)));
        }
    }
    node.replaceWith(astCase);
}
Also used : AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstWhen(com.developmentontheedge.sql.model.AstWhen) AstCase(com.developmentontheedge.sql.model.AstCase) AstNullPredicate(com.developmentontheedge.sql.model.AstNullPredicate) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSpecialConstant(com.developmentontheedge.sql.model.AstSpecialConstant)

Example 7 with SimpleNode

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

the class OracleTransformer method transformCoalesce.

@Override
protected void transformCoalesce(AstFunNode node) {
    if (node.jjtGetNumChildren() < 2)
        throw new IllegalStateException("COALESCE node must contain at least two arguments: " + node.format());
    int num = node.jjtGetNumChildren() - 1;
    SimpleNode lastNode = node.removeChild(num);
    while (--num >= 1) {
        lastNode = NVL.node(node.removeChild(num), lastNode);
    }
    node.addChild(lastNode);
    node.setFunction(NVL);
}
Also used : SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 8 with SimpleNode

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

the class PostgreSqlTransformer method transformDecode.

@Override
protected void transformDecode(AstFunNode node) {
    AstCase astCase = new AstCase();
    for (int i = 1; i < node.jjtGetNumChildren() - 1; i++) {
        SimpleNode cond;
        if (node.child(i) instanceof AstSpecialConstant) {
            cond = new AstNullPredicate(0);
            cond.addChild(node.child(0));
        } else {
            cond = DefaultParserContext.FUNC_EQ.node(node.child(0), node.child(i));
        }
        astCase.addChild(new AstWhen(cond, node.child(++i)));
        if (i == node.jjtGetNumChildren() - 2) {
            astCase.addChild(new AstCaseElse(node.child(++i)));
        }
    }
    node.replaceWith(astCase);
}
Also used : AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstWhen(com.developmentontheedge.sql.model.AstWhen) AstCase(com.developmentontheedge.sql.model.AstCase) AstNullPredicate(com.developmentontheedge.sql.model.AstNullPredicate) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstSpecialConstant(com.developmentontheedge.sql.model.AstSpecialConstant)

Example 9 with SimpleNode

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

the class ContextApplier method applySessionTag.

private void applySessionTag(AstBeSessionTag child) {
    String name = child.getName();
    Object value = context.getSessionVariable(name);
    if (value == null) {
        if (child.getDefault() != null) {
            value = SqlTypeUtils.parseValue(child.getDefault(), child.getType());
        } else {
            value = "";
        }
    }
    SimpleNode replacement;
    // TODO: support refColumn; smart quoting - in server module
    if (child.jjtGetParent() instanceof AstStringConstant) {
        replacement = new AstStringPart(value.toString());
    } else {
        if (SqlTypeUtils.isNumber(value.getClass())) {
            replacement = AstNumericConstant.of((Number) value);
        } else {
            replacement = new AstStringConstant(value.toString());
        }
    }
    child.replaceWith(replacement);
}
Also used : AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 10 with SimpleNode

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

the class ContextApplier method setIfResult.

private int setIfResult(SimpleNode newTree, int i, SimpleNode condNode, boolean correct) {
    SimpleNode child;
    for (int j = condNode.jjtGetNumChildren() - 1; j >= 0; j--) {
        child = condNode.child(j);
        if (child instanceof AstBeThen && correct || child instanceof AstBeElse && !correct) {
            if (child.jjtGetNumChildren() != 0)
                setChildren(newTree, i, child);
            else {
                newTree.removeChild(condNode);
                i--;
            }
            break;
        } else if (child instanceof AstBeThen && !correct) {
            newTree.removeChild(condNode);
            i--;
        }
    }
    return i;
}
Also used : AstBeThen(com.developmentontheedge.sql.model.AstBeThen) AstBeElse(com.developmentontheedge.sql.model.AstBeElse) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

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