Search in sources :

Example 1 with CloudSpannerResultSet

use of nl.topicus.jdbc.resultset.CloudSpannerResultSet in project spanner-jdbc by olavloite.

the class CloudSpannerStatement method executeQuery.

@Override
public ResultSet executeQuery(String sql) throws SQLException {
    String[] sqlTokens = getTokens(sql);
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null && custom.isQuery()) {
        return custom.executeQuery(sqlTokens);
    }
    try (ReadContext context = getReadContext()) {
        com.google.cloud.spanner.ResultSet rs = context.executeQuery(com.google.cloud.spanner.Statement.of(sql));
        return new CloudSpannerResultSet(this, rs);
    }
}
Also used : ReadContext(com.google.cloud.spanner.ReadContext) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 2 with CloudSpannerResultSet

use of nl.topicus.jdbc.resultset.CloudSpannerResultSet in project spanner-jdbc by olavloite.

the class CloudSpannerParameterMetaDataTest method createStatement.

private CloudSpannerPreparedStatement createStatement(String sql) throws SQLException {
    CloudSpannerConnection connection = mock(CloudSpannerConnection.class);
    CloudSpannerDatabaseMetaData metadata = mock(CloudSpannerDatabaseMetaData.class);
    CloudSpannerResultSet columns = mock(CloudSpannerResultSet.class);
    TableKeyMetaData table = mock(TableKeyMetaData.class);
    when(table.getKeyColumns()).thenReturn(Arrays.asList("COL1"));
    when(connection.getTable("FOO")).thenReturn(table);
    when(connection.getMetaData()).thenAnswer(new Returns(metadata));
    when(metadata.getColumns(null, null, "FOO", null)).thenReturn(columns);
    final ColumnsNextAnswer next = new ColumnsNextAnswer();
    when(columns.next()).thenAnswer(next);
    when(columns.getString("COLUMN_NAME")).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList("COL1", "COL2", "COL3").get(next.nextCalled - 1);
        }
    });
    when(columns.getInt("COLUMN_SIZE")).thenAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList(8, 50, 100).get(next.nextCalled - 1);
        }
    });
    when(columns.getInt("DATA_TYPE")).thenAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList(Types.BIGINT, Types.NVARCHAR, Types.NVARCHAR).get(next.nextCalled - 1);
        }
    });
    when(columns.getInt("NULLABLE")).thenAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList(ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNullable).get(next.nextCalled - 1);
        }
    });
    return new CloudSpannerPreparedStatement(sql, connection, null);
}
Also used : CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 3 with CloudSpannerResultSet

use of nl.topicus.jdbc.resultset.CloudSpannerResultSet in project spanner-jdbc by olavloite.

the class UpdateModeTest method mockConnection.

private static void mockConnection(CloudSpannerConnection connection) throws SQLException {
    CloudSpannerPreparedStatement insertStatement = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerPreparedStatement selectStatement = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerPreparedStatement countStatement = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet selectResult = Mockito.mock(CloudSpannerResultSet.class);
    CloudSpannerResultSet countResult = Mockito.mock(CloudSpannerResultSet.class);
    CloudSpannerResultSetMetaData selectMetadata = Mockito.mock(CloudSpannerResultSetMetaData.class);
    Mockito.when(selectMetadata.getColumnCount()).thenReturn(3);
    Mockito.when(connection.prepareStatement(Mockito.startsWith("INSERT"))).thenReturn(insertStatement);
    Mockito.when(connection.prepareStatement(Mockito.startsWith("SELECT `FOO`"))).thenReturn(selectStatement);
    Mockito.when(connection.prepareStatement(Mockito.startsWith("SELECT COUNT(*)"))).thenReturn(countStatement);
    Mockito.when(selectStatement.executeQuery()).thenReturn(selectResult);
    Mockito.when(selectResult.next()).thenReturn(true, true, false);
    Mockito.when(selectResult.getObject(1)).thenReturn(1L, 2L);
    Mockito.when(selectResult.getObject(2)).thenReturn("One", "Two");
    Mockito.when(selectResult.getObject(3)).thenReturn("En", "To");
    Mockito.when(selectResult.getMetaData()).thenReturn(selectMetadata);
    Mockito.when(countStatement.executeQuery()).thenReturn(countResult);
    Mockito.when(countResult.next()).thenReturn(true, false);
    Mockito.when(countResult.getLong(1)).thenReturn(2L);
    Mockito.when(insertStatement.executeUpdate()).thenReturn(1, 1);
}
Also used : CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) CloudSpannerResultSetMetaData(nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData)

