Search in sources :

Example 6 with AstParenthesis

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

the class Be5QueryExecutor method countFromQuery.

private void countFromQuery(AstQuery query) {
    AstSelect select = Ast.selectCount().from(AstTableRef.as(new AstParenthesis(query.clone()), new AstIdentifierConstant("data", true)));
    query.replaceWith(new AstQuery(select));
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Example 7 with AstParenthesis

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

the class LimitsApplier method transformQuery.

public boolean transformQuery(AstQuery query) {
    if (query.jjtGetNumChildren() == 1) {
        AstSelect select = (AstSelect) query.child(0);
        if (select.getLimit() != null)
            return false;
        AstLimit limit = new AstLimit();
        limit.setLimit(offset, count);
        select.addChild(limit);
    } else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        AstSelect select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        AstLimit limit = new AstLimit();
        limit.setLimit(offset, count);
        select.addChild(limit);
        query.replaceWith(new AstQuery(select));
    }
    // TODO: support offset, union, etc.
    return true;
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstLimit(com.developmentontheedge.sql.model.AstLimit) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis)

Example 8 with AstParenthesis

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

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

Example 10 with AstParenthesis

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

Aggregations

AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)17 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)10 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)7 Function (com.developmentontheedge.sql.model.Function)7 PredefinedFunction (com.developmentontheedge.sql.model.PredefinedFunction)7 AstFrom (com.developmentontheedge.sql.model.AstFrom)6 AstSelect (com.developmentontheedge.sql.model.AstSelect)6 AstQuery (com.developmentontheedge.sql.model.AstQuery)5 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)5 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)5 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)4 AstStringConstant (com.developmentontheedge.sql.model.AstStringConstant)4 AstInterval (com.developmentontheedge.sql.model.AstInterval)3 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)3 AstCast (com.developmentontheedge.sql.model.AstCast)2 AstDateAdd (com.developmentontheedge.sql.model.AstDateAdd)2 AstFirstDayOf (com.developmentontheedge.sql.model.AstFirstDayOf)2 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)2 AstWhere (com.developmentontheedge.sql.model.AstWhere)2 AstBooleanExpression (com.developmentontheedge.sql.model.AstBooleanExpression)1