Search in sources :

Example 6 with ContextApplier

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

the class SubQueryTest method testSubQueryResolverBeVar.

@Test
public void testSubQueryResolverBeVar() {
    AstStart start = SqlQuery.parse("SELECT 'select * from test' AS \"___code\", '<sql><var:___code safestr=\"no\"/></sql>' FROM table");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
    contextApplier.applyContext(start);
    assertEquals("SELECT 'select * from test' AS \"___code\", '<sql> SubQuery# 1</sql>' FROM table", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) Test(org.junit.Test)

Example 7 with ContextApplier

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

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

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

the class ContextApplierApplyParametersTest method typeSupportNumber.

@Test
public void typeSupportNumber() {
    AstStart start = SqlQuery.parse("SELECT * FROM table WHERE totalSize = <parameter:totalSize type=\"java.lang.Integer\"/>");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("totalSize", "123").build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table WHERE totalSize = 123", 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 10 with ContextApplier

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

the class ContextApplierApplyParametersTest method typeSupportNumberNoParameterInIfCondition.

@Test
public void typeSupportNumberNoParameterInIfCondition() {
    AstStart start = SqlQuery.parse("SELECT * FROM table WHERE (1=1)" + "<if parameter=\"totalSize\"> AND totalSize = <parameter:totalSize type=\"java.lang.Integer\"/></if>");
    ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
    contextApplier.applyContext(start);
    assertEquals("SELECT * FROM table WHERE (1 = 1)", start.getQuery().toString());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) ContextApplier(com.developmentontheedge.sql.format.ContextApplier) 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