use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadOnlyTransactionTest method testProfileQuery.
@Test
public void testProfileQuery() {
for (TimestampBound staleness : getTestTimestampBounds()) {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
when(parsedStatement.isQuery()).thenReturn(true);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
when(parsedStatement.getSqlWithoutComments()).thenReturn(statement.getSql());
ReadOnlyTransaction transaction = createSubject(staleness);
ResultSet rs = get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.PROFILE));
assertThat(rs, is(notNullValue()));
// get all results and then get the stats
while (rs.next()) {
// do nothing
}
assertThat(rs.getStats(), is(notNullValue()));
}
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadOnlyTransactionTest method testState.
@Test
public void testState() {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
when(parsedStatement.isQuery()).thenReturn(true);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
when(parsedStatement.getSqlWithoutComments()).thenReturn(statement.getSql());
ReadOnlyTransaction transaction = createSubject();
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
get(transaction.commitAsync());
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.COMMITTED)));
assertThat(transaction.isActive(), is(false));
transaction = createSubject();
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE)), is(notNullValue()));
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
get(transaction.commitAsync());
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.COMMITTED)));
assertThat(transaction.isActive(), is(false));
// start a new transaction
transaction = createSubject();
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
get(transaction.rollbackAsync());
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.ROLLED_BACK)));
assertThat(transaction.isActive(), is(false));
transaction = createSubject();
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE)), is(notNullValue()));
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
get(transaction.rollbackAsync());
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.ROLLED_BACK)));
assertThat(transaction.isActive(), is(false));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testProfileQuery.
@Test
public void testProfileQuery() {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
when(parsedStatement.isQuery()).thenReturn(true);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
ReadWriteTransaction transaction = createSubject();
ResultSet rs = get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.PROFILE));
assertThat(rs, is(notNullValue()));
while (rs.next()) {
// do nothing
}
assertThat(rs.getStats(), is(notNullValue()));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testChecksumResultSetWithArray.
@Test
public void testChecksumResultSetWithArray() {
DatabaseClient client = mock(DatabaseClient.class);
ReadWriteTransaction transaction = ReadWriteTransaction.newBuilder().setRetryAbortsInternally(true).setTransactionRetryListeners(Collections.emptyList()).setDatabaseClient(client).withStatementExecutor(new StatementExecutor()).build();
ParsedStatement parsedStatement = mock(ParsedStatement.class);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
ResultSet delegate1 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("PRICES", Type.array(Type.int64()))), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("PRICES").toInt64Array(new long[] { 1L, 2L }).build(), Struct.newBuilder().set("ID").to(2L).set("PRICES").toInt64Array(new long[] { 3L, 4L }).build()));
ChecksumResultSet rs1 = transaction.createChecksumResultSet(delegate1, parsedStatement, AnalyzeMode.NONE);
ResultSet delegate2 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("PRICES", Type.array(Type.int64()))), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("PRICES").toInt64Array(new long[] { 1L, 2L }).build(), Struct.newBuilder().set("ID").to(2L).set("PRICES").toInt64Array(new long[] { 3L, 5L }).build()));
ChecksumResultSet rs2 = transaction.createChecksumResultSet(delegate2, parsedStatement, AnalyzeMode.NONE);
rs1.next();
rs2.next();
assertThat(rs1.getChecksum(), is(equalTo(rs2.getChecksum())));
rs1.next();
rs2.next();
assertThat(rs1.getChecksum(), is(not(equalTo(rs2.getChecksum()))));
}
use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.
the class ReadWriteTransactionTest method testState.
@Test
public void testState() {
ParsedStatement parsedStatement = mock(ParsedStatement.class);
when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
when(parsedStatement.isQuery()).thenReturn(true);
Statement statement = Statement.of("SELECT * FROM FOO");
when(parsedStatement.getStatement()).thenReturn(statement);
ReadWriteTransaction transaction = createSubject();
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE)), is(notNullValue()));
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
get(transaction.commitAsync());
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.COMMITTED)));
assertThat(transaction.isActive(), is(false));
// start a new transaction
transaction = createSubject();
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
get(transaction.rollbackAsync());
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.ROLLED_BACK)));
assertThat(transaction.isActive(), is(false));
// start a new transaction that will fail on commit
transaction = createSubject(CommitBehavior.FAIL);
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
try {
get(transaction.commitAsync());
} catch (SpannerException e) {
// ignore
}
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.COMMIT_FAILED)));
assertThat(transaction.isActive(), is(false));
// start a new transaction that will abort on commit
transaction = createSubject(CommitBehavior.ABORT);
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
try {
get(transaction.commitAsync());
} catch (AbortedException e) {
// ignore
}
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.COMMIT_FAILED)));
assertThat(transaction.isActive(), is(false));
// Start a new transaction that will abort on commit, but with internal retry enabled, so it
// will in the end succeed.
transaction = createSubject(CommitBehavior.ABORT, true);
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.STARTED)));
assertThat(transaction.isActive(), is(true));
get(transaction.commitAsync());
assertThat(transaction.getState(), is(equalTo(com.google.cloud.spanner.connection.UnitOfWork.UnitOfWorkState.COMMITTED)));
assertThat(transaction.isActive(), is(false));
}
Aggregations