private static void insertRows(ConnectorTableMetadata tableMetadata, Handle handle, RecordSet data)
{
String vars = Joiner.on(',').join(nCopies(tableMetadata.getColumns().size(), "?"));
String sql = format("INSERT INTO %s VALUES (%s)", tableMetadata.getTable().getTableName(), vars);
RecordCursor cursor = data.cursor();
while (true) {
// insert 1000 rows at a time
PreparedBatch batch = handle.prepareBatch(sql);
for (int row = 0; row < 1000; row++) {
if (!cursor.advanceNextPosition()) {
batch.execute();
return;
}
PreparedBatchPart part = batch.add();
for (int column = 0; column < tableMetadata.getColumns().size(); column++) {
ColumnMetadata columnMetadata = tableMetadata.getColumns().get(column);
switch (columnMetadata.getType()) {
case BOOLEAN:
part.bind(column, cursor.getBoolean(column));
break;
case LONG:
part.bind(column, cursor.getLong(column));
break;
case DOUBLE:
part.bind(column, cursor.getDouble(column));
break;
case STRING:
part.bind(column, new String(cursor.getString(column), UTF_8));
break;
}
}
}
batch.execute();