Search in sources :

Example 11 with ParsedStatement

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()));
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) TimestampBound(com.google.cloud.spanner.TimestampBound) ResultSet(com.google.cloud.spanner.ResultSet) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 12 with ParsedStatement

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));
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 13 with ParsedStatement

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()));
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 14 with ParsedStatement

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()))));
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 15 with ParsedStatement

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));
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) AbortedException(com.google.cloud.spanner.AbortedException) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Aggregations

ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)75 Test (org.junit.Test)61 Statement (com.google.cloud.spanner.Statement)23 SpannerException (com.google.cloud.spanner.SpannerException)20 ResultSet (com.google.cloud.spanner.ResultSet)12 AsyncResultSet (com.google.cloud.spanner.AsyncResultSet)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)6 TimestampBound (com.google.cloud.spanner.TimestampBound)6 LinkedList (java.util.LinkedList)3 Mockito.anyString (org.mockito.Mockito.anyString)3 AbortedException (com.google.cloud.spanner.AbortedException)2 QueryOption (com.google.cloud.spanner.Options.QueryOption)2 Mutation (com.google.cloud.spanner.Mutation)1 ReadContext (com.google.cloud.spanner.ReadContext)1 TransactionContext (com.google.cloud.spanner.TransactionContext)1 TransactionManager (com.google.cloud.spanner.TransactionManager)1 ImmutableList (com.google.common.collect.ImmutableList)1 Duration (com.google.protobuf.Duration)1 BigDecimal (java.math.BigDecimal)1 TimeUnit (java.util.concurrent.TimeUnit)1