Search in sources :

Example 1 with DbmsConnector

use of com.developmentontheedge.dbms.DbmsConnector 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 DbmsConnector

use of com.developmentontheedge.dbms.DbmsConnector in project be5 by DevelopmentOnTheEdge.

the class H2SchemaReader 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<>();
    // }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with DbmsConnector

use of com.developmentontheedge.dbms.DbmsConnector in project be5 by DevelopmentOnTheEdge.

the class H2SchemaReader method getDefaultSchema.

@Override
public String getDefaultSchema(SqlExecutor sqlExecutor) {
    DbmsConnector connector = null;
    ResultSet rs = null;
    try {
        connector = sqlExecutor.getConnector();
        rs = sqlExecutor.getConnector().executeQuery("SELECT SCHEMA()");
        rs.next();
        String search_path = rs.getString(1);
        // for different settings: default and after setup
        if (search_path.contains(","))
            return search_path.split(",")[1].trim();
        else
            return search_path.trim();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (connector != null)
            connector.close(rs);
    }
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 4 with DbmsConnector

use of com.developmentontheedge.dbms.DbmsConnector in project be5 by DevelopmentOnTheEdge.

the class H2SchemaReader method readTableNames.

@Override
public Map<String, String> readTableNames(SqlExecutor sql, String defSchema, ProcessController controller) throws SQLException {
    DbmsConnector connector = sql.getConnector();
    Map<String, String> result = new HashMap<>();
    ResultSet rs = connector.executeQuery("SELECT table_name,table_type FROM information_schema.tables t WHERE table_schema='" + defSchema + "' AND table_type IN ('TABLE','VIEW')");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1).toLowerCase();
            // if(!tableName.equals( tableName.toLowerCase() ))
            // continue;
            String type = rs.getString(2);
            // if ( "TABLE".equals( type ) )
            // {
            // type = "TABLE";
            // }
            result.put(tableName, type);
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet)

Example 5 with DbmsConnector

use of com.developmentontheedge.dbms.DbmsConnector 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)

Aggregations

DbmsConnector (com.developmentontheedge.dbms.DbmsConnector)19 ResultSet (java.sql.ResultSet)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)12 List (java.util.List)12 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 SQLException (java.sql.SQLException)2 Connection (java.sql.Connection)1 NoSuchElementException (java.util.NoSuchElementException)1 StringTokenizer (java.util.StringTokenizer)1