this.columns = columns;
}
@Override
public SqlStatement[] generateStatements(Database database) {
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");
}
List<SqlStatement> statements = new ArrayList<SqlStatement>();
String[] line;
int lineNumber = 0;
while ((line = reader.readNext()) != null) {
lineNumber++;
if (line.length == 0 || (line.length == 1 && StringUtils.trimToNull(line[0]) == null)) {
continue; //nothing on this line
}
InsertStatement insertStatement = this.createStatement(getCatalogName(), getSchemaName(), getTableName());
for (int i=0; i<headers.length; i++) {
String columnName = null;
if( i >= line.length ) {
throw new UnexpectedLiquibaseException("CSV Line " + lineNumber + " has only " + (i-1) + " columns, the header has " + headers.length);
}
Object value = line[i];
ColumnConfig columnConfig = getColumnConfig(i, headers[i].trim());
if (columnConfig != null) {
columnName = columnConfig.getName();
if ("skip".equalsIgnoreCase(columnConfig.getType())) {
continue;
}
if (value.toString().equalsIgnoreCase("NULL")) {
value = "NULL";
} else if (columnConfig.getType() != null) {
ColumnConfig valueConfig = new ColumnConfig();
if (columnConfig.getType().equalsIgnoreCase("BOOLEAN")) {
valueConfig.setValueBoolean(BooleanParser.parseBoolean(value.toString().toLowerCase()));
} else if (columnConfig.getType().equalsIgnoreCase("NUMERIC")) {
valueConfig.setValueNumeric(value.toString());
} else if (columnConfig.getType().toLowerCase().contains("date") ||columnConfig.getType().toLowerCase().contains("time")) {
valueConfig.setValueDate(value.toString());
} else if (columnConfig.getType().equalsIgnoreCase("STRING")) {
valueConfig.setValue(value.toString());
} else if (columnConfig.getType().equalsIgnoreCase("COMPUTED")) {
liquibase.statement.DatabaseFunction function = new liquibase.statement.DatabaseFunction(value.toString());
valueConfig.setValueComputed(function);
} else {
throw new UnexpectedLiquibaseException("loadData type of "+columnConfig.getType()+" is not supported. Please use BOOLEAN, NUMERIC, DATE, STRING, COMPUTED or SKIP");
}
value = valueConfig.getValueObject();
}
}
if (columnName == null) {
columnName = headers[i];
}
if (columnName.contains("(") || columnName.contains(")") && database instanceof AbstractJdbcDatabase) {
columnName = ((AbstractJdbcDatabase) database).quoteObject(columnName, Column.class);
}
insertStatement.addColumnValue(columnName, value);
}
statements.add(insertStatement);
}
return statements.toArray(new SqlStatement[statements.size()]);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (UnexpectedLiquibaseException ule) {
if (getChangeSet() != null && getChangeSet().getFailOnError() != null && !getChangeSet().getFailOnError()) {
Logger log = LogFactory.getLogger();
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 (IOException e) {
;
}
}
}