Search in sources :

Example 6 with WriteBuilder

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);
}
Also used : WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Test(org.junit.Test)

Example 7 with 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);
    });
}
Also used : Date(com.google.cloud.Date) CustomConversions(org.springframework.data.convert.CustomConversions) ImmutableMap(com.google.common.collect.ImmutableMap) BiFunction(java.util.function.BiFunction) SpannerDataException(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException) Set(java.util.Set) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Timestamp(com.google.cloud.Timestamp) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) PropertyHandler(org.springframework.data.mapping.PropertyHandler) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty) Map(java.util.Map) ValueBinder(com.google.cloud.spanner.ValueBinder) BiConsumer(java.util.function.BiConsumer) ByteArray(com.google.cloud.ByteArray) SpannerPersistentEntity(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity) SpannerMappingContext(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext) EntityWriter(org.springframework.data.convert.EntityWriter) PersistentPropertyAccessor(org.springframework.data.mapping.PersistentPropertyAccessor) SpannerPersistentProperty(org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty)

Example 8 with WriteBuilder

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();
}
Also used : WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) Mutation(com.google.cloud.spanner.Mutation)

Example 9 with WriteBuilder

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();
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 10 with WriteBuilder

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();
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Aggregations

WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)11 Test (org.junit.Test)5 Mutation (com.google.cloud.spanner.Mutation)2 Expression (net.sf.jsqlparser.expression.Expression)2 Column (net.sf.jsqlparser.schema.Column)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2 ByteArray (com.google.cloud.ByteArray)1 Date (com.google.cloud.Date)1 Timestamp (com.google.cloud.Timestamp)1 ValueBinder (com.google.cloud.spanner.ValueBinder)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1 Set (java.util.Set)1 BiConsumer (java.util.function.BiConsumer)1 BiFunction (java.util.function.BiFunction)1 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)1 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)1 ArgumentMatchers.anyDouble (org.mockito.ArgumentMatchers.anyDouble)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 SpannerDataException (org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException)1