use of com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException in project be5 by DevelopmentOnTheEdge.
the class AppData method execute.
@Override
public void execute() throws MojoFailureException {
init();
PrintStream ps = null;
try {
if (logPath != null) {
logPath.mkdirs();
ps = new PrintStream(new File(logPath, be5Project.getName() + "_scripts_" + script.replace(';', '_').replace(':', '.') + ".sql"), "UTF-8");
}
ModuleLoader2.addModuleScripts(be5Project);
List<FreemarkerScript> scripts = new ArrayList<>();
for (String scriptName : script.split(";")) {
int pos = scriptName.indexOf(':');
FreemarkerCatalog scriptsCatalog = be5Project.getApplication().getFreemarkerScripts();
if (pos > 0) {
String moduleName = scriptName.substring(0, pos);
scriptName = scriptName.substring(pos + 1);
if (moduleName.equals("all")) {
for (Module module : be5Project.getModules()) {
scriptsCatalog = module.getFreemarkerScripts();
if (scriptsCatalog == null)
continue;
FreemarkerScript script = scriptsCatalog.optScript(scriptName);
if (script == null)
continue;
scripts.add(script);
}
FreemarkerScript script = be5Project.getApplication().getFreemarkerScripts().optScript(scriptName);
if (script != null) {
scripts.add(script);
}
continue;
} else {
Module module = be5Project.getModule(moduleName);
if (module == null) {
if (ignoreMissing) {
System.err.println("Warning: module '" + moduleName + "' not found");
continue;
} else
throw new MojoFailureException("Module '" + moduleName + "' not found");
}
scriptsCatalog = module.getFreemarkerScripts();
}
}
FreemarkerScript freemarkerScript = scriptsCatalog == null ? null : scriptsCatalog.optScript(scriptName);
if (freemarkerScript == null) {
if (ignoreMissing) {
System.err.println("Warning: FTL script " + scriptName + " not found");
continue;
} else
throw new MojoFailureException("FTL script " + scriptName + " not found");
}
scripts.add(freemarkerScript);
}
SqlExecutor sqlExecutor = new BeSqlExecutor(connector, ps);
for (FreemarkerScript freemarkerScript : scripts) {
executeScript(sqlExecutor, freemarkerScript);
}
DatabaseUtils.clearAllCache(sqlExecutor);
} catch (ProjectElementException | FreemarkerSqlException e) {
throw new MojoFailureException(e.getMessage(), e);
} catch (Exception e) {
throw new MojoFailureException(e.getMessage(), e);
} finally {
if (ps != null) {
ps.close();
}
}
}
use of com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException 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.exception.FreemarkerSqlException in project be5 by DevelopmentOnTheEdge.
the class AppDb method execute.
@Override
public void execute() throws MojoFailureException {
init();
try {
if (logPath != null) {
logPath.mkdirs();
ps = new PrintStream(new File(logPath, (moduleName == null ? be5Project.getName() : moduleName) + "_db.sql"), "UTF-8");
}
sql = new BeSqlExecutor(connector, ps);
if (moduleName != null) {
Module module = be5Project.getModule(moduleName);
if (module == null) {
throw new MojoFailureException("Module '" + moduleName + "' not found!");
}
createDb(module);
} else {
for (Module module : be5Project.getModules()) {
if (ModuleLoader2.containsModule(module.getName()))
createDb(module);
}
createDb(be5Project.getApplication());
}
getLog().info("Created tables: " + createdTables + ", created views: " + createdViews);
} catch (MojoFailureException e) {
throw e;
} catch (ProjectElementException | FreemarkerSqlException e) {
if (debug) {
e.printStackTrace();
throw new MojoFailureException("Setup db error", e);
}
throw new MojoFailureException(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
throw new MojoFailureException("Setup db error", e);
} finally {
if (ps != null) {
ps.close();
}
}
}
use of com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException in project be5 by DevelopmentOnTheEdge.
the class AppSync method execute.
// /////////////////////////////////////////////////////////////////
@Override
public void execute() throws MojoFailureException {
init();
PrintStream ps = null;
try {
if (logPath != null) {
logPath.mkdirs();
ps = new PrintStream(new File(logPath, be5Project.getName() + "_sync_ddl.sql"), "UTF-8");
}
sqlExecutor = new BeSqlExecutor(connector, ps);
if (be5Project.getDebugStream() != null) {
be5Project.getDebugStream().println("Modules and extras for " + be5Project.getName() + ":");
be5Project.allModules().map(m -> "- " + m.getName() + ": " + (m.getExtras() == null ? "" : String.join(", ", m.getExtras()))).forEach(be5Project.getDebugStream()::println);
}
readSchema();
createEntities();
String ddlString = getDdlStatements(false);
ddlString = MultiSqlParser.normalize(be5Project.getDatabaseSystem().getType(), ddlString);
if (ddlString.isEmpty()) {
getLog().info("Database scheme is up-to-date");
return;
}
if (forceUpdate) {
sqlExecutor.startSection("Sync schema");
logger.setOperationName("[>] Schema");
sqlExecutor.executeMultiple(ddlString);
sqlExecutor.startSection(null);
} else {
System.err.println("The following statements should be executed to update database scheme:");
System.err.println(ddlString);
System.err.println("Use -DBE5_FORCE_UPDATE=true, for apply");
}
checkSynchronizationStatus();
logger.setOperationName("Finished");
} catch (// ReadException | ProjectLoadException | SQLException e )
FreemarkerSqlException | ExtendedSqlException | SQLException e) {
if (debug)
throw new MojoFailureException("Synchronisation error: " + e.getMessage(), e);
throw new MojoFailureException("Synchronisation error: " + e.getMessage());
} catch (IOException | ProcessInterruptedException e) {
throw new MojoFailureException("Synchronisation error: " + e.getMessage(), e);
} catch (Throwable t) {
t.printStackTrace();
throw new MojoFailureException("Synchronisation error: " + t.getMessage(), t);
} finally {
if (ps != null) {
ps.close();
}
}
}
use of com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException 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);
}
}
Aggregations