}
@Override
public void appendRecords(Collection<Record> records) {
// Create jdbc template
final NamedParameterJdbcTemplate npjt = new NamedParameterJdbcTemplate(jdbcTemplate);
// init sql parameters for placeholder fields, for all records
final ArrayList<MapSqlParameterSource> parameters = new ArrayList<MapSqlParameterSource>();
// Copy record contents to sql parameter source
for (Record r: records) {
try {
MapSqlParameterSource p = new MapSqlParameterSource() ;
for (String f: placeholders) {
try {
if (! r.has(f) || r.get(f).getValue() == null) {
// Handle undefined or null-valued fields by simply adding null
p.addValue(placeholderMap.get(f), null);
} else {
if (typeMap.get(f).equals(java.sql.Types.NUMERIC)) {
try {
p.addValue(placeholderMap.get(f), Long.valueOf(r.get(f).getValue()),
java.sql.Types.NUMERIC);
} catch (NumberFormatException ex) {
// Parse to big decimal as a fallback option
p.addValue(placeholderMap.get(f), new java.math.BigDecimal(r.get(f).getValue()),
java.sql.Types.NUMERIC);
}
}
else {
p.addValue(placeholderMap.get(f), r.get(f).getValue(), typeMap.get(f)) ;
}
}
} catch (NullPointerException ex) {
log.warn("Target SQL placeholder '" + f + "' refers to a nonexistent field.");
p.addValue(placeholderMap.get(f), null);
}
}
parameters.add(p);
} catch (NumberFormatException ex) {
log.warn("Couldn't parse one of the fields of numeric-fields-to-convert; record: " + r.toString());
}
}
// Batch update in transaction
TransactionTemplate txTemplate = new TransactionTemplate(txManager);
try {
txTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus ts) {
int[] affectedRows =
npjt.batchUpdate(insertQuery.getSql(), parameters.toArray(new MapSqlParameterSource[0]));
int sum = 0;
for (int i = 0; i < affectedRows.length; ++i) {
sum += DatabaseUtils.getAffectedRowsNumber(affectedRows[i]);
}
insertedRecords += sum;
// log.debug(String.format("Inserted %1$d rows in batch.", sum));
}
});
} catch (RuntimeException ex) {
log.warn(this, ex);
// Do line by line
int insertCount = 0;
for (int i = 0; i < parameters.size(); ++i) {
try {
int affectedRows = npjt.update(insertQuery.getSql(), parameters.get(i));
insertCount += DatabaseUtils.getAffectedRowsNumber(affectedRows);
} catch (RuntimeException ex2) {
log.warn("Could not insert record: "
+ DatabaseUtils.sqlParameterSourceToText(parameters.get(i)), ex2);