Search in sources :

Example 6 with ColumnMeta

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

the class ImpalaDialect method getTableColumns.

@SuppressWarnings("unchecked")
@Override
public List<ColumnMeta> getTableColumns(String catalog, String schema, String tableName, Connection conn, Integer dbType, String dialect) throws Exception {
    List<ColumnMeta> tableCols = DefaultDialectUtils.getTableColumns(catalog, schema, tableName, conn, dbType, dialect);
    // 获取主键信息
    ResultSet rs = conn.createStatement().executeQuery("DESCRIBE " + tableName);
    Map<String, ColumnMeta> colMap = (Map<String, ColumnMeta>) SqlUtil.preparedStatementProcess(null, null, rs, new PreparedStatementResultHandler() {

        @Override
        public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
            Map<String, ColumnMeta> colComments = new HashMap<String, ColumnMeta>();
            while (rs.next()) {
                ColumnMeta colMeta = new ColumnMeta();
                colMeta.setColName(rs.getString("NAME"));
                colMeta.setPK(rs.getBoolean("PRIMARY_KEY"));
                colComments.put(colMeta.getColName(), colMeta);
            }
            this.setResult(colComments);
        }
    });
    ColumnMeta mapColMeta;
    for (ColumnMeta col : tableCols) {
        mapColMeta = colMap.get(col.getColName());
        if (mapColMeta != null) {
            col.setPK(mapColMeta.isPK());
        }
    }
    return tableCols;
}
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 7 with ColumnMeta

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

the class SqlServerDialectUtils 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 a.name COLUMN_NAME," + "				 cast(isnull(g.[value],'') as varchar(1000)) as COMMENTS " + "				 FROM syscolumns a  inner join sysobjects d on a.id=d.id " + "				 and d.xtype='U' and d.name<>'dtproperties' " + "				 left join syscomments e on a.cdefault=e.id" + "				 left join sys.extended_properties g " + "				 on a.id=g.major_id AND a.colid = g.minor_id   where d.name=? " + "   order by a.id,a.colorder";
    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) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) Map(java.util.Map)

Example 8 with ColumnMeta

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

the class DefaultDialectUtils method getTablePrimaryKeys.

/**
 * @TODO 获取表的主键字段
 * @param catalog
 * @param schema
 * @param tableName
 * @param conn
 * @param dbType
 * @param dialect
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static Map<String, ColumnMeta> getTablePrimaryKeys(String catalog, String schema, String tableName, Connection conn, final Integer dbType, String dialect) throws Exception {
    ResultSet rs = null;
    try {
        rs = conn.getMetaData().getPrimaryKeys(catalog, schema, tableName);
    } catch (Exception e) {
    }
    if (rs != null) {
        return (Map<String, ColumnMeta>) SqlUtil.preparedStatementProcess(null, null, rs, new PreparedStatementResultHandler() {

            @Override
            public void execute(Object rowData, PreparedStatement pst, ResultSet rs) throws Exception {
                Map<String, ColumnMeta> pkMeta = new HashMap<String, ColumnMeta>();
                while (rs.next()) {
                    ColumnMeta colMeta = new ColumnMeta();
                    colMeta.setColName(rs.getString("COLUMN_NAME"));
                    colMeta.setPK(true);
                    pkMeta.put(colMeta.getColName(), colMeta);
                }
                this.setResult(pkMeta);
            }
        });
    } else if (dbType == DBType.MYSQL || dbType == DBType.MYSQL57) {
        rs = conn.createStatement().executeQuery("desc " + tableName);
        return (Map<String, ColumnMeta>) SqlUtil.preparedStatementProcess(null, null, rs, new PreparedStatementResultHandler() {

            public void execute(Object obj, PreparedStatement pst, ResultSet rs) throws SQLException {
                Map<String, ColumnMeta> pkMeta = new HashMap<String, ColumnMeta>();
                while (rs.next()) {
                    ColumnMeta colMeta = new ColumnMeta();
                    colMeta.setColName(rs.getString("FIELD"));
                    colMeta.setPK(rs.getBoolean("KEY"));
                    if (colMeta.isPK()) {
                        pkMeta.put(colMeta.getColName(), colMeta);
                    }
                }
                this.setResult(pkMeta);
            }
        });
    }
    return null;
}
Also used : ColumnMeta(org.sagacity.sqltoy.model.ColumnMeta) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) PreparedStatementResultHandler(org.sagacity.sqltoy.callback.PreparedStatementResultHandler) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) SQLException(java.sql.SQLException)

Example 9 with ColumnMeta

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

the class Oracle11gDialect 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)

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