Search in sources :

Example 21 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.

the class XATransaction method commitPrepared.

static void commitPrepared(TransactionContext transaction, String xid) throws SQLException {
    try (ResultSet rs = transaction.executeQuery(getPreparedMutationsStatement(xid))) {
        while (rs.next()) {
            String serialized = rs.getString(1);
            Mutation mutation = deserializeMutation(serialized);
            transaction.buffer(mutation);
        }
    }
    cleanupPrepared(transaction, xid);
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet) Mutation(com.google.cloud.spanner.Mutation)

Example 22 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.

the class CloudSpannerDataTypeTest method testGetArrayElements.

@Test
public void testGetArrayElements() throws SQLException {
    ResultSet googleResultSet = Mockito.mock(ResultSet.class);
    when(googleResultSet.getBooleanList(0)).thenReturn(Arrays.asList(true, true, false));
    when(googleResultSet.getBytesList(0)).thenReturn(Arrays.asList(ByteArray.copyFrom("foo"), ByteArray.copyFrom("bar")));
    when(googleResultSet.getDateList(0)).thenReturn(Arrays.asList(com.google.cloud.Date.fromYearMonthDay(2017, 10, 1), com.google.cloud.Date.fromYearMonthDay(2017, 9, 1)));
    when(googleResultSet.getDoubleList(0)).thenReturn(Arrays.asList(1d, 2d, 3d));
    when(googleResultSet.getLongList(0)).thenReturn(Arrays.asList(1l, 2l, 3l));
    when(googleResultSet.getStringList(0)).thenReturn(Arrays.asList("foo", "bar"));
    when(googleResultSet.getTimestampList(0)).thenReturn(Arrays.asList(com.google.cloud.Timestamp.now(), com.google.cloud.Timestamp.now()));
    when(googleResultSet.next()).thenReturn(true);
    try (CloudSpannerResultSet rs = new CloudSpannerResultSet(Mockito.mock(CloudSpannerStatement.class), googleResultSet)) {
        rs.next();
        for (CloudSpannerDataType type : CloudSpannerDataType.values()) {
            when(googleResultSet.getColumnType(0)).thenReturn(Type.array(type.getGoogleType()));
            Array array = rs.getArray(1);
            assertTrue(array.getArray().getClass().isArray());
            assertArrayEquals((Object[]) array.getArray(), (Object[]) ((Array) rs.getObject(1)).getArray());
        }
    }
}
Also used : Array(java.sql.Array) ByteArray(com.google.cloud.ByteArray) CloudSpannerStatement(nl.topicus.jdbc.statement.CloudSpannerStatement) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) ResultSet(com.google.cloud.spanner.ResultSet) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Example 23 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.

the class CloudSpannerResultSetTest method getMockResultSet.

