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()));
}
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());
}
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());
}
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());
}
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());
}
Aggregations