Search in sources :

Example 1 with EmptyNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode in project cayenne by apache.

the class ColumnDescriptorStageTest method perform.

@Test
public void perform() {
    TranslatableQueryWrapper wrapper = new MockQueryWrapperBuilder().withDistinct(true).withMetaData(new MockQueryMetadataBuilder().withSuppressDistinct().build()).build();
    TranslatorContext context = new MockTranslatorContext(wrapper);
    context.addResultNode(new EmptyNode());
    context.addResultNode(new EmptyNode(), "key");
    context.addResultNode(new EmptyNode(), false, PropertyFactory.COUNT, "key2");
    ColumnDescriptorStage stage = new ColumnDescriptorStage();
    stage.perform(context);
    assertEquals(1, context.getColumnDescriptors().size());
    ColumnDescriptor descriptor = context.getColumnDescriptors().iterator().next();
    assertEquals("key", descriptor.getDataRowKey());
}
Also used : EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) Test(org.junit.Test)

Example 2 with EmptyNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode in project cayenne by apache.

the class OracleSQLTreeProcessor method onLimitOffsetNode.

@Override
protected void onLimitOffsetNode(Node parent, LimitOffsetNode child, int index) {
    if (child.getLimit() > 0 || child.getOffset() > 0) {
        int limit = child.getLimit();
        int offset = child.getOffset();
        int max = (limit <= 0) ? Integer.MAX_VALUE : limit + offset;
        /*
             Transform query with limit/offset into following form:
             SELECT * FROM (
                SELECT tid.*, ROWNUM rnum
                FROM ( MAIN_QUERY ) tid
                WHERE ROWNUM <= OFFSET + LIMIT
             ) WHERE rnum > OFFSET
             */
        selectBuilder = select(all()).from(// using text not column to avoid quoting
        select(text(" tid.*"), text(" ROWNUM rnum")).from(aliased(() -> root, "tid")).where(exp(text(" ROWNUM")).lte(value(max)))).where(exp(text(" rnum")).gt(value(offset)));
    }
    parent.replaceChild(index, new EmptyNode());
}
Also used : EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode)

Example 3 with EmptyNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode in project cayenne by apache.

the class OrderingNodeBuilder method build.

@Override
public Node build() {
    Node node = new EmptyNode();
    node.addChild(column.build());
    node.addChild(new TextNode(direction));
    return node;
}
Also used : EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)

Example 4 with EmptyNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode in project cayenne by apache.

the class SybaseSQLTreeProcessor method onLimitOffsetNode.

@Override
protected void onLimitOffsetNode(Node parent, LimitOffsetNode child, int index) {
    // SQLServer uses "SELECT DISTINCT TOP N" or "SELECT TOP N" instead of LIMIT
    // Offset will be calculated in-memory
    replaceChild(parent, index, new EmptyNode(), false);
    if (child.getLimit() > 0) {
        int limit = child.getLimit() + child.getOffset();
        // we have root actually as input for processor, but it's better to keep processor stateless
        // root shouldn't be far from limit's parent (likely it will be parent itself)
        Node root = getRoot(parent);
        int idx = 0;
        if (root.getChild(0).getType() == NodeType.DISTINCT) {
            idx = 1;
        }
        root.addChild(idx, new TopNode(limit));
    }
}
Also used : EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) TopNode(org.apache.cayenne.access.sqlbuilder.sqltree.TopNode) TextNode(org.apache.cayenne.access.sqlbuilder.sqltree.TextNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) FunctionNode(org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode) OpExpressionNode(org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode) LimitOffsetNode(org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode) TopNode(org.apache.cayenne.access.sqlbuilder.sqltree.TopNode) EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode)

Example 5 with EmptyNode

use of org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode in project cayenne by apache.

the class GroupByStageTest method groupByWithAggregates.

// result column + aggregate
@Test
public void groupByWithAggregates() {
    context.addResultNode(new ColumnNode("t0", "column", null, null));
    context.addResultNode(new EmptyNode(), true, PropertyFactory.COUNT, "count");
    GroupByStage stage = new GroupByStage();
    stage.perform(context);
    Node node = context.getSelectBuilder().build();
    assertEquals(1, node.getChildrenCount());
    assertThat(node.getChild(0), instanceOf(GroupByNode.class));
    assertEquals(1, node.getChild(0).getChildrenCount());
    assertThat(node.getChild(0).getChild(0), instanceOf(ColumnNode.class));
    ColumnNode columnNode = (ColumnNode) node.getChild(0).getChild(0);
    assertEquals("t0", columnNode.getTable());
    assertEquals("column", columnNode.getColumn());
}
Also used : EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) GroupByNode(org.apache.cayenne.access.sqlbuilder.sqltree.GroupByNode) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) Node(org.apache.cayenne.access.sqlbuilder.sqltree.Node) EmptyNode(org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode) GroupByNode(org.apache.cayenne.access.sqlbuilder.sqltree.GroupByNode) ColumnNode(org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode) Test(org.junit.Test)

Aggregations

EmptyNode (org.apache.cayenne.access.sqlbuilder.sqltree.EmptyNode)5 Node (org.apache.cayenne.access.sqlbuilder.sqltree.Node)3 TextNode (org.apache.cayenne.access.sqlbuilder.sqltree.TextNode)2 Test (org.junit.Test)2 ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)1 ColumnNode (org.apache.cayenne.access.sqlbuilder.sqltree.ColumnNode)1 FunctionNode (org.apache.cayenne.access.sqlbuilder.sqltree.FunctionNode)1 GroupByNode (org.apache.cayenne.access.sqlbuilder.sqltree.GroupByNode)1 LimitOffsetNode (org.apache.cayenne.access.sqlbuilder.sqltree.LimitOffsetNode)1 OpExpressionNode (org.apache.cayenne.access.sqlbuilder.sqltree.OpExpressionNode)1 TopNode (org.apache.cayenne.access.sqlbuilder.sqltree.TopNode)1