in tools/query_verification/src/main/java/com/google/bigquery/TeradataManager.java [323:365]
private void populateTablesFromData() {
for (QueryVerificationData queryVerificationData : data) {
String tableId = queryVerificationData.datasetName() + "." + queryVerificationData.tableName();
try {
// Clear out error tables
statementConnection.executeUpdate("DROP TABLE " + tableId + "_ERR_1;");
statementConnection.executeUpdate("DROP TABLE " + tableId + "_ERR_2;");
} catch (SQLException e) {
e.printStackTrace();
}
String csvContents = queryVerificationData.contents();
// Identifies how many columns of data are provideed
String firstRow = csvContents.split("\n")[0];
int columns = firstRow.length() - firstRow.replace(",", "").length() + 1;
// Assemble placeholders (question marks) in the INSERT INTO statement for each column
String[] columnPlaceholders = new String[columns];
Arrays.fill(columnPlaceholders, "?");
String columnValues = String.join(",", columnPlaceholders);
String insertIntoStatement = "INSERT INTO " + tableId + " VALUES(" + columnValues + ");";
// Workaround since Fastload CSV utility doesn't include first and last lines of CSV files
csvContents = columnValues + "\n" + csvContents;
if (!csvContents.endsWith("\n")) {
csvContents += "\n";
}
// Run INSERT INTO statement
try {
PreparedStatement insertCsv = csvConnection.prepareStatement(insertIntoStatement);
InputStream inputStream = new ByteArrayInputStream(csvContents.getBytes());
insertCsv.setAsciiStream(1, inputStream, -1);
insertCsv.executeUpdate();
insertCsv.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}