Search in sources :

Example 16 with DbmsConnector

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

the class MySqlSchemaReader 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 table_name,index_name,column_name,non_unique FROM information_schema.statistics " + "WHERE table_schema='" + defSchema + "' ORDER BY table_name,index_name,seq_in_index");
    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);
                int nonUnique = rs.getInt(4);
                curIndex.setUnique(nonUnique == 0);
            }
            String column = rs.getString(3);
            curIndex.addColumn(column);
        }
    } 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 17 with DbmsConnector

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

the class OracleSchemaReader 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 " + "t.table_name FROM user_tables t JOIN entities e ON (UPPER(e.name)=t.table_name)");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1);
            result.put(tableName.toLowerCase(), "TABLE");
        }
    } finally {
        connector.close(rs);
    }
    rs = connector.executeQuery("SELECT " + "view_name FROM user_views v JOIN entities e ON (UPPER(e.name)=v.view_name)");
    try {
        while (rs.next()) {
            String viewName = rs.getString(1);
            result.put(viewName.toLowerCase(), "VIEW");
        }
    } finally {
        connector.close(rs);
    }
    return result;
}
Also used : DbmsConnector(com.developmentontheedge.dbms.DbmsConnector) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet)

Example 18 with DbmsConnector

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

the class PostgresSchemaReader method getDefaultSchema.

@Override
public String getDefaultSchema(SqlExecutor sqlExecutor) {
    DbmsConnector connector = null;
    ResultSet rs = null;
    try {
        connector = sqlExecutor.getConnector();
        rs = sqlExecutor.getConnector().executeQuery("SHOW search_path");
        rs.next();
        String search_path = rs.getString("search_path");
        // 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 19 with DbmsConnector

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

the class PostgresSchemaReader 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 ('BASE TABLE','VIEW')");
    try {
        while (rs.next()) {
            String tableName = rs.getString(1);
            if (!tableName.equals(tableName.toLowerCase()))
                continue;
            String type = rs.getString(2);
            if ("BASE 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)

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