use of com.developmentontheedge.be5.metadata.model.TableDef in project be5 by DevelopmentOnTheEdge.
the class AppDb method processDdl.
private void processDdl(final DdlElement tableDef) throws ProjectElementException {
try {
final String generatedQuery = tableDef.getDdl();
sql.executeMultiple(generatedQuery);
} catch (Exception e) {
throw new ProjectElementException(tableDef, e);
}
}
use of com.developmentontheedge.be5.metadata.model.TableDef in project be5 by DevelopmentOnTheEdge.
the class AppSync method fixPrimaryKey.
private void fixPrimaryKey(TableDef orphanDdl, TableDef ddl) {
ColumnDef pk = ddl.getColumns().get(ddl.getEntity().getPrimaryKey());
// Orphans have no primary key set: try to set the same column as in original table
if (pk != null) {
ColumnDef orphanPk = orphanDdl.getColumns().getCaseInsensitive(pk.getName());
if (orphanPk != null) {
orphanDdl.getIndicesUsingColumn(orphanPk.getName()).stream().filter(idx -> idx.getSize() == 1 && idx.isUnique()).findFirst().ifPresent(idx -> {
// Remove primary key index
DataElementUtils.remove(idx);
orphanDdl.getEntity().setPrimaryKey(orphanPk.getName());
orphanPk.setPrimaryKey(true);
});
}
}
}
use of com.developmentontheedge.be5.metadata.model.TableDef in project be5 by DevelopmentOnTheEdge.
the class AppSync method getDdlStatements.
// /////////////////////////////////////////////////////////////////////////
// Synchronization
//
protected String getDdlStatements(boolean dangerousOnly) throws ExtendedSqlException {
Map<String, DdlElement> oldSchemes = new HashMap<>();
Map<String, DdlElement> newSchemes = new HashMap<>();
for (Module module : be5Project.getModulesAndApplication()) {
for (Entity entity : module.getEntities()) {
DdlElement scheme = entity.isAvailable() ? entity.getScheme() : null;
if (scheme != null) {
String normalizedName = entity.getName().toLowerCase();
newSchemes.put(normalizedName, scheme);
}
}
}
for (Entity entity : entities) {
DdlElement scheme = entity.isAvailable() ? entity.getScheme() : null;
if (scheme != null) {
String normalizedName = entity.getName().toLowerCase();
oldSchemes.put(normalizedName, scheme);
}
}
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, DdlElement> entity : newSchemes.entrySet()) {
DdlElement oldScheme = oldSchemes.get(entity.getKey());
DdlElement newScheme = entity.getValue();
if (newScheme.withoutDbScheme()) {
if (!dangerousOnly) {
// PENDING - list of other type
// warnings.addAll(newScheme.getWarnings());
}
if (oldScheme == null) {
if (!dangerousOnly) {
sb.append(newScheme.getCreateDdl());
}
continue;
}
if (newScheme.equals(oldScheme) || newScheme.getDiffDdl(oldScheme, null).isEmpty())
continue;
// PENDING
if (oldScheme instanceof TableDef && newScheme instanceof TableDef)
fixPrimaryKey((TableDef) oldScheme, (TableDef) newScheme);
sb.append(dangerousOnly ? newScheme.getDangerousDiffStatements(oldScheme, sqlExecutor) : newScheme.getDiffDdl(oldScheme, sqlExecutor));
} else {
System.out.println("Skip table with schema: " + newScheme.getEntityName());
}
}
return sb.toString();
}
use of com.developmentontheedge.be5.metadata.model.TableDef in project be5 by DevelopmentOnTheEdge.
the class AppSync method createEntities.
private void createEntities() throws ExtendedSqlException, SQLException {
Rdbms databaseSystem = DatabaseUtils.getRdbms(connector);
DbmsTypeManager typeManager = databaseSystem == null ? new DefaultTypeManager() : databaseSystem.getTypeManager();
boolean casePreserved = typeManager.normalizeIdentifierCase("aA").equals("aA");
entities = new ArrayList<>();
Project project = new Project("internal-db");
project.setDatabaseSystem(be5Project.getDatabaseSystem());
Module module = new Module("temp", project);
for (String table : tableTypes.keySet()) {
if (!"TABLE".equals(tableTypes.get(table.toLowerCase())))
continue;
List<SqlColumnInfo> columnInfos = columns.get(table.toLowerCase());
if (columnInfos == null)
continue;
Entity entity = new Entity(table, module, EntityType.TABLE);
entities.add(entity);
TableDef tableDef = new TableDef(entity);
for (SqlColumnInfo info : columnInfos) {
ColumnDef column = new ColumnDef(info.getName(), tableDef.getColumns());
column.setType(createColumnType(info));
typeManager.correctType(column.getType());
// PENDING
column.setPrimaryKey(info.getName().equalsIgnoreCase(entity.getPrimaryKey()));
column.setCanBeNull(info.isCanBeNull());
String defaultValue = info.getDefaultValue();
column.setAutoIncrement(info.isAutoIncrement());
if (!info.isAutoIncrement()) {
column.setDefaultValue(defaultValue);
}
if (column.isPrimaryKey() && typeManager.getKeyType().equals(typeManager.getTypeClause(column.getType()))) {
column.getType().setTypeName(SqlColumnType.TYPE_KEY);
}
// column.setOriginModuleName( module.getName() );
DataElementUtils.saveQuiet(column);
}
List<IndexInfo> indexInfos = indices.get(table.toLowerCase(Locale.ENGLISH));
if (indexInfos != null) {
INDEX: for (IndexInfo info : indexInfos) {
if (!casePreserved)
info.setName(info.getName().toUpperCase(Locale.ENGLISH));
IndexDef index = new IndexDef(info.getName(), tableDef.getIndices());
index.setUnique(info.isUnique());
for (String indexCol : info.getColumns()) {
IndexColumnDef indexColumnDef = IndexColumnDef.createFromString(indexCol, index);
if (tableDef.getColumns().get(indexColumnDef.getName()) == null) {
if (debug) {
warnings.add("Unsupported functional index found: " + index.getName() + " (problem is here: " + indexCol + "); skipped");
}
continue INDEX;
}
DataElementUtils.saveQuiet(indexColumnDef);
}
if (index.isUnique() && index.getSize() == 1) {
IndexColumnDef indexColumnDef = index.iterator().next();
if (!indexColumnDef.isFunctional()) {
if (index.getName().equalsIgnoreCase(table + "_pkey")) {
entity.setPrimaryKey(indexColumnDef.getName());
continue;
}
}
}
DataElementUtils.saveQuiet(index);
}
}
DataElementUtils.saveQuiet(tableDef);
}
if (sqlExecutor.getConnector().getType() != DbmsType.MYSQL)
return;
// For MySQL only now
for (Entity entity : entities) {
final String table = entity.getName();
if (!"VIEW".equalsIgnoreCase(tableTypes.get(table)))
continue;
String createTable;
ResultSet rs = sqlExecutor.executeNamedQuery("sql.getTableDefinition", table);
try {
if (!rs.next())
continue;
createTable = rs.getString(2);
} finally {
sqlExecutor.getConnector().close(rs);
}
int as = createTable.indexOf(" AS ");
if (as < 0)
continue;
createTable = createTable.substring(as + " AS ".length());
ViewDef def = new ViewDef(entity);
def.setDefinition(createTable);
DataElementUtils.saveQuiet(def);
}
}
use of com.developmentontheedge.be5.metadata.model.TableDef in project be5 by DevelopmentOnTheEdge.
the class TableDef method getDiffDdl.
@Override
public String getDiffDdl(DdlElement other, SqlExecutor sql) throws ExtendedSqlException {
if (other == null)
return getCreateDdl();
if (!(other instanceof TableDef))
return other.getDropDdl() + getCreateDdl();
TableDef def = (TableDef) ((TableDef) other).clone(other.getOrigin(), other.getName());
Rdbms dbms = getProject().getDatabaseSystem();
DbmsTypeManager typeManager = dbms.getTypeManager();
String columnsDiff = getColumnsDiff(def, typeManager, false, sql);
if (// no columns match
columnsDiff == null)
return getDdl();
String indicesDiff = getIndicesDiff(def, dbms, typeManager);
if (columnsDiff.isEmpty()) {
return indicesDiff;
}
if (getModule().getName().equals(getProject().getProjectOrigin()) && sql != null && sql.isEmpty(getEntityName())) {
return def.getDropDdl() + getCreateDdl();
}
return columnsDiff + indicesDiff;
}
Aggregations