Search in sources :

Example 1 with NodeBuilder

use of org.apache.cayenne.access.sqlbuilder.NodeBuilder in project cayenne by apache.

the class OrderingStage method processOrdering.

private void processOrdering(QualifierTranslator qualifierTranslator, TranslatorContext context, Ordering ordering) {
    Expression exp = ordering.getSortSpec();
    Node translatedNode = qualifierTranslator.translate(exp);
    NodeBuilder nodeBuilder = node(translatedNode);
    if (ordering.isCaseInsensitive()) {
        nodeBuilder = function("UPPER", nodeBuilder);
    }
    // If query is DISTINCT then we need to add all ORDER BY clauses as result columns
    if (!context.isDistinctSuppression()) {
        // TODO: need to check duplicates?
        // need UPPER() function here too, as some DB expect exactly the same expression in select and in ordering
        ResultNodeDescriptor descriptor = context.addResultNode(nodeBuilder.build().deepCopy());
        if (exp instanceof ASTAggregateFunctionCall) {
            descriptor.setAggregate(true);
        }
    }
    OrderingNodeBuilder orderingNodeBuilder = order(nodeBuilder);
    if (ordering.isDescending()) {
        orderingNodeBuilder.desc();
    }
    context.getSelectBuilder().orderBy(orderingNodeBuilder);
}
Also used : Expression(org.apache.cayenne.exp.Expression) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) ASTAggregateFunctionCall(org.apache.cayenne.exp.parser.ASTAggregateFunctionCall) NodeBuilder(org.apache.cayenne.access.sqlbuilder.NodeBuilder) OrderingNodeBuilder(org.apache.cayenne.access.sqlbuilder.OrderingNodeBuilder) OrderingNodeBuilder(org.apache.cayenne.access.sqlbuilder.OrderingNodeBuilder)

Example 2 with NodeBuilder

use of org.apache.cayenne.access.sqlbuilder.NodeBuilder in project cayenne by apache.

the class TableTreeStage method perform.

@Override
public void perform(TranslatorContext context) {
    context.getTableTree().visit(node -> {
        NodeBuilder tableNode = table(node.getEntity()).as(node.getTableAlias());
        if (node.getRelationship() != null) {
            tableNode = getJoin(node, tableNode).on(getJoinExpression(context, node));
        }
        context.getSelectBuilder().from(tableNode);
    });
}
Also used : NodeBuilder(org.apache.cayenne.access.sqlbuilder.NodeBuilder) ExpressionNodeBuilder(org.apache.cayenne.access.sqlbuilder.ExpressionNodeBuilder) JoinNodeBuilder(org.apache.cayenne.access.sqlbuilder.JoinNodeBuilder)

Example 3 with NodeBuilder

use of org.apache.cayenne.access.sqlbuilder.NodeBuilder in project cayenne by apache.

the class TableTreeQualifierStage method perform.

@Override
public void perform(TranslatorContext context) {
    context.getTableTree().visit(node -> {
        Expression dbQualifier = node.getEntity().getQualifier();
        if (dbQualifier != null) {
            String pathToRoot = node.getAttributePath().getPath();
            dbQualifier = dbQualifier.transform(input -> {
                if (input instanceof ASTPath) {
                    String path = ((ASTPath) input).getPath();
                    if (!pathToRoot.isEmpty()) {
                        path = pathToRoot + '.' + path;
                    }
                    return new ASTDbPath(path);
                }
                return input;
            });
            Node rootQualifier = context.getQualifierNode();
            Node translatedQualifier = context.getQualifierTranslator().translate(dbQualifier);
            if (rootQualifier != null) {
                NodeBuilder expressionNodeBuilder = exp(node(rootQualifier)).and(node(translatedQualifier));
                context.setQualifierNode(expressionNodeBuilder.build());
            } else {
                context.setQualifierNode(translatedQualifier);
            }
        }
    });
    if (context.getQualifierNode() != null) {
        context.getSelectBuilder().where(context.getQualifierNode());
    }
}
Also used : ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) SQLBuilder.node(org.apache.cayenne.access.sqlbuilder.SQLBuilder.node) NodeBuilder(org.apache.cayenne.access.sqlbuilder.NodeBuilder) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) Expression(org.apache.cayenne.exp.Expression) SQLBuilder.exp(org.apache.cayenne.access.sqlbuilder.SQLBuilder.exp) ASTPath(org.apache.cayenne.exp.parser.ASTPath) ASTDbPath(org.apache.cayenne.exp.parser.ASTDbPath) Expression(org.apache.cayenne.exp.Expression) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) ASTPath(org.apache.cayenne.exp.parser.ASTPath) NodeBuilder(org.apache.cayenne.access.sqlbuilder.NodeBuilder)

Aggregations

NodeBuilder (org.apache.cayenne.access.sqlbuilder.NodeBuilder)3 Node (org.apache.cayenne.access.sqlbuilder.sqltree.Node)2 Expression (org.apache.cayenne.exp.Expression)2 ExpressionNodeBuilder (org.apache.cayenne.access.sqlbuilder.ExpressionNodeBuilder)1 JoinNodeBuilder (org.apache.cayenne.access.sqlbuilder.JoinNodeBuilder)1 OrderingNodeBuilder (org.apache.cayenne.access.sqlbuilder.OrderingNodeBuilder)1 SQLBuilder.exp (org.apache.cayenne.access.sqlbuilder.SQLBuilder.exp)1 SQLBuilder.node (org.apache.cayenne.access.sqlbuilder.SQLBuilder.node)1 ASTAggregateFunctionCall (org.apache.cayenne.exp.parser.ASTAggregateFunctionCall)1 ASTDbPath (org.apache.cayenne.exp.parser.ASTDbPath)1 ASTPath (org.apache.cayenne.exp.parser.ASTPath)1