6
6
7
7
import java .sql .DatabaseMetaData ;
8
8
import java .sql .ResultSet ;
9
- import java .sql .ResultSetMetaData ;
10
9
import java .sql .SQLException ;
11
10
import java .util .ArrayList ;
12
11
import java .util .HashMap ;
42
41
43
42
import static java .util .Collections .addAll ;
44
43
import static org .hibernate .boot .model .naming .DatabaseIdentifier .toIdentifier ;
44
+ import static org .hibernate .internal .util .StringHelper .EMPTY_STRINGS ;
45
45
import static org .hibernate .internal .util .StringHelper .isBlank ;
46
46
import static org .hibernate .internal .util .StringHelper .splitTrimmingTokens ;
47
47
import static org .hibernate .internal .util .config .ConfigurationHelper .getBoolean ;
@@ -85,31 +85,37 @@ public AbstractInformationExtractorImpl(ExtractionContext extractionContext) {
85
85
""
86
86
)
87
87
);
88
+ final Dialect dialect = extractionContext .getJdbcEnvironment ().getDialect ();
89
+ this .extraPhysicalTableTypes = getPhysicalTableTypes ( extraPhysicalTableTypesConfig , dialect );
90
+ this .tableTypes = getTableTypes ( configService , dialect );
91
+ }
92
+
93
+ private String [] getPhysicalTableTypes (String extraPhysicalTableTypesConfig , Dialect dialect ) {
88
94
final List <String > physicalTableTypesList = new ArrayList <>();
89
95
if ( !isBlank ( extraPhysicalTableTypesConfig ) ) {
90
96
addAll ( physicalTableTypesList , splitTrimmingTokens ( ",;" , extraPhysicalTableTypesConfig , false ) );
91
97
}
92
- final Dialect dialect = extractionContext .getJdbcEnvironment ().getDialect ();
93
98
dialect .augmentPhysicalTableTypes ( physicalTableTypesList );
94
- this .extraPhysicalTableTypes = physicalTableTypesList .toArray ( new String [0 ] );
99
+ return physicalTableTypesList .toArray ( EMPTY_STRINGS );
100
+ }
95
101
102
+ private String [] getTableTypes (ConfigurationService configService , Dialect dialect ) {
96
103
final List <String > tableTypesList = new ArrayList <>();
97
104
tableTypesList .add ( "TABLE" );
98
105
tableTypesList .add ( "VIEW" );
99
106
if ( getBoolean ( AvailableSettings .ENABLE_SYNONYMS , configService .getSettings () ) ) {
100
- if ( dialect instanceof DB2Dialect ) {
107
+ if ( dialect instanceof DB2Dialect ) { //TODO: should not use Dialect types directly!
101
108
tableTypesList .add ( "ALIAS" );
102
109
}
103
110
tableTypesList .add ( "SYNONYM" );
104
111
}
105
112
addAll ( tableTypesList , extraPhysicalTableTypes );
106
113
dialect .augmentRecognizedTableTypes ( tableTypesList );
107
-
108
- this .tableTypes = tableTypesList .toArray ( new String [0 ] );
114
+ return tableTypesList .toArray ( EMPTY_STRINGS );
109
115
}
110
116
111
- protected IdentifierHelper identifierHelper () {
112
- return getIdentifierHelper ();
117
+ private IdentifierHelper getIdentifierHelper () {
118
+ return getJdbcEnvironment (). getIdentifierHelper ();
113
119
}
114
120
115
121
protected JDBCException convertSQLException (SQLException sqlException , String message ) {
@@ -272,13 +278,17 @@ public boolean catalogExists(Identifier catalog) {
272
278
protected abstract <T > T processSchemaResultSet (
273
279
String catalog ,
274
280
String schemaPattern ,
275
- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
281
+ ExtractionContext .ResultSetProcessor <T > processor )
282
+ throws SQLException ;
276
283
277
284
@ Override
278
285
public boolean schemaExists (Identifier catalog , Identifier schema ) {
279
- final String catalogFilter = determineCatalogFilter ( catalog );
280
- final String schemaFilter = determineSchemaFilter ( schema );
281
-
286
+ final String catalogFilter =
287
+ getIdentifierHelper ()
288
+ .toMetaDataCatalogName ( catalog == null ? extractionContext .getDefaultCatalog () : catalog );
289
+ final String schemaFilter =
290
+ getIdentifierHelper ()
291
+ .toMetaDataSchemaName ( schema == null ? extractionContext .getDefaultSchema () : schema );
282
292
try {
283
293
return processSchemaResultSet (
284
294
catalogFilter ,
@@ -308,32 +318,10 @@ public boolean schemaExists(Identifier catalog, Identifier schema) {
308
318
}
309
319
}
310
320
311
- private IdentifierHelper getIdentifierHelper () {
312
- return getJdbcEnvironment ().getIdentifierHelper ();
313
- }
314
-
315
- protected String determineCatalogFilter (Identifier catalog ) {
316
- Identifier identifierToUse = catalog ;
317
- if ( identifierToUse == null ) {
318
- identifierToUse = extractionContext .getDefaultCatalog ();
319
- }
320
-
321
- return getIdentifierHelper ().toMetaDataCatalogName ( identifierToUse );
322
- }
323
-
324
- protected String determineSchemaFilter (Identifier schema ) {
325
- Identifier identifierToUse = schema ;
326
- if ( identifierToUse == null ) {
327
- identifierToUse = extractionContext .getDefaultSchema ();
328
- }
329
-
330
- return getIdentifierHelper ().toMetaDataSchemaName ( identifierToUse );
331
- }
332
-
333
321
private TableInformation extractTableInformation (ResultSet resultSet ) throws SQLException {
334
322
return new TableInformationImpl (
335
323
this ,
336
- identifierHelper (),
324
+ getIdentifierHelper (),
337
325
extractTableName ( resultSet ),
338
326
isPhysicalTableType ( resultSet .getString ( getResultSetTableTypeLabel () ) ),
339
327
resultSet .getString ( getResultSetRemarksLabel () )
@@ -616,7 +604,8 @@ protected abstract <T> T processColumnsResultSet(
616
604
String schemaPattern ,
617
605
String tableNamePattern ,
618
606
String columnNamePattern ,
619
- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
607
+ ExtractionContext .ResultSetProcessor <T > processor )
608
+ throws SQLException ;
620
609
621
610
private void populateTablesWithColumns (
622
611
String catalogFilter ,
@@ -637,7 +626,7 @@ private void populateTablesWithColumns(
637
626
currentTable = tables .getTableInformation ( currentTableName );
638
627
}
639
628
if ( currentTable != null ) {
640
- addExtractedColumnInformation ( currentTable , resultSet );
629
+ currentTable . addColumn ( columnInformation ( currentTable , resultSet ) );
641
630
}
642
631
}
643
632
return null ;
@@ -649,9 +638,9 @@ private void populateTablesWithColumns(
649
638
}
650
639
}
651
640
652
- protected void addExtractedColumnInformation (TableInformation tableInformation , ResultSet resultSet )
641
+ private ColumnInformationImpl columnInformation (TableInformation tableInformation , ResultSet resultSet )
653
642
throws SQLException {
654
- final ColumnInformation columnInformation = new ColumnInformationImpl (
643
+ return new ColumnInformationImpl (
655
644
tableInformation ,
656
645
toIdentifier ( resultSet .getString ( getResultSetColumnNameLabel () ) ),
657
646
resultSet .getInt ( getResultSetSqlTypeCodeLabel () ),
@@ -660,14 +649,13 @@ protected void addExtractedColumnInformation(TableInformation tableInformation,
660
649
resultSet .getInt ( getResultSetDecimalDigitsLabel () ),
661
650
interpretTruthValue ( resultSet .getString ( getResultSetIsNullableLabel () ) )
662
651
);
663
- tableInformation .addColumn ( columnInformation );
664
652
}
665
653
666
- private NameSpaceTablesInformation extractNameSpaceTablesInformation (ResultSet resultSet ) throws SQLException {
667
- final NameSpaceTablesInformation tables = new NameSpaceTablesInformation ( identifierHelper () );
654
+ private NameSpaceTablesInformation extractNameSpaceTablesInformation (ResultSet resultSet )
655
+ throws SQLException {
656
+ final NameSpaceTablesInformation tables = new NameSpaceTablesInformation ( getIdentifierHelper () );
668
657
while ( resultSet .next () ) {
669
- final TableInformation tableInformation = extractTableInformation ( resultSet );
670
- tables .addTableInformation ( tableInformation );
658
+ tables .addTableInformation ( extractTableInformation ( resultSet ) );
671
659
}
672
660
return tables ;
673
661
}
@@ -721,8 +709,8 @@ protected abstract <T> T processTableResultSet(
721
709
String schemaPattern ,
722
710
String tableNamePattern ,
723
711
String [] types ,
724
- ExtractionContext .ResultSetProcessor <T > processor
725
- ) throws SQLException ;
712
+ ExtractionContext .ResultSetProcessor <T > processor )
713
+ throws SQLException ;
726
714
727
715
private TableInformation locateTableInNamespace (
728
716
Identifier catalog ,
@@ -735,6 +723,7 @@ private TableInformation locateTableInNamespace(
735
723
final String schemaFilter ;
736
724
737
725
final NameQualifierSupport nameQualifierSupport = getNameQualifierSupport ();
726
+
738
727
if ( nameQualifierSupport .supportsCatalogs () ) {
739
728
if ( catalog == null ) {
740
729
String defaultCatalog ;
@@ -780,12 +769,7 @@ private TableInformation locateTableInNamespace(
780
769
schemaFilter ,
781
770
tableNameFilter ,
782
771
tableTypes ,
783
- resultSet -> extractTableInformation (
784
- catalogToUse ,
785
- schemaToUse ,
786
- tableName ,
787
- resultSet
788
- )
772
+ resultSet -> extractTableInformation ( catalogToUse , schemaToUse , tableName , resultSet )
789
773
);
790
774
791
775
}
@@ -802,15 +786,16 @@ private TableInformation extractTableInformation(
802
786
Identifier catalog ,
803
787
Identifier schema ,
804
788
Identifier tableName ,
805
- ResultSet resultSet ) throws SQLException {
789
+ ResultSet resultSet )
790
+ throws SQLException {
806
791
807
792
boolean found = false ;
808
793
TableInformation tableInformation = null ;
809
794
while ( resultSet .next () ) {
810
- if ( tableName . equals ( Identifier . toIdentifier (
811
- resultSet .getString ( getResultSetTableNameLabel () ),
812
- tableName .isQuoted ()
813
- ) ) ) {
795
+ final Identifier identifier =
796
+ toIdentifier ( resultSet .getString ( getResultSetTableNameLabel () ),
797
+ tableName .isQuoted () );
798
+ if ( tableName . equals ( identifier ) ) {
814
799
if ( found ) {
815
800
LOG .multipleTablesFound ( tableName .render () );
816
801
throw new SchemaExtractionException (
@@ -867,7 +852,7 @@ protected void addColumns(TableInformation tableInformation) {
867
852
"%" ,
868
853
resultSet -> {
869
854
while ( resultSet .next () ) {
870
- addExtractedColumnInformation ( tableInformation , resultSet );
855
+ tableInformation . addColumn ( columnInformation ( tableInformation , resultSet ) );
871
856
}
872
857
return null ;
873
858
}
@@ -879,14 +864,6 @@ protected void addColumns(TableInformation tableInformation) {
879
864
}
880
865
}
881
866
882
- protected Boolean interpretNullable (int nullable ) {
883
- return switch ( nullable ) {
884
- case ResultSetMetaData .columnNullable -> Boolean .TRUE ;
885
- case ResultSetMetaData .columnNoNulls -> Boolean .FALSE ;
886
- default -> null ;
887
- };
888
- }
889
-
890
867
private Boolean interpretTruthValue (String nullable ) {
891
868
if ( "yes" .equalsIgnoreCase ( nullable ) ) {
892
869
return Boolean .TRUE ;
@@ -904,7 +881,8 @@ protected abstract <T> T processPrimaryKeysResultSet(
904
881
String catalogFilter ,
905
882
String schemaFilter ,
906
883
Identifier tableName ,
907
- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
884
+ ExtractionContext .ResultSetProcessor <T > processor )
885
+ throws SQLException ;
908
886
909
887
@ Override
910
888
public PrimaryKeyInformation getPrimaryKey (TableInformationImpl tableInformation ) {
@@ -926,30 +904,25 @@ public PrimaryKeyInformation getPrimaryKey(TableInformationImpl tableInformation
926
904
}
927
905
}
928
906
929
- private PrimaryKeyInformation extractPrimaryKeyInformation (
930
- TableInformation tableInformation ,
931
- ResultSet resultSet
932
- ) throws SQLException {
907
+ private PrimaryKeyInformation extractPrimaryKeyInformation (TableInformation tableInformation , ResultSet resultSet )
908
+ throws SQLException {
933
909
934
910
final List <ColumnInformation > pkColumns = new ArrayList <>();
935
911
boolean firstPass = true ;
936
912
Identifier pkIdentifier = null ;
937
913
938
914
while ( resultSet .next () ) {
939
915
final String currentPkName = resultSet .getString ( getResultSetPrimaryKeyNameLabel () );
940
- final Identifier currentPkIdentifier = currentPkName == null ? null : toIdentifier ( currentPkName );
916
+ final Identifier currentPkIdentifier =
917
+ currentPkName == null ? null : toIdentifier ( currentPkName );
941
918
if ( firstPass ) {
942
919
pkIdentifier = currentPkIdentifier ;
943
920
firstPass = false ;
944
921
}
945
922
else {
946
923
if ( !Objects .equals ( pkIdentifier , currentPkIdentifier ) ) {
947
- throw new SchemaExtractionException (
948
- String .format (
949
- "Encountered primary keys differing name on table %s" ,
950
- tableInformation .getName ().toString ()
951
- )
952
- );
924
+ throw new SchemaExtractionException ( "Encountered primary keys differing name on table "
925
+ + tableInformation .getName ().toString () );
953
926
}
954
927
}
955
928
@@ -1054,7 +1027,8 @@ protected abstract <T> T processIndexInfoResultSet(
1054
1027
String table ,
1055
1028
boolean unique ,
1056
1029
boolean approximate ,
1057
- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
1030
+ ExtractionContext .ResultSetProcessor <T > processor )
1031
+ throws SQLException ;
1058
1032
1059
1033
@ Override
1060
1034
public Iterable <IndexInformation > getIndexes (TableInformation tableInformation ) {
@@ -1186,8 +1160,8 @@ protected abstract <T> T processImportedKeysResultSet(
1186
1160
String catalog ,
1187
1161
String schema ,
1188
1162
String table ,
1189
- ExtractionContext .ResultSetProcessor <T > processor
1190
- ) throws SQLException ;
1163
+ ExtractionContext .ResultSetProcessor <T > processor )
1164
+ throws SQLException ;
1191
1165
1192
1166
/**
1193
1167
* Must do the following:
@@ -1271,8 +1245,8 @@ protected abstract <T> T processCrossReferenceResultSet(
1271
1245
String foreignCatalog ,
1272
1246
String foreignSchema ,
1273
1247
String foreignTable ,
1274
- ExtractionContext .ResultSetProcessor <T > processor
1275
- ) throws SQLException ;
1248
+ ExtractionContext .ResultSetProcessor <T > processor )
1249
+ throws SQLException ;
1276
1250
1277
1251
1278
1252
@ Override
@@ -1322,8 +1296,11 @@ public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInfo
1322
1296
return fks ;
1323
1297
}
1324
1298
1325
- private void process (TableInformation tableInformation , ResultSet resultSet , Map <Identifier , ForeignKeyBuilder > fkBuilders )
1326
- throws SQLException {
1299
+ private void process (
1300
+ TableInformation tableInformation ,
1301
+ ResultSet resultSet ,
1302
+ Map <Identifier , ForeignKeyBuilder > fkBuilders )
1303
+ throws SQLException {
1327
1304
while ( resultSet .next () ) {
1328
1305
// IMPL NOTE : The builder is mainly used to collect the column reference mappings
1329
1306
final Identifier fkIdentifier = toIdentifier ( resultSet .getString ( getResultSetForeignKeyLabel () ) );
0 commit comments