use of com.developmentontheedge.sql.model.AstStart 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());
}
use of com.developmentontheedge.sql.model.AstStart 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());
}
use of com.developmentontheedge.sql.model.AstStart 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());
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class DpsHelper method addParamsFromQuery.
public <T extends DynamicPropertySet> T addParamsFromQuery(T dps, BeModelElement modelElements, Query query) {
AstStart ast;
try {
ast = SqlQuery.parse(meta.getQueryCode(query, UserInfoHolder.getCurrentRoles()));
} catch (RuntimeException e) {
log.log(Level.SEVERE, "SqlQuery.parse error: ", e);
throw Be5Exception.internalInQuery(e, query);
}
List<String> usedParams = ast.tree().select(AstBeParameterTag.class).map(AstBeParameterTag::getName).toList();
addDpForColumns(dps, modelElements, usedParams);
return dps;
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class Be5QueryExecutor method hasColumnWithLabel.
private boolean hasColumnWithLabel(AstStart ast, String idColumnLabel) {
AstQuery query = ast.getQuery();
Optional<AstSelect> selectOpt = query.children().select(AstSelect.class).collect(MoreCollectors.onlyOne());
if (!selectOpt.isPresent())
return false;
AstSelect select = selectOpt.get();
return select.getSelectList().children().select(AstDerivedColumn.class).map(AstDerivedColumn::getAlias).nonNull().map(alias -> alias.replaceFirst("^\"(.+)\"$", "$1")).has(idColumnLabel);
}
Aggregations