Search in sources :

Example 1 with AstDerivedColumn

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

the class OrderByFilter method apply.

private void apply(Map<String, String> columns, AstQuery query, AstOrderBy orderBy) {
    for (Map.Entry<String, String> column : columns.entrySet()) {
        int num = 0;
        for (AstDerivedColumn derColumn : query.tree().select(AstDerivedColumn.class)) {
            if (column.getKey().equals(derColumn.getColumn()) || column.getKey().equals(derColumn.getAlias())) {
                num = derColumn.jjtGetParent().indexOf(derColumn) + 1;
                break;
            }
        }
        if (num == 0)
            throw new IllegalArgumentException("Unknown column " + column.getKey() + " in order clause");
        String dir = column.getValue();
        if (!dir.equalsIgnoreCase("ASC") && !dir.equalsIgnoreCase("DESC"))
            throw new IllegalArgumentException("Unknown direction " + dir + ". Was expecting ASC or DESC");
        AstOrderingElement elem = new AstOrderingElement(0);
        elem.addChild(AstNumericConstant.of(num));
        elem.setDirectionToken(new Token(0, dir));
        orderBy.addChild(elem);
    }
}
Also used : AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) Token(com.developmentontheedge.sql.model.Token) Map(java.util.Map)

Example 2 with AstDerivedColumn

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

the class Be5QueryExecutor method hasColumnWithLabel.

private boolean hasColumnWithLabel(AstStart ast, String idColumnLabel) {
    AstQuery query = ast.getQuery();
    Optional<AstSelect> selectOpt = query.children().select(AstSelect.class).collect(MoreCollectors.onlyOne());
    if (!selectOpt.isPresent())
        return false;
    AstSelect select = selectOpt.get();
    return select.getSelectList().children().select(AstDerivedColumn.class).map(AstDerivedColumn::getAlias).nonNull().map(alias -> alias.replaceFirst("^\"(.+)\"$", "$1")).has(idColumnLabel);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) Connection(java.sql.Connection) DynamicPropertySet(com.developmentontheedge.beans.DynamicPropertySet) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) Meta(com.developmentontheedge.be5.api.services.Meta) Query(com.developmentontheedge.be5.metadata.model.Query) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) ColumnAdder(com.developmentontheedge.sql.format.ColumnAdder) ResultSetParser(com.developmentontheedge.be5.api.sql.ResultSetParser) UserInfoHolder(com.developmentontheedge.be5.api.helpers.UserInfoHolder) Map(java.util.Map) AstLimit(com.developmentontheedge.sql.model.AstLimit) Be5Exception(com.developmentontheedge.be5.api.exceptions.Be5Exception) Context(com.developmentontheedge.sql.format.Context) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn) MoreCollectors(one.util.streamex.MoreCollectors) Set(java.util.Set) PreparedStatement(java.sql.PreparedStatement) Logger(java.util.logging.Logger) EntityModel(com.developmentontheedge.be5.databasemodel.EntityModel) Objects(java.util.Objects) List(java.util.List) QueryType(com.developmentontheedge.be5.metadata.QueryType) LimitsApplier(com.developmentontheedge.sql.format.LimitsApplier) Simplifier(com.developmentontheedge.sql.format.Simplifier) AstBeParameterTag(com.developmentontheedge.sql.model.AstBeParameterTag) AstOrderBy(com.developmentontheedge.sql.model.AstOrderBy) StreamEx(one.util.streamex.StreamEx) DatabaseConstants(com.developmentontheedge.be5.metadata.DatabaseConstants) Optional(java.util.Optional) FilterHelper(com.developmentontheedge.be5.api.helpers.FilterHelper) RecordModel(com.developmentontheedge.be5.databasemodel.RecordModel) ResultSetMetaData(java.sql.ResultSetMetaData) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstQuery(com.developmentontheedge.sql.model.AstQuery) DatabaseService(com.developmentontheedge.be5.api.services.DatabaseService) QueryContext(com.developmentontheedge.sql.format.QueryContext) HashMap(java.util.HashMap) AstStart(com.developmentontheedge.sql.model.AstStart) Token(com.developmentontheedge.sql.model.Token) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Entity(com.developmentontheedge.be5.metadata.model.Entity) SQLException(java.sql.SQLException) Formatter(com.developmentontheedge.sql.format.Formatter) Injector(com.developmentontheedge.be5.env.Injector) DynamicPropertySetSupport(com.developmentontheedge.beans.DynamicPropertySetSupport) ParserContext(com.developmentontheedge.sql.model.ParserContext) AstSelect(com.developmentontheedge.sql.model.AstSelect) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) SqlService(com.developmentontheedge.be5.api.services.SqlService) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) DatabaseModel(com.developmentontheedge.be5.databasemodel.impl.DatabaseModel) DpsRecordAdapter(com.developmentontheedge.be5.api.sql.DpsRecordAdapter) Ast(com.developmentontheedge.sql.format.Ast) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) AstNumericConstant(com.developmentontheedge.sql.model.AstNumericConstant) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta) AstOrderingElement(com.developmentontheedge.sql.model.AstOrderingElement) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Collections(java.util.Collections) AstQuery(com.developmentontheedge.sql.model.AstQuery)

