Search in sources :

Example 56 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class SingleUseTransactionTest method testExecuteUpdate_Partitioned_Invalid.

@Test
public void testExecuteUpdate_Partitioned_Invalid() {
    ParsedStatement update = createParsedUpdate(INVALID_UPDATE);
    SingleUseTransaction subject = createSubject(AutocommitDmlMode.PARTITIONED_NON_ATOMIC);
    try {
        get(subject.executeUpdateAsync(update));
        fail("missing expected exception");
    } catch (SpannerException e) {
        assertThat(e.getErrorCode()).isEqualTo(ErrorCode.UNKNOWN);
        assertThat(e.getMessage()).contains("invalid update");
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 57 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ReadOnlyTransactionTest method testPlanQuery.

@Test
public void testPlanQuery() {
    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.PLAN));
        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 58 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ReadOnlyTransactionTest method testExecuteQueryWithOptionsTest.

@Test
public void testExecuteQueryWithOptionsTest() {
    String sql = "SELECT * FROM FOO";
    QueryOption option = Options.prefetchChunks(10000);
    ParsedStatement parsedStatement = mock(ParsedStatement.class);
    when(parsedStatement.getType()).thenReturn(StatementType.QUERY);
    when(parsedStatement.isQuery()).thenReturn(true);
    Statement statement = Statement.of(sql);
    when(parsedStatement.getStatement()).thenReturn(statement);
    when(parsedStatement.getSqlWithoutComments()).thenReturn(statement.getSql());
    DatabaseClient client = mock(DatabaseClient.class);
    com.google.cloud.spanner.ReadOnlyTransaction tx = mock(com.google.cloud.spanner.ReadOnlyTransaction.class);
    ResultSet resWithOptions = mock(ResultSet.class);
    ResultSet resWithoutOptions = mock(ResultSet.class);
    when(tx.executeQuery(Statement.of(sql), option)).thenReturn(resWithOptions);
    when(tx.executeQuery(Statement.of(sql))).thenReturn(resWithoutOptions);
    when(client.readOnlyTransaction(TimestampBound.strong())).thenReturn(tx);
    ReadOnlyTransaction transaction = ReadOnlyTransaction.newBuilder().setDatabaseClient(client).setReadOnlyStaleness(TimestampBound.strong()).withStatementExecutor(new StatementExecutor()).build();
    ResultSet expectedWithOptions = DirectExecuteResultSet.ofResultSet(resWithOptions);
    assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE, option)), is(equalTo(expectedWithOptions)));
    ResultSet expectedWithoutOptions = DirectExecuteResultSet.ofResultSet(resWithoutOptions);
    assertThat(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE)), is(equalTo(expectedWithoutOptions)));
}
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) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) QueryOption(com.google.cloud.spanner.Options.QueryOption) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 59 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ReadOnlyTransactionTest method testExecuteDdl.

@Test
public void testExecuteDdl() {
    ParsedStatement ddl = mock(ParsedStatement.class);
    when(ddl.getType()).thenReturn(StatementType.DDL);
    try {
        createSubject().executeDdlAsync(ddl);
        fail("Expected exception");
    } catch (SpannerException ex) {
        assertEquals(ErrorCode.FAILED_PRECONDITION, ex.getErrorCode());
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 60 with ParsedStatement

use of com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement in project java-spanner by googleapis.

the class ReadWriteTransactionTest method testExecuteQuery.

@Test
public void testExecuteQuery() {
    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.NONE));
    assertThat(rs, is(notNullValue()));
    assertThat(rs.getStats(), is(nullValue()));
}
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)

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