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));
}
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);
}
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));
}
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();
}
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();
}
Aggregations