Search in sources :

Example 1 with FreemarkerSqlHandler

use of com.developmentontheedge.be5.metadata.freemarker.FreemarkerSqlHandler in project be5 by DevelopmentOnTheEdge.

the class FreemarkerSqlHandlerTest method testHandler.

@Test
public void testHandler() throws UnsupportedEncodingException, IOException, FreemarkerSqlException, ProjectElementException {
    ByteArrayOutputStream log = new ByteArrayOutputStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(log, true, "UTF-8");
    PrintStream psOut = new PrintStream(out, true, "UTF-8");
    SqlExecutor executor = new SqlExecutor(new NullConnector(), ps, SqlExecutor.class.getResource("basesql.properties"));
    FreemarkerSqlHandler handler = new FreemarkerSqlHandler(executor, true, new WriterLogger(psOut));
    Project proj = new Project("test");
    proj.setDatabaseSystem(Rdbms.POSTGRESQL);
    FreemarkerScript sql = new FreemarkerScript("sql", proj.getApplication().getFreemarkerScripts());
    DataElementUtils.save(sql);
    FreemarkerScript sql2 = new FreemarkerScript("sql2", proj.getApplication().getFreemarkerScripts());
    DataElementUtils.save(sql2);
    sql2.setSource("UPDATE test SET b = 'c';");
    sql.setSource("delete from test;-- hehehe\nINSERT INTO \"test\" VALUES('a','b','c');\nBEGIN update test SET a='a''b END;';END;\nDELETE FROM test;<#include 'sql2'/>");
    handler.execute(sql);
    String result = new String(log.toByteArray(), StandardCharsets.UTF_8);
    String expected = "\n" + "-- Execute test/application/Scripts/sql\n" + "-- At test/application/Scripts/sql[1,1]-[1,17]\n" + "delete from test;\n" + "-- At test/application/Scripts/sql[2,1]-[2,39]\n" + "INSERT INTO \"test\" VALUES('a','b','c');\n" + "-- At test/application/Scripts/sql[3,1]-[3,40]\n" + "BEGIN update test SET a='a''b END;';END;\n" + "-- At test/application/Scripts/sql[4,1]-[4,17]\n" + "DELETE FROM test;\n" + "-- At test/application/Scripts/sql[4,18]-[4,35]\n" + "-- At test/application/Scripts/sql2[1,1]-[1,24]\n" + "\n" + "-- Start of included test/application/Scripts/sql2\n" + "UPDATE test SET b = 'c';\n" + "-- End of included test/application/Scripts/sql2\n";
    assertEquals(expected, result.replace("\r", ""));
    String outResult = new String(out.toByteArray(), StandardCharsets.UTF_8);
    String outExpected = "xx:xx:xx: [>] test/application/Scripts/sql\n" + "xx:xx:xx: [>]   test/application/Scripts/sql2\n";
    assertEquals(outExpected, outResult.replace("\r", "").replaceAll("\\d\\d", "xx"));
}
Also used : PrintStream(java.io.PrintStream) SqlExecutor(com.developmentontheedge.dbms.SqlExecutor) WriterLogger(com.developmentontheedge.be5.metadata.util.WriterLogger) Project(com.developmentontheedge.be5.metadata.model.Project) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 2 with FreemarkerSqlHandler

use of com.developmentontheedge.be5.metadata.freemarker.FreemarkerSqlHandler in project be5 by DevelopmentOnTheEdge.

the class AppData method executeScript.

protected void executeScript(final SqlExecutor sqlExecutor, FreemarkerScript freemarkerScript) throws ProjectElementException, IOException {
    String compiled = freemarkerScript.getResult().validate();
    if (logPath != null) {
        Files.write(logPath.toPath().resolve(be5Project.getName() + "_script_" + freemarkerScript.getModule().getName() + "_" + freemarkerScript.getName() + ".compiled"), compiled.getBytes(StandardCharsets.UTF_8));
    }
    String sql = compiled.trim();
    if (sql.isEmpty())
        return;
    DataElementPath path = freemarkerScript.getCompletePath();
    if (debug)
        System.err.println(sql);
    sqlExecutor.comment("Execute " + path);
    new FreemarkerSqlHandler(sqlExecutor, debug, logger).execute(freemarkerScript);
}
Also used : DataElementPath(com.developmentontheedge.be5.metadata.model.base.DataElementPath) FreemarkerSqlHandler(com.developmentontheedge.be5.metadata.freemarker.FreemarkerSqlHandler)

Aggregations

FreemarkerSqlHandler (com.developmentontheedge.be5.metadata.freemarker.FreemarkerSqlHandler)1 FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)1 Project (com.developmentontheedge.be5.metadata.model.Project)1 DataElementPath (com.developmentontheedge.be5.metadata.model.base.DataElementPath)1 WriterLogger (com.developmentontheedge.be5.metadata.util.WriterLogger)1 SqlExecutor (com.developmentontheedge.dbms.SqlExecutor)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Test (org.junit.Test)1