static ResultSet getMockResultSet() {
    ResultSet res = mock(ResultSet.class);
    when(res.getString(STRING_COL_NULL)).thenReturn(null);
    when(res.isNull(STRING_COL_NULL)).thenReturn(true);
    when(res.getString(STRING_COL_NOT_NULL)).thenReturn("FOO");
    when(res.isNull(STRING_COL_NOT_NULL)).thenReturn(false);
    when(res.getString(STRING_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(STRING_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getString(STRING_COLINDEX_NOTNULL - 1)).thenReturn("BAR");
    when(res.isNull(STRING_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(STRING_COL_NULL)).thenReturn(Type.string());
    when(res.getColumnType(STRING_COL_NOT_NULL)).thenReturn(Type.string());
    when(res.getColumnType(STRING_COLINDEX_NULL - 1)).thenReturn(Type.string());
    when(res.getColumnType(STRING_COLINDEX_NOTNULL - 1)).thenReturn(Type.string());
    when(res.getBoolean(BOOLEAN_COL_NULL)).thenReturn(false);
    when(res.isNull(BOOLEAN_COL_NULL)).thenReturn(true);
    when(res.getBoolean(BOOLEAN_COL_NOT_NULL)).thenReturn(true);
    when(res.isNull(BOOLEAN_COL_NOT_NULL)).thenReturn(false);
    when(res.getBoolean(BOOLEAN_COLINDEX_NULL - 1)).thenReturn(false);
    when(res.isNull(BOOLEAN_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getBoolean(BOOLEAN_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.isNull(BOOLEAN_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(BOOLEAN_COL_NULL)).thenReturn(Type.bool());
    when(res.getColumnType(BOOLEAN_COL_NOT_NULL)).thenReturn(Type.bool());
    when(res.getColumnType(BOOLEAN_COLINDEX_NULL - 1)).thenReturn(Type.bool());
    when(res.getColumnType(BOOLEAN_COLINDEX_NOTNULL - 1)).thenReturn(Type.bool());
    when(res.getDouble(DOUBLE_COL_NULL)).thenReturn(0d);
    when(res.isNull(DOUBLE_COL_NULL)).thenReturn(true);
    when(res.getDouble(DOUBLE_COL_NOT_NULL)).thenReturn(1.123456789d);
    when(res.isNull(DOUBLE_COL_NOT_NULL)).thenReturn(false);
    when(res.getDouble(DOUBLE_COLINDEX_NULL - 1)).thenReturn(0d);
    when(res.isNull(DOUBLE_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getDouble(DOUBLE_COLINDEX_NOTNULL - 1)).thenReturn(2.123456789d);
    when(res.isNull(DOUBLE_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(DOUBLE_COL_NULL)).thenReturn(Type.float64());
    when(res.getColumnType(DOUBLE_COL_NOT_NULL)).thenReturn(Type.float64());
    when(res.getColumnType(DOUBLE_COLINDEX_NULL - 1)).thenReturn(Type.float64());
    when(res.getColumnType(DOUBLE_COLINDEX_NOTNULL - 1)).thenReturn(Type.float64());
    when(res.getString(BYTES_COL_NULL)).thenReturn(null);
    when(res.isNull(BYTES_COL_NULL)).thenReturn(true);
    when(res.getBytes(BYTES_COL_NOT_NULL)).thenReturn(ByteArray.copyFrom("FOO"));
    when(res.isNull(BYTES_COL_NOT_NULL)).thenReturn(false);
    when(res.getBytes(BYTES_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(BYTES_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getBytes(BYTES_COLINDEX_NOTNULL - 1)).thenReturn(ByteArray.copyFrom("BAR"));
    when(res.isNull(BYTES_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(BYTES_COL_NULL)).thenReturn(Type.bytes());
    when(res.getColumnType(BYTES_COL_NOT_NULL)).thenReturn(Type.bytes());
    when(res.getColumnType(BYTES_COLINDEX_NULL - 1)).thenReturn(Type.bytes());
    when(res.getColumnType(BYTES_COLINDEX_NOTNULL - 1)).thenReturn(Type.bytes());
    when(res.getLong(LONG_COL_NULL)).thenReturn(0l);
    when(res.isNull(LONG_COL_NULL)).thenReturn(true);
    when(res.getLong(LONG_COL_NOT_NULL)).thenReturn(1l);
    when(res.isNull(LONG_COL_NOT_NULL)).thenReturn(false);
    when(res.getLong(LONG_COLINDEX_NULL - 1)).thenReturn(0l);
    when(res.isNull(LONG_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getLong(LONG_COLINDEX_NOTNULL - 1)).thenReturn(2l);
    when(res.isNull(LONG_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(LONG_COL_NULL)).thenReturn(Type.int64());
    when(res.getColumnType(LONG_COL_NOT_NULL)).thenReturn(Type.int64());
    when(res.getColumnType(LONG_COLINDEX_NULL - 1)).thenReturn(Type.int64());
    when(res.getColumnType(LONG_COLINDEX_NOTNULL - 1)).thenReturn(Type.int64());
    when(res.getDate(DATE_COL_NULL)).thenAnswer(new Returns(null));
    when(res.isNull(DATE_COL_NULL)).thenAnswer(new Returns(true));
    when(res.getDate(DATE_COL_NOT_NULL)).thenAnswer(new Returns(Date.fromYearMonthDay(2017, 9, 10)));
    when(res.isNull(DATE_COL_NOT_NULL)).thenAnswer(new Returns(false));
    when(res.getDate(DATE_COLINDEX_NULL - 1)).thenAnswer(new Returns(null));
    when(res.isNull(DATE_COLINDEX_NULL - 1)).thenAnswer(new Returns(true));
    when(res.getDate(DATE_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Date.fromYearMonthDay(2017, 9, 10)));
    when(res.isNull(DATE_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(false));
    when(res.getColumnType(DATE_COL_NULL)).thenAnswer(new Returns(Type.date()));
    when(res.getColumnType(DATE_COL_NOT_NULL)).thenAnswer(new Returns(Type.date()));
    when(res.getColumnType(DATE_COLINDEX_NULL - 1)).thenAnswer(new Returns(Type.date()));
    when(res.getColumnType(DATE_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Type.date()));
    Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal1.clear();
    cal1.set(2017, 8, 10, 8, 15, 59);
    Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal2.clear();
    cal2.set(2017, 8, 11, 8, 15, 59);
    when(res.getTimestamp(TIMESTAMP_COL_NULL)).thenReturn(null);
    when(res.isNull(TIMESTAMP_COL_NULL)).thenReturn(true);
    when(res.getTimestamp(TIMESTAMP_COL_NOT_NULL)).thenReturn(Timestamp.of(cal1.getTime()));
    when(res.isNull(TIMESTAMP_COL_NOT_NULL)).thenReturn(false);
    when(res.getTimestamp(TIMESTAMP_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(TIMESTAMP_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getTimestamp(TIMESTAMP_COLINDEX_NOTNULL - 1)).thenReturn(Timestamp.of(cal2.getTime()));
    when(res.isNull(TIMESTAMP_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(TIMESTAMP_COL_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIMESTAMP_COL_NOT_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIMESTAMP_COLINDEX_NULL - 1)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIMESTAMP_COLINDEX_NOTNULL - 1)).thenReturn(Type.timestamp());
    Calendar cal3 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal3.clear();
    cal3.set(1970, 0, 1, 14, 6, 15);
    Calendar cal4 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal4.clear();
    cal4.set(1970, 0, 1, 14, 6, 15);
    when(res.getTimestamp(TIME_COL_NULL)).thenReturn(null);
    when(res.isNull(TIME_COL_NULL)).thenReturn(true);
    when(res.getTimestamp(TIME_COL_NOT_NULL)).thenReturn(Timestamp.of(cal3.getTime()));
    when(res.isNull(TIME_COL_NOT_NULL)).thenReturn(false);
    when(res.getTimestamp(TIME_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(TIME_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getTimestamp(TIME_COLINDEX_NOTNULL - 1)).thenReturn(Timestamp.of(cal4.getTime()));
    when(res.isNull(TIME_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(TIME_COL_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIME_COL_NOT_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIME_COLINDEX_NULL - 1)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIME_COLINDEX_NOTNULL - 1)).thenReturn(Type.timestamp());
    when(res.getLongList(ARRAY_COL_NULL)).thenAnswer(new Returns(null));
    when(res.isNull(ARRAY_COL_NULL)).thenAnswer(new Returns(true));
    when(res.getLongList(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(Arrays.asList(1L, 2L, 3L)));
    when(res.isNull(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(false));
    when(res.getLongList(ARRAY_COLINDEX_NULL - 1)).thenAnswer(new Returns(null));
    when(res.isNull(ARRAY_COLINDEX_NULL - 1)).thenAnswer(new Returns(true));
    when(res.getLongList(ARRAY_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Arrays.asList(1L, 2L, 3L)));
    when(res.isNull(ARRAY_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(false));
    when(res.getColumnType(ARRAY_COL_NULL)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnType(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnType(ARRAY_COLINDEX_NULL - 1)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnType(ARRAY_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnIndex(STRING_COL_NOT_NULL)).thenAnswer(new Returns(1));
    when(res.getColumnIndex(UNKNOWN_COLUMN)).thenThrow(IllegalArgumentException.class);
    when(res.getColumnIndex(DATE_COL_NOT_NULL)).thenAnswer(new Returns(DATE_COLINDEX_NOTNULL - 1));
    when(res.getColumnIndex(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(ARRAY_COLINDEX_NOTNULL - 1));
    when(res.getColumnIndex(ARRAY_COL_NULL)).thenAnswer(new Returns(ARRAY_COLINDEX_NULL - 1));
    when(res.getType()).thenReturn(Type.struct(StructField.of(STRING_COL_NULL, Type.string()), StructField.of(STRING_COL_NOT_NULL, Type.string()), StructField.of(BOOLEAN_COL_NULL, Type.bool()), StructField.of(BOOLEAN_COL_NOT_NULL, Type.bool()), StructField.of(DOUBLE_COL_NULL, Type.float64()), StructField.of(DOUBLE_COL_NOT_NULL, Type.float64()), StructField.of(BYTES_COL_NULL, Type.bytes()), StructField.of(BYTES_COL_NOT_NULL, Type.bytes()), StructField.of(LONG_COL_NULL, Type.int64()), StructField.of(LONG_COL_NOT_NULL, Type.int64()), StructField.of(DATE_COL_NULL, Type.date()), StructField.of(DATE_COL_NOT_NULL, Type.date()), StructField.of(TIMESTAMP_COL_NULL, Type.timestamp()), StructField.of(TIMESTAMP_COL_NOT_NULL, Type.timestamp()), StructField.of(TIME_COL_NULL, Type.timestamp()), StructField.of(TIME_COL_NOT_NULL, Type.timestamp())));
    // Next behaviour.
    when(res.next()).thenReturn(true, true, true, true, false);
    return res;
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Calendar(java.util.Calendar) ResultSet(com.google.cloud.spanner.ResultSet)

Example 24 with ResultSet

use of com.google.cloud.spanner.ResultSet in project spanner-jdbc by olavloite.

the class TransactionContextMock method mockPreparedWithMutationsResultSet.

private ResultSet mockPreparedWithMutationsResultSet() throws SQLException {
    List<String> mutations = createSerializedMutations();
    ResultSet rs = mock(ResultSet.class);
    when(rs.next()).thenReturn(true, true, true, false);
    when(rs.getLong(0)).thenReturn(1l, 2l, 3l);
    when(rs.getString(1)).thenReturn(mutations.get(0), mutations.get(1), mutations.get(2));
    return rs;
}
Also used : ResultSet(com.google.cloud.spanner.ResultSet)

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