Skip to content

Commit 2a11202

Browse files
committed
very minor cleanups to StandardTableXxxxx
1 parent 0727dbe commit 2a11202

File tree

3 files changed

+49
-46
lines changed

3 files changed

+49
-46
lines changed

hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableCleaner.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66

77
import org.hibernate.Incubating;
88
import org.hibernate.boot.Metadata;
9-
import org.hibernate.boot.model.naming.Identifier;
109
import org.hibernate.boot.model.relational.QualifiedNameParser;
1110
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
1211
import org.hibernate.dialect.Dialect;
13-
import org.hibernate.internal.util.collections.ArrayHelper;
1412
import org.hibernate.mapping.ForeignKey;
1513
import org.hibernate.mapping.Table;
1614
import org.hibernate.tool.schema.spi.Cleaner;
1715

1816
import java.util.Collection;
1917

2018
import static java.util.Arrays.stream;
21-
import static java.util.stream.Collectors.toList;
19+
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
2220
import static org.hibernate.internal.util.collections.ArrayHelper.join;
2321

2422
/**
@@ -46,16 +44,16 @@ public String getSqlAfterString() {
4644

4745
@Override
4846
public String[] getSqlTruncateStrings(Collection<Table> tables, Metadata metadata, SqlStringGenerationContext context) {
49-
String[] tableNames = tables.stream()
50-
.map( table -> context.format( getTableName( table ) ) )
51-
.collect( toList() )
52-
.toArray( ArrayHelper.EMPTY_STRING_ARRAY );
53-
String[] truncateTableStatements = dialect.getTruncateTableStatements( tableNames );
54-
String[] initStatements = tables.stream()
55-
.flatMap( table -> table.getInitCommands( context ).stream() )
56-
.flatMap( command -> stream( command.initCommands() ) )
57-
.toList()
58-
.toArray( ArrayHelper.EMPTY_STRING_ARRAY );
47+
final String[] tableNames =
48+
tables.stream()
49+
.map( table -> context.format( getTableName( table ) ) )
50+
.toArray( String[]::new );
51+
final String[] truncateTableStatements = dialect.getTruncateTableStatements( tableNames );
52+
final String[] initStatements =
53+
tables.stream()
54+
.flatMap( table -> table.getInitCommands( context ).stream() )
55+
.flatMap( command -> stream( command.initCommands() ) )
56+
.toArray( String[]::new );
5957
return join( truncateTableStatements, initStatements );
6058
}
6159

@@ -71,8 +69,8 @@ public String getSqlEnableConstraintString(ForeignKey foreignKey, Metadata metad
7169

7270
private static QualifiedNameParser.NameParts getTableName(Table table) {
7371
return new QualifiedNameParser.NameParts(
74-
Identifier.toIdentifier(table.getCatalog(), table.isCatalogQuoted()),
75-
Identifier.toIdentifier(table.getSchema(), table.isSchemaQuoted()),
72+
toIdentifier( table.getCatalog(), table.isCatalogQuoted() ),
73+
toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
7674
table.getNameIdentifier()
7775
);
7876
}

hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableExporter.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import org.hibernate.MappingException;
1111
import org.hibernate.boot.Metadata;
12-
import org.hibernate.boot.model.naming.Identifier;
1312
import org.hibernate.boot.model.relational.QualifiedName;
1413
import org.hibernate.boot.model.relational.QualifiedNameParser;
1514
import org.hibernate.boot.model.relational.QualifiedTableName;
@@ -30,6 +29,7 @@
3029

3130
import static java.util.Collections.addAll;
3231
import static java.util.Comparator.comparing;
32+
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
3333
import static org.hibernate.internal.util.StringHelper.EMPTY_STRINGS;
3434
import static org.hibernate.internal.util.StringHelper.isNotEmpty;
3535
import static org.hibernate.tool.schema.internal.ColumnDefinitions.appendColumn;
@@ -57,7 +57,7 @@ public String[] getSqlCreateStrings(
5757
try {
5858
final String formattedTableName = context.format( tableName );
5959

60-
final StringBuilder createTable = new StringBuilder();
60+
final var createTable = new StringBuilder();
6161

6262
final String viewQuery = table.getViewQuery();
6363
if ( viewQuery != null ) {
@@ -85,7 +85,7 @@ public String[] getSqlCreateStrings(
8585
.append( viewQuery );
8686
}
8787
else {
88-
final StringBuilder extra = new StringBuilder();
88+
final var extra = new StringBuilder();
8989

9090
createTable.append( tableCreateString( table.hasPrimaryKey() ) )
9191
.append( ' ' )
@@ -105,7 +105,7 @@ public String[] getSqlCreateStrings(
105105
extra.append( column.getValue().getExtraCreateTableInfo() );
106106
}
107107
if ( table.getRowId() != null ) {
108-
String rowIdColumn = dialect.getRowIdColumnString( table.getRowId() );
108+
final String rowIdColumn = dialect.getRowIdColumnString( table.getRowId() );
109109
if ( rowIdColumn != null ) {
110110
createTable.append(", ").append( rowIdColumn );
111111
}
@@ -165,12 +165,13 @@ protected void applyComments(Table table, QualifiedTableName tableName, List<Str
165165
*/
166166
protected void applyComments(Table table, String formattedTableName, List<String> sqlStrings) {
167167
if ( dialect.supportsCommentOn() ) {
168-
if ( table.getComment() != null && dialect.getTableComment( "" ).isEmpty() ) {
169-
sqlStrings.add( "comment on table " + formattedTableName + " is '" + table.getComment() + "'" );
168+
final String comment = table.getComment();
169+
if ( comment != null && dialect.getTableComment( "" ).isEmpty() ) {
170+
sqlStrings.add( "comment on table " + formattedTableName + " is '" + comment + "'" );
170171
}
171172
if ( dialect.getColumnComment( "" ).isEmpty() ){
172-
for ( Column column : table.getColumns() ) {
173-
String columnComment = column.getComment();
173+
for ( var column : table.getColumns() ) {
174+
final String columnComment = column.getComment();
174175
if ( columnComment != null ) {
175176
sqlStrings.add(
176177
"comment on column " + formattedTableName + '.' + column.getQuotedName( dialect )
@@ -252,7 +253,7 @@ protected void applyTableCheck(Table table, StringBuilder buf) {
252253
}
253254

254255
private boolean isArray(AggregateColumn aggregateColumn) {
255-
final BasicValue value = (BasicValue) aggregateColumn.getValue();
256+
final var value = (BasicValue) aggregateColumn.getValue();
256257
return switch ( value.getResolution().getJdbcType().getDefaultSqlTypeCode() ) {
257258
case SqlTypes.STRUCT_ARRAY, SqlTypes.STRUCT_TABLE, SqlTypes.JSON_ARRAY, SqlTypes.XML_ARRAY, SqlTypes.ARRAY
258259
-> true;
@@ -395,7 +396,7 @@ protected String primaryKeyString(PrimaryKey key) {
395396
}
396397
constraint.append( "primary key (" );
397398
boolean first = true;
398-
for ( Column column : key.getColumns() ) {
399+
for ( var column : key.getColumns() ) {
399400
if ( first ) {
400401
first = false;
401402
}
@@ -429,8 +430,8 @@ public String[] getSqlDropStrings(Table table, Metadata metadata, SqlStringGener
429430

430431
private static QualifiedName getTableName(Table table) {
431432
return new QualifiedNameParser.NameParts(
432-
Identifier.toIdentifier( table.getCatalog(), table.isCatalogQuoted() ),
433-
Identifier.toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
433+
toIdentifier( table.getCatalog(), table.isCatalogQuoted() ),
434+
toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
434435
table.getNameIdentifier()
435436
);
436437
}

hibernate-core/src/main/java/org/hibernate/tool/schema/internal/StandardTableMigrator.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@
88
import org.hibernate.Incubating;
99
import org.hibernate.Internal;
1010
import org.hibernate.boot.Metadata;
11-
import org.hibernate.boot.model.naming.Identifier;
1211
import org.hibernate.boot.model.relational.QualifiedTableName;
1312
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
1413
import org.hibernate.dialect.Dialect;
1514
import org.hibernate.mapping.Table;
16-
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
1715
import org.hibernate.tool.schema.extract.spi.TableInformation;
1816
import org.hibernate.tool.schema.spi.TableMigrator;
1917
import org.jboss.logging.Logger;
2018

2119
import java.util.ArrayList;
2220
import java.util.List;
2321

22+
import static org.hibernate.boot.model.naming.Identifier.toIdentifier;
2423
import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_STRING_ARRAY;
2524
import static org.hibernate.tool.schema.internal.ColumnDefinitions.getColumnDefinition;
2625
import static org.hibernate.tool.schema.internal.ColumnDefinitions.hasMatchingLength;
@@ -67,35 +66,32 @@ public static List<String> sqlAlterStrings(
6766
TableInformation tableInformation,
6867
SqlStringGenerationContext context) throws HibernateException {
6968

70-
final String tableName = context.format( new QualifiedTableName(
71-
Identifier.toIdentifier( table.getCatalog(), table.isCatalogQuoted() ),
72-
Identifier.toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
73-
table.getNameIdentifier() )
74-
);
69+
final String tableName = getTableName( table, context );
7570

7671
final String alterTable = dialect.getAlterTableString( tableName ) + ' ';
7772

7873
final List<String> results = new ArrayList<>();
7974

8075
for ( var column : table.getColumns() ) {
81-
final ColumnInformation columnInformation = tableInformation.getColumn(
82-
Identifier.toIdentifier( column.getName(), column.isQuoted() )
83-
);
76+
final var columnInformation =
77+
tableInformation.getColumn( toIdentifier( column.getName(), column.isQuoted() ) );
8478
if ( columnInformation == null ) {
85-
// the column doesn't exist at all.
86-
final String addColumn = dialect.getAddColumnString() + ' '
87-
+ getFullColumnDeclaration( column, table, metadata, dialect, context )
88-
+ dialect.getAddColumnSuffixString();
79+
// the column doesn't exist at all
80+
final String addColumn =
81+
dialect.getAddColumnString() + ' '
82+
+ getFullColumnDeclaration( column, table, metadata, dialect, context )
83+
+ dialect.getAddColumnSuffixString();
8984
results.add( alterTable + addColumn );
9085
}
9186
else if ( dialect.supportsAlterColumnType() ) {
9287
if ( !hasMatchingType( column, columnInformation, metadata, dialect )
9388
|| !hasMatchingLength( column, columnInformation, metadata, dialect ) ) {
94-
final String alterColumn = dialect.getAlterColumnTypeString(
95-
column.getQuotedName( dialect ),
96-
column.getSqlType(metadata),
97-
getColumnDefinition( column, metadata, dialect )
98-
);
89+
final String alterColumn =
90+
dialect.getAlterColumnTypeString(
91+
column.getQuotedName( dialect ),
92+
column.getSqlType(metadata),
93+
getColumnDefinition( column, metadata, dialect )
94+
);
9995
results.add( alterTable + alterColumn );
10096
}
10197
}
@@ -107,4 +103,12 @@ else if ( dialect.supportsAlterColumnType() ) {
107103

108104
return results;
109105
}
106+
107+
private static String getTableName(Table table, SqlStringGenerationContext context) {
108+
return context.format( new QualifiedTableName(
109+
toIdentifier( table.getCatalog(), table.isCatalogQuoted() ),
110+
toIdentifier( table.getSchema(), table.isSchemaQuoted() ),
111+
table.getNameIdentifier() )
112+
);
113+
}
110114
}

0 commit comments

Comments
 (0)