Search in sources :

Example 16 with ParsedStatement

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

the class ReadWriteTransactionTest method testChecksumResultSet.

@Test
public void testChecksumResultSet() {
    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);
    String arrayJson = "[{\"color\":\"red\",\"value\":\"#f00\"},{\"color\":\"green\",\"value\":\"#0f0\"},{\"color\":\"blue\",\"value\":\"#00f\"},{\"color\":\"cyan\",\"value\":\"#0ff\"},{\"color\":\"magenta\",\"value\":\"#f0f\"},{\"color\":\"yellow\",\"value\":\"#ff0\"},{\"color\":\"black\",\"value\":\"#000\"}]";
    String emptyArrayJson = "[]";
    String simpleJson = "{\"color\":\"red\",\"value\":\"#f00\"}";
    ResultSet delegate1 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(BigDecimal.valueOf(550, 2)).set("JSON").to(Value.json(simpleJson)).build(), Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(BigDecimal.valueOf(750, 2)).set("JSON").to(Value.json(arrayJson)).build()));
    ChecksumResultSet rs1 = transaction.createChecksumResultSet(delegate1, parsedStatement, AnalyzeMode.NONE);
    ResultSet delegate2 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(new BigDecimal("5.50")).set("JSON").to(Value.json(simpleJson)).build(), Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(new BigDecimal("7.50")).set("JSON").to(Value.json(arrayJson)).build()));
    ChecksumResultSet rs2 = transaction.createChecksumResultSet(delegate2, parsedStatement, AnalyzeMode.NONE);
    // rs1 and rs2 are equal, rs3 contains the same rows, but in a different order
    ResultSet delegate3 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(new BigDecimal("7.50")).set("JSON").to(Value.json(arrayJson)).build(), Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(new BigDecimal("5.50")).set("JSON").to(Value.json(simpleJson)).build()));
    ChecksumResultSet rs3 = transaction.createChecksumResultSet(delegate3, parsedStatement, AnalyzeMode.NONE);
    // rs4 contains the same rows as rs1 and rs2, but also an additional row
    ResultSet delegate4 = ResultSets.forRows(Type.struct(StructField.of("ID", Type.int64()), StructField.of("NAME", Type.string()), StructField.of("AMOUNT", Type.numeric()), StructField.of("JSON", Type.json())), Arrays.asList(Struct.newBuilder().set("ID").to(1L).set("NAME").to("TEST 1").set("AMOUNT").to(new BigDecimal("5.50")).set("JSON").to(Value.json(simpleJson)).build(), Struct.newBuilder().set("ID").to(2L).set("NAME").to("TEST 2").set("AMOUNT").to(new BigDecimal("7.50")).set("JSON").to(Value.json(arrayJson)).build(), Struct.newBuilder().set("ID").to(3L).set("NAME").to("TEST 3").set("AMOUNT").to(new BigDecimal("9.99")).set("JSON").to(Value.json(emptyArrayJson)).build()));
    ChecksumResultSet rs4 = transaction.createChecksumResultSet(delegate4, parsedStatement, AnalyzeMode.NONE);
    assertThat(rs1.getChecksum(), is(equalTo(rs2.getChecksum())));
    while (rs1.next() && rs2.next() && rs3.next() && rs4.next()) {
        assertThat(rs1.getChecksum(), is(equalTo(rs2.getChecksum())));
        assertThat(rs1.getChecksum(), is(not(equalTo(rs3.getChecksum()))));
        assertThat(rs1.getChecksum(), is(equalTo(rs4.getChecksum())));
    }
    assertThat(rs1.getChecksum(), is(equalTo(rs2.getChecksum())));
    assertThat(rs1.getChecksum(), is(not(equalTo(rs3.getChecksum()))));
    // rs4 contains one more row than rs1, but the last row of rs4 hasn't been consumed yet
    assertThat(rs1.getChecksum(), is(equalTo(rs4.getChecksum())));
    assertThat(rs4.next(), is(true));
    assertThat(rs1.getChecksum(), is(not(equalTo(rs4.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) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 17 with ParsedStatement

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

the class ReadWriteTransactionTest method testGetReadTimestamp.

@Test
public void testGetReadTimestamp() {
    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(get(transaction.executeQueryAsync(parsedStatement, AnalyzeMode.NONE)), is(notNullValue()));
    try {
        transaction.getReadTimestamp();
        fail("Expected exception");
    } catch (SpannerException ex) {
        assertEquals(ErrorCode.FAILED_PRECONDITION, ex.getErrorCode());
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Statement(com.google.cloud.spanner.Statement) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) SpannerException(com.google.cloud.spanner.SpannerException) Test(org.junit.Test)

Example 18 with ParsedStatement

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

the class ReadWriteTransactionTest method testExecuteUpdate.

@Test
public void testExecuteUpdate() {
    ParsedStatement parsedStatement = mock(ParsedStatement.class);
    when(parsedStatement.getType()).thenReturn(StatementType.UPDATE);
    when(parsedStatement.isUpdate()).thenReturn(true);
    Statement statement = Statement.of("UPDATE FOO SET BAR=1 WHERE ID=2");
    when(parsedStatement.getStatement()).thenReturn(statement);
    ReadWriteTransaction transaction = createSubject();
    assertThat(get(transaction.executeUpdateAsync(parsedStatement)), is(1L));
}
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 19 with ParsedStatement

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

the class ReadWriteTransactionTest method testPlanQuery.

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

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

the class ConnectionStatementWithOneParameterTest method testExecuteSetAutocommitDmlMode.

@Test
public void testExecuteSetAutocommitDmlMode() {
    ParsedStatement subject = parser.parse(Statement.of("set autocommit_dml_mode='foo'"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementSetAutocommitDmlMode(any(AutocommitDmlMode.class))).thenCallRealMethod();
    for (AutocommitDmlMode mode : AutocommitDmlMode.values()) {
        subject.getClientSideStatement().execute(executor, String.format("set autocommit_dml_mode='%s'", mode.name()));
        verify(connection, times(1)).setAutocommitDmlMode(mode);
    }
}
Also used : 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