@ -75,21 +75,38 @@ public class MetaDataLoaderImpl implements MetaDataLoader {
}
@Override
public List < Table > getTables ( DataSource dataSource , String catalogName , String schemaName ) throws MetaDataAccessException {
public List < Table > getTables ( DataSource dataSource , String catalogName , String schemaName , String . . . tableNames ) throws MetaDataAccessException {
List < Table > result = new ArrayList < > ( ) ;
Connection connection = null ;
ResultSet rs = null ;
try {
connection = dataSource . getConnection ( ) ;
DatabaseMetaData databaseMetaData = connection . getMetaData ( ) ;
rs = databaseMetaData . getTables ( catalogName , schemaName , null , new String [ ] { "TABLE" } ) ;
while ( rs . next ( ) ) {
Table table = new Table ( ) ;
if ( tableNames = = null | | tableNames . length = = 0 ) {
rs = databaseMetaData . getTables ( catalogName , schemaName , null , new String [ ] { "TABLE" } ) ;
while ( rs . next ( ) ) {
Table table = new Table ( ) ;
table . setName ( rs . getString ( "TABLE_NAME" ) ) ;
table . setRemarks ( rs . getString ( "REMARKS" ) ) ;
table . setColumns ( getColumns ( dataSource , catalogName , schemaName , table . getName ( ) ) ) ;
table . setForeignKeys ( getForeignKeys ( dataSource , catalogName , schemaName , table . getName ( ) ) ) ;
buildSelfReference ( table ) ;
result . add ( table ) ;
}
} else {
for ( String tableName : tableNames ) {
rs = databaseMetaData . getTables ( catalogName , schemaName , tableName , new String [ ] { "TABLE" } ) ;
while ( rs . next ( ) ) {
Table table = new Table ( ) ;
table . setName ( rs . getString ( "TABLE_NAME" ) ) ;
table . setRemarks ( rs . getString ( "REMARKS" ) ) ;
table . setColumns ( getColumns ( dataSource , catalogName , schemaName , table . getName ( ) ) ) ;
table . setColumns ( getColumns ( dataSource , catalogName , schemaName , table . getName ( ) ) ) ;
table . setForeignKeys ( getForeignKeys ( dataSource , catalogName , schemaName , table . getName ( ) ) ) ;
buildSelfReference ( table ) ;
result . add ( table ) ;
}
}
}
} catch ( SQLException e ) {
throw new MetaDataAccessException ( e ) ;
} finally {
@ -120,40 +137,6 @@ public class MetaDataLoaderImpl implements MetaDataLoader {
return result ;
}
private Column getColumn ( ResultSet rs ) throws SQLException {
String TABLE_CAT = rs . getString ( "TABLE_CAT" ) ; //table catalog (may be null)
String TABLE_SCHEM = rs . getString ( "TABLE_SCHEM" ) ; //table schema (may be null)
String TABLE_NAME = rs . getString ( "TABLE_NAME" ) ; //table name (表名称)
String COLUMN_NAME = rs . getString ( "COLUMN_NAME" ) ; //column name(列名)
int DATA_TYPE = rs . getInt ( "DATA_TYPE" ) ; //SQL type from java.sql.Types(列的数据类型)
String TYPE_NAME = rs . getString ( "TYPE_NAME" ) ; //Data source dependent type name, for a UDT the type name is fully qualified
int COLUMN_SIZE = rs . getInt ( "COLUMN_SIZE" ) ; //column size
int BUFFER_LENGTH = rs . getInt ( "BUFFER_LENGTH" ) ; //is not used
int DECIMAL_DIGITS = rs . getInt ( "DECIMAL_DIGITS" ) ; //the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable.
int NUM_PREC_RADIX = rs . getInt ( "NUM_PREC_RADIX" ) ; //Radix (typically either 10 or 2)
int NULLABLE = rs . getInt ( "NULLABLE" ) ; //is NULL allowed.
String REMARKS = rs . getString ( "REMARKS" ) ; //comment describing column (may be null)
String COLUMN_DEF = rs . getString ( "COLUMN_DEF" ) ; //default value for the column, (may be null)
int SQL_DATA_TYPE = rs . getInt ( "SQL_DATA_TYPE" ) ; //unused
int SQL_DATETIME_SUB = rs . getInt ( "SQL_DATETIME_SUB" ) ; //unused
int CHAR_OCTET_LENGTH = rs . getInt ( "CHAR_OCTET_LENGTH" ) ; //for char types the maximum number of bytes in the column
int ORDINAL_POSITION = rs . getInt ( "ORDINAL_POSITION" ) ; //index of column in table (starting at 1)
String IS_NULLABLE = rs . getString ( "IS_NULLABLE" ) ; //ISO rules are used to determine the nullability for a column.
//String SCOPE_CATLOG =rs.getString("SCOPE_CATLOG"); //catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
//String SCOPE_SCHEMA =rs.getString("SCOPE_SCHEMA"); //schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
//String SCOPE_TABLE =rs.getString("SCOPE_TABLE"); //table name that this the scope of a reference attribure (null if the DATA_TYPE isn't REF)
String SOURCE_DATA_TYPE = rs . getString ( "SOURCE_DATA_TYPE" ) ; //source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types
String IS_AUTOINCREMENT = rs . getString ( "IS_AUTOINCREMENT" ) ; //Indicates whether this column is auto incremented
Column column = new Column ( ) ;
column . setName ( COLUMN_NAME ) ;
column . setRemarks ( REMARKS ) ;
column . setSqlType ( SqlTypeUtil . getSqlTypeName ( DATA_TYPE ) ) ;
column . setJavaType ( SqlTypeUtil . getJavaType ( DATA_TYPE ) ) ;
return column ;
}
public List < TableSummary > getTableSummary ( DataSource dataSource , String catalogName , String schemaName , boolean isCount ) throws MetaDataAccessException {
List < Table > tables = this . getTables ( dataSource , catalogName , schemaName ) ;
if ( tables = = null | | tables . isEmpty ( ) ) {
@ -181,92 +164,77 @@ public class MetaDataLoaderImpl implements MetaDataLoader {
return result ;
}
public boolean isSelfReference ( schemacrawler . schema . Table table ) {
Collection < schemacrawler . schema . Table > parentTables = table . getRelatedTables ( TableRelationshipType . parent ) ;
if ( parentTables ! = null & & ! parentTables . isEmpty ( ) ) {
for ( schemacrawler . schema . Table parentTable : parentTables ) {
if ( parentTable . equals ( table ) ) {
return true ;
}
private void buildSelfReference ( Table table ) {
if ( table = = null ) { return ; }
List < ForeignKey > foreignKeys = table . getForeignKeys ( ) ;
if ( foreignKeys = = null | | foreignKeys . isEmpty ( ) ) { return ; }
for ( ForeignKey foreignKey : foreignKeys ) {
if ( table . getName ( ) . equals ( foreignKey . getPrimaryKeyTableName ( ) ) ) {
table . setSelfReference ( true ) ;
table . setSelfReferencePrimaryKeyColumnName ( foreignKey . getPrimaryKeyColumnName ( ) ) ;
table . setSelfReferenceForeignKeyColumnName ( foreignKey . getForeignKeyColumnName ( ) ) ;
return ;
}
}
return false ;
}
private Table from ( DatabaseMetaData databaseMetaData , String tableName , String tableRemarks ) {
// Table result =new Table();
// result.setName(tableName);
// result.setRemarks(tableRemarks);
// // 处理列
// for(schemacrawler.schema.Column column : table.getColumns()) {
// result.getColumns().add(from(column));
// }
// // 处理外键
// Collection<schemacrawler.schema.ForeignKey> foreignKeys =table.getForeignKeys();
// for(schemacrawler.schema.ForeignKey foreignKey : foreignKeys){
// result.getForeignKeys().add(from(foreignKey));
// }
// // 处理自引用
// for(ForeignKey foreignKey : result.getForeignKeys()){
// if(foreignKey.getForeignKeyTableName().equalsIgnoreCase(foreignKey.getPrimaryKeyTableName())){
// result.setSelfReferenceForeignKeyColumnName(foreignKey.getForeignKeyColumnName());
// result.setSelfReferencePrimaryKeyColumnName(foreignKey.getPrimaryKeyColumnName());
// }
// }
// // 处理索引
// Collection<schemacrawler.schema.Index> indexes =table.getIndexes();
// for(schemacrawler.schema.Index index : indexes){
// result.getIndexes().add(from(index));
// }
// return result;
return null ;
}
private ForeignKey from ( schemacrawler . schema . ForeignKey foreignKey ) {
ForeignKey result = new ForeignKey ( ) ;
result . setForeignKeyTableName ( foreignKey . getForeignKeyTable ( ) . getName ( ) ) ;
result . setPrimaryKeyTableName ( foreignKey . getPrimaryKeyTable ( ) . getName ( ) ) ;
private Column getColumn ( ResultSet rs ) throws SQLException {
String TABLE_CAT = rs . getString ( "TABLE_CAT" ) ; //table catalog (may be null)
String TABLE_SCHEM = rs . getString ( "TABLE_SCHEM" ) ; //table schema (may be null)
String TABLE_NAME = rs . getString ( "TABLE_NAME" ) ; //table name (表名称)
String COLUMN_NAME = rs . getString ( "COLUMN_NAME" ) ; //column name(列名)
int DATA_TYPE = rs . getInt ( "DATA_TYPE" ) ; //SQL type from java.sql.Types(列的数据类型)
String TYPE_NAME = rs . getString ( "TYPE_NAME" ) ; //Data source dependent type name, for a UDT the type name is fully qualified
int COLUMN_SIZE = rs . getInt ( "COLUMN_SIZE" ) ; //column size
int BUFFER_LENGTH = rs . getInt ( "BUFFER_LENGTH" ) ; //is not used
int DECIMAL_DIGITS = rs . getInt ( "DECIMAL_DIGITS" ) ; //the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable.
int NUM_PREC_RADIX = rs . getInt ( "NUM_PREC_RADIX" ) ; //Radix (typically either 10 or 2)
int NULLABLE = rs . getInt ( "NULLABLE" ) ; //is NULL allowed.
String REMARKS = rs . getString ( "REMARKS" ) ; //comment describing column (may be null)
String COLUMN_DEF = rs . getString ( "COLUMN_DEF" ) ; //default value for the column, (may be null)
int SQL_DATA_TYPE = rs . getInt ( "SQL_DATA_TYPE" ) ; //unused
int SQL_DATETIME_SUB = rs . getInt ( "SQL_DATETIME_SUB" ) ; //unused
int CHAR_OCTET_LENGTH = rs . getInt ( "CHAR_OCTET_LENGTH" ) ; //for char types the maximum number of bytes in the column
int ORDINAL_POSITION = rs . getInt ( "ORDINAL_POSITION" ) ; //index of column in table (starting at 1)
String IS_NULLABLE = rs . getString ( "IS_NULLABLE" ) ; //ISO rules are used to determine the nullability for a column.
//String SCOPE_CATLOG =rs.getString("SCOPE_CATLOG"); //catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
//String SCOPE_SCHEMA =rs.getString("SCOPE_SCHEMA"); //schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
//String SCOPE_TABLE =rs.getString("SCOPE_TABLE"); //table name that this the scope of a reference attribure (null if the DATA_TYPE isn't REF)
String SOURCE_DATA_TYPE = rs . getString ( "SOURCE_DATA_TYPE" ) ; //source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types
String IS_AUTOINCREMENT = rs . getString ( "IS_AUTOINCREMENT" ) ; //Indicates whether this column is auto incremented
List < ColumnReference > columnReferences = foreignKey . getColumnReferences ( ) ;
if ( columnReferences ! = null & & ! columnReferences . isEmpty ( ) ) {
for ( ColumnReference columnReference : columnReferences ) {
schemacrawler . schema . Column foreignKeyColumn = columnReference . getForeignKeyColumn ( ) ;
schemacrawler . schema . Column primaryKeyColumn = columnReference . getPrimaryKeyColumn ( ) ;
result . setForeignKeyColumnName ( foreignKeyColumn . getName ( ) ) ;
result . setPrimaryKeyColumnName ( primaryKeyColumn . getName ( ) ) ;
}
}
return result ;
Column column = new Column ( ) ;
column . setName ( COLUMN_NAME ) ;
column . setRemarks ( REMARKS ) ;
column . setSqlType ( SqlTypeUtil . getSqlTypeName ( DATA_TYPE ) ) ;
column . setJavaType ( SqlTypeUtil . getJavaType ( DATA_TYPE ) ) ;
column . setSize ( COLUMN_SIZE ) ;
column . setNullable ( "YES" . equalsIgnoreCase ( IS_NULLABLE ) ? true : false ) ;
column . setAutoIncremented ( "YES" . equalsIgnoreCase ( IS_AUTOINCREMENT ) ? true : false ) ;
return column ;
}
private Column from ( schemacrawler . schema . Column column ) {
Column result = new Column ( ) ;
result . setName ( column . getName ( ) ) ;
result . setRemarks ( column . getRemarks ( ) ) ;
result . setJavaType ( column . getType ( ) . getTypeMappedClass ( ) ) ;
result . setSqlType ( column . getColumnDataType ( ) . getJavaSqlType ( ) . getName ( ) ) ;
result . setVendorTypeNumber ( column . getColumnDataType ( ) . getJavaSqlType ( ) . getVendorTypeNumber ( ) ) ;
result . setNullable ( column . isNullable ( ) ) ;
result . setDefaultValue ( column . getDefaultValue ( ) ) ;
result . setGenerated ( column . isGenerated ( ) ) ;
result . setHidden ( column . isHidden ( ) ) ;
result . setAutoIncremented ( column . isAutoIncremented ( ) ) ;
result . setPartOfIndex ( column . isPartOfIndex ( ) ) ;
result . setPartOfUniqueIndex ( column . isPartOfUniqueIndex ( ) ) ;
result . setPartOfPrimaryKey ( column . isPartOfPrimaryKey ( ) ) ;
result . setSize ( column . getSize ( ) ) ;
result . setWidth ( column . getWidth ( ) ) ;
return result ;
private List < ForeignKey > getForeignKeys ( DataSource dataSource , String catalogName , String schemaName , String tableName ) throws MetaDataAccessException {
List < ForeignKey > result = new ArrayList < > ( ) ;
Connection connection = null ;
ResultSet rs = null ;
try {
connection = dataSource . getConnection ( ) ;
DatabaseMetaData databaseMetaData = connection . getMetaData ( ) ;
rs = databaseMetaData . getImportedKeys ( catalogName , schemaName , tableName ) ;
while ( rs . next ( ) ) {
ForeignKey foreignKey = new ForeignKey ( ) ;
foreignKey . setPrimaryKeyTableName ( rs . getString ( "PKTABLE_NAME" ) ) ;
foreignKey . setPrimaryKeyColumnName ( rs . getString ( "PKCOLUMN_NAME" ) ) ;
foreignKey . setForeignKeyTableName ( rs . getString ( "FKTABLE_NAME" ) ) ;
foreignKey . setForeignKeyColumnName ( rs . getString ( "FKCOLUMN_NAME" ) ) ;
result . add ( foreignKey ) ;
}
private Index from ( schemacrawler . schema . Index index ) {
Index result = new Index ( ) ;
result . setName ( index . getName ( ) ) ;
result . setUnique ( index . isUnique ( ) ) ;
result . setIndexType ( IndexType . from ( index . getIndexType ( ) . id ( ) ) ) ;
List < schemacrawler . schema . IndexColumn > columns = index . getColumns ( ) ;
for ( schemacrawler . schema . IndexColumn column : columns ) {
result . getColumns ( ) . add ( from ( column ) ) ;
} catch ( SQLException e ) {
throw new MetaDataAccessException ( e ) ;
} finally {
try { rs . close ( ) ; } catch ( SQLException e ) { }
try { connection . close ( ) ; } catch ( SQLException e ) { }
}
return result ;
}