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"));
}
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);
}
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);
}
}
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);
}
}
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;
}
Aggregations