23
23
import org .hibernate .engine .jdbc .internal .Formatter ;
24
24
import org .hibernate .internal .CoreLogging ;
25
25
import org .hibernate .internal .CoreMessageLogger ;
26
- import org .hibernate .internal .util .StringHelper ;
27
26
import org .hibernate .internal .util .config .ConfigurationHelper ;
28
27
import org .hibernate .resource .transaction .spi .DdlTransactionIsolator ;
29
28
import org .hibernate .service .ServiceRegistry ;
45
44
import org .hibernate .tool .schema .spi .ScriptTargetOutput ;
46
45
import org .hibernate .tool .schema .spi .SqlScriptCommandExtractor ;
47
46
47
+ import static org .hibernate .internal .util .StringHelper .isEmpty ;
48
48
import static org .hibernate .internal .util .StringHelper .splitAtCommas ;
49
49
50
50
/**
@@ -92,13 +92,10 @@ private static AbstractScriptSourceInput interpretScriptSourceSetting(
92
92
log .trace ( "Trying as URL..." );
93
93
// ClassLoaderService.locateResource() first tries the given resource name as url form...
94
94
final URL url = classLoaderService .locateResource ( scriptSourceSettingString );
95
- if ( url != null ) {
96
- return new ScriptSourceInputFromUrl ( url , charsetName );
97
- }
98
-
99
- // assume it is a File path
100
- final File file = new File ( scriptSourceSettingString );
101
- return new ScriptSourceInputFromFile ( file , charsetName );
95
+ return url != null
96
+ ? new ScriptSourceInputFromUrl ( url , charsetName )
97
+ // assume it is a File path
98
+ : new ScriptSourceInputFromFile ( new File ( scriptSourceSettingString ), charsetName );
102
99
}
103
100
104
101
public static ScriptTargetOutput interpretScriptTargetSetting (
@@ -109,8 +106,8 @@ public static ScriptTargetOutput interpretScriptTargetSetting(
109
106
if ( scriptTargetSetting == null ) {
110
107
return null ;
111
108
}
112
- else if ( scriptTargetSetting instanceof Writer ) {
113
- return new ScriptTargetOutputToWriter ( ( Writer ) scriptTargetSetting );
109
+ else if ( scriptTargetSetting instanceof Writer writer ) {
110
+ return new ScriptTargetOutputToWriter ( writer );
114
111
}
115
112
else {
116
113
final String scriptTargetSettingString = scriptTargetSetting .toString ();
@@ -124,31 +121,15 @@ else if ( scriptTargetSetting instanceof Writer ) {
124
121
log .trace ( "Trying as URL..." );
125
122
// ClassLoaderService.locateResource() first tries the given resource name as url form...
126
123
final URL url = classLoaderService .locateResource ( scriptTargetSettingString );
127
- if ( url != null ) {
128
- return new ScriptTargetOutputToUrl ( url , charsetName );
129
- }
130
-
131
- // assume it is a File path
132
- final File file = new File ( scriptTargetSettingString );
133
- return new ScriptTargetOutputToFile ( file , charsetName , append );
124
+ return url != null
125
+ ? new ScriptTargetOutputToUrl ( url , charsetName )
126
+ // assume it is a File path
127
+ : new ScriptTargetOutputToFile ( new File ( scriptTargetSettingString ), charsetName , append );
134
128
}
135
129
}
136
130
137
131
public static boolean interpretNamespaceHandling (Map <String ,Object > configurationValues ) {
138
- //Print a warning if multiple conflicting properties are being set:
139
- int count = 0 ;
140
- if ( configurationValues .containsKey ( AvailableSettings .HBM2DDL_CREATE_SCHEMAS ) ) {
141
- count ++;
142
- }
143
- if ( configurationValues .containsKey ( AvailableSettings .JAKARTA_HBM2DDL_CREATE_SCHEMAS ) ) {
144
- count ++;
145
- }
146
- if ( configurationValues .containsKey ( AvailableSettings .HBM2DDL_CREATE_NAMESPACES ) ) {
147
- count ++;
148
- }
149
- if ( count > 1 ) {
150
- log .multipleSchemaCreationSettingsDefined ();
151
- }
132
+ warnIfConflictingPropertiesSet ( configurationValues );
152
133
// prefer the JPA setting...
153
134
return ConfigurationHelper .getBoolean (
154
135
AvailableSettings .HBM2DDL_CREATE_SCHEMAS ,
@@ -166,19 +147,33 @@ public static boolean interpretNamespaceHandling(Map<String,Object> configuratio
166
147
);
167
148
}
168
149
150
+ private static void warnIfConflictingPropertiesSet (Map <String , Object > configurationValues ) {
151
+ //Print a warning if multiple conflicting properties are being set:
152
+ int count = 0 ;
153
+ if ( configurationValues .containsKey ( AvailableSettings .HBM2DDL_CREATE_SCHEMAS ) ) {
154
+ count ++;
155
+ }
156
+ if ( configurationValues .containsKey ( AvailableSettings .JAKARTA_HBM2DDL_CREATE_SCHEMAS ) ) {
157
+ count ++;
158
+ }
159
+ if ( configurationValues .containsKey ( AvailableSettings .HBM2DDL_CREATE_NAMESPACES ) ) {
160
+ count ++;
161
+ }
162
+ if ( count > 1 ) {
163
+ log .multipleSchemaCreationSettingsDefined ();
164
+ }
165
+ }
166
+
169
167
public static boolean interpretFormattingEnabled (Map <String ,Object > configurationValues ) {
170
- return ConfigurationHelper .getBoolean (
171
- AvailableSettings .FORMAT_SQL ,
172
- configurationValues
173
- );
168
+ return ConfigurationHelper .getBoolean ( AvailableSettings .FORMAT_SQL , configurationValues );
174
169
}
175
170
176
171
public static DatabaseInformation buildDatabaseInformation (
177
172
ServiceRegistry serviceRegistry ,
178
173
DdlTransactionIsolator ddlTransactionIsolator ,
179
174
SqlStringGenerationContext context ,
180
175
SchemaManagementTool tool ) {
181
- final JdbcEnvironment jdbcEnvironment = serviceRegistry .getService ( JdbcEnvironment .class );
176
+ final JdbcEnvironment jdbcEnvironment = serviceRegistry .requireService ( JdbcEnvironment .class );
182
177
try {
183
178
return new DatabaseInformationImpl (
184
179
serviceRegistry ,
@@ -207,12 +202,10 @@ public static void applySqlStrings(
207
202
Formatter formatter ,
208
203
ExecutionOptions options ,
209
204
GenerationTarget ... targets ) {
210
- if ( sqlStrings == null ) {
211
- return ;
212
- }
213
-
214
- for ( String sqlString : sqlStrings ) {
215
- applySqlString ( sqlString , formatter , options , targets );
205
+ if ( sqlStrings != null ) {
206
+ for ( String sqlString : sqlStrings ) {
207
+ applySqlString ( sqlString , formatter , options , targets );
208
+ }
216
209
}
217
210
}
218
211
@@ -221,17 +214,15 @@ public static void applySqlString(
221
214
Formatter formatter ,
222
215
ExecutionOptions options ,
223
216
GenerationTarget ... targets ) {
224
- if ( StringHelper .isEmpty ( sqlString ) ) {
225
- return ;
226
- }
227
-
228
- String sqlStringFormatted = formatter .format ( sqlString );
229
- for ( GenerationTarget target : targets ) {
230
- try {
231
- target .accept ( sqlStringFormatted );
232
- }
233
- catch (CommandAcceptanceException e ) {
234
- options .getExceptionHandler ().handleException ( e );
217
+ if ( !isEmpty ( sqlString ) ) {
218
+ final String sqlStringFormatted = formatter .format ( sqlString );
219
+ for ( GenerationTarget target : targets ) {
220
+ try {
221
+ target .accept ( sqlStringFormatted );
222
+ }
223
+ catch (CommandAcceptanceException e ) {
224
+ options .getExceptionHandler ().handleException ( e );
225
+ }
235
226
}
236
227
}
237
228
}
0 commit comments