@Test public void testGetDbColumnMetaList() throws Exception { final MockResultSet resultSet = new MockResultSet(); ArrayMap rowData = new ArrayMap(); rowData.put("COLUMN_NAME", "column1"); rowData.put("DATA_TYPE", Types.DECIMAL); rowData.put("TYPE_NAME", "DECIMAL"); rowData.put("COLUMN_SIZE", 10); rowData.put("DECIMAL_DIGITS", 3); rowData.put("NULLABLE", DatabaseMetaData.columnNoNulls); rowData.put("COLUMN_DEF", "10.5"); rowData.put("REMARKS", "comment1"); resultSet.addRowData(rowData); rowData = new ArrayMap(); rowData.put("COLUMN_NAME", "column2"); rowData.put("DATA_TYPE", Types.VARCHAR); rowData.put("TYPE_NAME", "VARCHAR"); rowData.put("COLUMN_SIZE", 10); rowData.put("DECIMAL_DIGITS", 0); rowData.put("NULLABLE", DatabaseMetaData.columnNullable); rowData.put("COLUMN_DEF", "aaa"); rowData.put("REMARKS", "comment2"); resultSet.addRowData(rowData); GenMockDatabaseMetaData metaData = new GenMockDatabaseMetaData() { @Override public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { return resultSet; } }; DbTableMetaReaderImpl reader = new DbTableMetaReaderImpl( new MockDataSource(), new StandardGenDialect(), "schemaName", ".*", "", true); List<DbColumnMeta> list = reader.getDbColumnMetaList(metaData, new DbTableMeta()); assertEquals(2, list.size()); DbColumnMeta columnMeta = list.get(0); assertEquals("column1", columnMeta.getName()); assertEquals(Types.DECIMAL, columnMeta.getSqlType()); assertEquals("DECIMAL", columnMeta.getTypeName()); assertEquals(10, columnMeta.getLength()); assertEquals(3, columnMeta.getScale()); assertFalse(columnMeta.isNullable()); assertEquals("10.5", columnMeta.getDefaultValue()); assertEquals("comment1", columnMeta.getComment()); columnMeta = list.get(1); assertEquals("column2", columnMeta.getName()); assertEquals(Types.VARCHAR, columnMeta.getSqlType()); assertEquals("VARCHAR", columnMeta.getTypeName()); assertEquals(10, columnMeta.getLength()); assertEquals(0, columnMeta.getScale()); assertTrue(columnMeta.isNullable()); assertEquals("aaa", columnMeta.getDefaultValue()); assertEquals("comment2", columnMeta.getComment()); }
protected List<DbColumnMeta> getDbColumnMetaList(DatabaseMetaData metaData, DbTableMeta tableMeta) { List<DbColumnMeta> result = new ArrayList<DbColumnMeta>(); try { ResultSet rs = metaData.getColumns(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName(), null); try { while (rs.next()) { DbColumnMeta columnDesc = new DbColumnMeta(); columnDesc.setName(rs.getString("COLUMN_NAME")); columnDesc.setSqlType(rs.getInt("DATA_TYPE")); columnDesc.setTypeName(rs.getString("TYPE_NAME")); columnDesc.setLength(rs.getInt("COLUMN_SIZE")); columnDesc.setScale(rs.getInt("DECIMAL_DIGITS")); columnDesc.setNullable(rs.getBoolean("NULLABLE")); columnDesc.setDefaultValue(rs.getString("COLUMN_DEF")); if (readComment) { columnDesc.setComment(rs.getString("REMARKS")); } result.add(columnDesc); } return result; } finally { ResultSetUtil.close(rs); } } catch (SQLException ex) { throw new SQLRuntimeException(ex); } }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbColumnMeta> getDbColumnMetaList(DatabaseMetaData metaData, DbTableMeta tableMeta) { List<DbColumnMeta> result = new ArrayList<DbColumnMeta>(); try { ResultSet rs = metaData.getColumns(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName(), null); try { while (rs.next()) { DbColumnMeta columnDesc = new DbColumnMeta(); columnDesc.setName(rs.getString("COLUMN_NAME")); columnDesc.setSqlType(rs.getInt("DATA_TYPE")); columnDesc.setTypeName(rs.getString("TYPE_NAME")); columnDesc.setLength(rs.getInt("COLUMN_SIZE")); columnDesc.setScale(rs.getInt("DECIMAL_DIGITS")); columnDesc.setNullable(rs.getBoolean("NULLABLE")); columnDesc.setDefaultValue(rs.getString("COLUMN_DEF")); if (readComment) { columnDesc.setComment(rs.getString("REMARKS")); } result.add(columnDesc); } return result; } finally { ResultSetUtil.close(rs); } } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbColumnMeta> getDbColumnMetaList(DatabaseMetaData metaData, DbTableMeta tableMeta) { List<DbColumnMeta> result = new ArrayList<DbColumnMeta>(); try { ResultSet rs = metaData.getColumns(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName(), null); try { while (rs.next()) { DbColumnMeta columnDesc = new DbColumnMeta(); columnDesc.setName(rs.getString("COLUMN_NAME")); columnDesc.setSqlType(rs.getInt("DATA_TYPE")); columnDesc.setTypeName(rs.getString("TYPE_NAME")); columnDesc.setLength(rs.getInt("COLUMN_SIZE")); columnDesc.setScale(rs.getInt("DECIMAL_DIGITS")); columnDesc.setNullable(rs.getBoolean("NULLABLE")); columnDesc.setDefaultValue(rs.getString("COLUMN_DEF")); if (readComment) { columnDesc.setComment(rs.getString("REMARKS")); } result.add(columnDesc); } return result; } finally { ResultSetUtil.close(rs); } } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } DbTableMetaReaderImpl(DataSource dataSource, GenDialect dialect,
            String schemaName, String tableNamePattern,
            String ignoreTableNamePattern, boolean readComment); }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbColumnMeta> getDbColumnMetaList(DatabaseMetaData metaData, DbTableMeta tableMeta) { List<DbColumnMeta> result = new ArrayList<DbColumnMeta>(); try { ResultSet rs = metaData.getColumns(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName(), null); try { while (rs.next()) { DbColumnMeta columnDesc = new DbColumnMeta(); columnDesc.setName(rs.getString("COLUMN_NAME")); columnDesc.setSqlType(rs.getInt("DATA_TYPE")); columnDesc.setTypeName(rs.getString("TYPE_NAME")); columnDesc.setLength(rs.getInt("COLUMN_SIZE")); columnDesc.setScale(rs.getInt("DECIMAL_DIGITS")); columnDesc.setNullable(rs.getBoolean("NULLABLE")); columnDesc.setDefaultValue(rs.getString("COLUMN_DEF")); if (readComment) { columnDesc.setComment(rs.getString("REMARKS")); } result.add(columnDesc); } return result; } finally { ResultSetUtil.close(rs); } } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } DbTableMetaReaderImpl(DataSource dataSource, GenDialect dialect,
            String schemaName, String tableNamePattern,
            String ignoreTableNamePattern, boolean readComment); List<DbTableMeta> read(); }
