/** * 返回cgForm中列的描述信息 * * @param table * @return */ public static Map<String, ColumnMeta> getColumnMetadataFormCgForm(CgFormHeadEntity table) { Map<String, ColumnMeta> map = new HashMap<String, ColumnMeta>(); List<CgFormFieldEntity> cgFormFieldEntities = table.getColumns(); ColumnMeta columnMeta; for (CgFormFieldEntity cgFormFieldEntity : cgFormFieldEntities) { columnMeta = new ColumnMeta(); columnMeta.setTableName(table.getTableName().toLowerCase()); columnMeta.setColumnId(cgFormFieldEntity.getId()); columnMeta.setColumnName(cgFormFieldEntity.getFieldName().toLowerCase()); columnMeta.setColumnSize(cgFormFieldEntity.getLength()); columnMeta.setColunmType(cgFormFieldEntity.getType().toLowerCase()); columnMeta.setIsNullable(cgFormFieldEntity.getIsNull()); columnMeta.setComment(cgFormFieldEntity.getContent()); columnMeta.setDecimalDigits(cgFormFieldEntity.getPointLength()); columnMeta.setFieldDefault(judgeIsNumber(cgFormFieldEntity.getFieldDefault())); columnMeta.setPkType(table.getJformPkType() == null ? "UUID" : table.getJformPkType()); columnMeta.setOldColumnName( cgFormFieldEntity.getOldFieldName() != null ? cgFormFieldEntity.getOldFieldName().toLowerCase() : null); logger.info( "getColumnMetadataFormCgForm ---->COLUMN_NAME:" + cgFormFieldEntity.getFieldName().toLowerCase() + " TYPE_NAME:" + cgFormFieldEntity.getType().toLowerCase() + " DECIMAL_DIGITS:" + cgFormFieldEntity.getPointLength() + " COLUMN_SIZE:" + cgFormFieldEntity.getLength()); map.put(cgFormFieldEntity.getFieldName().toLowerCase(), columnMeta); } return map; }
/** * 获取数据库中列的描述 * * @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; }