Search in sources :

Example 6 with AstBeSqlSubQuery

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

the class SubQueryTest method testApplyWithVars.

@Test
public void testApplyWithVars() {
    AstStart start = SqlQuery.parse("SELECT ID, '<sql limit=\"2\">SELECT COUNT(*) FROM subTable WHERE tableID=<var:ID/></sql> entries' 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 COUNT(*) FROM subTable WHERE tableID = 5 LIMIT 2", subQuery.getQuery().format());
    assertEquals("SELECT ID, '" + key + " entries' FROM table", start.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 7 with AstBeSqlSubQuery

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

the class SubQueryTest method testSubQueryResolverCheckExpression.

@Test
public void testSubQueryResolverCheckExpression() {
    AstStart start = SqlQuery.parse("SELECT ID, '<sql limit=\"2\" queryName=\"test\"></sql>' FROM table");
    QueryResolver resolver = (entity, query) -> {
        assertNull(entity);
        assertEquals("test", query);
        return "SELECT * FROM subTable WHERE tableID=<var:ID/>" + "<if parameter=\"idList\">\n" + "   AND us.ID IN <parameter:idList multiple=\"true\" refColumn=\"utilitySuppliers.ID\" />\n" + "</if>";
    };
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().queryResolver(resolver).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 : QueryResolver(com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver) Ignore(org.junit.Ignore) Map(java.util.Map) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) HashMap(java.util.HashMap) Test(org.junit.Test) AstStart(com.developmentontheedge.sql.model.AstStart) Assert(org.junit.Assert) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) SqlQuery(com.developmentontheedge.sql.model.SqlQuery) Collections(java.util.Collections) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) QueryResolver(com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver) AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) Test(org.junit.Test)

Example 8 with AstBeSqlSubQuery

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

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

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

the class ContextApplier method applyVar.

private void applyVar(String key, AstBeSqlVar varNode, Function<String, String> varResolver) {
    String value = varResolver.apply(context.getSubQueries().get(key).translateVar(varNode.getName()));
    if (value == null)
        value = varNode.getDefault();
    SimpleNode constant;
    if (varNode.jjtGetParent() instanceof AstBeSqlSubQuery && value != null && !"".equals(value.trim())) {
        constant = SqlQuery.parse(value).getQuery();
    } else {
        if (value == null) {
            value = "null";
        }
        constant = varNode.jjtGetParent() instanceof AstStringConstant ? new AstStringPart(value) : new AstIdentifierConstant(value);
    }
    varNode.replaceWith(constant);
}
Also used : AstIdentifierConstant(com.developmentontheedge.sql.model.AstIdentifierConstant) AstBeSqlSubQuery(com.developmentontheedge.sql.model.AstBeSqlSubQuery) AstStringConstant(com.developmentontheedge.sql.model.AstStringConstant) AstStringPart(com.developmentontheedge.sql.model.AstStringPart) SimpleNode(com.developmentontheedge.sql.model.SimpleNode)

Aggregations

AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)11 ContextApplier (com.developmentontheedge.sql.format.ContextApplier)6 AstStart (com.developmentontheedge.sql.model.AstStart)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)3 QueryResolver (com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver)3 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)3 Collections (java.util.Collections)3 Map (java.util.Map)3 Assert (org.junit.Assert)3 Ignore (org.junit.Ignore)3 SimpleNode (com.developmentontheedge.sql.model.SimpleNode)2 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)1 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)1 DynamicPropertySet (com.developmentontheedge.beans.DynamicPropertySet)1 DynamicPropertySetSupport (com.developmentontheedge.beans.DynamicPropertySetSupport)1 Formatter (com.developmentontheedge.sql.format.Formatter)1 AstBeCondition (com.developmentontheedge.sql.model.AstBeCondition)1 AstBeConditionChain (com.developmentontheedge.sql.model.AstBeConditionChain)1