Search in sources :

Example 1 with ParserContext

use of com.developmentontheedge.sql.model.ParserContext 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)

Aggregations

Context (com.developmentontheedge.sql.format.Context)1 Formatter (com.developmentontheedge.sql.format.Formatter)1 MacroExpander (com.developmentontheedge.sql.format.MacroExpander)1 AstStart (com.developmentontheedge.sql.model.AstStart)1 ParserContext (com.developmentontheedge.sql.model.ParserContext)1 SqlParser (com.developmentontheedge.sql.model.SqlParser)1 Test (org.junit.Test)1