use of com.developmentontheedge.dbms.ExtendedSqlException in project be5 by DevelopmentOnTheEdge.
the class TableDef method isSafeTypeUpdate.
private boolean isSafeTypeUpdate(ColumnDef oldColumn, ColumnDef column, DbmsTypeManager typeManager, SqlExecutor sql) throws ExtendedSqlException {
SqlColumnType oldType = oldColumn.getType();
SqlColumnType type = column.getType();
if (typeManager.getTypeClause(oldType).equals(typeManager.getTypeClause(type)))
return true;
// Enlarging VARCHAR column
if (oldType.getTypeName().equals(TYPE_VARCHAR) && type.getTypeName().equals(TYPE_VARCHAR)) {
if (type.getSize() >= oldType.getSize())
return true;
return sql != null && !sql.hasResult("sql.select.longer", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), type.getSize());
}
// Enlarging DECIMAL column
if (oldType.getTypeName().equals(TYPE_DECIMAL) && type.getTypeName().equals(TYPE_DECIMAL)) {
if (type.getSize() >= oldType.getSize() && type.getPrecision() >= oldType.getPrecision())
return true;
}
// Adding new variants for ENUM column
if ((oldType.getTypeName().equals(TYPE_ENUM) || oldType.getTypeName().equals(TYPE_BOOL) || oldType.getTypeName().equals(TYPE_VARCHAR)) && (type.getTypeName().equals(TYPE_ENUM) || type.getTypeName().equals(TYPE_BOOL))) {
List<String> newValues = Arrays.asList(type.getEnumValues());
if (!oldType.getTypeName().equals(TYPE_VARCHAR) && newValues.containsAll(Arrays.asList(oldType.getEnumValues())))
return true;
return sql != null && !sql.hasResult("sql.select.not.in.range", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), MetadataUtils.toInClause(newValues));
}
// Changing ENUM to varchar
if ((oldType.getTypeName().equals(TYPE_ENUM) || oldType.getTypeName().equals(TYPE_BOOL)) && type.getTypeName().equals(TYPE_VARCHAR)) {
int len = 0;
for (String value : oldType.getEnumValues()) {
len = Math.max(len, value.length());
}
if (type.getSize() >= len)
return true;
return sql != null && !sql.hasResult("sql.select.longer", typeManager.normalizeIdentifier(getEntityName()), typeManager.normalizeIdentifier(oldColumn.getName()), type.getSize());
}
// Changing ENUM to char
if (oldType.getTypeName().equals(TYPE_ENUM) && type.getTypeName().equals(TYPE_CHAR)) {
return StreamEx.of(oldType.getEnumValues()).map(String::length).distinct().collect(MoreCollectors.onlyOne()).filter(len -> type.getSize() == len).isPresent();
}
return false;
}
use of com.developmentontheedge.dbms.ExtendedSqlException 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.dbms.ExtendedSqlException 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.dbms.ExtendedSqlException in project be5 by DevelopmentOnTheEdge.
the class FreemarkerSqlHandler method endStatement.
@Override
public void endStatement(String statement) {
List<Position> positions = calculatePosition();
if (debug) {
for (Position position : positions) {
sqlExecutor.comment("At " + position, false);
}
}
List<String> newIncludeChain = getIncludeChain(positions);
if (!newIncludeChain.equals(includeChain)) {
int i = 0;
while (newIncludeChain.size() > i && includeChain.size() > i && newIncludeChain.get(i).equals(includeChain.get(i))) {
i++;
}
for (int j = includeChain.size() - 1; j >= i; j--) {
sqlExecutor.comment("End of included " + includeChain.get(j), false);
}
for (int j = i; j < newIncludeChain.size(); j++) {
if (log != null) {
StringBuilder sb = new StringBuilder("[>] ");
for (int k = 0; k < j; k++) sb.append(" ");
sb.append(newIncludeChain.get(j));
log.setOperationName(sb.toString());
}
sqlExecutor.comment("Start of included " + newIncludeChain.get(j));
}
}
includeChain = newIncludeChain;
try {
sqlExecutor.executeSingle(statement);
} catch (ExtendedSqlException e) {
if (!debug) {
for (Position position : positions) {
sqlExecutor.comment("At " + position, false);
}
}
throw new FreemarkerSqlException(e, positions.toArray(new Position[positions.size()]));
}
}
Aggregations