Search in sources :

Example 1 with ColumnMeta

use of org.sagacity.sqltoy.model.ColumnMeta in project sagacity-sqltoy by chenrenfei.

the class DefaultDialectUtils method getTableColumns.

@SuppressWarnings("unchecked")
public static List<ColumnMeta> getTableColumns(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    ResultSet rs = conn.getMetaData().getColumns(catalog, schema, tableName, "%");
    // 通过preparedStatementProcess反调,第二个参数是pst
    List<ColumnMeta> tableCols = (List<ColumnMeta>) SqlUtil.preparedStatementProcess(null, null, rs, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
            List<ColumnMeta> colMetas = new ArrayList<ColumnMeta>();
            String isAutoIncrement;
            while (rs.next()) {
                ColumnMeta colMeta = new ColumnMeta();
                colMeta.setColName(rs.getString("COLUMN_NAME"));
                colMeta.setDataType(rs.getInt("DATA_TYPE"));
                colMeta.setTypeName(rs.getString("TYPE_NAME"));
                colMeta.setDefaultValue(SqlUtil.clearDefaultValue(rs.getString("COLUMN_DEF")));
                colMeta.setColumnSize(rs.getInt("COLUMN_SIZE"));
                colMeta.setDecimalDigits(rs.getInt("DECIMAL_DIGITS"));
                colMeta.setNumPrecRadix(rs.getInt("NUM_PREC_RADIX"));
                colMeta.setComments(rs.getString("REMARKS"));
                isAutoIncrement = rs.getString("IS_AUTOINCREMENT");
                if (isAutoIncrement != null && (isAutoIncrement.equalsIgnoreCase("true") || isAutoIncrement.equalsIgnoreCase("YES") || isAutoIncrement.equalsIgnoreCase("Y") || isAutoIncrement.equals("1"))) {
                    colMeta.setAutoIncrement(true);
                } else {
                    colMeta.setAutoIncrement(false);
                }
                if (rs.getInt("NULLABLE") == 1) {
                    colMeta.setNullable(true);
                } else {
                    colMeta.setNullable(false);
                }
                colMetas.add(colMeta);
            }
            this.setResult(colMetas);
        }
    });
    // 获取主键信息
    Map<String, ColumnMeta> pkMap = getTablePrimaryKeys(catalog, schema, tableName, conn, dbType, dialect);
    if (pkMap == null || pkMap.isEmpty()) {
        return tableCols;
    }
    ColumnMeta mapMeta;
    for (ColumnMeta colMeta : tableCols) {
        mapMeta = pkMap.get(colMeta.getColName());
        if (mapMeta != null) {
            colMeta.setPK(true);
        }
    }
    return tableCols;
}
Also used : ColumnMeta(org.sagacity.sqltoy.model.ColumnMeta) PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement)

Example 2 with ColumnMeta

use of org.sagacity.sqltoy.model.ColumnMeta in project sagacity-sqltoy by chenrenfei.

the class ClickHouseDialectUtils method getTableColumns.

@SuppressWarnings("unchecked")
public static List<ColumnMeta> getTableColumns(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    List<ColumnMeta> tableColumns = DefaultDialectUtils.getTableColumns(catalog, schema, tableName, conn, dbType, dialect);
    String sql = "SELECT name COLUMN_NAME,comment COMMENTS,is_in_primary_key PRIMARY_KEY,is_in_partition_key PARTITION_KEY from system.columns t where t.table=?";
    PreparedStatement pst = conn.prepareStatement(sql);
    ResultSet rs = null;
    // 通过preparedStatementProcess反调,第二个参数是pst
    Map<String, ColumnMeta> colMap = (Map<String, ColumnMeta>) SqlUtil.preparedStatementProcess(null, pst, rs, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
            pst.setString(1, tableName);
            rs = pst.executeQuery();
            Map<String, ColumnMeta> colComments = new HashMap<String, ColumnMeta>();
            while (rs.next()) {
                ColumnMeta colMeta = new ColumnMeta();
                colMeta.setColName(rs.getString("COLUMN_NAME"));
                colMeta.setComments(rs.getString("COMMENTS"));
                colMeta.setPK(rs.getString("PRIMARY_KEY").equals("1") ? true : false);
                colMeta.setPartitionKey(rs.getString("PARTITION_KEY").equals("1") ? true : false);
                colComments.put(colMeta.getColName(), colMeta);
            }
            this.setResult(colComments);
        }
    });
    ColumnMeta mapColMeta;
    for (ColumnMeta col : tableColumns) {
        mapColMeta = colMap.get(col.getColName());
        if (mapColMeta != null) {
            col.setComments(mapColMeta.getComments());
            col.setPK(mapColMeta.isPK());
            col.setPartitionKey(mapColMeta.isPartitionKey());
        }
    }
    return tableColumns;
}
Also used : ColumnMeta(org.sagacity.sqltoy.model.ColumnMeta) HashMap(java.util.HashMap) PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ColumnMeta

