Search in sources :

Example 1 with QueryResolver

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

the class SubQueryTest method testApplyWithVarsExecDelayedAddFilter.

@Test
@Ignore
public void testApplyWithVarsExecDelayedAddFilter() {
    // TODO us.ID vs ID in be3 (us from entity)
    AstStart start = SqlQuery.parse("SELECT '<sql exec=\"delayed\" filterKey=\"us.ID\" filterValProperty=\"___usID\" limit=\"1\" " + "entity=\"utilitySuppliers\" queryName=\"*** Selection view ***\" outColumns=\"Name\"></sql>' AS \"Услуга\",\n" + "ID AS \"___usID\" FROM table");
    QueryResolver resolver = (entity, query) -> {
        assertEquals("utilitySuppliers", entity);
        assertEquals("*** Selection view ***", query);
        return "SELECT us.ID AS \"CODE\",us.utilityType AS \"Name\" FROM utilitySuppliers us";
    };
    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("___usID", "5");
    AstBeSqlSubQuery subQuery = contextApplier.applyVars(key, vars::get);
    assertEquals("SELECT '" + key + "' AS \"Услуга\",\n" + "ID AS \"___usID\" FROM table", start.format());
    assertEquals("SELECT us.utilityType AS \"Name\" " + "FROM utilitySuppliers us WHERE us.ID = 5 LIMIT 1", 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with QueryResolver

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

the class SubQueryTest method testSubQueryResolver.

@Test
public void testSubQueryResolver() {
    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/>";
    };
    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 3 with QueryResolver

use of com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver 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 4 with QueryResolver

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

Aggregations

BasicQueryContext (com.developmentontheedge.sql.format.BasicQueryContext)4 QueryResolver (com.developmentontheedge.sql.format.BasicQueryContext.QueryResolver)4 ContextApplier (com.developmentontheedge.sql.format.ContextApplier)4 AstBeSqlSubQuery (com.developmentontheedge.sql.model.AstBeSqlSubQuery)4 AstStart (com.developmentontheedge.sql.model.AstStart)4 SqlQuery (com.developmentontheedge.sql.model.SqlQuery)4 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Assert (org.junit.Assert)4 Ignore (org.junit.Ignore)4 Test (org.junit.Test)4