|
@ -75,21 +75,38 @@ public class MetaDataLoaderImpl implements MetaDataLoader { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@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<>(); |
|
|
List<Table> result =new ArrayList<>(); |
|
|
Connection connection =null; |
|
|
Connection connection =null; |
|
|
ResultSet rs =null; |
|
|
ResultSet rs =null; |
|
|
try { |
|
|
try { |
|
|
connection =dataSource.getConnection(); |
|
|
connection =dataSource.getConnection(); |
|
|
DatabaseMetaData databaseMetaData =connection.getMetaData(); |
|
|
DatabaseMetaData databaseMetaData =connection.getMetaData(); |
|
|
|
|
|
if(tableNames==null || tableNames.length==0) { |
|
|
rs = databaseMetaData.getTables(catalogName, schemaName, null, new String[]{"TABLE"}); |
|
|
rs = databaseMetaData.getTables(catalogName, schemaName, null, new String[]{"TABLE"}); |
|
|
while (rs.next()) { |
|
|
while (rs.next()) { |
|
|
Table table = new Table(); |
|
|
Table table = new Table(); |
|
|
table.setName(rs.getString("TABLE_NAME")); |
|
|
table.setName(rs.getString("TABLE_NAME")); |
|
|
table.setRemarks(rs.getString("REMARKS")); |
|
|
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); |
|
|
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.setForeignKeys(getForeignKeys(dataSource, catalogName, schemaName, table.getName())); |
|
|
|
|
|
buildSelfReference(table); |
|
|
|
|
|
result.add(table); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} catch (SQLException e) { |
|
|
} catch (SQLException e) { |
|
|
throw new MetaDataAccessException(e); |
|
|
throw new MetaDataAccessException(e); |
|
|
}finally { |
|
|
}finally { |
|
@ -120,6 +137,47 @@ public class MetaDataLoaderImpl implements MetaDataLoader { |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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()){ |
|
|
|
|
|
return Collections.emptyList(); |
|
|
|
|
|
} |
|
|
|
|
|
List<TableSummary> result = new ArrayList<>(); |
|
|
|
|
|
for (Table table : tables) { |
|
|
|
|
|
TableSummary tableSummary =new TableSummary(); |
|
|
|
|
|
tableSummary.setName(table.getName()); |
|
|
|
|
|
tableSummary.setRemarks(table.getRemarks()); |
|
|
|
|
|
if(isCount){ |
|
|
|
|
|
try( |
|
|
|
|
|
Connection connection =dataSource.getConnection(); |
|
|
|
|
|
PreparedStatement ps =connection.prepareStatement("select count(1) from " + table.getName()); |
|
|
|
|
|
ResultSet rs =ps.executeQuery(); |
|
|
|
|
|
){ |
|
|
|
|
|
rs.next(); |
|
|
|
|
|
tableSummary.setCount(rs.getLong(1)); |
|
|
|
|
|
}catch (SQLException e){ |
|
|
|
|
|
throw new MetaDataAccessException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
result.add(tableSummary); |
|
|
|
|
|
} |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private Column getColumn(ResultSet rs) throws SQLException { |
|
|
private Column getColumn(ResultSet rs) throws SQLException { |
|
|
String TABLE_CAT =rs.getString("TABLE_CAT"); //table catalog (may be null)
|
|
|
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_SCHEM =rs.getString("TABLE_SCHEM"); //table schema (may be null)
|
|
@ -150,123 +208,33 @@ public class MetaDataLoaderImpl implements MetaDataLoader { |
|
|
column.setRemarks(REMARKS); |
|
|
column.setRemarks(REMARKS); |
|
|
column.setSqlType(SqlTypeUtil.getSqlTypeName(DATA_TYPE)); |
|
|
column.setSqlType(SqlTypeUtil.getSqlTypeName(DATA_TYPE)); |
|
|
column.setJavaType(SqlTypeUtil.getJavaType(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; |
|
|
return column; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public List<TableSummary> getTableSummary(DataSource dataSource, String catalogName, String schemaName, boolean isCount) throws MetaDataAccessException { |
|
|
private List<ForeignKey> getForeignKeys(DataSource dataSource, String catalogName, String schemaName, String tableName) throws MetaDataAccessException { |
|
|
List<Table> tables =this.getTables(dataSource,catalogName,schemaName); |
|
|
List<ForeignKey> result =new ArrayList<>(); |
|
|
if(tables==null || tables.isEmpty()){ |
|
|
Connection connection =null; |
|
|
return Collections.emptyList(); |
|
|
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); |
|
|
} |
|
|
} |
|
|
List<TableSummary> result = new ArrayList<>(); |
|
|
|
|
|
for (Table table : tables) { |
|
|
|
|
|
TableSummary tableSummary =new TableSummary(); |
|
|
|
|
|
tableSummary.setName(table.getName()); |
|
|
|
|
|
tableSummary.setRemarks(table.getRemarks()); |
|
|
|
|
|
if(isCount){ |
|
|
|
|
|
try( |
|
|
|
|
|
Connection connection =dataSource.getConnection(); |
|
|
|
|
|
PreparedStatement ps =connection.prepareStatement("select count(1) from " + table.getName()); |
|
|
|
|
|
ResultSet rs =ps.executeQuery(); |
|
|
|
|
|
){ |
|
|
|
|
|
rs.next(); |
|
|
|
|
|
tableSummary.setCount(rs.getLong(1)); |
|
|
|
|
|
} catch (SQLException e) { |
|
|
} catch (SQLException e) { |
|
|
throw new MetaDataAccessException(e); |
|
|
throw new MetaDataAccessException(e); |
|
|
} |
|
|
}finally { |
|
|
} |
|
|
try{rs.close();} catch (SQLException e) {} |
|
|
result.add(tableSummary); |
|
|
try{connection.close();} catch (SQLException e) {} |
|
|
} |
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
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()); |
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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 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)); |
|
|
|
|
|
} |
|
|
} |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|