Search in sources :

Example 11 with ContextApplier

use of com.developmentontheedge.sql.format.ContextApplier in project be5 by DevelopmentOnTheEdge.

the class ContextApplierApplyParametersTest method typeSupport.

@Test
public void typeSupport() {
    AstStart start = SqlQuery.parse("SELECT * FROM table WHERE date = <parameter:date type=\"java.sql.Date\"/>");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("date", "1900-01-01").build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table WHERE date = '1900-01-01'", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) BasicQueryContext(com.developmentontheedge.sql.format.BasicQueryContext) Test(org.junit.Test)

Example 12 with ContextApplier

use of com.developmentontheedge.sql.format.ContextApplier 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 13 with ContextApplier

use of com.developmentontheedge.sql.format.ContextApplier in project be5 by DevelopmentOnTheEdge.

the class SubQueryTest method testSubQueryResolverExecInclude.

@Test
public void testSubQueryResolverExecInclude() {
    AstStart start = SqlQuery.parse("SELECT * FROM table t\n" + "            LEFT JOIN (<sql exec=\"include\" entity=\"meters\" queryName=\"*** Selection view ***\"></sql>) m\n" + "                ON m.ID = t.meterID");
    QueryResolver resolver = (entity, query) -> {
        assertEquals("meters", entity);
        assertEquals("*** Selection view ***", query);
        return "SELECT m.ID FROM public.meters m";
    };
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().queryResolver(resolver).build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table t\n" + "            LEFT JOIN (SELECT m.ID FROM public.meters m) m\n" + "                ON m.ID = t.meterID", start.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) Test(org.junit.Test)

Example 14 with ContextApplier

use of com.developmentontheedge.sql.format.ContextApplier 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 15 with ContextApplier

use of com.developmentontheedge.sql.format.ContextApplier 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)

Aggregations

ContextApplier (com.developmentontheedge.sql.format.ContextApplier)16 AstStart (com.developmentontheedge.sql.model.AstStart)15 Test (org.junit.Test)15 BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)11 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)7 HashMap (java.util.HashMap)5 QueryResolver (com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver)4 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Assert (org.junit.Assert)4 Ignore (org.junit.Ignore)4 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1