Search in sources :

Example 66 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method existsByIdTest.

@Test
public void existsByIdTest() {
    ResultSet results = mock(ResultSet.class);
    when(results.next()).thenReturn(true);
    when(this.readContext.read(any(), any(), any(), any())).thenReturn(results);
    when(this.databaseClient.singleUse(any())).thenReturn(this.readContext);
    Key key = Key.of("key");
    KeySet keySet = KeySet.singleKey(key);
    assertThat(this.spannerTemplate.existsById(TestEntity.class, key)).isTrue();
    verify(this.databaseClient, times(1)).singleUse();
    verify(this.readContext, times(1)).read(eq("custom_test_table"), eq(keySet), eq(Collections.singleton("id")));
}
Also used : KeySet(com.google.cloud.spanner.KeySet) ResultSet(com.google.cloud.spanner.ResultSet) Key(com.google.cloud.spanner.Key) PrimaryKey(org.springframework.cloud.gcp.data.spanner.core.mapping.PrimaryKey) Test(org.junit.Test)

Example 67 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplateTests method countTest.

@Test
public void countTest() {
    ResultSet results = mock(ResultSet.class);
    when(this.readContext.executeQuery(eq(Statement.of("SELECT COUNT(*) FROM custom_test_table")))).thenReturn(results);
    this.spannerTemplate.count(TestEntity.class);
    verify(results, times(1)).next();
    verify(results, times(1)).getLong(eq(0));
    verify(results, times(1)).close();
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) Test(org.junit.Test)

Example 68 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.

the class SpannerTemplate method count.

@Override
public <T> long count(Class<T> entityClass) {
    SpannerPersistentEntity<?> persistentEntity = this.mappingContext.getPersistentEntity(entityClass);
    Statement statement = Statement.of(String.format("SELECT COUNT(*) FROM %s", persistentEntity.tableName()));
    try (ResultSet resultSet = executeQuery(statement, null)) {
        resultSet.next();
        return resultSet.getLong(0);
    }
}
Also used : Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet)

Example 69 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.

the class SpannerDatabaseAdminTemplate method getChildParentTablesMap.

/**
 * Return a map where key is the table name and the value is the parent table name. If
 * the table name in the key has no parent then the value is null.
 * @return the map of the table names.
 */
public Map<String, String> getChildParentTablesMap() {
    Map<String, String> relationships = new HashMap<>();
    try (ResultSet results = this.databaseClientProvider.get().singleUse().executeQuery(TABLE_AND_PARENT_QUERY)) {
        while (results.next()) {
            Struct row = results.getCurrentRowAsStruct();
            relationships.put(row.getString(TABLE_NAME_COL_NAME), row.isNull(PARENT_TABLE_NAME_COL_NAME) ? null : row.getString(PARENT_TABLE_NAME_COL_NAME));
        }
        return relationships;
    }
}
Also used : HashMap(java.util.HashMap) ResultSet(com.google.cloud.spanner.ResultSet) Struct(com.google.cloud.spanner.Struct)

Example 70 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spring-cloud-gcp by spring-cloud.

the class SpannerTransactionManager method doBegin.

