Search in sources :

Example 6 with AstStringConstant

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

the class OracleTransformer method transformYearMonthDay.

@Override
protected void transformYearMonthDay(SimpleNode node) {
    String dateField = "";
    String name = node instanceof AstFunNode ? ((AstFunNode) node).getFunction().getName() : ((AstExtract) node).getDateField();
    if ("year".equalsIgnoreCase(name))
        dateField = "YYYY";
    if ("month".equalsIgnoreCase(name))
        dateField = "MM";
    if ("day".equalsIgnoreCase(name))
        dateField = "DD";
    node.replaceWith(parserContext.getFunction("to_number").node(parserContext.getFunction("to_char").node(node.child(0), new AstStringConstant(dateField))));
}
Also used : AstFunNode(com.developmentontheedge.sql.model.AstFunNode) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant)

Example 7 with AstStringConstant

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

the class OracleTransformer method getDateTimeDiff.

@Override
protected SimpleNode getDateTimeDiff(SimpleNode startDate, SimpleNode endDate, String format) {
    Function trunc = parserContext.getFunction("trunc");
    AstParenthesis dateDifference = new AstParenthesis(DefaultParserContext.FUNC_MINUS.node(new AstCast(endDate, "DATE"), new AstCast(startDate, "DATE")));
    PredefinedFunction opTimes = DefaultParserContext.FUNC_TIMES;
    switch(format) {
        case "SECOND":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24 * 60 * 60)));
        case "MINUTE":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24 * 60)));
        case "HOUR":
            return trunc.node(opTimes.node(dateDifference, AstNumericConstant.of(24)));
        case "DAY":
            return trunc.node(dateDifference);
        case "MONTH":
            return MONTHS_BETWEEN.node(trunc.node(endDate, new AstStringConstant(format)), trunc.node(startDate, new AstStringConstant(format)));
        case "YEAR":
            PredefinedFunction opDivide = DefaultParserContext.FUNC_DIVIDE;
            return parserContext.getFunction("floor").node(opDivide.node(MONTHS_BETWEEN.node(endDate, startDate), AstNumericConstant.of(12)));
        default:
            throw new IllegalStateException("Unsupported value for datepart in TIMESTAMPDIFF: " + format);
    }
}
Also used : Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstCast(com.developmentontheedge.sql.model.AstCast) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction)

Example 8 with AstStringConstant

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

the class PostgreSqlTransformer method transformLastDay.

@Override
protected void transformLastDay(AstFunNode node) {
    Function opPlus = DefaultParserContext.FUNC_PLUS;
    Function opMinus = DefaultParserContext.FUNC_MINUS;
    SimpleNode dateTrunc = parserContext.getFunction("date_trunc").node(new AstStringConstant("MONTH"), node.child(0));
    AstInterval monthInterval = new AstInterval(new AstStringConstant("1 MONTH"));
    AstInterval dayInterval = new AstInterval(new AstStringConstant("1 DAY"));
    SimpleNode expr = opPlus.node(dateTrunc, opMinus.node(monthInterval, dayInterval));
    node.replaceWith(new AstCast(expr, "DATE"));
}
Also used : PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) Function(com.developmentontheedge.sql.model.Function) AstCast(com.developmentontheedge.sql.model.AstCast) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstInterval(com.developmentontheedge.sql.model.AstInterval) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 9 with AstStringConstant

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

the class PostgreSqlTransformer 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 = new AstParenthesis(node.child(1));
    String name = node.getFunction().getName();
    String type = name.equalsIgnoreCase("add_months") ? "1 MONTH" : name.equalsIgnoreCase("add_days") ? "1 DAY" : "1 MILLISECOND";
    AstInterval interval = new AstInterval(new AstStringConstant(type));
    node.replaceWith(new AstParenthesis(opPlus.node(date, opTimes.node(interval, number))));
}
Also used : PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) Function(com.developmentontheedge.sql.model.Function) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) AstInterval(com.developmentontheedge.sql.model.AstInterval) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 10 with AstStringConstant

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

Aggregations

AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)15 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)10 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)5 Function (com.developmentontheedge.sql.model.Function)4 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)4 AstCast (com.developmentontheedge.sql.model.AstCast)3 AstInterval (com.developmentontheedge.sql.model.AstInterval)3 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)3 AstStringPart (com.developmentontheedge.sql.model.AstStringPart)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)2 AstBeListPlaceHolder (com.developmentontheedge.sql.model.AstBeListPlaceHolder)1 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)1 AstBeSqlVar (com.developmentontheedge.sql.model.AstBeSqlVar)1 AstExcept (com.developmentontheedge.sql.model.AstExcept)1 AstExtract (com.developmentontheedge.sql.model.AstExtract)1 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)1 AstFirstDayOf (com.developmentontheedge.sql.model.AstFirstDayOf)1 AstInValueList (com.developmentontheedge.sql.model.AstInValueList)1 AstNumericConstant (com.developmentontheedge.sql.model.AstNumericConstant)1 AstOrderingElement (com.developmentontheedge.sql.model.AstOrderingElement)1