DbTableMetaReaderImpl implements DbTableMetaReader { protected List<DbColumnMeta> getDbColumnMetaList(DatabaseMetaData metaData, DbTableMeta tableMeta) { List<DbColumnMeta> result = new ArrayList<DbColumnMeta>(); try { ResultSet rs = metaData.getColumns(tableMeta.getCatalogName(), tableMeta.getSchemaName(), tableMeta.getName(), null); try { while (rs.next()) { DbColumnMeta columnDesc = new DbColumnMeta(); columnDesc.setName(rs.getString("COLUMN_NAME")); columnDesc.setSqlType(rs.getInt("DATA_TYPE")); columnDesc.setTypeName(rs.getString("TYPE_NAME")); columnDesc.setLength(rs.getInt("COLUMN_SIZE")); columnDesc.setScale(rs.getInt("DECIMAL_DIGITS")); columnDesc.setNullable(rs.getBoolean("NULLABLE")); columnDesc.setDefaultValue(rs.getString("COLUMN_DEF")); if (readComment) { columnDesc.setComment(rs.getString("REMARKS")); } result.add(columnDesc); } return result; } finally { ResultSetUtil.close(rs); } } catch (SQLException ex) { throw new SQLRuntimeException(ex); } } DbTableMetaReaderImpl(DataSource dataSource, GenDialect dialect,
            String schemaName, String tableNamePattern,
            String ignoreTableNamePattern, boolean readComment); List<DbTableMeta> read();  }