use of com.developmentontheedge.sql.model.AstBeSqlSubQuery 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.AstBeSqlSubQuery 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());
}
use of com.developmentontheedge.sql.model.AstBeSqlSubQuery 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());
}
use of com.developmentontheedge.sql.model.AstBeSqlSubQuery in project be5 by DevelopmentOnTheEdge.
the class SubQueryTest method testVarsInFieldReference.
@Test
public void testVarsInFieldReference() {
AstStart start = SqlQuery.parse("SELECT ID, '<sql limit=\"2\">SELECT field.<var:reference/> FROM table.<var:name/></sql>' FROM table");
ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
contextApplier.applyContext(start);
String key = contextApplier.subQueryKeys().findFirst().get();
Map<String, String> vars = new HashMap<String, String>();
vars.put("reference", "ref");
vars.put("name", "name");
AstBeSqlSubQuery subQuery = contextApplier.applyVars(key, vars::get);
assertEquals("SELECT field.ref FROM table.name LIMIT 2", subQuery.getQuery().format());
}
use of com.developmentontheedge.sql.model.AstBeSqlSubQuery in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method applyVar.
private void applyVar(String key, AstBeSqlVar varNode, Function<String, String> varResolver) {
String value = varResolver.apply(context.getSubQueries().get(key).translateVar(varNode.getName()));
if (value == null)
value = varNode.getDefault();
SimpleNode constant;
if (varNode.jjtGetParent() instanceof AstBeSqlSubQuery && value != null && !"".equals(value.trim())) {
constant = SqlQuery.parse(value).getQuery();
} else {
if (value == null) {
value = "null";
}
constant = varNode.jjtGetParent() instanceof AstStringConstant ? new AstStringPart(value) : new AstIdentifierConstant(value);
}
varNode.replaceWith(constant);
}
Aggregations