Example 4 with CloudSpannerResultSet

use of nl.topicus.jdbc.resultset.CloudSpannerResultSet in project spanner-jdbc by olavloite.

the class CloudSpannerTestObjects method createMetaData.

private static CloudSpannerDatabaseMetaData createMetaData() throws SQLException {
    CloudSpannerDatabaseMetaData metadata = Mockito.mock(CloudSpannerDatabaseMetaData.class);
    Mockito.when(metadata.getPrimaryKeys(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet primaryKeyFoo = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(primaryKeyFoo.next()).thenReturn(true, false);
            Mockito.when(primaryKeyFoo.getString("COLUMN_NAME")).thenReturn("ID");
            return primaryKeyFoo;
        }
    });
    Mockito.when(metadata.getPrimaryKeys(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("BAR", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet primaryKeyBar = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(primaryKeyBar.next()).thenReturn(true, true, false);
            Mockito.when(primaryKeyBar.getString("COLUMN_NAME")).thenReturn("ID1", "ID2");
            return primaryKeyBar;
        }
    });
    Mockito.when(metadata.getColumns(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)), Mockito.any())).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet fooColumns = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(fooColumns.next()).thenReturn(true, true, true, false);
            Mockito.when(fooColumns.getString("COLUMN_NAME")).thenReturn("ID", "COL1", "COL2");
            Mockito.when(fooColumns.getInt("COLUMN_SIZE")).thenReturn(8, 50, 100);
            Mockito.when(fooColumns.getInt("DATA_TYPE")).thenReturn(Types.BIGINT, Types.NVARCHAR, Types.NVARCHAR);
            Mockito.when(fooColumns.getInt("NULLABLE")).thenReturn(ResultSetMetaData.columnNoNulls, ResultSetMetaData.columnNoNulls, ResultSetMetaData.columnNullable);
            return fooColumns;
        }
    });
    Mockito.when(metadata.getIndexInfo(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)), Mockito.anyBoolean(), Mockito.anyBoolean())).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet indices = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(indices.next()).thenReturn(true, false);
            return indices;
        }
    });
    return metadata;
}
Also used : CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) ResultSet(java.sql.ResultSet) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 5 with CloudSpannerResultSet

use of nl.topicus.jdbc.resultset.CloudSpannerResultSet 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)

Aggregations

CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)8 Returns (org.mockito.internal.stubbing.answers.Returns)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ReadContext (com.google.cloud.spanner.ReadContext)2 SQLException (java.sql.SQLException)2 CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)2 CloudSpannerDatabaseMetaData (nl.topicus.jdbc.CloudSpannerDatabaseMetaData)2 CloudSpannerResultSetMetaData (nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData)2 ByteArray (com.google.cloud.ByteArray)1 ResultSet (com.google.cloud.spanner.ResultSet)1 Field (java.lang.reflect.Field)1 Array (java.sql.Array)1 ResultSet (java.sql.ResultSet)1 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 TokenMgrError (net.sf.jsqlparser.parser.TokenMgrError)1 Statement (net.sf.jsqlparser.statement.Statement)1 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)1 Select (net.sf.jsqlparser.statement.select.Select)1 SubSelect (net.sf.jsqlparser.statement.select.SubSelect)1 TableKeyMetaData (nl.topicus.jdbc.MetaDataStore.TableKeyMetaData)1