Example 3 with AstDerivedColumn

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

the class MySqlTransformer method transformIdentifier.

@Override
protected void transformIdentifier(AstIdentifierConstant identifier) {
    if (identifier.getQuoteSymbol() == QuoteSymbol.DOUBLE_QUOTE) {
        Node parent = identifier.jjtGetParent();
        if (parent instanceof AstDerivedColumn)
            return;
        identifier.setQuoteSymbol(QuoteSymbol.BACKTICK);
    }
    if (identifier.getValue().equalsIgnoreCase("current_timestamp")) {
        identifier.replaceWith(parserContext.getFunction("now").node());
    }
}
Also used : Node(com.developmentontheedge.sql.model.Node) AstFunNode(com.developmentontheedge.sql.model.AstFunNode) SimpleNode(com.developmentontheedge.sql.model.SimpleNode) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn)

Example 4 with AstDerivedColumn

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

the class SqlServerTransformer method transformSelect.

@Override
protected void transformSelect(AstSelect select) {
    AstLimit limit = select.getLimit();
    if (limit != null) {
        if (limit.getOffset() == 0) {
            limit.setShape("TOP", null);
            select.moveToFront(limit);
        } 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 5 with AstDerivedColumn

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

the class CountApplier method transform.

public void transform(ParserContext ctx, AstStart ast) {
    AstQuery query = ast.getQuery();
    query.children().select(AstSelect.class).forEach(AstSelect::dropOrder);
    if (query.jjtGetNumChildren() > 1)
        throw new UnsupportedOperationException("UNION queries are not supported for COUNT");
    AstSelect select = query.children().select(AstSelect.class).findFirst().get();
    AstSelectList selectList = select.getSelectList();
    selectList.removeChildren();
    AstCount countFn = new AstCount();
    AstDerivedColumn countNode = new AstDerivedColumn(countFn, "CNT");
    countNode.setAsToken(true);
    selectList.addChild(countNode);
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstCount(com.developmentontheedge.sql.model.AstCount) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn)

Aggregations

AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)8 AstSelect (com.developmentontheedge.sql.model.AstSelect)5 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)5 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)3 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)3 AstLimit (com.developmentontheedge.sql.model.AstLimit)3 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)3 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)3 AstBetweenPredicate (com.developmentontheedge.sql.model.AstBetweenPredicate)2 AstFrom (com.developmentontheedge.sql.model.AstFrom)2 AstQuery (com.developmentontheedge.sql.model.AstQuery)2 AstWhere (com.developmentontheedge.sql.model.AstWhere)2 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)2 AstWindowSpecification (com.developmentontheedge.sql.model.AstWindowSpecification)2 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)1 FilterHelper (com.developmentontheedge.be5.api.helpers.FilterHelper)1 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)1 UserInfoHolder (com.developmentontheedge.be5.api.helpers.UserInfoHolder)1 DatabaseService (com.developmentontheedge.be5.api.services.DatabaseService)1