use of com.google.cloud.spanner.Mutation.WriteBuilder in project spring-cloud-gcp by spring-cloud.
the class SpannerObjectMapperImplTests method writeIncompatibleTypeTest.
@Test(expected = SpannerDataException.class)
public void writeIncompatibleTypeTest() {
FaultyTestEntity ft = new FaultyTestEntity();
ft.fieldWithUnsupportedType = new TestEntity();
WriteBuilder writeBuilder = Mutation.newInsertBuilder("faulty_test_table");
this.objectMapper.write(ft, writeBuilder);
}
use of com.google.cloud.spanner.Mutation.WriteBuilder in project spring-cloud-gcp by spring-cloud.
the class MappingSpannerWriteConverter method write.
/**
* Writes an object's properties to the sink.
* @param source the object to write
* @param sink the sink to which to write
* @param includeColumns the properties/columns to write. If null, then all columns
* are written.
*/
public void write(Object source, WriteBuilder sink, Set<String> includeColumns) {
boolean writeAllColumns = includeColumns == null;
SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(source.getClass());
PersistentPropertyAccessor accessor = persistentEntity.getPropertyAccessor(source);
persistentEntity.doWithProperties((PropertyHandler<SpannerPersistentProperty>) spannerPersistentProperty -> {
if (!writeAllColumns && !includeColumns.contains(spannerPersistentProperty.getColumnName())) {
return;
}
writeProperty(sink, accessor, spannerPersistentProperty);
});
}
use of com.google.cloud.spanner.Mutation.WriteBuilder in project spring-cloud-gcp by spring-cloud.
the class SpannerMutationFactoryImpl method saveObject.
private Mutation saveObject(Op op, Object object, Set<String> includeColumns) {
SpannerPersistentEntity<?> persistentEntity = this.spannerMappingContext.getPersistentEntity(object.getClass());
Mutation.WriteBuilder writeBuilder = writeBuilder(op, persistentEntity.tableName());
this.spannerConverter.write(object, writeBuilder, includeColumns);
return writeBuilder.build();
}
use of com.google.cloud.spanner.Mutation.WriteBuilder in project spanner-jdbc by olavloite.
the class CloudSpannerPreparedStatement method createUpdateMutation.
private Mutation createUpdateMutation(Update update, boolean generateParameterMetaData) throws SQLException {
if (update.getTables().isEmpty())
throw new CloudSpannerSQLException("No table found in update statement", Code.INVALID_ARGUMENT);
if (update.getTables().size() > 1)
throw new CloudSpannerSQLException("Update statements for multiple tables at once are not supported", Code.INVALID_ARGUMENT);
String table = unquoteIdentifier(update.getTables().get(0).getFullyQualifiedName());
getParameterStore().setTable(table);
List<Expression> expressions = update.getExpressions();
WriteBuilder builder = Mutation.newUpdateBuilder(table);
int index = 0;
for (Column col : update.getColumns()) {
String columnName = unquoteIdentifier(col.getFullyQualifiedName());
expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName));
index++;
}
visitUpdateWhereClause(update.getWhere(), builder, generateParameterMetaData);
return builder.build();
}
use of com.google.cloud.spanner.Mutation.WriteBuilder in project spanner-jdbc by olavloite.
the class CloudSpannerPreparedStatement method createInsertMutation.
private Mutation createInsertMutation(Insert insert, boolean generateParameterMetaData) throws SQLException {
ItemsList items = insert.getItemsList();
if (generateParameterMetaData && items == null && insert.getSelect() != null) {
// Just initialize the parameter meta data of the select statement
createSelectBuilder(insert.getSelect(), insert.getSelect().toString());
return null;
}
if (!(items instanceof ExpressionList)) {
throw new CloudSpannerSQLException("Insert statement must specify a list of values", Code.INVALID_ARGUMENT);
}
if (insert.getColumns() == null || insert.getColumns().isEmpty()) {
throw new CloudSpannerSQLException("Insert statement must specify a list of column names", Code.INVALID_ARGUMENT);
}
List<Expression> expressions = ((ExpressionList) items).getExpressions();
String table = unquoteIdentifier(insert.getTable().getFullyQualifiedName());
getParameterStore().setTable(table);
WriteBuilder builder;
if (insert.isUseDuplicate()) {
/**
* Do an insert-or-update. BUT: Cloud Spanner does not support supplying different values for
* the insert and update statements, meaning that only the values specified in the INSERT part
* of the statement will be considered. Anything specified in the 'ON DUPLICATE KEY UPDATE
* ...' statement will be ignored.
*/
if (this.forceUpdate)
builder = Mutation.newUpdateBuilder(table);
else
builder = Mutation.newInsertOrUpdateBuilder(table);
} else {
/**
* Just do an insert and throw an error if a row with the specified key alread exists.
*/
builder = Mutation.newInsertBuilder(table);
}
int index = 0;
for (Column col : insert.getColumns()) {
String columnName = unquoteIdentifier(col.getFullyQualifiedName());
expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName));
index++;
}
return builder.build();
}
Aggregations