use of org.sagacity.sqltoy.model.ColumnMeta in project sagacity-sqltoy by chenrenfei.

the class OracleDialectUtils method getTableColumns.

@SuppressWarnings("unchecked")
public static List<ColumnMeta> getTableColumns(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    List<ColumnMeta> tableColumns = DefaultDialectUtils.getTableColumns(catalog, schema, tableName, conn, dbType, dialect);
    String sql = "SELECT COLUMN_NAME,COMMENTS FROM USER_COL_COMMENTS WHERE TABLE_NAME=?";
    PreparedStatement pst = conn.prepareStatement(sql);
    ResultSet rs = null;
    // 通过preparedStatementProcess反调,第二个参数是pst
    Map<String, String> colMap = (Map<String, String>) SqlUtil.preparedStatementProcess(null, pst, rs, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
            pst.setString(1, tableName);
            rs = pst.executeQuery();
            Map<String, String> colComments = new HashMap<String, String>();
            String comment;
            while (rs.next()) {
                comment = rs.getString("COMMENTS");
                if (comment != null) {
                    colComments.put(rs.getString("COLUMN_NAME").toUpperCase(), comment);
                }
            }
            this.setResult(colComments);
        }
    });
    for (ColumnMeta col : tableColumns) {
        col.setComments(colMap.get(col.getColName().toUpperCase()));
    }
    return tableColumns;
}
Also used : ColumnMeta(org.sagacity.sqltoy.model.ColumnMeta) HashMap(java.util.HashMap) PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ColumnMeta

use of org.sagacity.sqltoy.model.ColumnMeta in project sagacity-sqltoy by chenrenfei.

the class OracleDialect method getTableColumns.

@Override
public List<ColumnMeta> getTableColumns(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    List<ColumnMeta> tableColumns = OracleDialectUtils.getTableColumns(catalog, schema, tableName, conn, dbType, dialect);
    // 获取主键信息
    Map<String, ColumnMeta> pkMap = DefaultDialectUtils.getTablePrimaryKeys(catalog, schema, tableName, conn, dbType, dialect);
    if (pkMap == null || pkMap.isEmpty()) {
        return tableColumns;
    }
    ColumnMeta mapMeta;
    for (ColumnMeta colMeta : tableColumns) {
        mapMeta = pkMap.get(colMeta.getColName());
        if (mapMeta != null) {
            colMeta.setPK(true);
        }
    }
    return tableColumns;
}
Also used : ColumnMeta(org.sagacity.sqltoy.model.ColumnMeta)

Example 5 with ColumnMeta

use of org.sagacity.sqltoy.model.ColumnMeta in project sagacity-sqltoy by chenrenfei.

the class SqlServerDialect method getTableColumns.

@Override
public List<ColumnMeta> getTableColumns(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    List<ColumnMeta> tableColumns = SqlServerDialectUtils.getTableColumns(catalog, schema, tableName, conn, dbType, dialect);
    // 获取主键信息
    Map<String, ColumnMeta> pkMap = DefaultDialectUtils.getTablePrimaryKeys(catalog, schema, tableName, conn, dbType, dialect);
    if (pkMap == null || pkMap.isEmpty()) {
        return tableColumns;
    }
    ColumnMeta mapMeta;
    for (ColumnMeta colMeta : tableColumns) {
        mapMeta = pkMap.get(colMeta.getColName());
        if (mapMeta != null) {
            colMeta.setPK(true);
        }
    }
    return tableColumns;
}
Also used : ColumnMeta(org.sagacity.sqltoy.model.ColumnMeta)

Aggregations

ColumnMeta (org.sagacity.sqltoy.model.ColumnMeta)9 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 PreparedStatementResultHandler (org.sagacity.sqltoy.callback.PreparedStatementResultHandler)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)1 IgnoreKeyCaseMap (org.sagacity.sqltoy.model.IgnoreKeyCaseMap)1