use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method SendPayloadStatementWithBinaryOptionTest.
@Test
public void SendPayloadStatementWithBinaryOptionTest() throws Exception {
int COLUMN_COUNT = 1;
String COLUMN_NAME = "default-column-name";
Type rowType = Type.struct(StructField.of(COLUMN_NAME, Type.string()));
when(metadata.getColumnCount()).thenReturn(COLUMN_COUNT);
when(metadata.getType()).thenReturn(rowType);
when(metadata.getColumnType(Mockito.anyInt())).thenReturn(Type.int64());
when(statement.getResultFormatCode(Mockito.anyInt())).thenReturn((short) 0);
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, true, false, false, false, commandMetadata);
QueryMode mode = QueryMode.EXTENDED;
RowDescriptionResponse response = new RowDescriptionResponse(output, statement, metadata, options, mode);
response.sendPayload();
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// column count
assertEquals(COLUMN_COUNT, outputReader.readShort());
// column name
int numOfBytes = COLUMN_NAME.getBytes(UTF8).length;
byte[] bytes = new byte[numOfBytes];
assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
assertEquals(COLUMN_NAME, new String(bytes, UTF8));
// null terminator
assertEquals(DEFAULT_FLAG, outputReader.readByte());
// table oid
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// column index
assertEquals(DEFAULT_FLAG, outputReader.readShort());
// type oid
assertEquals(Oid.INT8, outputReader.readInt());
// type size
assertEquals(8, outputReader.readShort());
// type modifier
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// format code
assertEquals(1, outputReader.readShort());
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method SendPayloadNullStatementTest.
@Test
public void SendPayloadNullStatementTest() throws Exception {
int COLUMN_COUNT = 1;
String COLUMN_NAME = "default-column-name";
Type rowType = Type.struct(StructField.of(COLUMN_NAME, Type.string()));
when(metadata.getColumnCount()).thenReturn(COLUMN_COUNT);
when(metadata.getType()).thenReturn(rowType);
when(metadata.getColumnType(Mockito.anyInt())).thenReturn(Type.int64());
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, false, false, false, false, commandMetadata);
QueryMode mode = QueryMode.EXTENDED;
RowDescriptionResponse response = new RowDescriptionResponse(output, null, metadata, options, mode);
response.sendPayload();
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// column count
assertEquals(COLUMN_COUNT, outputReader.readShort());
// column name
int numOfBytes = COLUMN_NAME.getBytes(UTF8).length;
byte[] bytes = new byte[numOfBytes];
assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
assertEquals(new String(bytes, UTF8), COLUMN_NAME);
// null terminator
assertEquals(DEFAULT_FLAG, outputReader.readByte());
// table oid
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// column index
assertEquals(DEFAULT_FLAG, outputReader.readShort());
// type oid
assertEquals(Oid.INT8, outputReader.readInt());
// type size
assertEquals(8, outputReader.readShort());
// type modifier
assertEquals(DEFAULT_FLAG, outputReader.readInt());
// format code
assertEquals(0, outputReader.readShort());
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method OidTest.
@Test
public void OidTest() throws Exception {
when(metadata.getColumnCount()).thenReturn(0);
when(metadata.getColumnType(0)).thenReturn(Type.int64());
when(metadata.getColumnType(1)).thenReturn(Type.pgNumeric());
when(metadata.getColumnType(2)).thenReturn(Type.float64());
when(metadata.getColumnType(3)).thenReturn(Type.string());
when(metadata.getColumnType(4)).thenReturn(Type.bytes());
when(metadata.getColumnType(5)).thenReturn(Type.bool());
when(metadata.getColumnType(6)).thenReturn(Type.date());
when(metadata.getColumnType(7)).thenReturn(Type.timestamp());
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, false, false, false, false, commandMetadata);
QueryMode mode = QueryMode.SIMPLE;
RowDescriptionResponse response = new RowDescriptionResponse(output, statement, metadata, options, mode);
// Types.BIGINT
assertEquals(Oid.INT8, response.getOidType(0));
assertEquals(8, response.getOidTypeSize(Oid.INT8));
// Types.NUMERIC
assertEquals(Oid.NUMERIC, response.getOidType(1));
assertEquals(-1, response.getOidTypeSize(Oid.NUMERIC));
// Types.DOUBLE
assertEquals(Oid.FLOAT8, response.getOidType(2));
assertEquals(8, response.getOidTypeSize(Oid.FLOAT8));
// Types.VARCHAR
assertEquals(Oid.VARCHAR, response.getOidType(3));
assertEquals(-1, response.getOidTypeSize(Oid.VARCHAR));
// Types.BINARY
assertEquals(Oid.BYTEA, response.getOidType(4));
assertEquals(-1, response.getOidTypeSize(Oid.BYTEA));
// Types.BIT
assertEquals(Oid.BOOL, response.getOidType(5));
assertEquals(1, response.getOidTypeSize(Oid.BOOL));
// Types.DATE
assertEquals(Oid.DATE, response.getOidType(6));
assertEquals(8, response.getOidTypeSize(Oid.DATE));
// Types.TIMESTAMP
assertEquals(Oid.TIMESTAMPTZ, response.getOidType(7));
assertEquals(12, response.getOidTypeSize(Oid.TIMESTAMPTZ));
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testQueryMessageInTransaction.
@Test
public void testQueryMessageInTransaction() throws Exception {
byte[] messageMetadata = { 'Q', 0, 0, 0, 45 };
String payload = "INSERT INTO users (name) VALUES ('test')\0";
byte[] value = Bytes.concat(messageMetadata, payload.getBytes());
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
String expectedSQL = "INSERT INTO users (name) VALUES ('test')";
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
when(connectionHandler.getStatus()).thenReturn(ConnectionStatus.TRANSACTION);
when(statementResult.getResultType()).thenReturn(ResultType.UPDATE_COUNT);
when(statementResult.getUpdateCount()).thenReturn(1L);
when(connection.execute(Statement.of(expectedSQL))).thenReturn(statementResult);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionHandler.getServer()).thenReturn(server);
OptionsMetadata options = mock(OptionsMetadata.class);
when(server.getOptions()).thenReturn(options);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(QueryMessage.class, message.getClass());
assertEquals(expectedSQL, ((QueryMessage) message).getStatement().getSql());
message.send();
// NoData response (query does not return any results).
DataInputStream outputResult = inputStreamFromOutputStream(result);
// CommandComplete
assertEquals('C', outputResult.readByte());
assertEquals('\0', outputResult.readByte());
assertEquals('\0', outputResult.readByte());
assertEquals('\0', outputResult.readByte());
// 15 = 4 + "INSERT".length() + " 0 1".length() + 1 (header + command length + null terminator)
assertEquals(15, outputResult.readByte());
byte[] command = new byte[10];
assertEquals(10, outputResult.read(command, 0, 10));
assertEquals("INSERT 0 1", new String(command));
assertEquals('\0', outputResult.readByte());
// ReadyResponse in transaction ('T')
assertEquals('Z', outputResult.readByte());
assertEquals(5, outputResult.readInt());
assertEquals('T', outputResult.readByte());
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class AbstractMockServerTest method doStartMockSpannerAndPgAdapterServers.
protected static void doStartMockSpannerAndPgAdapterServers(String defaultDatabase, Iterable<String> extraPGAdapterOptions) throws Exception {
mockSpanner = new MockSpannerServiceImpl();
// We don't want any unpredictable aborted transactions.
mockSpanner.setAbortProbability(0.0D);
mockSpanner.putStatementResult(StatementResult.query(SELECT1, SELECT1_RESULTSET));
mockSpanner.putStatementResult(StatementResult.query(SELECT2, SELECT2_RESULTSET));
mockSpanner.putStatementResult(StatementResult.query(SELECT_FIVE_ROWS, SELECT_FIVE_ROWS_RESULTSET));
mockSpanner.putStatementResult(StatementResult.update(UPDATE_STATEMENT, UPDATE_COUNT));
mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, INSERT_COUNT));
mockSpanner.putStatementResult(MockSpannerServiceImpl.StatementResult.detectDialectResult(Dialect.POSTGRESQL));
mockSpanner.putStatementResult(StatementResult.exception(INVALID_SELECT, EXCEPTION));
mockSpanner.putStatementResult(StatementResult.exception(INVALID_DML, EXCEPTION));
mockSpanner.putStatementResult(StatementResult.exception(INVALID_DDL, EXCEPTION));
mockDatabaseAdmin = new MockDatabaseAdminImpl();
InetSocketAddress address = new InetSocketAddress("localhost", 0);
spannerServer = NettyServerBuilder.forAddress(address).addService(mockSpanner).addService(mockDatabaseAdmin).intercept(new ServerInterceptor() {
@Override
public <ReqT, RespT> Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata, ServerCallHandler<ReqT, RespT> serverCallHandler) {
if (SpannerGrpc.getExecuteStreamingSqlMethod().getFullMethodName().equals(serverCall.getMethodDescriptor().getFullMethodName())) {
String userAgent = metadata.get(Metadata.Key.of("x-goog-api-client", Metadata.ASCII_STRING_MARSHALLER));
assertNotNull(userAgent);
assertTrue(userAgent.contains("pg-adapter"));
}
return Contexts.interceptCall(Context.current(), serverCall, metadata, serverCallHandler);
}
}).build().start();
ImmutableList.Builder<String> argsListBuilder = ImmutableList.<String>builder().add("-p", "p", "-i", "i");
if (defaultDatabase != null) {
argsListBuilder.add("-d", defaultDatabase);
}
argsListBuilder.add("-jdbc", "-debug", "-c", // empty credentials file, as we are using a plain text connection.
"", "-s", // port 0 to let the OS pick an available port
"0", "-e", String.format("localhost:%d", spannerServer.getPort()), "-r", "usePlainText=true;");
argsListBuilder.addAll(extraPGAdapterOptions);
String[] args = argsListBuilder.build().toArray(new String[0]);
pgServer = new ProxyServer(new OptionsMetadata(args));
pgServer.startServer();
}
Aggregations