use of org.apache.drill.exec.expr.holders.VarCharHolder in project drill by apache.
the class JdbcRecordWriter method endRecord.
@Override
public void endRecord() throws IOException {
logger.debug("Ending record");
// Add values to rowString
for (int i = 0; i < rowList.size(); i++) {
if (i > 0) {
insertQueryBuilder.append(",");
}
// Add null value to rowstring
if (rowList.get(i) instanceof String && ((String) rowList.get(i)).equalsIgnoreCase("null")) {
insertQueryBuilder.append("null");
continue;
}
JdbcWriterField currentField = fields.get(i);
if (currentField.getDataType() == MinorType.VARCHAR) {
String value = null;
// Get the string value
if (currentField.getMode() == DataMode.REQUIRED) {
VarCharHolder varCharHolder = (VarCharHolder) rowList.get(i);
value = StringFunctionHelpers.getStringFromVarCharHolder(varCharHolder);
} else {
try {
NullableVarCharHolder nullableVarCharHolder = (NullableVarCharHolder) rowList.get(i);
value = StringFunctionHelpers.getStringFromVarCharHolder(nullableVarCharHolder);
} catch (ClassCastException e) {
logger.error("Unable to read field: {}", rowList.get(i));
}
}
// Add to value string
insertQueryBuilder.literal(value);
} else if (currentField.getDataType() == MinorType.DATE) {
String dateString = formatDateForInsertQuery((Long) rowList.get(i));
insertQueryBuilder.literal(dateString);
} else if (currentField.getDataType() == MinorType.TIME) {
String timeString = formatTimeForInsertQuery((Integer) rowList.get(i));
insertQueryBuilder.literal(timeString);
} else if (currentField.getDataType() == MinorType.TIMESTAMP) {
String timeString = formatTimeStampForInsertQuery((Long) rowList.get(i));
insertQueryBuilder.literal(timeString);
} else {
if (Strings.isNullOrEmpty(rowList.get(i).toString())) {
insertQueryBuilder.append("null");
} else {
insertQueryBuilder.append(rowList.get(i).toString());
}
}
}
recordCount++;
firstRecord = false;
insertQueryBuilder.append(")");
if (recordCount >= config.getPlugin().getConfig().getWriterBatchSize()) {
// Execute the insert query
String insertQuery = insertQueryBuilder.toString();
executeInsert(insertQuery);
// Reset the batch
recordCount = 0;
firstRecord = true;
insertQueryBuilder = initializeInsertQuery();
}
rowList.clear();
}
Aggregations