@Override
protected void doBegin(Object transactionObject, TransactionDefinition transactionDefinition) throws TransactionException {
    if (transactionDefinition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
        throw new IllegalStateException("SpannerTransactionManager supports only isolation level TransactionDefinition.ISOLATION_DEFAULT");
    }
    if (transactionDefinition.getPropagationBehavior() != TransactionDefinition.PROPAGATION_REQUIRED) {
        throw new IllegalStateException("SpannerTransactionManager supports only propagation behavior " + "TransactionDefinition.PROPAGATION_REQUIRED");
    }
    Tx tx = (Tx) transactionObject;
    if (transactionDefinition.isReadOnly()) {
        final ReadContext targetTransactionContext = this.databaseClientProvider.get().readOnlyTransaction();
        tx.isReadOnly = true;
        tx.transactionManager = null;
        tx.transactionContext = new TransactionContext() {

            @Override
            public void buffer(Mutation mutation) {
                throw new IllegalStateException("Spanner transaction cannot apply" + " mutation because it is in readonly mode");
            }

            @Override
            public void buffer(Iterable<Mutation> iterable) {
                throw new IllegalStateException("Spanner transaction cannot apply" + " mutations because it is in readonly mode");
            }

            @Override
            public long executeUpdate(Statement statement) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ApiFuture<Long> executeUpdateAsync(Statement statement) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public long[] batchUpdate(Iterable<Statement> iterable) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ApiFuture<long[]> batchUpdateAsync(Iterable<Statement> iterable) {
                throw new IllegalStateException("Spanner transaction cannot execute DML " + "because it is in readonly mode");
            }

            @Override
            public ResultSet read(String s, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.read(s, keySet, iterable, readOptions);
            }

            @Override
            public AsyncResultSet readAsync(String s, KeySet keySet, Iterable<String> iterable, ReadOption... readOptions) {
                return targetTransactionContext.readAsync(s, keySet, iterable, readOptions);
            }

            @Override
            public ResultSet readUsingIndex(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.readUsingIndex(s, s1, keySet, iterable, readOptions);
            }

            @Override
            public AsyncResultSet readUsingIndexAsync(String s, String s1, KeySet keySet, Iterable<String> iterable, Options.ReadOption... readOptions) {
                return targetTransactionContext.readUsingIndexAsync(s, s1, keySet, iterable, readOptions);
            }

            @Nullable
            @Override
            public Struct readRow(String s, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRow(s, key, iterable);
            }

            @Override
            public ApiFuture<Struct> readRowAsync(String s, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowAsync(s, key, iterable);
            }

            @Nullable
            @Override
            public Struct readRowUsingIndex(String s, String s1, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowUsingIndex(s, s1, key, iterable);
            }

            @Override
            public ApiFuture<Struct> readRowUsingIndexAsync(String s, String s1, Key key, Iterable<String> iterable) {
                return targetTransactionContext.readRowUsingIndexAsync(s, s1, key, iterable);
            }

            @Override
            public ResultSet executeQuery(Statement statement, Options.QueryOption... queryOptions) {
                return targetTransactionContext.executeQuery(statement, queryOptions);
            }

            @Override
            public AsyncResultSet executeQueryAsync(Statement statement, QueryOption... queryOptions) {
                return targetTransactionContext.executeQueryAsync(statement, queryOptions);
            }

            @Override
            public ResultSet analyzeQuery(Statement statement, QueryAnalyzeMode queryAnalyzeMode) {
                return targetTransactionContext.analyzeQuery(statement, queryAnalyzeMode);
            }

            @Override
            public void close() {
                targetTransactionContext.close();
            }
        };
    } else {
        tx.transactionManager = tx.databaseClient.transactionManager();
        tx.transactionContext = tx.getTransactionManager().begin();
        tx.isReadOnly = false;
    }
    TransactionSynchronizationManager.bindResource(tx.getDatabaseClient(), tx);
}
Also used : KeySet(com.google.cloud.spanner.KeySet) Options(com.google.cloud.spanner.Options) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) Statement(com.google.cloud.spanner.Statement) QueryOption(com.google.cloud.spanner.Options.QueryOption) Struct(com.google.cloud.spanner.Struct) ApiFuture(com.google.api.core.ApiFuture) QueryOption(com.google.cloud.spanner.Options.QueryOption) TransactionContext(com.google.cloud.spanner.TransactionContext) ReadContext(com.google.cloud.spanner.ReadContext) ResultSet(com.google.cloud.spanner.ResultSet) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) Mutation(com.google.cloud.spanner.Mutation) ReadOption(com.google.cloud.spanner.Options.ReadOption) ReadOption(com.google.cloud.spanner.Options.ReadOption) Nullable(javax.annotation.Nullable) Key(com.google.cloud.spanner.Key)

Aggregations

ResultSet (com.google.cloud.spanner.ResultSet)71 Test (org.junit.Test)25 Statement (com.google.cloud.spanner.Statement)15 Struct (com.google.cloud.spanner.Struct)12 ArrayList (java.util.ArrayList)12 KeySet (com.google.cloud.spanner.KeySet)11 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 ReadContext (com.google.cloud.spanner.ReadContext)10 DatabaseClient (com.google.cloud.spanner.DatabaseClient)8 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)5 Mutation (com.google.cloud.spanner.Mutation)5 Partition (com.google.cloud.spanner.Partition)5 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)5 HashMap (java.util.HashMap)5 Key (com.google.cloud.spanner.Key)4 ReadOption (com.google.cloud.spanner.Options.ReadOption)4 Spanner (com.google.cloud.spanner.Spanner)4 SpannerOptions (com.google.cloud.spanner.SpannerOptions)4 TransactionContext (com.google.cloud.spanner.TransactionContext)4 Joiner (com.google.common.base.Joiner)4