use of com.developmentontheedge.sql.model.AstStart 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.AstStart 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.AstStart in project be5 by DevelopmentOnTheEdge.
the class Formatter method format.
public String format(AstStart start, Context context, ParserContext parserContext) {
DbmsTransformer dbmsTransformer = context.getDbmsTransformer();
dbmsTransformer.setParserContext(parserContext);
AstStart clone = start.clone();
dbmsTransformer.transformAst(clone);
return clone.format();
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method applySubQuery.
private void applySubQuery(AstBeSqlSubQuery subQuery) {
if (subQuery.getExec() != null || subQuery.getQueryName() != null) {
String name = subQuery.getQueryName();
if (name == null) {
throw new IllegalStateException("Empty subQuery without queryName parameter: " + subQuery.format());
}
String entity = subQuery.getEntityName();
String subQueryText = context.resolveQuery(entity, name);
if (subQueryText == null) {
throw new IllegalStateException("Unable to resolve subquery: " + (entity == null ? "" : entity + ".") + name);
}
AstStart start = SqlQuery.parse(subQueryText);
subQuery.addChild(start.getQuery());
}
if (subQuery.getQuery() != null) {
if (subQuery.getOutColumns() != null)
new ColumnsApplier().keepOnlyOutColumns(subQuery);
if (subQuery.getLimit() != null)
new LimitsApplier(0, subQuery.getLimit()).transformQuery(subQuery.getQuery());
String keyStr = subQuery.getFilterKeys();
String valPropStr = subQuery.getFilterValProperties();
if (keyStr != null && valPropStr != null) {
String[] keys = keyStr.split(",");
String[] valProps = valPropStr.split(",");
Map<ColumnRef, String> conditions = new HashMap<>();
for (int i = 0; i < keys.length; i++) {
// ColumnRef.resolve(subQuery.getQuery(), keys[i])
conditions.put(new ColumnRef(null, keys[i]), "<var:" + valProps[i] + "/>");
// subQuery.addParameter(keys[i], valProps[i]);
}
// new FilterApplier().addFilter(subQuery.getQuery(), conditions);
}
}
// AstBeSqlVar beSqlVar = subQuery.getAstBeSqlVar();
// AstStart start = SqlQuery.parse(context.getParameter(beSqlVar.getName()));
// subQuery.addChild(start.getQuery());
// subQuery.replaceWith(beSqlVar);
// subQuery.removeChildren();
String key = "<sql> SubQuery# " + (context.getSubQueries().size() + 1) + "</sql>";
context.getSubQueries().put(key, subQuery);
subQuery.replaceWith(new AstStringPart(key));
}
Aggregations