66
77import java .sql .DatabaseMetaData ;
88import java .sql .ResultSet ;
9- import java .sql .ResultSetMetaData ;
109import java .sql .SQLException ;
1110import java .util .ArrayList ;
1211import java .util .HashMap ;
4241
4342import static java .util .Collections .addAll ;
4443import static org .hibernate .boot .model .naming .DatabaseIdentifier .toIdentifier ;
44+ import static org .hibernate .internal .util .StringHelper .EMPTY_STRINGS ;
4545import static org .hibernate .internal .util .StringHelper .isBlank ;
4646import static org .hibernate .internal .util .StringHelper .splitTrimmingTokens ;
4747import static org .hibernate .internal .util .config .ConfigurationHelper .getBoolean ;
@@ -85,31 +85,37 @@ public AbstractInformationExtractorImpl(ExtractionContext extractionContext) {
8585 ""
8686 )
8787 );
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 ) {
8894 final List <String > physicalTableTypesList = new ArrayList <>();
8995 if ( !isBlank ( extraPhysicalTableTypesConfig ) ) {
9096 addAll ( physicalTableTypesList , splitTrimmingTokens ( ",;" , extraPhysicalTableTypesConfig , false ) );
9197 }
92- final Dialect dialect = extractionContext .getJdbcEnvironment ().getDialect ();
9398 dialect .augmentPhysicalTableTypes ( physicalTableTypesList );
94- this .extraPhysicalTableTypes = physicalTableTypesList .toArray ( new String [0 ] );
99+ return physicalTableTypesList .toArray ( EMPTY_STRINGS );
100+ }
95101
102+ private String [] getTableTypes (ConfigurationService configService , Dialect dialect ) {
96103 final List <String > tableTypesList = new ArrayList <>();
97104 tableTypesList .add ( "TABLE" );
98105 tableTypesList .add ( "VIEW" );
99106 if ( getBoolean ( AvailableSettings .ENABLE_SYNONYMS , configService .getSettings () ) ) {
100- if ( dialect instanceof DB2Dialect ) {
107+ if ( dialect instanceof DB2Dialect ) { //TODO: should not use Dialect types directly!
101108 tableTypesList .add ( "ALIAS" );
102109 }
103110 tableTypesList .add ( "SYNONYM" );
104111 }
105112 addAll ( tableTypesList , extraPhysicalTableTypes );
106113 dialect .augmentRecognizedTableTypes ( tableTypesList );
107-
108- this .tableTypes = tableTypesList .toArray ( new String [0 ] );
114+ return tableTypesList .toArray ( EMPTY_STRINGS );
109115 }
110116
111- protected IdentifierHelper identifierHelper () {
112- return getIdentifierHelper ();
117+ private IdentifierHelper getIdentifierHelper () {
118+ return getJdbcEnvironment (). getIdentifierHelper ();
113119 }
114120
115121 protected JDBCException convertSQLException (SQLException sqlException , String message ) {
@@ -272,13 +278,17 @@ public boolean catalogExists(Identifier catalog) {
272278 protected abstract <T > T processSchemaResultSet (
273279 String catalog ,
274280 String schemaPattern ,
275- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
281+ ExtractionContext .ResultSetProcessor <T > processor )
282+ throws SQLException ;
276283
277284 @ Override
278285 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 );
282292 try {
283293 return processSchemaResultSet (
284294 catalogFilter ,
@@ -308,32 +318,10 @@ public boolean schemaExists(Identifier catalog, Identifier schema) {
308318 }
309319 }
310320
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-
333321 private TableInformation extractTableInformation (ResultSet resultSet ) throws SQLException {
334322 return new TableInformationImpl (
335323 this ,
336- identifierHelper (),
324+ getIdentifierHelper (),
337325 extractTableName ( resultSet ),
338326 isPhysicalTableType ( resultSet .getString ( getResultSetTableTypeLabel () ) ),
339327 resultSet .getString ( getResultSetRemarksLabel () )
@@ -616,7 +604,8 @@ protected abstract <T> T processColumnsResultSet(
616604 String schemaPattern ,
617605 String tableNamePattern ,
618606 String columnNamePattern ,
619- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
607+ ExtractionContext .ResultSetProcessor <T > processor )
608+ throws SQLException ;
620609
621610 private void populateTablesWithColumns (
622611 String catalogFilter ,
@@ -637,7 +626,7 @@ private void populateTablesWithColumns(
637626 currentTable = tables .getTableInformation ( currentTableName );
638627 }
639628 if ( currentTable != null ) {
640- addExtractedColumnInformation ( currentTable , resultSet );
629+ currentTable . addColumn ( columnInformation ( currentTable , resultSet ) );
641630 }
642631 }
643632 return null ;
@@ -649,9 +638,9 @@ private void populateTablesWithColumns(
649638 }
650639 }
651640
652- protected void addExtractedColumnInformation (TableInformation tableInformation , ResultSet resultSet )
641+ private ColumnInformationImpl columnInformation (TableInformation tableInformation , ResultSet resultSet )
653642 throws SQLException {
654- final ColumnInformation columnInformation = new ColumnInformationImpl (
643+ return new ColumnInformationImpl (
655644 tableInformation ,
656645 toIdentifier ( resultSet .getString ( getResultSetColumnNameLabel () ) ),
657646 resultSet .getInt ( getResultSetSqlTypeCodeLabel () ),
@@ -660,14 +649,13 @@ protected void addExtractedColumnInformation(TableInformation tableInformation,
660649 resultSet .getInt ( getResultSetDecimalDigitsLabel () ),
661650 interpretTruthValue ( resultSet .getString ( getResultSetIsNullableLabel () ) )
662651 );
663- tableInformation .addColumn ( columnInformation );
664652 }
665653
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 () );
668657 while ( resultSet .next () ) {
669- final TableInformation tableInformation = extractTableInformation ( resultSet );
670- tables .addTableInformation ( tableInformation );
658+ tables .addTableInformation ( extractTableInformation ( resultSet ) );
671659 }
672660 return tables ;
673661 }
@@ -721,8 +709,8 @@ protected abstract <T> T processTableResultSet(
721709 String schemaPattern ,
722710 String tableNamePattern ,
723711 String [] types ,
724- ExtractionContext .ResultSetProcessor <T > processor
725- ) throws SQLException ;
712+ ExtractionContext .ResultSetProcessor <T > processor )
713+ throws SQLException ;
726714
727715 private TableInformation locateTableInNamespace (
728716 Identifier catalog ,
@@ -735,6 +723,7 @@ private TableInformation locateTableInNamespace(
735723 final String schemaFilter ;
736724
737725 final NameQualifierSupport nameQualifierSupport = getNameQualifierSupport ();
726+
738727 if ( nameQualifierSupport .supportsCatalogs () ) {
739728 if ( catalog == null ) {
740729 String defaultCatalog ;
@@ -780,12 +769,7 @@ private TableInformation locateTableInNamespace(
780769 schemaFilter ,
781770 tableNameFilter ,
782771 tableTypes ,
783- resultSet -> extractTableInformation (
784- catalogToUse ,
785- schemaToUse ,
786- tableName ,
787- resultSet
788- )
772+ resultSet -> extractTableInformation ( catalogToUse , schemaToUse , tableName , resultSet )
789773 );
790774
791775 }
@@ -802,15 +786,16 @@ private TableInformation extractTableInformation(
802786 Identifier catalog ,
803787 Identifier schema ,
804788 Identifier tableName ,
805- ResultSet resultSet ) throws SQLException {
789+ ResultSet resultSet )
790+ throws SQLException {
806791
807792 boolean found = false ;
808793 TableInformation tableInformation = null ;
809794 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 ) ) {
814799 if ( found ) {
815800 LOG .multipleTablesFound ( tableName .render () );
816801 throw new SchemaExtractionException (
@@ -867,7 +852,7 @@ protected void addColumns(TableInformation tableInformation) {
867852 "%" ,
868853 resultSet -> {
869854 while ( resultSet .next () ) {
870- addExtractedColumnInformation ( tableInformation , resultSet );
855+ tableInformation . addColumn ( columnInformation ( tableInformation , resultSet ) );
871856 }
872857 return null ;
873858 }
@@ -879,14 +864,6 @@ protected void addColumns(TableInformation tableInformation) {
879864 }
880865 }
881866
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-
890867 private Boolean interpretTruthValue (String nullable ) {
891868 if ( "yes" .equalsIgnoreCase ( nullable ) ) {
892869 return Boolean .TRUE ;
@@ -904,7 +881,8 @@ protected abstract <T> T processPrimaryKeysResultSet(
904881 String catalogFilter ,
905882 String schemaFilter ,
906883 Identifier tableName ,
907- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
884+ ExtractionContext .ResultSetProcessor <T > processor )
885+ throws SQLException ;
908886
909887 @ Override
910888 public PrimaryKeyInformation getPrimaryKey (TableInformationImpl tableInformation ) {
@@ -926,30 +904,25 @@ public PrimaryKeyInformation getPrimaryKey(TableInformationImpl tableInformation
926904 }
927905 }
928906
929- private PrimaryKeyInformation extractPrimaryKeyInformation (
930- TableInformation tableInformation ,
931- ResultSet resultSet
932- ) throws SQLException {
907+ private PrimaryKeyInformation extractPrimaryKeyInformation (TableInformation tableInformation , ResultSet resultSet )
908+ throws SQLException {
933909
934910 final List <ColumnInformation > pkColumns = new ArrayList <>();
935911 boolean firstPass = true ;
936912 Identifier pkIdentifier = null ;
937913
938914 while ( resultSet .next () ) {
939915 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 );
941918 if ( firstPass ) {
942919 pkIdentifier = currentPkIdentifier ;
943920 firstPass = false ;
944921 }
945922 else {
946923 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 () );
953926 }
954927 }
955928
@@ -1054,7 +1027,8 @@ protected abstract <T> T processIndexInfoResultSet(
10541027 String table ,
10551028 boolean unique ,
10561029 boolean approximate ,
1057- ExtractionContext .ResultSetProcessor <T > processor ) throws SQLException ;
1030+ ExtractionContext .ResultSetProcessor <T > processor )
1031+ throws SQLException ;
10581032
10591033 @ Override
10601034 public Iterable <IndexInformation > getIndexes (TableInformation tableInformation ) {
@@ -1186,8 +1160,8 @@ protected abstract <T> T processImportedKeysResultSet(
11861160 String catalog ,
11871161 String schema ,
11881162 String table ,
1189- ExtractionContext .ResultSetProcessor <T > processor
1190- ) throws SQLException ;
1163+ ExtractionContext .ResultSetProcessor <T > processor )
1164+ throws SQLException ;
11911165
11921166 /**
11931167 * Must do the following:
@@ -1271,8 +1245,8 @@ protected abstract <T> T processCrossReferenceResultSet(
12711245 String foreignCatalog ,
12721246 String foreignSchema ,
12731247 String foreignTable ,
1274- ExtractionContext .ResultSetProcessor <T > processor
1275- ) throws SQLException ;
1248+ ExtractionContext .ResultSetProcessor <T > processor )
1249+ throws SQLException ;
12761250
12771251
12781252 @ Override
@@ -1322,8 +1296,11 @@ public Iterable<ForeignKeyInformation> getForeignKeys(TableInformation tableInfo
13221296 return fks ;
13231297 }
13241298
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 {
13271304 while ( resultSet .next () ) {
13281305 // IMPL NOTE : The builder is mainly used to collect the column reference mappings
13291306 final Identifier fkIdentifier = toIdentifier ( resultSet .getString ( getResultSetForeignKeyLabel () ) );
0 commit comments