-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
LoadDataChange.java
879 lines (791 loc) · 40.3 KB
/
LoadDataChange.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
package liquibase.change.core;
import com.opencsv.CSVReaderBuilder;
import liquibase.CatalogAndSchema;
import liquibase.Scope;
import liquibase.change.*;
import liquibase.changelog.ChangeSet;
import liquibase.database.AbstractJdbcDatabase;
import liquibase.database.Database;
import liquibase.database.core.MSSQLDatabase;
import liquibase.database.core.MySQLDatabase;
import liquibase.database.core.PostgresDatabase;
import liquibase.datatype.DataTypeFactory;
import liquibase.datatype.LiquibaseDataType;
import liquibase.exception.*;
import liquibase.executor.ExecutorService;
import liquibase.executor.LoggingExecutor;
import liquibase.io.EmptyLineAndCommentSkippingInputStream;
import liquibase.logging.Logger;
import liquibase.resource.ResourceAccessor;
import liquibase.snapshot.InvalidExampleException;
import liquibase.snapshot.SnapshotControl;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.statement.BatchDmlExecutablePreparedStatement;
import liquibase.statement.ExecutablePreparedStatementBase;
import liquibase.statement.InsertExecutablePreparedStatement;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.InsertOrUpdateStatement;
import liquibase.statement.core.InsertSetStatement;
import liquibase.statement.core.InsertStatement;
import liquibase.structure.core.Column;
import liquibase.structure.core.DataType;
import liquibase.structure.core.Table;
import liquibase.util.BooleanUtil;
import liquibase.util.ObjectUtil;
import liquibase.util.StreamUtil;
import liquibase.util.StringUtil;
import liquibase.util.csv.CSVReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.*;
import java.util.regex.Pattern;
import static java.util.ResourceBundle.getBundle;
import static liquibase.change.ChangeParameterMetaData.ALL;
@DatabaseChange(name = "loadData",
description = "Loads data from a CSV file into an existing table. A value of NULL in a cell will be " +
"converted to a database NULL rather than the string 'NULL'.\n" +
"Lines starting with # (hash) sign are treated as comments. You can change comment pattern by " +
"specifying 'commentLineStartsWith' attribute." +
"To disable comments set 'commentLineStartsWith' to empty value'\n" +
"\n" +
"If the data type for a load column is set to NUMERIC, numbers are parsed in US locale (e.g. 123.45)." +
"\n" +
"Date/Time values included in the CSV file should be in ISO format " +
"http://en.wikipedia.org/wiki/ISO_8601 in order to be parsed correctly by Liquibase. Liquibase will " +
"initially set the date format to be 'yyyy-MM-dd'T'HH:mm:ss' and then it checks for two special " +
"cases which will override the data format string.\n" +
"\n" +
"If the string representing the date/time includes a '.', then the date format is changed to " +
"'yyyy-MM-dd'T'HH:mm:ss.SSS'\n" +
"If the string representing the date/time includes a space, then the date format is changed " +
"to 'yyyy-MM-dd HH:mm:ss'\n" +
"Once the date format string is set, Liquibase will then call the SimpleDateFormat.parse() method " +
"attempting to parse the input string so that it can return a Date/Time. If problems occur, " +
"then a ParseException is thrown and the input string is treated as a String for the INSERT command " +
"to be generated.\n" +
"If UUID type is used UUID value is stored as string and NULL in cell is supported.",
priority = ChangeMetaData.PRIORITY_DEFAULT, appliesTo = "table",
since = "1.7")
@SuppressWarnings("java:S2583")
public class LoadDataChange extends AbstractTableChange implements ChangeWithColumns<LoadDataColumnConfig> {
/**
* CSV Lines starting with that sign(s) will be treated as comments by default
*/
public static final String DEFAULT_COMMENT_PATTERN = "#";
public static final Pattern BASE64_PATTERN = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
private static final Logger LOG = Scope.getCurrentScope().getLog(LoadDataChange.class);
private static ResourceBundle coreBundle = getBundle("liquibase/i18n/liquibase-core");
private String file;
private String commentLineStartsWith = DEFAULT_COMMENT_PATTERN;
private Boolean relativeToChangelogFile;
private String encoding;
private String separator = CSVReader.DEFAULT_SEPARATOR + "";
private String quotchar = CSVReader.DEFAULT_QUOTE_CHARACTER + "";
private List<LoadDataColumnConfig> columns = new ArrayList<>();
private Boolean usePreparedStatements;
/**
* Transform a value read from a CSV file into a string to be written into the database if the column type
* is not known.
*
* @param value the value to transform
* @return if the value is empty or the string "NULL" (case-insensitive), return the empty string.
* If not, the value "toString()" representation (trimmed of spaces left and right) is returned.
*/
protected static String getValueToWrite(Object value) {
if ((value == null) || "NULL".equalsIgnoreCase(value.toString())) {
return "";
} else {
return value.toString().trim();
}
}
// TODO: We can currently do this for INSERT operations, but not yet for UPDATE operations, so loadUpdateDataChange
// will overwrite this flag for now.
protected boolean hasPreparedStatementsImplemented() {
return true;
}
@Override
public boolean supports(Database database) {
return true;
}
@Override
public boolean generateRollbackStatementsVolatile(Database database) {
return true;
}
@DatabaseChangeProperty( description = "Name of the table to insert data into",
requiredForDatabase = ALL, mustEqualExisting = "table" )
public String getTableName() {
return super.getTableName();
}
@DatabaseChangeProperty(
description = "CSV file to load", exampleValue = "com/example/users.csv",
requiredForDatabase = ALL)
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
@DatabaseChangeProperty(
description = "Use prepared statements instead of insert statement strings if the DB supports it")
public Boolean getUsePreparedStatements() {
return usePreparedStatements;
}
public void setUsePreparedStatements(Boolean usePreparedStatements) {
this.usePreparedStatements = usePreparedStatements;
}
@DatabaseChangeProperty( supportsDatabase = ALL,
description = "Lines staring with this are treated as comment and ignored. Default: " + DEFAULT_COMMENT_PATTERN)
public String getCommentLineStartsWith() {
return commentLineStartsWith;
}
public void setCommentLineStartsWith(String commentLineStartsWith) {
//if the value is null (not provided) we want to use default value
if (commentLineStartsWith == null) {
this.commentLineStartsWith = DEFAULT_COMMENT_PATTERN;
} else if ("".equals(commentLineStartsWith)) {
this.commentLineStartsWith = null;
} else {
this.commentLineStartsWith = commentLineStartsWith;
}
}
@DatabaseChangeProperty( supportsDatabase = ALL,
description = "Option whether the 'file' is relative to the changelog file")
public Boolean isRelativeToChangelogFile() {
return relativeToChangelogFile;
}
public void setRelativeToChangelogFile(Boolean relativeToChangelogFile) {
this.relativeToChangelogFile = relativeToChangelogFile;
}
@DatabaseChangeProperty(exampleValue = "UTF-8", supportsDatabase = ALL,
description = "Encoding of the CSV file (defaults to UTF-8)")
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
@DatabaseChangeProperty(exampleValue = ",", supportsDatabase = ALL,
description = "Character separating the fields. Default: " + CSVReader.DEFAULT_SEPARATOR)
public String getSeparator() {
return separator;
}
public void setSeparator(String separator) {
if ((separator != null) && "\\t".equals(separator)) {
separator = "\t";
}
this.separator = separator;
}
@DatabaseChangeProperty(exampleValue = "'", supportsDatabase = ALL,
description = "The quote character for string fields containing the separator character. " +
"Default: " + CSVReader.DEFAULT_QUOTE_CHARACTER)
public String getQuotchar() {
return quotchar;
}
public void setQuotchar(String quotchar) {
this.quotchar = quotchar;
}
@Override
public void addColumn(LoadDataColumnConfig column) {
columns.add(column);
}
@Override
@DatabaseChangeProperty( supportsDatabase = ALL, serializationType = SerializationType.NESTED_OBJECT,
description = "Column mapping and defaults can be defined.\n\n" +
"'header' or 'index' attributes needs to be defined if the header name in the CSV " +
"is different than the column name needs to be inserted\n" +
"Not defined column type it is taken from the DB.\n" +
"The 'defaultValue[XXX]' attributes can define value for empty fields.")
public List<LoadDataColumnConfig> getColumns() { return columns; }
@Override
public void setColumns(List<LoadDataColumnConfig> columns) {
this.columns = columns;
}
/**
* Unique string for the column for better identification
* @param index index of the column
* @param columnConfig the column
* @return
*/
protected String columnIdString(int index, LoadDataColumnConfig columnConfig) {
return " / column[" + index + "]" +
(StringUtil.trimToNull(columnConfig.getName()) != null ?
" (name:'" + columnConfig.getName() + "')" : "") ;
}
/**
* Validate all columns and collect errors in 'validationErrors'
* @param validationErrors ValidationErrors to collect errors
* @return validationErrors
*/
protected ValidationErrors validateColumns(ValidationErrors validationErrors) {
if (getColumns() != null) {
int i = 1;
for (LoadDataColumnConfig columnConfig : getColumns()) {
validateColumn(columnConfig, validationErrors, columnIdString(i, columnConfig));
i++;
}
}
return validationErrors;
}
@Override
public SqlStatement[] generateStatements(Database database) {
boolean databaseSupportsBatchUpdates = false;
try {
databaseSupportsBatchUpdates = database.supportsBatchUpdates();
} catch (DatabaseException e) {
throw new UnexpectedLiquibaseException(e);
}
CSVReader reader = null;
try {
reader = getCSVReader();
if (reader == null) {
throw new UnexpectedLiquibaseException("Unable to read file " + this.getFile());
}
String[] headers = reader.readNext();
if (headers == null) {
throw new UnexpectedLiquibaseException("Data file " + getFile() + " was empty");
}
// Make sure all take the column list we interpolated from the CSV headers
addColumnsFromHeaders(headers);
// If we have an real JDBC connection to the database, ask the database for any missing column types.
try {
retrieveMissingColumnLoadTypes(columns, database);
} catch (DatabaseException e) {
throw new UnexpectedLiquibaseException(e);
}
List<ExecutablePreparedStatementBase> preparedStatements = new ArrayList<>();
boolean anyPreparedStatements = false;
String[] line;
// Start at '1' to take into account the header (already processed):
int lineNumber = 1;
boolean isCommentingEnabled = StringUtil.isNotEmpty(commentLineStartsWith);
List<SqlStatement> statements = new ArrayList<>();
while ((line = reader.readNext()) != null) {
lineNumber++;
if
((line.length == 0) || ((line.length == 1) && (StringUtil.trimToNull(line[0]) == null)) ||
(isCommentingEnabled && isLineCommented(line))
) {
//nothing interesting on this line
continue;
}
// Ensure each line has the same number of columns defined as does the header.
// (Failure could indicate unquoted strings with commas, for example).
if (line.length != headers.length) {
throw new UnexpectedLiquibaseException(
"CSV file " + getFile() + " Line " + lineNumber + " has " + line.length +
" values defined, Header has " + headers.length +
". Numbers MUST be equal (check for unquoted string with embedded commas)"
);
}
boolean needsPreparedStatement = false;
List<LoadDataColumnConfig> columnsFromCsv = new ArrayList<>();
for (int i = 0; i < headers.length; i++) {
String value = line[i];
String columnName = headers[i].trim();
LoadDataColumnConfig valueConfig = new LoadDataColumnConfig();
LoadDataColumnConfig columnConfig = getColumnConfig(i, columnName);
if (columnConfig != null) {
if ("skip".equalsIgnoreCase(columnConfig.getType())) {
continue;
}
// don't overwrite header name unless there is actually a value to override it with
if (columnConfig.getName() != null) {
columnName = columnConfig.getName();
}
//
// Always set the type for the valueConfig if the value is NULL
//
if ("NULL".equalsIgnoreCase(value)) {
valueConfig.setType(columnConfig.getType());
}
valueConfig.setName(columnName);
valueConfig.setAllowUpdate(columnConfig.getAllowUpdate());
if (value.isEmpty()) {
value = columnConfig.getDefaultValue();
}
if (StringUtil.equalsWordNull(value)) {
valueConfig.setValue(null);
} else if (columnConfig.getType() == null) {
// columnConfig did not specify a type
valueConfig.setValue(value);
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.UNKNOWN) {
// columnConfig did not match a specific type
valueConfig.setValue(value);
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.BOOLEAN) {
if (value == null) { // TODO getDefaultValueBoolean should use BooleanUtil.parseBoolean also for consistent behaviour
valueConfig.setValueBoolean(columnConfig.getDefaultValueBoolean());
} else {
valueConfig.setValueBoolean(BooleanUtil.parseBoolean(value));
}
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.NUMERIC) {
if (value != null) {
valueConfig.setValueNumeric(value);
} else {
valueConfig.setValueNumeric(columnConfig.getDefaultValueNumeric());
}
} else if ( columnConfig.getType().toLowerCase().contains("date")
|| columnConfig.getType().toLowerCase().contains("time")
) {
if ("NULL".equalsIgnoreCase(value) ||
"".equals(value)) {
valueConfig.setValue(null);
} else {
try {
// Need the column type for handling 'NOW' or 'TODAY' type column value
valueConfig.setType(columnConfig.getType());
if (value != null) {
valueConfig.setValueDate(value);
} else {
valueConfig.setValueDate(columnConfig.getDefaultValueDate());
}
} catch (DateParseException e) {
throw new UnexpectedLiquibaseException(e);
}
}
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.STRING) {
valueConfig.setType(columnConfig.getType());
valueConfig.setValue(value == null ? "" : value);
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.COMPUTED) {
if (null != value) {
liquibase.statement.DatabaseFunction function =
new liquibase.statement.DatabaseFunction(value);
valueConfig.setValueComputed(function);
} else {
valueConfig.setValueComputed(columnConfig.getDefaultValueComputed());
}
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.SEQUENCE) {
if (value == null) {
throw new UnexpectedLiquibaseException(
"Must set a sequence name in the loadData column defaultValue attribute"
);
}
liquibase.statement.SequenceNextValueFunction function =
new liquibase.statement.SequenceNextValueFunction(value);
valueConfig.setValueComputed(function);
} else if (columnConfig.getType().equalsIgnoreCase(LOAD_DATA_TYPE.BLOB.toString())) {
if ("NULL".equalsIgnoreCase(value)) {
valueConfig.setValue(null);
} else if (BASE64_PATTERN.matcher(value).matches()) {
valueConfig.setType(columnConfig.getType());
valueConfig.setValue(value);
needsPreparedStatement = true;
} else {
valueConfig.setValueBlobFile(value);
needsPreparedStatement = true;
}
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.CLOB) {
valueConfig.setValueClobFile(value);
needsPreparedStatement = true;
} else if (columnConfig.getTypeEnum() == LOAD_DATA_TYPE.UUID) {
valueConfig.setType(columnConfig.getType());
if ("NULL".equalsIgnoreCase(value)) {
valueConfig.setValue(null);
} else {
valueConfig.setValue(value);
}
} else if (columnConfig.getType().equalsIgnoreCase(LOAD_DATA_TYPE.OTHER.toString())) {
valueConfig.setType(columnConfig.getType());
if ("NULL".equalsIgnoreCase(value)) {
valueConfig.setValue(null);
} else {
valueConfig.setValue(value);
}
} else {
throw new UnexpectedLiquibaseException(
String.format(coreBundle.getString("loaddata.type.is.not.supported"),
columnConfig.getType()
)
);
}
} else {
// No columnConfig found. Assume header column name to be the table column name.
if (columnName.contains("(") || (columnName.contains(")") && (database instanceof
AbstractJdbcDatabase))) {
columnName = ((AbstractJdbcDatabase) database).quoteObject(columnName, Column.class);
}
valueConfig.setName(columnName);
valueConfig.setValue(getValueToWrite(value));
}
columnsFromCsv.add(valueConfig);
}
// end of: iterate through all the columns of a CSV line
// Try to use prepared statements if any of the following conditions apply:
// 1. There is no other option than using a prepared statement (e.g. in cases of LOBs) regardless
// of whether the 'usePreparedStatement' is set to false
// 2. The database supports batched statements (for improved performance) AND we are not in an
// "SQL" mode (i.e. we generate an SQL file instead of actually modifying the database).
// BUT: if the user specifically requests usePreparedStatement=false, then respect that
boolean actuallyUsePreparedStatements = false;
if (hasPreparedStatementsImplemented()) {
if (usePreparedStatements != null) {
if (!usePreparedStatements && needsPreparedStatement) {
throw new UnexpectedLiquibaseException("loadData is requesting usePreparedStatements=false but prepared statements are required");
}
actuallyUsePreparedStatements = usePreparedStatements;
} else {
actuallyUsePreparedStatements = needsPreparedStatement || (databaseSupportsBatchUpdates && !isLoggingExecutor(database));
}
}
if (actuallyUsePreparedStatements) {
anyPreparedStatements = true;
ExecutablePreparedStatementBase stmt =
this.createPreparedStatement(
database, getCatalogName(), getSchemaName(), getTableName(), columnsFromCsv,
getChangeSet(), Scope.getCurrentScope().getResourceAccessor()
);
preparedStatements.add(stmt);
} else {
InsertStatement insertStatement =
this.createStatement(getCatalogName(), getSchemaName(), getTableName());
for (LoadDataColumnConfig column : columnsFromCsv) {
String columnName = column.getName();
Object value = column.getValueObject();
if (value == null) {
value = "NULL";
}
insertStatement.addColumnValue(columnName, value);
if (insertStatement instanceof InsertOrUpdateStatement) {
((InsertOrUpdateStatement) insertStatement).setAllowColumnUpdate(columnName, column.getAllowUpdate() == null || column.getAllowUpdate());
}
}
statements.add(insertStatement);
}
// end of: will we use a PreparedStatement?
}
// end of: loop for every input line from the CSV file
if (anyPreparedStatements) {
// If we have only prepared statements and the database supports batching, let's roll
if (databaseSupportsBatchUpdates && statements.isEmpty() && (!preparedStatements.isEmpty())) {
if (database instanceof PostgresDatabase) {
// we don't do batch updates for Postgres but we still send as a prepared statement, see LB-744
return preparedStatements.toArray(new SqlStatement[preparedStatements.size()]);
} else {
return new SqlStatement[] {
new BatchDmlExecutablePreparedStatement(
database, getCatalogName(), getSchemaName(),
getTableName(), columns,
getChangeSet(), Scope.getCurrentScope().getResourceAccessor(),
preparedStatements)
};
}
} else {
return statements.toArray(new SqlStatement[statements.size()]);
}
} else {
if (statements.isEmpty()) {
// avoid returning unnecessary dummy statement
return new SqlStatement[0];
}
InsertSetStatement statementSet = this.createStatementSet(
getCatalogName(), getSchemaName(), getTableName()
);
for (SqlStatement stmt : statements) {
statementSet.addInsertStatement((InsertStatement) stmt);
}
if ((database instanceof MSSQLDatabase) || (database instanceof MySQLDatabase) || (database
instanceof PostgresDatabase)) {
List<InsertStatement> innerStatements = statementSet.getStatements();
if ((innerStatements != null) && (!innerStatements.isEmpty()) && (innerStatements.get(0)
instanceof InsertOrUpdateStatement)) {
//cannot do insert or update in a single statement
return statementSet.getStatementsArray();
}
// we only return a single "statement" - it's capable of emitting multiple sub-statements,
// should the need arise, on generation.
return new SqlStatement[]{statementSet};
} else {
return statementSet.getStatementsArray();
}
}
} catch (IOException | LiquibaseException e) {
throw new RuntimeException(e);
} catch (UnexpectedLiquibaseException ule) {
if ((getChangeSet() != null) && (getChangeSet().getFailOnError() != null) && !getChangeSet()
.getFailOnError()) {
LOG.info("Change set " + getChangeSet().toString(false) +
" failed, but failOnError was false. Error: " + ule.getMessage());
return new SqlStatement[0];
} else {
throw ule;
}
} finally {
if (null != reader) {
try {
reader.close();
} catch (Exception e) {
// Do nothing
}
}
}
}
private boolean isLoggingExecutor(Database database) {
final ExecutorService executorService = Scope.getCurrentScope().getSingleton(ExecutorService.class);
return executorService.executorExists("logging", database) &&
(executorService.getExecutor("logging", database) instanceof LoggingExecutor);
}
/**
* Iterate through the List of LoadDataColumnConfig and ask the database for any column types that we have
* no data type of.
* @param columns a list of LoadDataColumnConfigs to process
*/
@SuppressWarnings("CommentedOutCodeLine")
private void retrieveMissingColumnLoadTypes(List<LoadDataColumnConfig> columns, Database database) throws
DatabaseException {
// If no column is missing type information, we are already done.
if (columns.stream().noneMatch(c -> c.getType() == null)) {
return;
}
// Snapshot the database table
CatalogAndSchema catalogAndSchema = new CatalogAndSchema(getCatalogName(), getSchemaName());
catalogAndSchema = catalogAndSchema.standardize(database);
Table targetTable = new Table(catalogAndSchema.getCatalogName(), catalogAndSchema.getSchemaName(),
database.correctObjectName(getTableName(), Table.class));
Table snapshotOfTable;
try {
snapshotOfTable = SnapshotGeneratorFactory.getInstance().createSnapshot(
targetTable,
database, new SnapshotControl(database, Table.class, Column.class));
} catch (InvalidExampleException e) {
throw new DatabaseException(e);
}
if (snapshotOfTable == null) {
LOG.warning(String.format(
coreBundle.getString("could.not.snapshot.table.to.get.the.missing.column.type.information"),
database.escapeTableName(
targetTable.getSchema().getCatalogName(),
targetTable.getSchema().getName(),
targetTable.getName())
));
return;
}
// Save the columns of the database table in a lookup table
Map<String, Column> tableColumns = new HashMap<>();
for (Column c : snapshotOfTable.getColumns()) {
// Normalise the LoadDataColumnConfig column names to the database
tableColumns.put(database.correctObjectName(c.getName(), Column.class), c);
}
/* The above is the JDK7 version of:
snapshotOfTable.getColumns().forEach(c -> tableColumns.put(c.getName(), c));
*/
// Normalise the LoadDataColumnConfig column names to the database
Map<String, LoadDataColumnConfig> columnConfigs = new HashMap<>();
for (LoadDataColumnConfig c : columns) {
columnConfigs.put(
database.correctObjectName(c.getName(), Column.class),
c
);
}
/* The above is the JDK7 version of:
columns.forEach(c -> columnConfigs.put(
database.correctObjectName(c.getName(), Column.class),
c
));
*/
for (Map.Entry<String, LoadDataColumnConfig> entry : columnConfigs.entrySet()) {
if (entry.getValue().getType() != null) {
continue;
}
LoadDataColumnConfig columnConfig = entry.getValue();
Column c = tableColumns.get(entry.getKey());
if (null == c) {
LOG.severe(String.format(coreBundle.getString("unable.to.find.column.in.table"),
columnConfig.getName(), snapshotOfTable));
} else {
DataType dataType = c.getType();
if (dataType == null) {
LOG.warning(String.format(coreBundle.getString("unable.to.find.load.data.type"),
columnConfig.toString(), snapshotOfTable));
columnConfig.setType(LOAD_DATA_TYPE.STRING);
} else {
LiquibaseDataType liquibaseDataType = DataTypeFactory.getInstance()
.fromDescription(dataType.toString(), database);
if (liquibaseDataType != null) {
columnConfig.setType(liquibaseDataType.getLoadTypeName());
} else {
LOG.warning(String.format(coreBundle.getString("unable.to.convert.load.data.type"),
columnConfig.toString(), snapshotOfTable, dataType));
}
}
}
}
/* The above is the JDK7 version of:
columnConfigs.entrySet().stream()
.filter(entry -> entry.getValue().getType() == null)
.forEach(entry -> {
LoadDataColumnConfig columnConfig = entry.getValue();
DataType dataType = tableColumns.get(entry.getKey()).getType();
if (dataType == null) {
LOG.warning(String.format(coreBundle.getString("unable.to.find.load.data.type"),
columnConfig.toString(), snapshotOfTable.toString() ));
columnConfig.setType(LOAD_DATA_TYPE.STRING.toString());
} else {
LiquibaseDataType liquibaseDataType = DataTypeFactory.getInstance()
.fromDescription(dataType.toString(), database);
if (liquibaseDataType != null) {
columnConfig.setType(liquibaseDataType.getLoadTypeName().toString());
} else {
LOG.warning(String.format(coreBundle.getString("unable.to.convert.load.data.type"),
columnConfig.toString(), snapshotOfTable.toString(), liquibaseDataType.toString()));
}
}
}
);
*/
}
@Override
public ValidationErrors validate(Database database) {
ValidationErrors validationErrors = new ValidationErrors(this);
validationErrors.addAll(super.validate(database));
return validateColumns(validationErrors);
}
public void validateColumn(LoadDataColumnConfig columnConfig, ValidationErrors validationErrors, String columnIDString) {
if(columnConfig.getHeader() != null && columnConfig.getIndex() != null) {
validationErrors.addWarning("Since attribute 'header' is also defined, 'index' ignored for "
+ validationErrors.getChangeName() + columnIDString);
}
}
/**
* Get the column using the name, index or the header
* @param name name or header of the column searched
* @param idx the index of the column searched
* @return The column having the name or header equal to "name" or the index equal to idx
* or a new LoadDataColumnConfig if not found
*/
protected LoadDataColumnConfig columnConfigFromName(String name, Integer idx) {
for (LoadDataColumnConfig c : this.columns) {
if (name.equals(c.getName()) || name.equals(c.getHeader()) || idx.equals(c.getIndex())) {
return c;
}
}
if(null == StringUtil.trimToNull(name)) {
throw new UnexpectedLiquibaseException("Unreferenced unnamed column is not supported");
}
LoadDataColumnConfig cfg = new LoadDataColumnConfig();
columns.add(cfg);
cfg.setName(name);
return cfg;
}
/**
* Add columns if they were not specified in the loadData change, we interpolate their names from
* the header columns of the CSV file.
*
* @param headers the headers of the CSV file
* @return a List of LoadDataColumnConfigs
*/
private void addColumnsFromHeaders(String[] headers) {
int i = 0;
for (String columnNameFromHeader : headers) {
LoadDataColumnConfig loadDataColumnConfig = columnConfigFromName(columnNameFromHeader,i);
loadDataColumnConfig.setIndex(i);
loadDataColumnConfig.setHeader(columnNameFromHeader);
i++;
}
}
private boolean isLineCommented(String[] line) {
return StringUtil.startsWith(line[0], commentLineStartsWith);
}
@Override
public boolean generateStatementsVolatile(Database database) {
return true;
}
public CSVReader getCSVReader() throws IOException, LiquibaseException {
ResourceAccessor resourceAccessor = Scope.getCurrentScope().getResourceAccessor();
if (resourceAccessor == null) {
throw new UnexpectedLiquibaseException("No file resourceAccessor specified for " + getFile());
}
String relativeTo = getRelativeTo();
InputStream stream = resourceAccessor.openStream(relativeTo, file);
if (stream == null) {
return null;
}
Reader streamReader = StreamUtil.readStreamWithReader(stream, getEncoding());
char quotchar;
if (StringUtil.trimToEmpty(this.quotchar).isEmpty()) {
// hope this is impossible to have a field surrounded with non ascii char 0x01
quotchar = '\1';
} else {
quotchar = this.quotchar.charAt(0);
}
if (separator == null) {
separator = CSVReader.DEFAULT_SEPARATOR + "";
}
return new CSVReader(streamReader, separator.charAt(0), quotchar);
}
protected String getRelativeTo() {
String relativeTo = null;
if (ObjectUtil.defaultIfNull(isRelativeToChangelogFile(), false)) {
relativeTo = getChangeSet().getFilePath();
}
return relativeTo;
}
protected ExecutablePreparedStatementBase createPreparedStatement(
Database database, String catalogName, String schemaName, String tableName,
List<LoadDataColumnConfig> columns, ChangeSet changeSet, ResourceAccessor resourceAccessor) {
return new InsertExecutablePreparedStatement(database, catalogName, schemaName, tableName, columns,
changeSet, resourceAccessor);
}
protected InsertStatement createStatement(String catalogName, String schemaName, String tableName) {
return new InsertStatement(catalogName, schemaName, tableName);
}
protected InsertSetStatement createStatementSet(String catalogName, String schemaName, String tableName) {
return new InsertSetStatement(catalogName, schemaName, tableName);
}
protected LoadDataColumnConfig getColumnConfig(int index, String header) {
for (LoadDataColumnConfig config : columns) {
if ((config.getIndex() != null) && config.getIndex().equals(index)) {
return config;
}
if ((config.getHeader() != null) && config.getHeader().equalsIgnoreCase(header)) {
return config;
}
if ((config.getName() != null) && config.getName().equalsIgnoreCase(header)) {
return config;
}
}
return null;
}
@Override
public ChangeStatus checkStatus(Database database) {
return new ChangeStatus().unknown("Cannot check loadData status");
}
@Override
public String getConfirmationMessage() {
return String.format(coreBundle.getString("loaddata.successful"), getFile(), getTableName());
}
@Override
public CheckSum generateCheckSum() {
InputStream stream = null;
try {
stream = Scope.getCurrentScope().getResourceAccessor().openStream(getRelativeTo(), file);
if (stream == null) {
throw new UnexpectedLiquibaseException(String.format(
coreBundle.getString("file.not.found"), file));
}
stream = new EmptyLineAndCommentSkippingInputStream(stream, commentLineStartsWith);
return CheckSum.compute(getTableName() + ":" + CheckSum.compute(stream, /*standardizeLineEndings*/ true));
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException ignore) {
// Do nothing
}
}
}
}
@Override
public Warnings warn(Database database) {
return null;
}
@Override
public String getSerializedObjectNamespace() {
return STANDARD_CHANGELOG_NAMESPACE;
}
@SuppressWarnings("HardCodedStringLiteral")
public enum LOAD_DATA_TYPE {
BOOLEAN, NUMERIC, DATE, STRING, COMPUTED, SEQUENCE, BLOB, CLOB, SKIP,UUID, OTHER, UNKNOWN
}
}