/** * 获取数据库中列的描述 * * @param tableName * @param session * @return * @throws SQLException */ public static Map<String, ColumnMeta> getColumnMetadataFormDataBase( String schemaName, String tableName, Session session) throws SQLException { Connection conn = SessionFactoryUtils.getDataSource(session.getSessionFactory()).getConnection(); DatabaseMetaData dbMetaData = conn.getMetaData(); ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%"); ColumnMeta columnMeta; Map<String, ColumnMeta> columnMap = new HashMap<String, ColumnMeta>(); while (rs.next()) { columnMeta = new ColumnMeta(); columnMeta.setTableName(rs.getString("COLUMN_NAME").toLowerCase()); columnMeta.setColumnName(rs.getString("COLUMN_NAME").toLowerCase()); columnMeta.setColunmType( dbTableHandle.getMatchClassTypeByDataType( rs.getString("TYPE_NAME"), rs.getInt("DECIMAL_DIGITS"))); columnMeta.setColumnSize(rs.getInt("COLUMN_SIZE")); columnMeta.setDecimalDigits(rs.getInt("DECIMAL_DIGITS")); columnMeta.setIsNullable(rs.getInt("NULLABLE") == 1 ? "Y" : "N"); columnMeta.setComment(rs.getString("REMARKS")); columnMeta.setFieldDefault( judgeIsNumber(rs.getString("COLUMN_DEF")) == null ? "" : judgeIsNumber(rs.getString("COLUMN_DEF"))); logger.info( "getColumnMetadataFormDataBase --->COLUMN_NAME:" + rs.getString("COLUMN_NAME") + " TYPE_NAME :" + rs.getString("TYPE_NAME") + " DECIMAL_DIGITS:" + rs.getInt("DECIMAL_DIGITS") + " COLUMN_SIZE:" + rs.getInt("COLUMN_SIZE")); columnMap.put(rs.getString("COLUMN_NAME").toLowerCase(), columnMeta); } return columnMap; }
/** * 添加注释的sql @Author JueYue * * @date 2013年12月1日 * @param cgFormColumnMeta * @return */ private String getCommentSql(ColumnMeta columnMeta) { return dbTableHandle.getCommentSql(columnMeta); }
/** * 创建增加字段的sql * * @param column * @param agoColumn * @return */ private static String getAddColumnSql(ColumnMeta columnMeta) { // return " ADD COLUMN "+getFieldDesc(columnMeta)+","; return dbTableHandle.getAddColumnSql(columnMeta); }
/** * 修改列名 * * @param columnMeta * @return */ private static String getReNameFieldName(ColumnMeta columnMeta) { // CHANGE COLUMN `name1` `name2` varchar(50) NULL COMMENT '姓名'; // return "CHANGE COLUMN "+columnMeta.getOldColumnName() +" "+getFieldDesc(columnMeta)+","; return dbTableHandle.getReNameFieldName(columnMeta); }
/** * 处理特殊sql * * @param cgformcolumnMeta * @param datacolumnMeta * @return */ private static String getUpdateSpecialSql( ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) { return dbTableHandle.getSpecialHandle(cgformcolumnMeta, datacolumnMeta); }
/** * 创建更新字段的sql * * @param newColumn * @param agoColumn * @return */ private static String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) throws DBException { // modify birthday varchar2(10) not null; // return " MODIFY COLUMN "+getFieldDesc(columnMeta)+","; return dbTableHandle.getUpdateColumnSql(cgformcolumnMeta, datacolumnMeta); }
/** * 创建删除字段的sql * * @param fieldName * @return */ private static String getDropColumnSql(String fieldName) { // ALTER TABLE `test` DROP COLUMN `aaaa`; return dbTableHandle.getDropColumnSql(fieldName); }