Search in sources :

Example 6 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project java-spanner-jdbc by googleapis.

the class JdbcStatementTest method testQueryTimeout.

@Test
public void testQueryTimeout() throws SQLException {
    final String select = "SELECT 1";
    JdbcConnection connection = mock(JdbcConnection.class);
    when(connection.getDialect()).thenReturn(dialect);
    Connection spanner = mock(Connection.class);
    when(connection.getSpannerConnection()).thenReturn(spanner);
    StatementResult result = mock(StatementResult.class);
    when(result.getResultType()).thenReturn(ResultType.RESULT_SET);
    when(result.getResultSet()).thenReturn(mock(com.google.cloud.spanner.ResultSet.class));
    when(spanner.execute(com.google.cloud.spanner.Statement.of(select))).thenReturn(result);
    try (Statement statement = new JdbcStatement(connection)) {
        assertThat(statement.getQueryTimeout()).isEqualTo(0);
        statement.setQueryTimeout(1);
        assertThat(statement.getQueryTimeout()).isEqualTo(1);
        statement.setQueryTimeout(99);
        assertThat(statement.getQueryTimeout()).isEqualTo(99);
        statement.setQueryTimeout(0);
        assertThat(statement.getQueryTimeout()).isEqualTo(0);
    }
    when(spanner.getStatementTimeout(TimeUnit.SECONDS)).thenReturn(1L);
    when(spanner.getStatementTimeout(TimeUnit.MILLISECONDS)).thenReturn(1000L);
    when(spanner.getStatementTimeout(TimeUnit.MICROSECONDS)).thenReturn(1000000L);
    when(spanner.getStatementTimeout(TimeUnit.NANOSECONDS)).thenReturn(1000000000L);
    when(spanner.hasStatementTimeout()).thenReturn(true);
    try (Statement statement = new JdbcStatement(connection)) {
        assertThat(statement.getQueryTimeout()).isEqualTo(0);
        statement.execute(select);
        // statement has no timeout, so it should also not be set on the connection
        verify(spanner, never()).setStatementTimeout(1L, TimeUnit.SECONDS);
    }
    try (Statement statement = new JdbcStatement(connection)) {
        // now set a query timeout that should temporarily applied to the connection
        statement.setQueryTimeout(2);
        statement.execute(select);
        // assert that it is temporarily set to 2 seconds, and then back to the original 1 second
        // value
        verify(spanner).setStatementTimeout(2L, TimeUnit.SECONDS);
        verify(spanner).setStatementTimeout(1L, TimeUnit.SECONDS);
    }
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) Statement(java.sql.Statement) Connection(com.google.cloud.spanner.connection.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 7 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project java-spanner-jdbc by googleapis.

the class JdbcStatement method executeLargeUpdate.

/**
 * @see java.sql.Statement#executeLargeUpdate(String)
 *     <p>This method allows both DML and DDL statements to be executed. It assumes that the user
 *     knows what kind of statement is being executed, and the method will therefore return 0 for
 *     both DML statements that changed 0 rows as well as for all DDL statements.
 */
@Override
public long executeLargeUpdate(String sql) throws SQLException {
    checkClosed();
    Statement statement = Statement.of(sql);
    StatementResult result = execute(statement);
    switch(result.getResultType()) {
        case RESULT_SET:
            throw JdbcSqlExceptionFactory.of("The statement is not an update or DDL statement", Code.INVALID_ARGUMENT);
        case UPDATE_COUNT:
            return result.getUpdateCount();
        case NO_RESULT:
            return 0L;
        default:
            throw JdbcSqlExceptionFactory.of("unknown result: " + result.getResultType(), Code.FAILED_PRECONDITION);
    }
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) Statement(com.google.cloud.spanner.Statement)

Example 8 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project java-spanner-jdbc by googleapis.

the class JdbcStatement method executeStatement.

boolean executeStatement(Statement statement) throws SQLException {
    StatementResult result = execute(statement);
    switch(result.getResultType()) {
        case RESULT_SET:
            currentResultSet = JdbcResultSet.of(this, result.getResultSet());
            currentUpdateCount = JdbcConstants.STATEMENT_RESULT_SET;
            return true;
        case UPDATE_COUNT:
            currentResultSet = null;
            currentUpdateCount = result.getUpdateCount();
            return false;
        case NO_RESULT:
            currentResultSet = null;
            currentUpdateCount = JdbcConstants.STATEMENT_NO_RESULT;
            return false;
        default:
            throw JdbcSqlExceptionFactory.of("unknown result: " + result.getResultType(), Code.FAILED_PRECONDITION);
    }
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult)

Example 9 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project java-spanner-jdbc by googleapis.

the class AbstractJdbcStatement method execute.

/**
 * Executes a SQL statement on the connection of this {@link Statement}. The SQL statement can be
 * any supported SQL statement, including client side statements such as SET AUTOCOMMIT ON|OFF.
 *
 * @param statement The SQL statement to execute.
 * @return a {@link StatementResult} containing either a {@link ResultSet}, an update count or
 *     nothing depending on the type of SQL statement.
 * @throws SQLException if a database error occurs.
 */
StatementResult execute(com.google.cloud.spanner.Statement statement) throws SQLException {
    StatementTimeout originalTimeout = setTemporaryStatementTimeout();
    boolean mustResetTimeout = false;
    try {
        StatementResult result = connection.getSpannerConnection().execute(statement);
        mustResetTimeout = !resultIsSetStatementTimeout(result);
        if (mustResetTimeout && resultIsShowStatementTimeout(result)) {
            // it was a 'SHOW STATEMENT_TIMEOUT statement, we need to re-run to get the correct value
            mustResetTimeout = false;
            result = rerunShowStatementTimeout(statement, originalTimeout);
        }
        return result;
    } catch (SpannerException e) {
        throw JdbcSqlExceptionFactory.of(e);
    } finally {
        if (mustResetTimeout) {
            resetStatementTimeout(originalTimeout);
        }
    }
}
Also used : StatementResult(com.google.cloud.spanner.connection.StatementResult) SpannerException(com.google.cloud.spanner.SpannerException)

Example 10 with StatementResult

use of com.google.cloud.spanner.connection.StatementResult in project java-spanner-jdbc by googleapis.

the class JdbcStatementTest method createStatement.

@SuppressWarnings("unchecked")
private JdbcStatement createStatement() throws SQLException {
    Connection spanner = mock(Connection.class);
    when(spanner.getDialect()).thenReturn(dialect);
    com.google.cloud.spanner.ResultSet resultSet = mock(com.google.cloud.spanner.ResultSet.class);
    when(resultSet.next()).thenReturn(true, false);
    when(resultSet.getColumnType(0)).thenReturn(Type.int64());
    when(resultSet.getLong(0)).thenReturn(1L);
    StatementResult selectResult = mock(StatementResult.class);
    when(selectResult.getResultType()).thenReturn(ResultType.RESULT_SET);
    when(selectResult.getResultSet()).thenReturn(resultSet);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(SELECT))).thenReturn(selectResult);
    StatementResult updateResult = mock(StatementResult.class);
    when(updateResult.getResultType()).thenReturn(ResultType.UPDATE_COUNT);
    when(updateResult.getUpdateCount()).thenReturn(1L);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(UPDATE))).thenReturn(updateResult);
    StatementResult largeUpdateResult = mock(StatementResult.class);
    when(largeUpdateResult.getResultType()).thenReturn(ResultType.UPDATE_COUNT);
    when(largeUpdateResult.getUpdateCount()).thenReturn(Integer.MAX_VALUE + 1L);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(LARGE_UPDATE))).thenReturn(largeUpdateResult);
    StatementResult ddlResult = mock(StatementResult.class);
    when(ddlResult.getResultType()).thenReturn(ResultType.NO_RESULT);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(DDL))).thenReturn(ddlResult);
    when(spanner.executeQuery(com.google.cloud.spanner.Statement.of(SELECT))).thenReturn(resultSet);
    when(spanner.executeQuery(com.google.cloud.spanner.Statement.of(UPDATE))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not a query"));
    when(spanner.executeQuery(com.google.cloud.spanner.Statement.of(DDL))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not a query"));
    when(spanner.executeUpdate(com.google.cloud.spanner.Statement.of(UPDATE))).thenReturn(1L);
    when(spanner.executeUpdate(com.google.cloud.spanner.Statement.of(SELECT))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not an update"));
    when(spanner.executeUpdate(com.google.cloud.spanner.Statement.of(DDL))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not an update"));
    when(spanner.executeBatchUpdate(anyList())).thenAnswer((Answer<long[]>) invocation -> {
        List<com.google.cloud.spanner.Statement> statements = (List<com.google.cloud.spanner.Statement>) invocation.getArguments()[0];
        if (statements.isEmpty() || AbstractStatementParser.getInstance(dialect).isDdlStatement(statements.get(0).getSql())) {
            return new long[0];
        }
        long[] res = new long[((List<com.google.cloud.spanner.Statement>) invocation.getArguments()[0]).size()];
        Arrays.fill(res, 1L);
        return res;
    });
    JdbcConnection connection = mock(JdbcConnection.class);
    when(connection.getDialect()).thenReturn(dialect);
    when(connection.getParser()).thenReturn(AbstractStatementParser.getInstance(dialect));
    when(connection.getSpannerConnection()).thenReturn(spanner);
    return new JdbcStatement(connection);
}
Also used : Arrays(java.util.Arrays) Dialect(com.google.cloud.spanner.Dialect) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) SpannerExceptionFactory(com.google.cloud.spanner.SpannerExceptionFactory) Connection(com.google.cloud.spanner.connection.Connection) Answer(org.mockito.stubbing.Answer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Assert.fail(org.junit.Assert.fail) AbstractStatementParser(com.google.cloud.spanner.connection.AbstractStatementParser) Code(com.google.rpc.Code) Parameterized(org.junit.runners.Parameterized) Parameter(org.junit.runners.Parameterized.Parameter) Type(com.google.cloud.spanner.Type) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) ErrorCode(com.google.cloud.spanner.ErrorCode) TimeUnit(java.util.concurrent.TimeUnit) JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Mockito.anyList(org.mockito.Mockito.anyList) ResultType(com.google.cloud.spanner.connection.StatementResult.ResultType) StatementResult(com.google.cloud.spanner.connection.StatementResult) Statement(java.sql.Statement) Mockito.mock(org.mockito.Mockito.mock) StatementResult(com.google.cloud.spanner.connection.StatementResult) Statement(java.sql.Statement) Connection(com.google.cloud.spanner.connection.Connection) List(java.util.List) Mockito.anyList(org.mockito.Mockito.anyList)

Aggregations

StatementResult (com.google.cloud.spanner.connection.StatementResult)10 Test (org.junit.Test)5 SpannerException (com.google.cloud.spanner.SpannerException)3 OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)3 Connection (com.google.cloud.spanner.connection.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 Dialect (com.google.cloud.spanner.Dialect)1 ErrorCode (com.google.cloud.spanner.ErrorCode)1 ResultSet (com.google.cloud.spanner.ResultSet)1 SpannerExceptionFactory (com.google.cloud.spanner.SpannerExceptionFactory)1 Statement (com.google.cloud.spanner.Statement)1 Type (com.google.cloud.spanner.Type)1 AbstractStatementParser (com.google.cloud.spanner.connection.AbstractStatementParser)1 ResultType (com.google.cloud.spanner.connection.StatementResult.ResultType)1 JdbcSqlExceptionImpl (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Code (com.google.rpc.Code)1 SQLException (java.sql.SQLException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1