Search in sources :

Example 11 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 12 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 13 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 14 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 15 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)24 Test (org.junit.Test)12 IntegrationTest (com.google.cloud.spanner.IntegrationTest)10 Statement (com.google.cloud.spanner.Statement)7 KeySet (com.google.cloud.spanner.KeySet)3 Mutation (com.google.cloud.spanner.Mutation)3 DBException (com.yahoo.ycsb.DBException)3 HashMap (java.util.HashMap)3 ByteArray (com.google.cloud.ByteArray)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 Struct (com.google.cloud.spanner.Struct)2 TransactionContext (com.google.cloud.spanner.TransactionContext)2 Joiner (com.google.common.base.Joiner)2 ResultSetStats (com.google.spanner.v1.ResultSetStats)2 ByteIterator (com.yahoo.ycsb.ByteIterator)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Database (com.google.cloud.spanner.Database)1 SpannerException (com.google.cloud.spanner.SpannerException)1