Search in sources :

Example 21 with AstSelect

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

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

the class AstTest method selectWhereLike.

@Test
public void selectWhereLike() {
    Map<String, ? super Object> names = new HashMap<>();
    names.put("name", "test%");
    AstSelect users = Ast.selectCount().from("users").where(names);
    assertEquals("SELECT COUNT(*) AS \"count\" FROM users WHERE name LIKE ?", users.format());
    names.clear();
    names.put("name", "%test");
    AstSelect users2 = Ast.selectCount().from("users").where(names);
    assertEquals("SELECT COUNT(*) AS \"count\" FROM users WHERE name LIKE ?", users2.format());
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 23 with AstSelect

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

the class AstTest method selectWhere.

@Test
public void selectWhere() {
    Map<String, ? super Object> name = Collections.singletonMap("name", "test");
    AstSelect users = Ast.selectCount().from("users").where(name);
    assertEquals("SELECT COUNT(*) AS \"count\" FROM users WHERE name =?", users.format());
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) Test(org.junit.Test)

Example 24 with AstSelect

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

the class AstTest method selectColumns.

@Test
public void selectColumns() {
    AstSelect users = Ast.select(ImmutableList.of("name", "email")).from("users");
    assertEquals("SELECT name, email FROM users", users.format());
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) Test(org.junit.Test)

Example 25 with AstSelect

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

AstSelect (com.developmentontheedge.sql.model.AstSelect)28 AstIdentifierConstant (com.developmentontheedge.sql.model.AstIdentifierConstant)11 AstSelectList (com.developmentontheedge.sql.model.AstSelectList)10 AstTableRef (com.developmentontheedge.sql.model.AstTableRef)9 AstFrom (com.developmentontheedge.sql.model.AstFrom)8 AstQuery (com.developmentontheedge.sql.model.AstQuery)8 Test (org.junit.Test)8 AstParenthesis (com.developmentontheedge.sql.model.AstParenthesis)7 AstDerivedColumn (com.developmentontheedge.sql.model.AstDerivedColumn)6 AstLimit (com.developmentontheedge.sql.model.AstLimit)6 AstWhere (com.developmentontheedge.sql.model.AstWhere)5 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)5 HashMap (java.util.HashMap)5 AstFunNode (com.developmentontheedge.sql.model.AstFunNode)4 AstFieldReference (com.developmentontheedge.sql.model.AstFieldReference)3 AstOrderBy (com.developmentontheedge.sql.model.AstOrderBy)3 AstWindowFunction (com.developmentontheedge.sql.model.AstWindowFunction)3 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)2 DynamicPropertySet (com.developmentontheedge.beans.DynamicPropertySet)2 DynamicPropertySetSupport (com.developmentontheedge.beans.DynamicPropertySetSupport)2