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);
}
}
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);
}
}
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);
}
}
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);
}
}
}
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);
}
Aggregations