use of com.developmentontheedge.sql.format.Formatter in project be5 by DevelopmentOnTheEdge.
the class OrderByFilterTest method testOrderByFilterUnion.
@Test
public void testOrderByFilterUnion() {
AstStart start = SqlQuery.parse("SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%'");
Map<String, String> columns = Collections.singletonMap("name", "DESC");
new OrderByFilter().apply(start, columns);
assertEquals("SELECT * FROM (SELECT name FROM bbc WHERE name LIKE 'Z%' UNION SELECT name FROM actor WHERE name LIKE 'Z%') " + "tmp ORDER BY 1 DESC", new Formatter().format(start, new Context(Dbms.MYSQL), new DefaultParserContext()));
}
use of com.developmentontheedge.sql.format.Formatter in project be5 by DevelopmentOnTheEdge.
the class ParserTarget method format.
protected void format(String dbmsName, Element element) {
Dbms dbms;
if (Dbms.DB2.getName().equals(dbmsName))
dbms = Dbms.DB2;
else if (Dbms.MYSQL.getName().equals(dbmsName))
dbms = Dbms.MYSQL;
else if (Dbms.ORACLE.getName().equals(dbmsName))
dbms = Dbms.ORACLE;
else if (Dbms.POSTGRESQL.getName().equals(dbmsName))
dbms = Dbms.POSTGRESQL;
else if (Dbms.SQLSERVER.getName().equals(dbmsName))
dbms = Dbms.SQLSERVER;
else {
test.getResult().addError(test, new Exception("Unknown DBMS \"" + dbmsName + "\". DBMS should be: db2, mysql, oracle, postgresql or sqlserver."));
return;
}
Formatter formatter = new Formatter();
String formatResult = trimAllLine(formatter.format(astStart, new Context(dbms), parser.getContext()));
String result = trimAllLine(XmlTest.getCData(element));
assertEquals("Incorrect result, dbms=" + dbms.getName(), result, formatResult);
if (!formatResult.equals(result))
test.getResult().addFailure(test, new ComparisonFailure("Incorrect result, dbms=" + dbms.getName(), result, formatResult));
}
use of com.developmentontheedge.sql.format.Formatter in project be5 by DevelopmentOnTheEdge.
the class Be5QueryExecutor method applySort.
private void applySort(DebugQueryLogger dql, AstStart ast) {
if (sortColumn >= 0) {
try {
DynamicProperty[] schema = getSchema(new Formatter().format(ast, context, parserContext));
int sortCol = getQuerySortingColumn(schema);
if (sortCol > 0) {
AstSelect sel = (AstSelect) ast.getQuery().jjtGetChild(ast.getQuery().jjtGetNumChildren() - 1);
AstOrderBy orderBy = sel.getOrderBy();
if (orderBy == null) {
orderBy = new AstOrderBy();
sel.addChild(orderBy);
AstLimit astLimit = sel.children().select(AstLimit.class).findFirst().orElse(null);
if (astLimit != null) {
sel.removeChild(astLimit);
sel.addChild(astLimit);
}
}
AstOrderingElement oe = new AstOrderingElement(AstNumericConstant.of(sortCol));
if (sortDesc) {
oe.setDirectionToken(new Token(0, "DESC"));
}
orderBy.addChild(oe);
orderBy.moveToFront(oe);
}
dql.log("With sort", ast);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
use of com.developmentontheedge.sql.format.Formatter 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;
}
use of com.developmentontheedge.sql.format.Formatter 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);
}
Aggregations