Search in sources :

Example 31 with AstStart

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

the class SubQueryTest method testSeveralSubQueries.

@Test
public void testSeveralSubQueries() {
    AstStart start = SqlQuery.parse("SELECT ID, '<sql limit=\"2\">SELECT * FROM subTable WHERE tableID=<var:ID/></sql>' FROM table");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
    contextApplier.applyContext(start);
    String key = contextApplier.subQueryKeys().findFirst().get();
    Map<String, String> vars = Collections.singletonMap("ID", "5");
    AstBeSqlSubQuery subQuery = contextApplier.applyVars(key, vars::get);
    assertEquals("SELECT * FROM subTable WHERE tableID = 5 LIMIT 2", subQuery.getQuery().format());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Test(org.junit.Test)

Example 32 with AstStart

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

the class SubQueryTest method testVarsInFieldReference.

@Test
public void testVarsInFieldReference() {
    AstStart start = SqlQuery.parse("SELECT ID, '<sql limit=\"2\">SELECT field.<var:reference/> FROM table.<var:name/></sql>' FROM table");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
    contextApplier.applyContext(start);
    String key = contextApplier.subQueryKeys().findFirst().get();
    Map<String, String> vars = new HashMap<String, String>();
    vars.put("reference", "ref");
    vars.put("name", "name");
    AstBeSqlSubQuery subQuery = contextApplier.applyVars(key, vars::get);
    assertEquals("SELECT field.ref FROM table.name LIMIT 2", subQuery.getQuery().format());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) HashMap(java.util.HashMap) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Test(org.junit.Test)

Example 33 with AstStart

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

the class Formatter method format.

public String format(AstStart start, Context context, ParserContext parserContext) {
    DbmsTransformer dbmsTransformer = context.getDbmsTransformer();
    dbmsTransformer.setParserContext(parserContext);
    AstStart clone = start.clone();
    dbmsTransformer.transformAst(clone);
    return clone.format();
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart)

Example 34 with AstStart

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

the class ContextApplier method applySubQuery.

private void applySubQuery(AstBeSqlSubQuery subQuery) {
    if (subQuery.getExec() != null || subQuery.getQueryName() != null) {
        String name = subQuery.getQueryName();
        if (name == null) {
            throw new IllegalStateException("Empty subQuery without queryName parameter: " + subQuery.format());
        }
        String entity = subQuery.getEntityName();
        String subQueryText = context.resolveQuery(entity, name);
        if (subQueryText == null) {
            throw new IllegalStateException("Unable to resolve subquery: " + (entity == null ? "" : entity + ".") + name);
        }
        AstStart start = SqlQuery.parse(subQueryText);
        subQuery.addChild(start.getQuery());
    }
    if (subQuery.getQuery() != null) {
        if (subQuery.getOutColumns() != null)
            new ColumnsApplier().keepOnlyOutColumns(subQuery);
        if (subQuery.getLimit() != null)
            new LimitsApplier(0, subQuery.getLimit()).transformQuery(subQuery.getQuery());
        String keyStr = subQuery.getFilterKeys();
        String valPropStr = subQuery.getFilterValProperties();
        if (keyStr != null && valPropStr != null) {
            String[] keys = keyStr.split(",");
            String[] valProps = valPropStr.split(",");
            Map<ColumnRef, String> conditions = new HashMap<>();
            for (int i = 0; i < keys.length; i++) {
                // ColumnRef.resolve(subQuery.getQuery(), keys[i])
                conditions.put(new ColumnRef(null, keys[i]), "<var:" + valProps[i] + "/>");
            // subQuery.addParameter(keys[i], valProps[i]);
            }
        // new FilterApplier().addFilter(subQuery.getQuery(), conditions);
        }
    }
    // AstBeSqlVar beSqlVar = subQuery.getAstBeSqlVar();
    // AstStart start = SqlQuery.parse(context.getParameter(beSqlVar.getName()));
    // subQuery.addChild(start.getQuery());
    // subQuery.replaceWith(beSqlVar);
    // subQuery.removeChildren();
    String key = "<sql> SubQuery# " + (context.getSubQueries().size() + 1) + "</sql>";
    context.getSubQueries().put(key, subQuery);
    subQuery.replaceWith(new AstStringPart(key));
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) HashMap(java.util.HashMap) AstStringPart(com.developmentontheedge.sql.model.AstStringPart)

Aggregations

AstStart (com.developmentontheedge.sql.model.AstStart)34 Test (org.junit.Test)26 ContextApplier (com.developmentontheedge.sql.format.ContextApplier)16 Context (com.developmentontheedge.sql.format.Context)13 Formatter (com.developmentontheedge.sql.format.Formatter)13 DefaultParserContext (com.developmentontheedge.sql.model.DefaultParserContext)11 BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)10 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)8 HashMap (java.util.HashMap)8 Map (java.util.Map)7 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)6 Collections (java.util.Collections)6 Assert (org.junit.Assert)5 QueryResolver (com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver)4 ColumnRef (com.developmentontheedge.sql.format.ColumnRef)4 FilterApplier (com.developmentontheedge.sql.format.FilterApplier)4 Ignore (org.junit.Ignore)4 ColumnAdder (com.developmentontheedge.sql.format.ColumnAdder)3 Dbms (com.developmentontheedge.sql.format.Dbms)3 LimitsApplier (com.developmentontheedge.sql.format.LimitsApplier)3