Search in sources :

Example 6 with SqlColumnInfo

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

the class H2SchemaReader method readColumns.

@Override
public Map<String, List<SqlColumnInfo>> readColumns(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException, ProcessInterruptedException {
    DbmsConnector connector = sql.getConnector();
    Map<String, List<SqlColumnInfo>> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT " + "c.table_name, " + "c.column_name, " + "c.column_default, " + "c.TYPE_NAME, " + "c.character_maximum_length, " + "c.numeric_precision, " + "c.numeric_scale, " + "c.is_nullable, " + "c.CHECK_CONSTRAINT " + "FROM INFORMATION_SCHEMA.COLUMNS c " + "WHERE c.TABLE_SCHEMA <> 'INFORMATION_SCHEMA'");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            if (!tableName.equals(tableName.toLowerCase()))
                continue;
            List<SqlColumnInfo> list = result.get(tableName);
            if (list == null) {
                list = new ArrayList<>();
                result.put(tableName, list);
            }
            SqlColumnInfo info = new SqlColumnInfo();
            list.add(info);
            info.setName(rs.getString(2));
            info.setType(rs.getString(4));
            info.setCanBeNull("YES".equals(rs.getString(8)));
            String defaultValue = rs.getString(3);
            if (defaultValue != null) {
                for (String suffix : SUFFICES) {
                    if (defaultValue.endsWith(suffix))
                        defaultValue = defaultValue.substring(0, defaultValue.length() - suffix.length());
                }
                defaultValue = DEFAULT_DATE_PATTERN.matcher(defaultValue).replaceFirst("'$1'");
            }
            info.setDefaultValue(defaultValue);
            info.setSize(rs.getInt(5));
            if (rs.wasNull()) {
                info.setSize(rs.getInt(6));
            }
            info.setPrecision(rs.getInt(7));
            info.setAutoIncrement(defaultValue != null && defaultValue.startsWith("nextval"));
            String check = rs.getString(9);
            if (check == null)
                continue;
            Matcher matcher = ENUM_VALUES_PATTERN.matcher(check);
            List<String> vals = new ArrayList<>();
            while (matcher.find()) {
                vals.add(matcher.group(1));
            }
            info.setEnumValues(vals.toArray(new String[vals.size()]));
            // Just to check for interrupts
            controller.setProgress(0);
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)

Example 7 with SqlColumnInfo

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

the class AppSync method createColumnType.

private static SqlColumnType createColumnType(final SqlColumnInfo info) {
    SqlColumnType type = new SqlColumnType();
    String[] enumValues = info.getEnumValues();
    if (enumValues != null) {
        if (isBool(enumValues)) {
            type.setTypeName(SqlColumnType.TYPE_BOOL);
        } else {
            type.setTypeName(SqlColumnType.TYPE_ENUM);
            Arrays.sort(enumValues);
            type.setEnumValues(enumValues);
        }
    } else {
        type.setTypeName(info.getType());
        type.setSize(info.getSize());
        type.setPrecision(info.getPrecision());
    }
    return type;
}
Also used : SqlColumnType(com.developmentontheedge.be5.metadata.model.SqlColumnType)

Example 8 with SqlColumnInfo

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

Aggregations

SqlColumnInfo (com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)7 ResultSet (java.sql.ResultSet)7 DbmsConnector (com.developmentontheedge.dbms.DbmsConnector)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Matcher (java.util.regex.Matcher)5 ColumnFunction (com.developmentontheedge.be5.metadata.model.ColumnFunction)3 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 SqlColumnType (com.developmentontheedge.be5.metadata.model.SqlColumnType)1 TableDef (com.developmentontheedge.be5.metadata.model.TableDef)1 ViewDef (com.developmentontheedge.be5.metadata.model.ViewDef)1 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)1 IndexInfo (com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)1 DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)1