use of com.google.spanner.v1.Type in project pgadapter by GoogleCloudPlatform.
the class MutationWriter method buildMutationList.
/**
* Build mutation to add to mutations list with data contained within a CopyData payload
*/
public void buildMutationList(ConnectionHandler connectionHandler) throws Exception {
List<CSVRecord> records = parsePayloadData(this.payload.toByteArray());
for (CSVRecord record : records) {
// Check that the number of columns in a record matches the number of columns in the table
if (record.size() != this.tableColumns.keySet().size()) {
handleError(connectionHandler);
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid COPY data: Row length mismatched. Expected " + this.tableColumns.keySet().size() + " columns, but only found " + record.size());
}
WriteBuilder builder = Mutation.newInsertBuilder(this.tableName);
// Iterate through all table column to copy into
for (String columnName : this.tableColumns.keySet()) {
TypeCode columnType = this.tableColumns.get(columnName);
String recordValue = "";
try {
recordValue = record.get(columnName).trim();
switch(columnType) {
case STRING:
builder.set(columnName).to(recordValue);
break;
case BOOL:
builder.set(columnName).to(Boolean.parseBoolean(recordValue));
break;
case INT64:
builder.set(columnName).to(Long.parseLong(recordValue));
break;
case FLOAT64:
builder.set(columnName).to(Double.parseDouble(recordValue));
break;
case BYTES:
builder.set(columnName).to(Byte.parseByte(recordValue));
break;
case TIMESTAMP:
builder.set(columnName).to(com.google.cloud.Timestamp.parseTimestamp(recordValue));
break;
}
} catch (NumberFormatException | DateTimeParseException e) {
handleError(connectionHandler);
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid input syntax for type " + columnType.toString() + ":" + "\"" + recordValue + "\"");
} catch (IllegalArgumentException e) {
handleError(connectionHandler);
throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid input syntax for column \"" + columnName + "\"");
} catch (Exception e) {
handleError(connectionHandler);
throw e;
}
}
// Add write builder to mutation list
this.mutations.add(builder.build());
// Increment the number of mutations being added
this.mutationCount += record.size();
// Increment the number of COPY rows by one
this.rowCount++;
}
if (!mutationCountIsWithinLimit()) {
handleError(connectionHandler);
throw SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "Mutation count: " + mutationCount + " has exceeded the limit: " + MUTATION_LIMIT);
}
}
Aggregations