Search in sources :

Example 6 with FreemarkerScript

use of com.developmentontheedge.be5.metadata.model.FreemarkerScript 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 7 with FreemarkerScript

use of com.developmentontheedge.be5.metadata.model.FreemarkerScript 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)

Example 8 with FreemarkerScript

use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.

the class AppTools method execute.

@Override
public void execute() throws MojoFailureException {
    init();
    BeConnectionProfile prof = be5Project.getConnectionProfile();
    if (prof == null) {
        throw new MojoFailureException("Connection profile is required for SQL console");
    }
    try {
        getLog().info("Welcome to FTL/SQL console!");
        BeSqlExecutor sql = new BeSqlExecutor(connector) {

            @Override
            public void executeSingle(String statement) throws ExtendedSqlException {
                ResultSet rs = null;
                try {
                    rs = connector.executeQuery(statement);
                    format(rs, System.err, 20);
                } catch (SQLException e) {
                    throw new ExtendedSqlException(connector, statement, e);
                } finally {
                    connector.close(rs);
                }
            }
        };
        FreemarkerScript fs = new FreemarkerScript("SQL", be5Project.getApplication().getFreemarkerScripts());
        DataElementUtils.save(fs);
        ProcessController log = new NullLogger();
        while (true) {
            getLog().info("Enter FTL/SQL (use 'quit' to exit):");
            String line = new BufferedReader(new InputStreamReader(inputStream)).readLine();
            if (line == null) {
                break;
            }
            line = line.trim();
            if (line.equals("quit")) {
                break;
            }
            fs.setSource(line);
            ParseResult result = fs.getResult();
            if (result.getResult() != null) {
                getLog().info("SQL> " + result.getResult());
            } else {
                getLog().info("ERROR> " + result.getError());
                continue;
            }
            try {
                sql.executeScript(fs, log);
            } catch (Exception e) {
                getLog().info("ERROR> " + e.getMessage());
            }
        }
    } catch (Exception e) {
        throw new MojoFailureException("Console error: " + e.getMessage(), e);
    }
}
Also used : ParseResult(com.developmentontheedge.be5.metadata.model.ParseResult) SQLException(java.sql.SQLException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SQLException(java.sql.SQLException) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) NullLogger(com.developmentontheedge.be5.metadata.util.NullLogger) BeConnectionProfile(com.developmentontheedge.be5.metadata.model.BeConnectionProfile) ProcessController(com.developmentontheedge.be5.metadata.util.ProcessController) ResultSet(java.sql.ResultSet) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript)

Example 9 with FreemarkerScript

use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.

the class FreemarkerSqlHandler method execute.

public void execute(FreemarkerScript freemarkerScript) throws ProjectElementException, FreemarkerSqlException {
    DataElementPath path = freemarkerScript.getCompletePath();
    if (log != null) {
        log.setOperationName("[>] " + path);
    }
    sqlExecutor.comment("Execute " + path);
    Project project = freemarkerScript.getProject();
    ResultToConsumerWriter out = new ResultToConsumerWriter(new MultiSqlConsumer(project.getDatabaseSystem().getType(), this));
    FreemarkerUtils.mergeTemplateByPath(path.toString(), project.getContext(freemarkerScript), project.getConfiguration(), out);
    for (int j = includeChain.size() - 1; j >= 0; j--) {
        sqlExecutor.comment("End of included " + includeChain.get(j), false);
    }
}
Also used : Project(com.developmentontheedge.be5.metadata.model.Project) DataElementPath(com.developmentontheedge.be5.metadata.model.base.DataElementPath) MultiSqlConsumer(com.developmentontheedge.dbms.MultiSqlConsumer)

Example 10 with FreemarkerScript

use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.

the class FreemarkerUtils method getConfiguration.

public static Configuration getConfiguration(Project project) {
    Configuration config = (Configuration) configTemplate.clone();
    config.setCacheStorage(new SoftCacheStorage());
    try {
        config.setSharedVariable("project", project);
    } catch (TemplateModelException e) {
        throw new RuntimeException("Unexpected error: " + e, e);
    }
    FreemarkerScript macroCollection = project.getMacroCollection().optScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY);
    if (macroCollection != null) {
        config.addAutoInclude(macroCollection.getCompletePath().toString());
    }
    for (Module module : project.getModules()) {
        FreemarkerCatalog collection = module.getMacroCollection();
        if (collection != null) {
            FreemarkerScript script = collection.optScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY);
            if (script != null) {
                config.addAutoInclude(script.getCompletePath().toString());
            }
        }
    }
    config.setTemplateLoader(new ProjectTemplateLoader(project));
    return config;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Configuration(freemarker.template.Configuration) SoftCacheStorage(freemarker.cache.SoftCacheStorage) FreemarkerCatalog(com.developmentontheedge.be5.metadata.model.FreemarkerCatalog) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) Module(com.developmentontheedge.be5.metadata.model.Module)

Aggregations

FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)9 FreemarkerCatalog (com.developmentontheedge.be5.metadata.model.FreemarkerCatalog)6 Module (com.developmentontheedge.be5.metadata.model.Module)3 Project (com.developmentontheedge.be5.metadata.model.Project)3 DataElementPath (com.developmentontheedge.be5.metadata.model.base.DataElementPath)3 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)2 BeSqlExecutor (com.developmentontheedge.be5.metadata.sql.BeSqlExecutor)2 SqlExecutor (com.developmentontheedge.dbms.SqlExecutor)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 Path (java.nio.file.Path)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 Test (org.junit.Test)2 FreemarkerSqlException (com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException)1 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)1 WriteException (com.developmentontheedge.be5.metadata.exception.WriteException)1 FreemarkerSqlHandler (com.developmentontheedge.be5.metadata.freemarker.FreemarkerSqlHandler)1 BeConnectionProfile (com.developmentontheedge.be5.metadata.model.BeConnectionProfile)1 ParseResult (com.developmentontheedge.be5.metadata.model.ParseResult)1 NullLogger (com.developmentontheedge.be5.metadata.util.NullLogger)1