-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
CreateProcedureChange.java
370 lines (319 loc) · 12.6 KB
/
CreateProcedureChange.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package liquibase.change.core;
import liquibase.GlobalConfiguration;
import liquibase.Scope;
import liquibase.change.*;
import liquibase.changelog.ChangeLogParameters;
import liquibase.database.Database;
import liquibase.database.DatabaseList;
import liquibase.database.core.*;
import liquibase.exception.DatabaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.exception.ValidationErrors;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.CreateProcedureStatement;
import liquibase.util.FileUtil;
import liquibase.util.StreamUtil;
import liquibase.util.StringUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
@DatabaseChange(
name = "createProcedure",
description = "Defines the definition for a stored procedure. This command is better to use for creating " +
"procedures than the raw sql command because it will not attempt to strip comments or break up lines.\n\n" +
"Often times it is best to use the CREATE OR REPLACE syntax along with setting runOnChange='true' on the " +
"enclosing changeSet tag. That way if you need to make a change to your procedure you can simply change your " +
"existing code rather than creating a new REPLACE PROCEDURE call. The advantage to this approach is that it " +
"keeps your change log smaller and allows you to more easily see what has changed in your procedure code " +
"through your source control system's diff command.",
priority = ChangeMetaData.PRIORITY_DEFAULT)
public class CreateProcedureChange extends AbstractChange implements DbmsTargetedChange {
private String comments;
private String catalogName;
private String schemaName;
private String procedureName;
private String procedureText;
private String dbms;
private String path;
private Boolean relativeToChangelogFile;
private String encoding;
private Boolean replaceIfExists;
@Override
public boolean generateStatementsVolatile(Database database) {
return false;
}
@Override
public boolean generateRollbackStatementsVolatile(Database database) {
return false;
}
public String getCatalogName() {
return catalogName;
}
public void setCatalogName(String catalogName) {
this.catalogName = catalogName;
}
public String getSchemaName() {
return schemaName;
}
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}
@DatabaseChangeProperty(exampleValue = "new_customer")
public String getProcedureName() {
return procedureName;
}
public void setProcedureName(String procedureName) {
this.procedureName = procedureName;
}
@DatabaseChangeProperty(exampleValue = "utf8")
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
@DatabaseChangeProperty(
description = "File containing the procedure text. Either this attribute or a nested procedure text is " +
"required.",
exampleValue = "com/example/my-logic.sql"
)
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Boolean isRelativeToChangelogFile() {
return relativeToChangelogFile;
}
public void setRelativeToChangelogFile(Boolean relativeToChangelogFile) {
this.relativeToChangelogFile = relativeToChangelogFile;
}
@DatabaseChangeProperty(
exampleValue = "CREATE OR REPLACE PROCEDURE testHello\n" +
" IS\n" +
" BEGIN\n" +
" DBMS_OUTPUT.PUT_LINE('Hello From The Database!');\n" +
" END;",
serializationType = SerializationType.DIRECT_VALUE)
/**
* @deprecated Use getProcedureText() instead
*/
public String getProcedureBody() {
return procedureText;
}
/**
* @deprecated Use setProcedureText() instead
*/
@Deprecated
public void setProcedureBody(String procedureText) {
this.procedureText = procedureText;
}
@DatabaseChangeProperty(isChangeProperty = false)
public String getProcedureText() {
return procedureText;
}
public void setProcedureText(String procedureText) {
this.procedureText = procedureText;
}
@DatabaseChangeProperty(
exampleValue = "h2, oracle",
since = "3.1"
)
public String getDbms() {
return dbms;
}
public void setDbms(final String dbms) {
this.dbms = dbms;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
@DatabaseChangeProperty
public Boolean getReplaceIfExists() {
return replaceIfExists;
}
public void setReplaceIfExists(Boolean replaceIfExists) {
this.replaceIfExists = replaceIfExists;
}
@Override
public ValidationErrors validate(Database database) {
// Not falling back to default because of path/procedureText option group. Need to specify everything.
ValidationErrors validate = new ValidationErrors();
validate.checkDisallowedField("catalogName", this.getCatalogName(), database, MSSQLDatabase.class);
if(getDbms() != null) {
DatabaseList.validateDefinitions(getDbms(), validate);
}
if ((StringUtil.trimToNull(getProcedureText()) != null) && (StringUtil.trimToNull(getPath()) != null)) {
validate.addError(
"Cannot specify both 'path' and a nested procedure text in " +
Scope.getCurrentScope().getSingleton(ChangeFactory.class).getChangeMetaData(this).getName()
);
}
if ((StringUtil.trimToNull(getProcedureText()) == null) && (StringUtil.trimToNull(getPath()) == null)) {
validate.addError(
"Must specify either 'path' or a nested procedure text in " +
Scope.getCurrentScope().getSingleton(ChangeFactory.class).getChangeMetaData(this).getName()
);
}
if ((this.getReplaceIfExists() != null) && (DatabaseList.definitionMatches(getDbms(), database, true))) {
if (database instanceof MSSQLDatabase || database instanceof MySQLDatabase) {
if (this.getReplaceIfExists() && (this.getProcedureName() == null)) {
validate.addError("procedureName is required if replaceIfExists = true");
}
} else {
validate.checkDisallowedField("replaceIfExists", this.getReplaceIfExists(), database);
}
}
return validate;
}
public InputStream openSqlStream() throws IOException {
if (path == null) {
return null;
}
try {
String path = getPath();
String relativeTo = null;
final Boolean isRelative = isRelativeToChangelogFile();
if (isRelative != null && isRelative) {
relativeTo = getChangeSet().getChangeLog().getPhysicalFilePath();
}
return Scope.getCurrentScope().getResourceAccessor().openStream(relativeTo, path);
} catch (IOException e) {
throw new IOException(
"<" + Scope.getCurrentScope().getSingleton(ChangeFactory.class).getChangeMetaData(this).getName() + " path=" +
path +
"> -Unable to read file",
e
);
}
}
/**
* Calculates the checksum based on the contained SQL.
*
* @see liquibase.change.AbstractChange#generateCheckSum()
*/
@Override
public CheckSum generateCheckSum() {
if (this.path == null) {
return super.generateCheckSum();
}
InputStream stream = null;
try {
stream = openSqlStream();
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
}
try {
String procedureText = this.procedureText;
if ((stream == null) && (procedureText == null)) {
procedureText = "";
}
String encoding = GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue();
if (procedureText != null) {
try {
stream = new ByteArrayInputStream(procedureText.getBytes(encoding));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(encoding +
" is not supported by the JVM, this should not happen according to the JavaDoc of " +
"the Charset class"
);
}
}
CheckSum checkSum = CheckSum.compute(new AbstractSQLChange.NormalizingStream(";", false, false, stream), false);
return CheckSum.compute(super.generateCheckSum().toString() + ":" + checkSum.toString());
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException ignore) {
// Do nothing
}
}
}
}
@Override
public SqlStatement[] generateStatements(Database database) {
String endDelimiter = ";";
if (database instanceof OracleDatabase) {
endDelimiter = "\n/";
} else if (database instanceof AbstractDb2Database) {
endDelimiter = "";
}
String procedureText;
String path = getPath();
if (path == null) {
procedureText = StringUtil.trimToNull(getProcedureText());
} else {
if (getChangeSet() == null) {
//only try to read a file when inside a changest. Not when analyizing supported
procedureText = "NO CHANGESET";
} else {
try {
InputStream stream = openSqlStream();
if (stream == null) {
throw new IOException(FileUtil.getFileNotFoundMessage(path));
}
procedureText = StreamUtil.readStreamAsString(stream, encoding);
if (getChangeSet() != null) {
ChangeLogParameters parameters = getChangeSet().getChangeLogParameters();
if (parameters != null) {
procedureText = parameters.expandExpressions(procedureText, getChangeSet().getChangeLog());
}
}
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
}
}
}
return generateStatements(procedureText, endDelimiter, database);
}
protected SqlStatement[] generateStatements(String logicText, String endDelimiter, Database database) {
CreateProcedureStatement statement =
new CreateProcedureStatement(
getCatalogName(),
getSchemaName(),
getProcedureName(),
logicText,
endDelimiter
);
statement.setReplaceIfExists(getReplaceIfExists());
return new SqlStatement[]{
statement,
};
}
@Override
public ChangeStatus checkStatus(Database database) {
return new ChangeStatus().unknown("Cannot check createProcedure status");
}
@Override
public String getConfirmationMessage() {
return "Stored procedure created";
}
@Override
public String getSerializedObjectNamespace() {
return STANDARD_CHANGELOG_NAMESPACE;
}
@SuppressWarnings("java:S2095")
@Override
protected Map<String, Object> createExampleValueMetaData(
String parameterName, DatabaseChangeProperty changePropertyAnnotation) {
if ("procedureText".equals(parameterName) || "procedureBody".equals(parameterName)) {
Map<String, Object> returnMap = super.createExampleValueMetaData(parameterName, changePropertyAnnotation);
returnMap.put(
new HsqlDatabase().getShortName(),
"CREATE PROCEDURE new_customer(firstname VARCHAR(50), lastname VARCHAR(50))\n" +
" MODIFIES SQL DATA\n" +
" INSERT INTO CUSTOMERS (first_name, last_name) VALUES (firstname, lastname)"
);
return returnMap;
} else {
return super.createExampleValueMetaData(parameterName, changePropertyAnnotation);
}
}
}