Search in sources :

Example 1 with ProcessInterruptedException

use of com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException in project be5 by DevelopmentOnTheEdge.

the class Db2SchemaReader 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 tabname,colname,typename,nulls,default,length,scale,identity,text FROM syscat.columns c " + (defSchema == null ? "" : "WHERE c.tabschema='" + defSchema + "' ") + " ORDER BY c.tabname,c.colno");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            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(3));
            info.setCanBeNull(rs.getString(4).equals("Y"));
            info.setDefaultValue(rs.getString(5));
            info.setSize(rs.getInt(6));
            info.setPrecision(rs.getInt(7));
            info.setAutoIncrement(rs.getString(8).equals("Y"));
            String text = rs.getString(9);
            if (text != null) {
                Matcher m = GENERIC_COLUMN_PATTERN.matcher(text);
                if (m.matches()) {
                    String colName = m.group(2);
                    info.setDefaultValue(new ColumnFunction(colName, ColumnFunction.TRANSFORM_GENERIC).toString());
                }
            }
        }
    } finally {
        connector.close(rs);
    }
    for (Entry<String, List<SqlColumnInfo>> table : result.entrySet()) {
        HashMap<String, String[]> enums = loadEntityEnums(connector, table.getKey());
        for (SqlColumnInfo column : table.getValue()) {
            column.setEnumValues(enums.get(column.getName()));
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ColumnFunction(com.developmentontheedge.be5.metadata.model.ColumnFunction) DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)

Example 2 with ProcessInterruptedException

use of com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException in project be5 by DevelopmentOnTheEdge.

the class MySqlSchemaReader 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 table_name,column_name,column_type,column_default,is_nullable," + "numeric_precision,numeric_scale,character_maximum_length,extra " + "FROM information_schema.columns " + "WHERE table_schema='" + defSchema + "' ORDER BY table_name, ordinal_position");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            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));
            String type = rs.getString(3);
            info.setCanBeNull("YES".equals(rs.getString(5)));
            String defaultValue = rs.getString(4);
            if (defaultValue != null) {
                if (type.startsWith("text") || type.startsWith("enum") || type.startsWith("varchar") || type.startsWith("char")) {
                    defaultValue = "'" + defaultValue + "'";
                } else if (type.startsWith("date") || type.startsWith("time")) {
                    if (defaultValue.equalsIgnoreCase("CURRENT_TIMESTAMP")) {
                        defaultValue = "NOW()";
                    } else {
                        defaultValue = "'" + defaultValue + "'";
                    }
                }
            }
            info.setDefaultValue(defaultValue);
            info.setSize(rs.getInt(8));
            if (rs.wasNull()) {
                info.setSize(rs.getInt(6));
            }
            info.setPrecision(rs.getInt(7));
            info.setAutoIncrement("auto_increment".equals(rs.getString(9)));
            if (type.startsWith("enum(")) {
                String[] enumValues = type.substring("enum(".length(), type.length() - 1).split(",", -1);
                for (int i = 0; i < enumValues.length; i++) {
                    enumValues[i] = enumValues[i].substring(1, enumValues[i].length() - 1);
                }
                type = "enum";
                info.setEnumValues(enumValues);
            }
            type = UNNECESSARY_TYPE_LENGTH_PATTERN.matcher(type).replaceFirst("$1");
            info.setType(type.toUpperCase(Locale.ENGLISH));
            // 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) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)

Example 3 with ProcessInterruptedException

use of com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException in project be5 by DevelopmentOnTheEdge.

the class OracleSchemaReader method readIndices.

@Override
public Map<String, List<IndexInfo>> readIndices(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException, ProcessInterruptedException {
    DbmsConnector connector = sql.getConnector();
    Map<String, List<IndexInfo>> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT " + "i.table_name,i.index_name,ic.column_name,i.uniqueness " + "FROM user_indexes i " + "JOIN user_ind_columns ic ON i.index_name=ic.index_name " + "JOIN entities e ON (UPPER(e.name)=i.table_name) " + "ORDER BY i.table_name,i.index_name,ic.column_position");
    try {
        IndexInfo curIndex = null;
        String lastTable = null;
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            String indexName = rs.getString(2);
            if (!tableName.equals(lastTable) || curIndex == null || !curIndex.getName().equals(indexName)) {
                List<IndexInfo> list = result.get(tableName);
                if (list == null) {
                    list = new ArrayList<>();
                    result.put(tableName, list);
                }
                curIndex = new IndexInfo();
                lastTable = tableName;
                list.add(curIndex);
                curIndex.setName(indexName);
                curIndex.setUnique("UNIQUE".equals(rs.getString(4)));
            }
            String column = rs.getString(3);
            curIndex.addColumn(column);
        }
    } finally {
        connector.close(rs);
    }
    // Read functional indices separately
    // as this query contains streaming column which is read slowly
    rs = connector.executeQuery("SELECT " + "i.table_name,i.index_name,ic.column_position,c.data_default " + "FROM user_indexes i " + "JOIN user_ind_columns ic ON i.index_name=ic.index_name " + "JOIN entities e ON (UPPER(e.name)=i.table_name) " + "JOIN user_tab_cols c ON (c.column_name=ic.column_name AND c.table_name=ic.table_name) " + "WHERE c.virtual_column='YES' " + "ORDER BY i.table_name,i.index_name,ic.column_position");
    try {
        while (rs.next()) {
            // Read streaming column at first
            String defaultValue = rs.getString(4);
            String tableName = rs.getString(1).toLowerCase();
            String indexName = rs.getString(2);
            int pos = rs.getInt(3) - 1;
            List<IndexInfo> list = result.get(tableName);
            if (list == null)
                continue;
            for (IndexInfo indexInfo : list) {
                if (indexInfo.getName().equals(indexName)) {
                    defaultValue = GENERIC_REF_INDEX_PATTERN.matcher(defaultValue).replaceFirst("generic($2)");
                    defaultValue = UPPER_INDEX_PATTERN.matcher(defaultValue).replaceFirst("upper($1)");
                    defaultValue = LOWER_INDEX_PATTERN.matcher(defaultValue).replaceFirst("lower($1)");
                    indexInfo.getColumns().set(pos, defaultValue);
                }
            }
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) IndexInfo(com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)

Example 4 with ProcessInterruptedException

use of com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException in project be5 by DevelopmentOnTheEdge.

the class PostgresSchemaReader method readIndices.

@Override
public Map<String, List<IndexInfo>> readIndices(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException, ProcessInterruptedException {
    DbmsConnector connector = sql.getConnector();
    Map<String, List<IndexInfo>> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT ct.relname AS TABLE_NAME, i.indisunique AS IS_UNIQUE, ci.relname AS INDEX_NAME, " + "pg_catalog.pg_get_indexdef(ci.oid, (i.keys).n, false) AS COLUMN_NAME, (i.keys).n AS ORDINAL " + "FROM pg_catalog.pg_class ct " + "JOIN pg_catalog.pg_namespace n " + "ON (ct.relnamespace = n.oid)" + "JOIN (" + "SELECT i.indexrelid, i.indrelid, i.indisunique, " + "information_schema._pg_expandarray(i.indkey) AS keys FROM pg_catalog.pg_index i " + ") i " + "ON (ct.oid = i.indrelid) " + "JOIN pg_catalog.pg_class ci ON (ci.oid = i.indexrelid) " + (defSchema == null ? "" : "AND n.nspname = '" + defSchema + "' ") + " ORDER BY 1,3,5");
    try {
        IndexInfo curIndex = null;
        String lastTable = null;
        while (rs.next()) {
            String tableName = rs.getString(1);
            String indexName = rs.getString(3);
            if (indexName == null)
                continue;
            if (!tableName.equals(lastTable) || curIndex == null || !curIndex.getName().equals(indexName)) {
                List<IndexInfo> list = result.get(tableName);
                if (list == null) {
                    list = new ArrayList<>();
                    result.put(tableName, list);
                }
                curIndex = new IndexInfo();
                lastTable = tableName;
                list.add(curIndex);
                curIndex.setName(indexName);
                curIndex.setUnique(rs.getBoolean(2));
            }
            String column = rs.getString(4);
            column = GENERIC_REF_INDEX_PATTERN.matcher(column).replaceFirst("generic($2)");
            column = UPPER_INDEX_PATTERN.matcher(column).replaceFirst("upper($1)");
            column = LOWER_INDEX_PATTERN.matcher(column).replaceFirst("lower($1)");
            column = QUOTE_INDEX_PATTERN.matcher(column).replaceFirst("$1");
            curIndex.addColumn(column);
            controller.setProgress(0);
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) IndexInfo(com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)

Example 5 with ProcessInterruptedException

use of com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException in project be5 by DevelopmentOnTheEdge.

the class PostgresSchemaReader 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.udt_name, " + "c.character_maximum_length, " + "c.numeric_precision, " + "c.numeric_scale, " + "c.is_nullable, " + "ch.check_clause " + "FROM information_schema.columns c " + "LEFT JOIN information_schema.constraint_column_usage cc ON (cc.table_name=c.table_name AND cc.table_schema=c.table_schema AND cc.column_name=c.column_name) " + "LEFT JOIN information_schema.table_constraints tc ON (cc.constraint_name = tc.constraint_name) " + "LEFT JOIN information_schema.check_constraints ch ON (cc.constraint_name = ch.constraint_name) " + "WHERE (ch.check_clause IS NULL OR tc.constraint_type = 'CHECK') " + (defSchema == null ? "" : "AND c.table_schema='" + defSchema + "' ") + "ORDER BY c.table_name,c.ordinal_position");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1);
            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)

Aggregations

ResultSet (java.sql.ResultSet)10 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 List (java.util.List)10 DbmsConnector (com.developmentontheedge.dbms.DbmsConnector)9 SqlColumnInfo (com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)6 IndexInfo (com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)5 Matcher (java.util.regex.Matcher)3 ColumnFunction (com.developmentontheedge.be5.metadata.model.ColumnFunction)2 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)2 DbmsSchemaReader (com.developmentontheedge.be5.metadata.sql.schema.DbmsSchemaReader)2 NullLogger (com.developmentontheedge.be5.metadata.util.NullLogger)2 ProcessController (com.developmentontheedge.be5.metadata.util.ProcessController)2 FreemarkerSqlException (com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException)1 ProcessInterruptedException (com.developmentontheedge.be5.metadata.exception.ProcessInterruptedException)1 ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)1 DataElementUtils (com.developmentontheedge.be5.metadata.model.DataElementUtils)1 DdlElement (com.developmentontheedge.be5.metadata.model.DdlElement)1 Entity (com.developmentontheedge.be5.metadata.model.Entity)1 EntityType (com.developmentontheedge.be5.metadata.model.EntityType)1