Search in sources :

Example 36 with ResultSet

use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.

the class ITReadOnlyTxnTest method queryRow.

private static Struct queryRow(ReadContext ctx) {
    ResultSet resultSet = Statement.of("SELECT StringValue FROM TestTable").executeQuery(ctx);
    // TODO(user): Consider a library routine to consume a single row from a query.
    assertThat(resultSet.next()).isTrue();
    Struct row = resultSet.getCurrentRowAsStruct();
    assertThat(resultSet.next()).isFalse();
    return row;
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) Struct(com.google.cloud.spanner.Struct)

Example 37 with ResultSet

use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.

the class ITReadTest method emptyRead.

@Test
public void emptyRead() {
    ResultSet resultSet = client.singleUse(TimestampBound.strong()).read(TABLE_NAME, KeySet.range(KeyRange.closedOpen(Key.of("k99"), Key.of("z"))), ALL_COLUMNS);
    assertThat(resultSet.next()).isFalse();
    assertThat(resultSet.getType()).isEqualTo(TABLE_TYPE);
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 38 with ResultSet

use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.

the class ITReadTest method checkReadRange.

private void checkReadRange(Source source, KeySet keySet, long limit, int[] expectedRows) {
    Map<String, String> expected = new LinkedHashMap<>();
    for (int expectedRow : expectedRows) {
        expected.put("k" + expectedRow, "v" + expectedRow);
    }
    ResultSet resultSet;
    switch(source) {
        case INDEX:
            resultSet = limit != 0 ? client.singleUse(TimestampBound.strong()).readUsingIndex(TABLE_NAME, INDEX_NAME, keySet, ALL_COLUMNS, Options.limit(limit)) : client.singleUse(TimestampBound.strong()).readUsingIndex(TABLE_NAME, INDEX_NAME, keySet, ALL_COLUMNS);
            break;
        case DESC_INDEX:
            resultSet = limit != 0 ? client.singleUse(TimestampBound.strong()).readUsingIndex(TABLE_NAME, DESC_INDEX_NAME, keySet, ALL_COLUMNS, Options.limit(limit)) : client.singleUse(TimestampBound.strong()).readUsingIndex(TABLE_NAME, DESC_INDEX_NAME, keySet, ALL_COLUMNS);
            break;
        case BASE_TABLE:
            resultSet = limit != 0 ? client.singleUse(TimestampBound.strong()).read(TABLE_NAME, keySet, ALL_COLUMNS, Options.limit(limit)) : client.singleUse(TimestampBound.strong()).read(TABLE_NAME, keySet, ALL_COLUMNS);
            break;
        default:
            throw new IllegalArgumentException("Invalid source");
    }
    Map<String, String> rows = new LinkedHashMap<>();
    while (resultSet.next()) {
        rows.put(resultSet.getString(0), resultSet.getString(1));
    }
    assertThat(rows).named("read of " + keySet).isEqualTo(expected);
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) LinkedHashMap(java.util.LinkedHashMap)

Example 39 with ResultSet

use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.

the class ITReadTest method indexEmptyRead.

@Test
public void indexEmptyRead() {
    ResultSet resultSet = client.singleUse(TimestampBound.strong()).readUsingIndex(TABLE_NAME, INDEX_NAME, KeySet.range(KeyRange.closedOpen(Key.of("v99"), Key.of("z"))), ALL_COLUMNS);
    assertThat(resultSet.next()).isFalse();
    assertThat(resultSet.getType()).isEqualTo(TABLE_TYPE);
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 40 with ResultSet

use of com.google.cloud.spanner.ResultSet in project google-cloud-java by GoogleCloudPlatform.

the class ITReadTest method cursorErrorDeferred.

@Test
public void cursorErrorDeferred() {
    // Error should be deferred until next().  This gives consistent behavior with respect to
    // non-blocking implementations (e.g., gRPC).
    ResultSet resultSet = client.singleUse(TimestampBound.strong()).read("BadTableName", KeySet.singleKey(Key.of("k1")), ALL_COLUMNS);
    expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
    expectedException.expectMessage("BadTableName");
    resultSet.next();
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

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