Search in sources :

Example 1 with DbmsTypeManager

use of com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager 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;
}
Also used : DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager) Arrays(java.util.Arrays) BeCaseInsensitiveCollection(com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection) SqlExecutor(com.developmentontheedge.dbms.SqlExecutor) Iterator(java.util.Iterator) BeVectorCollection(com.developmentontheedge.be5.metadata.model.base.BeVectorCollection) MoreCollectors(one.util.streamex.MoreCollectors) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) Set(java.util.Set) MetadataUtils(com.developmentontheedge.be5.metadata.MetadataUtils) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) ArrayList(java.util.ArrayList) BeModelElement(com.developmentontheedge.be5.metadata.model.base.BeModelElement) List(java.util.List) Strings2(com.developmentontheedge.be5.metadata.util.Strings2) PropertyName(com.developmentontheedge.beans.annot.PropertyName) StreamEx(one.util.streamex.StreamEx) SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType) Map(java.util.Map) ExtendedSqlException(com.developmentontheedge.dbms.ExtendedSqlException) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType)

Example 2 with DbmsTypeManager

use of com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager in project be5 by DevelopmentOnTheEdge.

the class TableDef method getDangerousDiffStatements.

@Override
public String getDangerousDiffStatements(DdlElement other, SqlExecutor sql) throws ExtendedSqlException {
    if (other == null || (sql != null && sql.isEmpty(getEntityName()))) {
        return "";
    }
    if (!(other instanceof TableDef))
        return other.getDropDdl();
    TableDef def = (TableDef) ((TableDef) other).clone(other.getOrigin(), other.getName());
    DbmsTypeManager typeManager = getProject().getDatabaseSystem().getTypeManager();
    String diff = getColumnsDiff(def, typeManager, true, sql);
    if (diff == null)
        return getDropDdl();
    return diff;
}
Also used : DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)

Example 3 with DbmsTypeManager

use of com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager in project be5 by DevelopmentOnTheEdge.

the class BaseTypeManagerTest method assertTypeTranslation.

protected void assertTypeTranslation(DbmsTypeManager tm, String input, String expected) {
    SqlColumnType type = new SqlColumnType();
    type.setTypeName(input);
    tm.correctType(type);
    assertEquals(expected, type.toString());
}
Also used : SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType)

Example 4 with DbmsTypeManager

use of com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager 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);
    }
}
Also used : DefaultTypeManager(com.developmentontheedge.be5.metadata.sql.type.DefaultTypeManager) Entity(com.developmentontheedge.be5.metadata.model.Entity) IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) IndexInfo(com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo) ViewDef(com.developmentontheedge.be5.metadata.model.ViewDef) TableDef(com.developmentontheedge.be5.metadata.model.TableDef) Project(com.developmentontheedge.be5.metadata.model.Project) ResultSet(java.sql.ResultSet) Module(com.developmentontheedge.be5.metadata.model.Module) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo) IndexDef(com.developmentontheedge.be5.metadata.model.IndexDef) DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)

Example 5 with DbmsTypeManager

use of com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager 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;
}
Also used : Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)

Aggregations

DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)6 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)5 ArrayList (java.util.ArrayList)3 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)2 SqlColumnType (com.developmentontheedge.be5.metadata.model.SqlColumnType)2 MetadataUtils (com.developmentontheedge.be5.metadata.MetadataUtils)1 ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)1 Entity (com.developmentontheedge.be5.metadata.model.Entity)1 IndexColumnDef (com.developmentontheedge.be5.metadata.model.IndexColumnDef)1 IndexDef (com.developmentontheedge.be5.metadata.model.IndexDef)1 Module (com.developmentontheedge.be5.metadata.model.Module)1 Project (com.developmentontheedge.be5.metadata.model.Project)1 TableDef (com.developmentontheedge.be5.metadata.model.TableDef)1 ViewDef (com.developmentontheedge.be5.metadata.model.ViewDef)1 BeCaseInsensitiveCollection (com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection)1 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)1 BeVectorCollection (com.developmentontheedge.be5.metadata.model.base.BeVectorCollection)1 IndexInfo (com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)1 SqlColumnInfo (com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)1 DefaultTypeManager (com.developmentontheedge.be5.metadata.sql.type.DefaultTypeManager)1