Search in sources :

Example 1 with ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController 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 ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController 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 ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController in project be5 by DevelopmentOnTheEdge.

the class OracleSchemaReader method readColumns.

@Override
public Map<String, List<SqlColumnInfo>> readColumns(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException {
    DbmsConnector connector = sql.getConnector();
    Map<String, List<SqlColumnInfo>> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT " + "c.table_name," + "c.column_name," + "c.data_type," + "c.char_length," + "c.data_precision," + "c.data_scale," + "c.nullable " + "from user_tab_cols c" + " JOIN entities e ON (UPPER(e.name)=c.table_name)" + " WHERE NOT(c.column_id IS NULL) ORDER BY c.table_name,c.column_id");
    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("Y".equals(rs.getString(7)));
            info.setSize(rs.getInt(5));
            if (rs.wasNull()) {
                info.setSize(rs.getInt(4));
            }
            info.setPrecision(rs.getInt(6));
        }
    } finally {
        connector.close(rs);
    }
    // Read default values as separate query, because it's LONG column which is streaming and
    // transmitted slowly
    rs = connector.executeQuery("SELECT " + "c.data_default," + "c.table_name," + "c.column_name " + "from user_tab_cols c" + " JOIN entities e ON (UPPER(e.name)=c.table_name)" + " WHERE NOT(c.column_id IS NULL) AND NOT (data_default IS NULL) ORDER BY c.table_name");
    try {
        while (rs.next()) {
            // Read streaming column at first
            String defaultValue = rs.getString(1);
            String tableName = rs.getString(2);
            String columnName = rs.getString(3);
            SqlColumnInfo column = findColumn(result, tableName, columnName);
            if (column == null)
                continue;
            defaultValue = defaultValue.trim();
            defaultValue = DEFAULT_DATE_PATTERN.matcher(defaultValue).replaceFirst("'$1'");
            if ("'auto-identity'".equals(defaultValue)) {
                column.setAutoIncrement(true);
            } else {
                column.setDefaultValue(defaultValue);
            }
        }
    } finally {
        connector.close(rs);
    }
    /*rs = connector.executeQuery( "SELECT uc.SEARCH_CONDITION,uc.TABLE_NAME FROM user_constraints uc "
            + " JOIN entities e ON (UPPER(e.name)=uc.table_name)"
            + " WHERE uc.CONSTRAINT_TYPE = 'C'" );*/
    // The following query works faster (much faster!) as it doesn't return "NOT NULL" constraints
    // though it's probably Oracle version specific (at least undocumented)
    // tested on Oracle 11r2
    rs = connector.executeQuery("SELECT c.condition,o.name " + "FROM sys.cdef$ c, sys.\"_CURRENT_EDITION_OBJ\" o,entities e " + "WHERE c.type#=1 AND c.obj# = o.obj# " + "AND o.owner# = userenv('SCHEMAID') " + "AND UPPER(e.name)=o.name");
    try {
        while (rs.next()) {
            String constr = rs.getString(1);
            String table = rs.getString(2);
            // ENUM VALUES
            // Copied from OperationSupport.loadEntityEnums
            StringTokenizer st = new StringTokenizer(constr.trim());
            int nTok = st.countTokens();
            if (nTok < 3) {
                continue;
            }
            String colName = st.nextToken().toUpperCase();
            String in = st.nextToken();
            if (!"IN".equalsIgnoreCase(in)) {
                continue;
            }
            SqlColumnInfo column = findColumn(result, table, colName);
            if (column == null) {
                continue;
            }
            List<String> values = new ArrayList<>();
            try {
                do {
                    String val = st.nextToken("(,')");
                    if (!val.trim().isEmpty()) {
                        values.add(val);
                    }
                } while (st.hasMoreTokens());
            } catch (NoSuchElementException ignore) {
            }
            if (values.size() > 0) {
                column.setEnumValues(values.toArray(new String[values.size()]));
            }
        }
    } finally {
        connector.close(rs);
    }
    rs = connector.executeQuery("SELECT trigger_name,table_name,trigger_body FROM user_triggers " + "WHERE triggering_event='INSERT OR UPDATE' " + "AND TRIGGER_TYPE='BEFORE EACH ROW'");
    try {
        while (rs.next()) {
            // Read streaming column as first
            String triggerBody = rs.getString(3);
            String tableName = rs.getString(2);
            Matcher matcher = GENERATED_TRIGGER_PATTERN.matcher(triggerBody);
            if (matcher.find()) {
                String columnName = matcher.group(1);
                String targetName = matcher.group(3);
                SqlColumnInfo column = findColumn(result, tableName, columnName);
                if (column == null)
                    continue;
                column.setDefaultValue(new ColumnFunction(targetName, ColumnFunction.TRANSFORM_GENERIC).toString());
            }
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ColumnFunction(com.developmentontheedge.be5.metadata.model.ColumnFunction) ArrayList(java.util.ArrayList) DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) StringTokenizer(java.util.StringTokenizer) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with ProcessController

use of com.developmentontheedge.be5.metadata.util.ProcessController 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 5 with ProcessController

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

Aggregations

ResultSet (java.sql.ResultSet)12 ArrayList (java.util.ArrayList)12 DbmsConnector (com.developmentontheedge.dbms.DbmsConnector)11 HashMap (java.util.HashMap)11 List (java.util.List)11 SqlColumnInfo (com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo)6 IndexInfo (com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo)5 Matcher (java.util.regex.Matcher)5 ColumnFunction (com.developmentontheedge.be5.metadata.model.ColumnFunction)3 NullLogger (com.developmentontheedge.be5.metadata.util.NullLogger)2 ProcessController (com.developmentontheedge.be5.metadata.util.ProcessController)2 ProjectLoadException (com.developmentontheedge.be5.metadata.exception.ProjectLoadException)1 BeConnectionProfile (com.developmentontheedge.be5.metadata.model.BeConnectionProfile)1 FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)1 Module (com.developmentontheedge.be5.metadata.model.Module)1 ParseResult (com.developmentontheedge.be5.metadata.model.ParseResult)1 Project (com.developmentontheedge.be5.metadata.model.Project)1 BeSqlExecutor (com.developmentontheedge.be5.metadata.sql.BeSqlExecutor)1 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)1 DbmsSchemaReader (com.developmentontheedge.be5.metadata.sql.schema.DbmsSchemaReader)1