Search in sources :

Example 1 with MacroExpander

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();
}
Also used : LoadContext(com.developmentontheedge.be5.metadata.serialization.LoadContext) Context(com.developmentontheedge.sql.format.Context) AstStart(com.developmentontheedge.sql.model.AstStart) MacroExpander(com.developmentontheedge.sql.format.MacroExpander) Dbms(com.developmentontheedge.sql.format.Dbms) DbmsTransformer(com.developmentontheedge.sql.format.DbmsTransformer)

Example 2 with MacroExpander

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));
}
Also used : Context(com.developmentontheedge.sql.format.Context) ParserContext(com.developmentontheedge.sql.model.ParserContext) AstStart(com.developmentontheedge.sql.model.AstStart) MacroExpander(com.developmentontheedge.sql.format.MacroExpander) Formatter(com.developmentontheedge.sql.format.Formatter) SqlParser(com.developmentontheedge.sql.model.SqlParser) ParserContext(com.developmentontheedge.sql.model.ParserContext) Test(org.junit.Test)

Example 3 with MacroExpander

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);
}
Also used : MacroExpander(com.developmentontheedge.sql.format.MacroExpander)

Aggregations

MacroExpander (com.developmentontheedge.sql.format.MacroExpander)3 Context (com.developmentontheedge.sql.format.Context)2 AstStart (com.developmentontheedge.sql.model.AstStart)2 LoadContext (com.developmentontheedge.be5.metadata.serialization.LoadContext)1 Dbms (com.developmentontheedge.sql.format.Dbms)1 DbmsTransformer (com.developmentontheedge.sql.format.DbmsTransformer)1 Formatter (com.developmentontheedge.sql.format.Formatter)1 ParserContext (com.developmentontheedge.sql.model.ParserContext)1 SqlParser (com.developmentontheedge.sql.model.SqlParser)1 Test (org.junit.Test)1