Search in sources :

Example 1 with Rdbms

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

the class BaseTypeManagerTest method createTable.

protected TableDef createTable(Rdbms dbms) {
    Project proj = new Project("test");
    proj.setDatabaseSystem(dbms);
    Entity ent = new Entity("table", proj.getApplication(), EntityType.TABLE);
    DataElementUtils.save(ent);
    TableDef def = new TableDef(ent);
    DataElementUtils.save(def);
    return def;
}
Also used : Project(com.developmentontheedge.be5.metadata.model.Project) Entity(com.developmentontheedge.be5.metadata.model.Entity) TableDef(com.developmentontheedge.be5.metadata.model.TableDef)

Example 2 with Rdbms

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

the class AppSync method readSchema.

private void readSchema() throws ExtendedSqlException, SQLException, ProcessInterruptedException {
    getLog().info("Read database scheme ...");
    long time = System.currentTimeMillis();
    ProcessController controller = new NullLogger();
    Rdbms rdbms = DatabaseUtils.getRdbms(connector);
    DbmsSchemaReader schemaReader = rdbms.getSchemaReader();
    defSchema = schemaReader.getDefaultSchema(sqlExecutor);
    tableTypes = schemaReader.readTableNames(sqlExecutor, defSchema, controller);
    columns = schemaReader.readColumns(sqlExecutor, defSchema, controller);
    indices = schemaReader.readIndices(sqlExecutor, defSchema, controller);
    if (debug) {
        if (!warnings.isEmpty()) {
            System.err.println(warnings.size() + " warning(s) during loading the project from " + sqlExecutor.getConnector().getConnectString());
            Collections.sort(warnings);
            for (String warning : warnings) {
                System.err.println(warning);
            }
        }
    }
    getLog().info("comleted, " + (System.currentTimeMillis() - time) + "ms.");
}
Also used : NullLogger(com.developmentontheedge.be5.metadata.util.NullLogger) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) ProcessController(com.developmentontheedge.be5.metadata.util.ProcessController) DbmsSchemaReader(com.developmentontheedge.be5.metadata.sql.schema.DbmsSchemaReader)

Example 3 with Rdbms

use of com.developmentontheedge.be5.metadata.sql.Rdbms 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 4 with Rdbms

use of com.developmentontheedge.be5.metadata.sql.Rdbms 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)

Example 5 with Rdbms

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

Aggregations

Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)5 DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)4 Entity (com.developmentontheedge.be5.metadata.model.Entity)2 Project (com.developmentontheedge.be5.metadata.model.Project)2 TableDef (com.developmentontheedge.be5.metadata.model.TableDef)2 ArrayList (java.util.ArrayList)2 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)1 ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)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 ViewDef (com.developmentontheedge.be5.metadata.model.ViewDef)1 IndexInfo (com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)1 SqlColumnInfo (com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)1 DbmsSchemaReader (com.developmentontheedge.be5.metadata.sql.schema.DbmsSchemaReader)1 DefaultTypeManager (com.developmentontheedge.be5.metadata.sql.type.DefaultTypeManager)1 NullLogger (com.developmentontheedge.be5.metadata.util.NullLogger)1 ProcessController (com.developmentontheedge.be5.metadata.util.ProcessController)1 ResultSet (java.sql.ResultSet)1