use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class Be5QueryExecutor method getFinalSql.
@Override
public String getFinalSql() {
DebugQueryLogger dql = new DebugQueryLogger();
dql.log("Orig", query.getQuery());
String queryText = meta.getQueryCode(query, UserInfoHolder.getCurrentRoles());
dql.log("After FreeMarker", queryText);
if (queryText.isEmpty())
return "";
AstStart ast;
try {
ast = SqlQuery.parse(queryText);
} catch (RuntimeException e) {
log.log(Level.SEVERE, "SqlQuery.parse error: ", e);
throw Be5Exception.internalInQuery(e, query);
// ast = SqlQuery.parse("select 'error'");
}
dql.log("Compiled", ast);
resolveTypeOfRefColumn(ast);
// CONTEXT
// FILTERS
filterHelper.applyFilters(ast, query.getEntity().getName(), new HashMap<>(parametersMap));
// CATEGORY
// applyCategory( dql, ast );
contextApplier.applyContext(ast);
subQueryKeys = contextApplier.subQueryKeys().toSet();
dql.log("With context", ast);
// ID COLUMN
if (query.getType() == QueryType.D1 && query.getEntity().findTableDefinition() != null && !hasColumnWithLabel(ast, DatabaseConstants.ID_COLUMN_LABEL)) {
new ColumnAdder().addColumn(ast, query.getEntity().getName(), query.getEntity().getPrimaryKey(), DatabaseConstants.ID_COLUMN_LABEL);
dql.log("With ID column", ast);
} else {
dql.log("Without ID column", ast);
}
// SIMPLIFY
Simplifier.simplify(ast);
dql.log("Simplified", ast);
if (extraQuery == ExtraQuery.COUNT) {
countFromQuery(ast.getQuery());
dql.log("Count(1) from query", ast);
}
if (extraQuery == ExtraQuery.DEFAULT) {
// SORT ORDER
applySort(dql, ast);
// LIMITS
new LimitsApplier(offset, limit).transform(ast);
dql.log("With limits", ast);
}
return new Formatter().format(ast, context, parserContext);
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class Project method translateSQL.
/**
* Try to translate the SQL query to the current DBMS using com.developmentontheedge.sql
* @param sql
* @return
*/
public String translateSQL(String sql) {
if (beSQL > 0) {
if (--beSQL == 0)
reconfigureFreemarker();
}
if (sqlParser == null) {
throw new IllegalStateException("translateSQL was called without enterSQL");
}
sqlParser.parse(sql);
List<String> messages = sqlParser.getMessages();
if (!messages.isEmpty()) {
throw new IllegalArgumentException(("SQL cannot be parsed:\nQuery:" + sql + "\nErrors: " + String.join("\n", messages)).replace("\r", "").replace("\n", System.lineSeparator()));
}
AstStart ast = sqlParser.getStartNode();
if (databaseSystem != Rdbms.BESQL) {
new MacroExpander().expandMacros(ast);
Dbms dbms = databaseSystem == null ? Dbms.POSTGRESQL : Dbms.valueOf(databaseSystem.name());
DbmsTransformer dbmsTransformer = new Context(dbms).getDbmsTransformer();
dbmsTransformer.setParserContext(sqlParser.getContext());
dbmsTransformer.transformAst(ast);
}
return ast.format();
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class ColumnAdderTest method check.
protected void check(String tableName, String columnName, String query, String expected) {
AstStart ast = parse(query);
new ColumnAdder().addColumn(ast, tableName, columnName, "___ID");
assertEquals(expected, ast.format());
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class ContextApplierApplyParametersTest method typeSupportNumber.
@Test
public void typeSupportNumber() {
AstStart start = SqlQuery.parse("SELECT * FROM table WHERE totalSize = <parameter:totalSize type=\"java.lang.Integer\"/>");
ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().parameter("totalSize", "123").build());
contextApplier.applyContext(start);
assertEquals("SELECT * FROM table WHERE totalSize = 123", start.getQuery().toString());
}
use of com.developmentontheedge.sql.model.AstStart in project be5 by DevelopmentOnTheEdge.
the class ContextApplierApplyParametersTest method typeSupportNumberNoParameterInIfCondition.
@Test
public void typeSupportNumberNoParameterInIfCondition() {
AstStart start = SqlQuery.parse("SELECT * FROM table WHERE (1=1)" + "<if parameter=\"totalSize\"> AND totalSize = <parameter:totalSize type=\"java.lang.Integer\"/></if>");
ContextApplier contextApplier = new ContextApplier(new BasicQueryContext.Builder().build());
contextApplier.applyContext(start);
assertEquals("SELECT * FROM table WHERE (1 = 1)", start.getQuery().toString());
}
Aggregations