use of com.developmentontheedge.sql.format.MacroExpander 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.format.MacroExpander in project be5 by DevelopmentOnTheEdge.
the class MacroTest method testMacro.
@Test
public void testMacro() {
SqlParser parser = new SqlParser();
String input = "MACRO A(arg1=default, arg2=NOW()) \'<!--\' || CAST((arg2) AS CHAR) || \'-->\' || \'<a href=\"...\">\' || arg1 || \'</a>\' END";
parser.parse(input);
if (!parser.getMessages().isEmpty()) {
throw new IllegalArgumentException(String.join("\n", parser.getMessages()));
}
ParserContext context = parser.getContext();
AstStart start = SqlQuery.parse("SELECT A(a, b) FROM table t", context);
new MacroExpander().expandMacros(start);
assertEquals("SELECT \'<!--\' || TO_CHAR(( b))|| \'-->\' || \'<a href=\"...\">\' || a || \'</a>\' FROM table t", new Formatter().format(start, new Context(Dbms.ORACLE), context));
start = SqlQuery.parse("SELECT A(a) FROM table t", context);
new MacroExpander().expandMacros(start);
assertEquals("SELECT \'<!--\' || TO_CHAR((SYSDATE))|| \'-->\' || \'<a href=\"...\">\' || a || \'</a>\' FROM table t", new Formatter().format(start, new Context(Dbms.ORACLE), context));
SqlParser newParser = new SqlParser();
input = "MACRO B(arg1, arg2, arg3) arg1 || arg2 || A(arg3) END";
newParser.setContext(context);
newParser.parse(input);
if (!newParser.getMessages().isEmpty()) {
throw new IllegalArgumentException(String.join("\n", newParser.getMessages()));
}
start = SqlQuery.parse("SELECT B(a, b, c) FROM table t", context);
new MacroExpander().expandMacros(start);
assertEquals("SELECT a || b || '<!--' || TO_CHAR((SYSDATE))|| \'-->\' || '<a href=\"...\">\' || c || \'</a>\' FROM table t", new Formatter().format(start, new Context(Dbms.ORACLE), context));
}
use of com.developmentontheedge.sql.format.MacroExpander in project be5 by DevelopmentOnTheEdge.
the class ParserTarget method loadAndParseQuery.
protected void loadAndParseQuery(Element element) {
query = XmlTest.getCData(element);
parser.parse(query);
if (!parser.getMessages().isEmpty())
fail(parser.getMessages().get(0));
astStart = parser.getStartNode();
new MacroExpander().expandMacros(astStart);
}
Aggregations