use of com.developmentontheedge.be5.metadata.model.IndexDef in project be5 by DevelopmentOnTheEdge.
the class SqlServerTypeManager method getAlterColumnStatements.
@Override
public String getAlterColumnStatements(ColumnDef column, ColumnDef oldColumn) {
String tableName = column.getTable().getEntityName();
String columnName = column.getName();
String prefix = "ALTER TABLE " + normalizeIdentifier(tableName) + " ALTER COLUMN " + normalizeIdentifier(columnName) + " ";
StringBuilder sb = new StringBuilder();
StringBuilder endSb = new StringBuilder();
for (IndexDef index : oldColumn.getTable().getIndices().getAvailableElements()) {
for (IndexColumnDef indexColumn : index.getAvailableElements()) {
if (indexColumn.getName().equals(oldColumn.getName())) {
sb.append(index.getDropDdl());
endSb.append(index.getCreateDdl());
break;
}
}
}
if (column.isCanBeNull() && (!oldColumn.isCanBeNull() || (!column.getType().toString().equals(oldColumn.getType().toString())))) {
sb.append(prefix).append(getTypeClause(column.getType())).append(" NULL;");
}
if (!column.isCanBeNull() && (oldColumn.isCanBeNull() || (!column.getType().toString().equals(oldColumn.getType().toString())))) {
if (oldColumn.isCanBeNull() && !Strings2.isNullOrEmpty(column.getDefaultValue())) {
sb.append("UPDATE ").append(normalizeIdentifier(tableName)).append(" SET ").append(normalizeIdentifier(columnName)).append('=').append(column.getDefaultValue()).append(" WHERE ").append(normalizeIdentifier(columnName)).append(" IS NULL;");
}
sb.append(prefix).append(getTypeClause(column.getType())).append(" NOT NULL;");
}
if (!Strings2.nullToEmpty(column.getDefaultValue()).equals(Strings2.nullToEmpty(oldColumn.getDefaultValue()))) {
if (!Strings2.isNullOrEmpty(oldColumn.getDefaultValue())) {
sb.append(getDropDefaultStatements(tableName, columnName));
}
if (!Strings2.isNullOrEmpty(column.getDefaultValue())) {
sb.append("ALTER TABLE ").append(normalizeIdentifier(tableName)).append(" ADD CONSTRAINT df_").append(tableName).append('_').append(columnName).append(" DEFAULT ").append(column.getDefaultValue()).append(" FOR ").append(normalizeIdentifier(columnName)).append(';');
}
}
sb.append(endSb);
return sb.toString();
}
use of com.developmentontheedge.be5.metadata.model.IndexDef in project be5 by DevelopmentOnTheEdge.
the class PostgresTypeManagerTest method testJsonB.
@Test
public void testJsonB() {
TableDef def = createTable(Rdbms.POSTGRESQL);
ColumnDef col = addColumn(def, "rec", SqlColumnType.TYPE_BIGINT);
IndexDef idx = new IndexDef("recidx", def.getIndices());
DataElementUtils.save(idx);
IndexColumnDef ic = new IndexColumnDef("rec", idx);
DataElementUtils.save(ic);
assertEquals("DROP TABLE IF EXISTS \"table\";\n" + "CREATE TABLE \"table\" (\n" + "rec BIGINT NOT NULL);\n" + "CREATE INDEX recidx ON \"table\"(rec);\n", def.getDdl());
col.setTypeString(SqlColumnType.TYPE_JSONB);
assertEquals("DROP TABLE IF EXISTS \"table\";\n" + "CREATE TABLE \"table\" (\n" + "rec JSONB NOT NULL);\n" + "CREATE INDEX recidx ON \"table\" USING GIN (rec);\n", def.getDdl());
}
use of com.developmentontheedge.be5.metadata.model.IndexDef in project be5 by DevelopmentOnTheEdge.
the class PostgresTypeManager method getCreateIndexClause.
@Override
public String getCreateIndexClause(IndexDef indexDef) {
Optional<IndexColumnDef> col = indexDef.stream().collect(MoreCollectors.onlyOne()).filter(c -> !c.isFunctional());
if (col.isPresent()) {
ColumnDef columnDef = indexDef.getTable().getColumns().get(col.get().getName());
if (columnDef != null && columnDef.getType().getTypeName().equals(SqlColumnType.TYPE_JSONB)) {
StringBuilder sb = new StringBuilder();
sb.append("CREATE ");
if (indexDef.isUnique())
sb.append("UNIQUE ");
sb.append("INDEX ");
sb.append(normalizeIdentifier(indexDef.getName()));
sb.append(" ON ");
sb.append(normalizeIdentifier(indexDef.getTable().getEntityName()));
sb.append(" USING GIN (");
sb.append(columnDef.getName());
sb.append(");");
return sb.toString();
}
}
return super.getCreateIndexClause(indexDef);
}
use of com.developmentontheedge.be5.metadata.model.IndexDef 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.IndexDef in project be5 by DevelopmentOnTheEdge.
the class TableDef method getWarnings.
@Override
public List<ProjectElementException> getWarnings() {
ArrayList<ProjectElementException> warnings = new ArrayList<>();
Rdbms rdbms = getProject().getDatabaseSystem();
DbmsTypeManager typeManager = rdbms.getTypeManager();
if (!typeManager.isFunctionalIndexSupported()) {
for (IndexDef index : getIndices().getAvailableElements()) {
if (index.isFunctional()) {
warnings.add(new ProjectElementException(index, "Functional indices are not supported by " + rdbms));
}
}
}
if (rdbms == Rdbms.DB2) {
for (IndexDef index : getIndices().getAvailableElements()) {
if (index.getName().length() > Rdbms.DB2_INDEX_LENGTH) {
warnings.add(new ProjectElementException(index, "Index name too long for DB2 (" + index.getName().length() + ">" + Rdbms.DB2_INDEX_LENGTH + "); will be skipped"));
}
}
}
if (!typeManager.isCustomAutoincrementSupported()) {
ColumnDef autoincrementColumn = getAutoincrementColumn();
if (autoincrementColumn != null && getStartId() != null) {
warnings.add(new ProjectElementException(autoincrementColumn, "Custom starting autoincrement value is not supported by " + rdbms));
}
}
for (ColumnDef def : getColumns().getAvailableElements()) {
if (def.getType().getTypeName().equals(TYPE_UNKNOWN)) {
warnings.add(new ProjectElementException(def, "Cannot determine column type: probably it references to invalid table or column"));
}
}
for (IndexDef def : getIndices().getAvailableElements()) {
for (IndexColumnDef col : def) {
ColumnDef column = getColumns().getCaseInsensitive(col.getName());
if (column == null)
warnings.add(new ProjectElementException(col, "Index refers to unknown column"));
else if (!column.isAvailable())
warnings.add(new ProjectElementException(col, "Index refers to non-available column"));
}
}
return warnings;
}
Aggregations