Search in sources :

Example 16 with SimpleNode

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

the class SqlServerTransformer method transformLastDay.

protected void transformLastDay(SimpleNode node, SimpleNode date) {
    Function opConcat = DefaultParserContext.FUNC_PLUS;
    SimpleNode dateDiff = DATEDIFF.node(new AstIdentifierConstant("MONTH"), AstNumericConstant.of(0), date);
    SimpleNode dateAdd = DATEADD.node(new AstIdentifierConstant("MONTH"), opConcat.node(dateDiff, AstNumericConstant.of(1)), AstNumericConstant.of(0));
    SimpleNode expr = DATEADD.node(new AstIdentifierConstant("DAY"), AstNumericConstant.of(-1), dateAdd);
    node.replaceWith(CONVERT.node(new AstIdentifierConstant("DATE"), expr, AstNumericConstant.of(104)));
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 17 with SimpleNode

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

the class SqlServerTransformer method transformDateFormat.

@Override
protected void transformDateFormat(AstFunNode node, DateFormat df) {
    Number length, codePage;
    switch(df) {
        case FORMAT_DATE:
            length = 10;
            codePage = 120;
            break;
        case FORMAT_DATETIME:
            length = 19;
            codePage = 120;
            break;
        case FORMAT_DATE_RUS:
            length = 10;
            codePage = 104;
            break;
        case FORMAT_DATE_RUS_SHORT:
            length = 8;
            codePage = 4;
            break;
        case FORMAT_MONTHYEAR:
            length = 4;
            codePage = 120;
            break;
        case FORMAT_FMDAYMONTH:
        case FORMAT_DAYMONTH:
            length = 5;
            codePage = 4;
            break;
        case FORMAT_HOURMINUTE:
            length = 5;
            codePage = 114;
            break;
        case FORMAT_YYYYMMDD:
            length = 8;
            codePage = 112;
            break;
        case FORMAT_YYYYMM:
            length = 6;
            codePage = 112;
            break;
        case FORMAT_DAY_OF_WEEK:
            node.replaceWith(DATEPART.node(new AstIdentifierConstant("dw"), node.child(0)));
            return;
        default:
            SimpleNode rtrim = RTRIM.node(parserContext.getFunction(df.name()).node(node.child(0)));
            SimpleNode padded = DefaultParserContext.FUNC_PLUS.node(new AstStringConstant("0"), rtrim);
            node.replaceWith(RIGHT.node(padded, AstNumericConstant.of(df == DateFormat.YEAR ? 4 : 2)));
            return;
    }
    SimpleNode replacement = CONVERT.node(VARCHAR.node(AstNumericConstant.of(length)), node.child(0), AstNumericConstant.of(codePage));
    if (DateFormat.FORMAT_MONTHYEAR.equals(df))
        replacement = DefaultParserContext.FUNC_PLUS.node(DATENAME.node(new AstIdentifierConstant("month"), node.child(0)), new AstStringConstant(" "), replacement);
    node.replaceWith(replacement);
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 18 with SimpleNode

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

the class MacroExpander method transformMacroFunction.

private void transformMacroFunction(AstFunNode node) {
    BeMacroFunction function = (BeMacroFunction) node.getFunction();
    SimpleNode replacement = function.getReplacement(node);
    expandMacros(replacement);
    node.replaceWith(replacement);
}
Also used : BeMacroFunction(com.developmentontheedge.sql.model.BeMacroFunction) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 19 with SimpleNode

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

the class MySqlTransformer method transformConcat.

@Override
protected void transformConcat(AstFunNode node) {
    if (isConcat(node))
        return;
    node.setFunction(parserContext.getFunction("concat"));
    List<SimpleNode> flatChildren = node.children().flatMap(child -> isConcat(child) ? child.children() : Stream.of(child)).toList();
    node.removeChildren();
    flatChildren.forEach(node::addChild);
    SimpleNode parent = node.jjtGetParent();
    if (parent instanceof AstParenthesis)
        parent.replaceWith(node);
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstCaseElse(com.developmentontheedge.sql.model.AstCaseElse) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstPosition(com.developmentontheedge.sql.model.AstPosition) AstExcept(com.developmentontheedge.sql.model.AstExcept) Node(com.developmentontheedge.sql.model.Node) AstExtract(com.developmentontheedge.sql.model.AstExtract) QuoteSymbol(com.developmentontheedge.sql.model.AstIdentifierConstant.QuoteSymbol) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) Function(com.developmentontheedge.sql.model.Function) AstNullPredicate(com.developmentontheedge.sql.model.AstNullPredicate) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstCast(com.developmentontheedge.sql.model.AstCast) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstCase(com.developmentontheedge.sql.model.AstCase) AstWith(com.developmentontheedge.sql.model.AstWith) AstSpecialConstant(com.developmentontheedge.sql.model.AstSpecialConstant) List(java.util.List) AstWhen(com.developmentontheedge.sql.model.AstWhen) Stream(java.util.stream.Stream) AstInterval(com.developmentontheedge.sql.model.AstInterval) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) AstNumericConstant(com.developmentontheedge.sql.model.AstNumericConstant) AstDateAdd(com.developmentontheedge.sql.model.AstDateAdd) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 20 with SimpleNode

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

the class MySqlTransformer method transformDateAdd.

@Override
protected void transformDateAdd(AstFunNode node) {
    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") ? "MONTH" : name.equalsIgnoreCase("add_days") ? "DAY" : "MICROSECOND";
    if (type.equals("MICROSECOND"))
        number = new AstParenthesis(opTimes.node(number, AstNumericConstant.of(1000)));
    node.replaceWith(new AstDateAdd(date, new AstInterval(number), type));
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstDateAdd(com.developmentontheedge.sql.model.AstDateAdd) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) AstInterval(com.developmentontheedge.sql.model.AstInterval) 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