use of com.developmentontheedge.sql.model.AstBeSqlSubQuery in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method getSubQueryPropertyMap.
public Map<String, String> getSubQueryPropertyMap(String key, Function<String, String> varResolver) {
Map<String, String> propertyMap = new HashMap<>();
AstBeSqlSubQuery subQuery = context.getSubQueries().get(key);
if (subQuery == null)
return propertyMap;
String keyStr = subQuery.getFilterKeys();
String valPropStr = subQuery.getFilterValProperties();
if (keyStr != null && valPropStr != null) {
String[] keys = keyStr.split(",");
String[] valProps = valPropStr.split(",");
for (int i = 0; i < keys.length; i++) {
propertyMap.put(keys[i], varResolver.apply(valProps[i]));
}
}
return propertyMap;
}
use of com.developmentontheedge.sql.model.AstBeSqlSubQuery in project be5 by DevelopmentOnTheEdge.
the class ContextApplier method applyVars.
/**
* todo rename getSubQuery
*/
public AstBeSqlSubQuery applyVars(String key, Function<String, String> varResolver) {
AstBeSqlSubQuery subQuery = context.getSubQueries().get(key);
if (subQuery == null)
return null;
AstBeSqlSubQuery result = (AstBeSqlSubQuery) subQuery.clone();
if (result.getLimit() != null)
new LimitsApplier(0, result.getLimit()).transformQuery(result.getQuery());
applyFirstLevelVars(1, result, (AstBeSqlVar node) -> applyVar(key, node, varResolver));
// result.tree().select( AstBeSqlVar.class ).forEach( varNode -> {
// String value = varResolver.apply( subQuery.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 );
// } );
checkExpression(result, key, varResolver);
return result;
}
use of com.developmentontheedge.sql.model.AstBeSqlSubQuery 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.AstBeSqlSubQuery 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.AstBeSqlSubQuery in project be5 by DevelopmentOnTheEdge.
the class Be5QueryExecutor method executeSubQuery.
@Override
public List<DynamicPropertySet> executeSubQuery(String subqueryName, CellFormatter.VarResolver varResolver) {
AstBeSqlSubQuery subQuery = contextApplier.applyVars(subqueryName, varResolver::resolve);
if (subQuery.getQuery() == null) {
return Collections.emptyList();
}
String finalSql = new Formatter().format(subQuery.getQuery(), context, parserContext);
List<DynamicPropertySet> dynamicPropertySets;
try {
dynamicPropertySets = listDps(finalSql);
} catch (Throwable e) {
// TODO only for Document presentation, for operations must be error throw
Be5Exception be5Exception = Be5Exception.internalInQuery(e, query);
log.log(Level.SEVERE, be5Exception.toString() + " Final SQL: " + finalSql, be5Exception);
DynamicPropertySetSupport dynamicProperties = new DynamicPropertySetSupport();
dynamicProperties.add(new DynamicProperty("___ID", String.class, "-1"));
dynamicProperties.add(new DynamicProperty("error", String.class, UserInfoHolder.isSystemDeveloper() ? Be5Exception.getMessage(e) : "error"));
dynamicPropertySets = Collections.singletonList(dynamicProperties);
}
// return Collections.singletonList(dynamicProperties);
return dynamicPropertySets;
}
Aggregations