Search in sources :

Example 1 with AstSelect

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

the class FilterApplier method addFilter.

public void addFilter(AstQuery query, Map<ColumnRef, Object> conditions) {
    if (conditions.size() == 0)
        return;
    AstWhere where = new AstWhere();
    if (query.jjtGetNumChildren() == 1) {
        AstSelect select = (AstSelect) query.child(0);
        if (select.getWhere() != null)
            where = select.getWhere();
        else
            select.where(where);
    } else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        AstSelect select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        select.where(where);
        query.replaceWith(new AstQuery(select));
    }
    addWhere(where, conditions);
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstQuery(com.developmentontheedge.sql.model.AstQuery) 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 2 with AstSelect

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

the class FilterApplier method setFilter.

public void setFilter(AstStart ast, Map<ColumnRef, Object> conditions) {
    AstQuery query = ast.getQuery();
    dropOldConditions(query);
    if (conditions.size() == 0)
        return;
    AstWhere where = new AstWhere();
    addWhere(where, conditions);
    if (query.jjtGetNumChildren() == 1)
        ((AstSelect) query.child(0)).where(where);
    else {
        AstTableRef tableRef = new AstTableRef(new AstParenthesis(query.clone()), new AstIdentifierConstant("tmp"));
        AstSelect select = new AstSelect(new AstSelectList(), new AstFrom(tableRef));
        select.where(where);
        query.replaceWith(new AstQuery(select));
    }
}
Also used : AstWhere(com.developmentontheedge.sql.model.AstWhere) AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) 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 3 with AstSelect

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

the class ColumnAdder method addColumn.

/**
 * Add a column to the main query with given alias. Do nothing if something with given alias already present
 * (even if something else is in the columnName).
 *
 * @param ast AST to modify (clone it if necessary)
 * @param tableName tableName to look for
 * @param columnName column name to add to select list
 * @param alias alias to use/search for
 * @throws IllegalArgumentException if given AST has no such table in the FROM field
 */
public void addColumn(AstStart ast, String tableName, String columnName, String alias) {
    AstQuery query = ast.getQuery();
    AstSelect firstSelect = query.children().select(AstSelect.class).findFirst().get();
    if (firstSelect.getSelectList().children().select(AstDerivedColumn.class).anyMatch(dc -> alias.equalsIgnoreCase(dc.getAlias())))
        return;
    query.children().select(AstSelect.class).forEach(select -> processSelect(select, tableName, columnName, alias));
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) AstQuery(com.developmentontheedge.sql.model.AstQuery) AstDerivedColumn(com.developmentontheedge.sql.model.AstDerivedColumn)

Example 4 with AstSelect

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

the class SqlServerTransformer method transformLeastGreatest.

@Override
protected void transformLeastGreatest(AstFunNode node) {
    String name = node.getFunction().getName();
    AstIdentifierConstant v = new AstIdentifierConstant("V");
    Function func;
    if ("LEAST".equals(name))
        func = parserContext.getFunction("min");
    else
        func = parserContext.getFunction("max");
    AstValues values = new AstValues(0);
    for (SimpleNode child : node.children()) values.addChild(new AstParenthesis(child));
    AstTableRef tableRef = new AstTableRef(new AstParenthesis(values), new AstIdentifierConstant(name), new AstColumnList(v));
    node.replaceWith(new AstParenthesis(new AstSelect(new AstSelectList(func.node(v)), new AstFrom(tableRef))));
}
Also used : AstValues(com.developmentontheedge.sql.model.AstValues) AstSelect(com.developmentontheedge.sql.model.AstSelect) Function(com.developmentontheedge.sql.model.Function) PredefinedFunction(com.developmentontheedge.sql.model.PredefinedFunction) AstWindowFunction(com.developmentontheedge.sql.model.AstWindowFunction) AstSelectList(com.developmentontheedge.sql.model.AstSelectList) AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstColumnList(com.developmentontheedge.sql.model.AstColumnList) AstFrom(com.developmentontheedge.sql.model.AstFrom) AstParenthesis(com.developmentontheedge.sql.model.AstParenthesis) AstTableRef(com.developmentontheedge.sql.model.AstTableRef) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Example 5 with AstSelect

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

the class AstTest method selectCount.

@Test
public void selectCount() {
    AstSelect users = Ast.selectCount().from("users");
    assertEquals("SELECT COUNT(*) AS \"count\" FROM users", users.format());
}
Also used : AstSelect(com.developmentontheedge.sql.model.AstSelect) Test(org.junit.Test)

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