Search in sources :

Example 6 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testBatchStatementsWithComments.

@Test
public void testBatchStatementsWithComments() throws Exception {
    byte[] messageMetadata = { 'Q', 0, 0, 0, (byte) 132 };
    String payload = "INSERT INTO users (name) VALUES (';;test;;'); /* Comment;; Comment; */INSERT INTO users (name1, name2) VALUES ('''''', ';'';');\0";
    byte[] value = Bytes.concat(messageMetadata, payload.getBytes());
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionHandler.getServer()).thenReturn(server);
    when(server.getOptions()).thenReturn(options);
    when(options.requiresMatcher()).thenReturn(false);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(QueryMessage.class, message.getClass());
    IntermediateStatement intermediateStatement = ((QueryMessage) message).getStatement();
    assertTrue(intermediateStatement.isBatchedQuery());
    assertEquals(2, intermediateStatement.getStatements().size());
    assertEquals("INSERT INTO users (name) VALUES (';;test;;')", intermediateStatement.getStatement(0));
    assertEquals("INSERT INTO users (name1, name2) VALUES ('''''', ';'';')", intermediateStatement.getStatement(1));
}
Also used : QueryMessage(com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 7 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testDescribeBasicStatementThrowsException.

@Test
public void testDescribeBasicStatementThrowsException() {
    IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse("SELECT * FROM users"), connectionHandler);
    assertThrows(IllegalStateException.class, intermediateStatement::describe);
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Example 8 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testBatchStatementsWithQuotes.

@Test
public void testBatchStatementsWithQuotes() {
    String sql = "INSERT INTO users (name) VALUES (';;test;;'); INSERT INTO users (name1, name2) VALUES ('''''', ';'';');";
    IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse(sql), connectionHandler);
    assertTrue(intermediateStatement.isBatchedQuery());
    assertEquals(2, intermediateStatement.getStatements().size());
    assertEquals("INSERT INTO users (name) VALUES (';;test;;')", intermediateStatement.getStatement(0));
    assertEquals("INSERT INTO users (name1, name2) VALUES ('''''', ';'';')", intermediateStatement.getStatement(1));
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Example 9 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testBasicNoResultStatement.

@Test
public void testBasicNoResultStatement() throws Exception {
    when(options.getDdlTransactionMode()).thenReturn(DdlTransactionMode.Batch);
    when(statementResult.getResultType()).thenReturn(StatementResult.ResultType.NO_RESULT);
    when(connection.execute(Statement.of("CREATE TABLE users (name varchar(100) primary key)"))).thenReturn(statementResult);
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse("CREATE TABLE users (name varchar(100) primary key)"), connectionHandler);
    assertFalse(intermediateStatement.isExecuted());
    assertEquals("CREATE", intermediateStatement.getCommand(0));
    intermediateStatement.execute();
    verify(connection).execute(Statement.of("CREATE TABLE users (name varchar(100) primary key)"));
    assertFalse(intermediateStatement.containsResultSet(0));
    assertEquals(0, intermediateStatement.getUpdateCount(0));
    assertTrue(intermediateStatement.isExecuted());
    assertEquals(StatementType.DDL, intermediateStatement.getStatementType(0));
    assertNull(intermediateStatement.getStatementResult(0));
    assertFalse(intermediateStatement.isHasMoreData(0));
    assertFalse(intermediateStatement.hasException(0));
    assertEquals(0, intermediateStatement.getResultFormatCode(0));
    intermediateStatement.close();
    Mockito.verify(resultSet, never()).close();
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Example 10 with IntermediateStatement

use of com.google.cloud.spanner.pgadapter.statements.IntermediateStatement in project pgadapter by GoogleCloudPlatform.

the class StatementTest method testBasicUpdateStatement.

@Test
public void testBasicUpdateStatement() throws Exception {
    when(statementResult.getResultType()).thenReturn(StatementResult.ResultType.UPDATE_COUNT);
    when(statementResult.getUpdateCount()).thenReturn(1L);
    when(connection.execute(Statement.of("UPDATE users SET name = someName WHERE id = 10"))).thenReturn(statementResult);
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    IntermediateStatement intermediateStatement = new IntermediateStatement(options, parse("UPDATE users SET name = someName WHERE id = 10"), connectionHandler);
    assertFalse(intermediateStatement.isExecuted());
    assertEquals("UPDATE", intermediateStatement.getCommand(0));
    intermediateStatement.execute();
    verify(connection).execute(Statement.of("UPDATE users SET name = someName WHERE id = 10"));
    assertFalse(intermediateStatement.containsResultSet(0));
    assertEquals(1L, intermediateStatement.getUpdateCount(0));
    assertTrue(intermediateStatement.isExecuted());
    assertEquals(StatementType.UPDATE, intermediateStatement.getStatementType(0));
    assertNull(intermediateStatement.getStatementResult(0));
    assertFalse(intermediateStatement.isHasMoreData(0));
    assertFalse(intermediateStatement.hasException(0));
    assertEquals(0, intermediateStatement.getResultFormatCode(0));
    intermediateStatement.close();
    verify(resultSet, never()).close();
}
Also used : IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) Test(org.junit.Test)

Aggregations

IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)13 Test (org.junit.Test)12 SpannerException (com.google.cloud.spanner.SpannerException)1 Statement (com.google.cloud.spanner.Statement)1 ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)1 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)1 IntermediatePortalStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)1 IntermediatePreparedStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement)1 QueryMessage (com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage)1 WireMessage (com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1