Search in sources :

Example 6 with CloudSpannerResultSet

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

the class InsertWorkerTest method createMocks.

private void createMocks(CloudSpannerConnection connection, String selectSQL, long count, String updateSQL, boolean throwExceptionOnUpdate) throws SQLException {
    when(connection.createCopyConnection()).thenAnswer(new Answer<CloudSpannerConnection>() {

        @Override
        public CloudSpannerConnection answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerConnection copy = CloudSpannerTestObjects.createConnection();
            createMocks(copy, selectSQL, count, updateSQL);
            return copy;
        }
    });
    CloudSpannerPreparedStatement countStatement = mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet countResultSet = mock(CloudSpannerResultSet.class);
    when(countResultSet.next()).thenReturn(true, false);
    when(countResultSet.getLong(1)).thenReturn(count);
    when(countStatement.executeQuery()).thenReturn(countResultSet);
    when(connection.prepareStatement("SELECT COUNT(*) AS C FROM (" + selectSQL + ") Q")).thenReturn(countStatement);
    CloudSpannerPreparedStatement selectStatement = mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet selectResultSet = mock(CloudSpannerResultSet.class);
    CloudSpannerResultSetMetaData metadata = mock(CloudSpannerResultSetMetaData.class);
    when(metadata.getColumnCount()).thenReturn(3);
    when(selectResultSet.next()).then(new Answer<Boolean>() {

        private long called = 0;

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            called++;
            if (called <= count)
                return true;
            return false;
        }
    });
    when(selectResultSet.getObject(1)).then(new Returns(1L));
    when(selectResultSet.getObject(2)).then(new Returns("TWO"));
    when(selectResultSet.getObject(3)).then(new Returns("TO"));
    when(selectResultSet.getMetaData()).thenReturn(metadata);
    when(selectStatement.executeQuery()).thenReturn(selectResultSet);
    when(connection.prepareStatement(selectSQL)).thenReturn(selectStatement);
    CloudSpannerPreparedStatement updateStatement = mock(CloudSpannerPreparedStatement.class);
    if (throwExceptionOnUpdate)
        when(updateStatement.executeUpdate()).thenThrow(SQLException.class);
    else
        when(updateStatement.executeUpdate()).thenReturn(1);
    when(connection.prepareStatement(updateSQL)).thenReturn(updateStatement);
}
Also used : SQLException(java.sql.SQLException) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) CloudSpannerResultSetMetaData(nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData)

Example 7 with CloudSpannerResultSet

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

the class CloudSpannerPreparedStatement method executeQuery.

@Override
public ResultSet executeQuery() throws SQLException {
    CustomDriverStatement custom = getCustomDriverStatement(sqlTokens);
    if (custom != null && custom.isQuery()) {
        return custom.executeQuery(sqlTokens);
    }
    Statement statement;
    try {
        statement = CCJSqlParserUtil.parse(sanitizeSQL(sql));
    } catch (JSQLParserException | TokenMgrError e) {
        throw new CloudSpannerSQLException(PARSE_ERROR + sql + ": " + e.getLocalizedMessage(), Code.INVALID_ARGUMENT, e);
    }
    if (statement instanceof Select) {
        determineForceSingleUseReadContext((Select) statement);
        com.google.cloud.spanner.Statement.Builder builder = createSelectBuilder(statement, sql);
        try (ReadContext context = getReadContext()) {
            com.google.cloud.spanner.ResultSet rs = context.executeQuery(builder.build());
            return new CloudSpannerResultSet(this, rs);
        }
    }
    throw new CloudSpannerSQLException("SQL statement not suitable for executeQuery. Expected SELECT-statement.", Code.INVALID_ARGUMENT);
}
Also used : Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) TokenMgrError(net.sf.jsqlparser.parser.TokenMgrError) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) ReadContext(com.google.cloud.spanner.ReadContext) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Select(net.sf.jsqlparser.statement.select.Select) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 8 with CloudSpannerResultSet

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

the class CloudSpannerTestObjects method mockXAMethods.

private static void mockXAMethods(CloudSpannerConnection connection) throws SQLException {
    String checkTable = null;
    try {
        Field field = CloudSpannerXAConnection.class.getDeclaredField("CHECK_TABLE_EXISTENCE");
        field.setAccessible(true);
        checkTable = (String) field.get(null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    CloudSpannerPreparedStatement ps = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet rs = Mockito.mock(CloudSpannerResultSet.class);
    Mockito.when(rs.next()).thenReturn(true, false);
    Mockito.when(connection.prepareStatement(checkTable)).thenAnswer(new Returns(ps));
    Mockito.when(ps.executeQuery()).thenAnswer(new Returns(rs));
}
Also used : Field(java.lang.reflect.Field) Returns(org.mockito.internal.stubbing.answers.Returns) CloudSpannerPreparedStatement(nl.topicus.jdbc.statement.CloudSpannerPreparedStatement) SQLException(java.sql.SQLException) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

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