Search in sources :

Example 26 with AstStart

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

the class OrderByFilterTest method testOrderByFilter.

@Test
public void testOrderByFilter() {
    AstStart start = SqlQuery.parse("SELECT t.a, t.b, t.c AS foo FROM myTable t WHERE t.b > 2");
    Map<String, String> columns = new HashMap<String, String>();
    columns.put("t.a", "ASC");
    columns.put("foo", "DESC");
    new OrderByFilter().apply(start, columns);
    assertEquals("SELECT t.a, t.b, t.c AS foo FROM myTable t WHERE t.b > 2 ORDER BY 1 ASC, 3 DESC", new Formatter().format(start, new Context(Dbms.MYSQL), new DefaultParserContext()));
}
Also used : Context(com.developmentontheedge.sql.format.Context) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) HashMap(java.util.HashMap) Formatter(com.developmentontheedge.sql.format.Formatter) DefaultParserContext(com.developmentontheedge.sql.model.DefaultParserContext) OrderByFilter(com.developmentontheedge.sql.format.OrderByFilter) Test(org.junit.Test)

Example 27 with AstStart

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

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

the class SubQueryTest method testSubQueryResultParse.

@Test
public void testSubQueryResultParse() {
    String sql = "SELECT\n" + "      t.name AS \"___Name\",\n" + "      '<sql>SubQuery#1</sql>' AS \"testtUserValues\"\n" + "    FROM\n" + "      testtable t ORDER BY 2 LIMIT 2147483647";
    AstStart start = SqlQuery.parse(sql);
// assertEquals(sql, start.format());
}
Also used : AstStart(com.developmentontheedge.sql.model.AstStart) Test(org.junit.Test)

Example 29 with AstStart

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

use of com.developmentontheedge.sql.model.AstStart 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

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