Search in sources :

Example 31 with ParsedStatement

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

the class ConnectionStatementWithNoParametersTest method testExecuteGetAutocommit.

@Test
public void testExecuteGetAutocommit() {
    ParsedStatement statement = parser.parse(Statement.of("show variable autocommit"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementShowAutocommit()).thenCallRealMethod();
    statement.getClientSideStatement().execute(executor, "show variable autocommit");
    verify(connection, times(1)).isAutocommit();
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 32 with ParsedStatement

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

the class ConnectionStatementWithNoParametersTest method testExecuteGetReadTimestamp.

@Test
public void testExecuteGetReadTimestamp() {
    ParsedStatement statement = parser.parse(Statement.of("show variable read_timestamp"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementShowReadTimestamp()).thenCallRealMethod();
    when(connection.getReadTimestampOrNull()).thenReturn(Timestamp.now());
    statement.getClientSideStatement().execute(executor, "show variable read_timestamp");
    verify(connection, times(1)).getReadTimestampOrNull();
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 33 with ParsedStatement

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

the class ConnectionStatementWithOneParameterTest method testExecuteSetReadOnly.

@Test
public void testExecuteSetReadOnly() {
    ParsedStatement subject = parser.parse(Statement.of("set readonly = true"));
    ConnectionImpl connection = mock(ConnectionImpl.class);
    ConnectionStatementExecutorImpl executor = mock(ConnectionStatementExecutorImpl.class);
    when(executor.getConnection()).thenReturn(connection);
    when(executor.statementSetReadOnly(any(Boolean.class))).thenCallRealMethod();
    for (Boolean mode : new Boolean[] { Boolean.FALSE, Boolean.TRUE }) {
        subject.getClientSideStatement().execute(executor, String.format("set readonly = %s", mode));
        verify(connection, times(1)).setReadOnly(mode);
    }
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 34 with ParsedStatement

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

the class StatementParserTest method testStatementWithCommentContainingSlash.

@Test
public void testStatementWithCommentContainingSlash() {
    String sql = "/*\n" + " * Script for testing invalid/unrecognized statements\n" + " */\n" + "\n" + "-- MERGE into test comment MERGE -- \n" + "@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n" + "MERGE INTO Singers s\n" + "/*** test ****/" + "USING (VALUES (1, 'John', 'Doe')) v\n" + "ON v.column1 = s.SingerId\n" + "WHEN NOT MATCHED \n" + "  INSERT VALUES (v.column1, v.column2, v.column3)\n" + "WHEN MATCHED\n" + "  UPDATE SET FirstName = v.column2,\n" + "             LastName = v.column3;";
    String sqlWithoutComments = "@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n" + "MERGE INTO Singers s\n" + "USING (VALUES (1, 'John', 'Doe')) v\n" + "ON v.column1 = s.SingerId\n" + "WHEN NOT MATCHED \n" + "  INSERT VALUES (v.column1, v.column2, v.column3)\n" + "WHEN MATCHED\n" + "  UPDATE SET FirstName = v.column2,\n" + "             LastName = v.column3";
    ParsedStatement statement = parser.parse(Statement.of(sql));
    assertThat(statement.getSqlWithoutComments()).isEqualTo(sqlWithoutComments);
}
Also used : ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Test(org.junit.Test)

Example 35 with ParsedStatement

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

the class StatementParserTest method testStatementWithCommentContainingSlashAndNoAsteriskOnNewLine.

@Test
public void testStatementWithCommentContainingSlashAndNoAsteriskOnNewLine() {
    String sql = "/*\n" + " * Script for testing invalid/unrecognized statements\n" + " foo bar baz" + " */\n" + "\n" + "-- MERGE INTO test comment MERGE\n" + "@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n" + "MERGE INTO Singers s\n" + "USING (VALUES (1, 'John', 'Doe')) v\n" + "ON v.column1 = s.SingerId\n" + "-- test again --\n" + "WHEN NOT MATCHED \n" + "  INSERT VALUES (v.column1, v.column2, v.column3)\n" + "WHEN MATCHED\n" + "  UPDATE SET FirstName = v.column2,\n" + "             LastName = v.column3;";
    String sqlWithoutComments = "@EXPECT EXCEPTION INVALID_ARGUMENT 'INVALID_ARGUMENT: Unknown statement'\n" + "MERGE INTO Singers s\n" + "USING (VALUES (1, 'John', 'Doe')) v\n" + "ON v.column1 = s.SingerId\n" + "\nWHEN NOT MATCHED \n" + "  INSERT VALUES (v.column1, v.column2, v.column3)\n" + "WHEN MATCHED\n" + "  UPDATE SET FirstName = v.column2,\n" + "             LastName = v.column3";
    ParsedStatement statement = parser.parse(Statement.of(sql));
    assertThat(statement.getSqlWithoutComments()).isEqualTo(sqlWithoutComments);
}
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