use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class ITParameterizedQueryTest method setUp.
@Before
public void setUp() throws Exception {
final String ddl = "CREATE TABLE users (\n" + " id bigint PRIMARY KEY,\n" + " age bigint,\n" + " name text\n" + ");";
List<String> values = new ArrayList<>(Arrays.asList("(1, 1, '1')", "(2, 20, 'Joe')", "(3, 23, 'Jack')"));
String dml = "INSERT INTO users (id, age, name) VALUES " + String.join(", ", values);
// TODO: Refactor the integration tests to use a common subclass, as this is repeated in each
// class.
testEnv.setUp();
Database db = testEnv.createDatabase();
testEnv.updateDdl(db.getId().getDatabase(), Arrays.asList(ddl));
testEnv.updateTables(db.getId().getDatabase(), Arrays.asList(dml));
String credentials = testEnv.getCredentials();
ImmutableList.Builder<String> argsListBuilder = ImmutableList.<String>builder().add("-p", testEnv.getProjectId(), "-i", testEnv.getInstanceId(), "-d", db.getId().getDatabase(), "-s", String.valueOf(testEnv.getPort()), "-e", testEnv.getUrl().getHost());
if (credentials != null) {
argsListBuilder.add("-c", testEnv.getCredentials());
}
args = argsListBuilder.build().toArray(new String[0]);
server = new ProxyServer(new OptionsMetadata(args));
server.startServer();
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class ControlMessageTest method testInsertResult.
@Test
public void testInsertResult() throws Exception {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(buffer);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(new byte[] { (byte) QUERY_IDENTIFIER, 0, 0, 0, 5, 0 }));
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(intermediateStatement.getStatementType(0)).thenReturn(StatementType.UPDATE);
when(intermediateStatement.getCommandTag(0)).thenReturn("INSERT");
when(intermediateStatement.getUpdateCount(0)).thenReturn(1L);
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
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);
ProxyServer server = new ProxyServer(options);
when(connectionHandler.getServer()).thenReturn(server);
ControlMessage controlMessage = ControlMessage.create(connectionHandler);
controlMessage.sendSpannerResult(0, intermediateStatement, QueryMode.SIMPLE, 0L);
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// identifier
outputReader.readByte();
// length
outputReader.readInt();
final String resultMessage = "INSERT 0 1";
int numOfBytes = resultMessage.getBytes(UTF8).length;
byte[] bytes = new byte[numOfBytes];
assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
assertEquals(resultMessage, new String(bytes, UTF8));
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class ITPgxTest method data.
@Parameters(name = "preferQueryMode = {0}, useDomainSocket = {1}")
public static List<Object[]> data() {
OptionsMetadata options = new OptionsMetadata(new String[] { "-p p", "-i i" });
boolean[] useDomainSockets;
if (options.isDomainSocketEnabled()) {
useDomainSockets = new boolean[] { true, false };
} else {
useDomainSockets = new boolean[] { false };
}
String[] queryModes = { "extended", "simple" };
List<Object[]> parameters = new ArrayList<>();
for (String queryMode : queryModes) {
for (boolean useDomainSocket : useDomainSockets) {
parameters.add(new Object[] { queryMode, useDomainSocket });
}
}
return parameters;
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class RowDescriptionTest method SendPayloadStatementTest.
@Test
public void SendPayloadStatementTest() throws Exception {
int COLUMN_COUNT = 2;
String COLUMN_NAME = "default-column-name";
Type rowType = Type.struct(StructField.of(COLUMN_NAME, Type.string()), 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).thenReturn((short) 0).thenReturn((short) 1);
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, statement, metadata, options, mode);
response.sendPayload();
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// column count
assertEquals(COLUMN_COUNT, outputReader.readShort());
for (int i = 0; i < COLUMN_COUNT; i++) {
// 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(i, outputReader.readShort());
}
}
use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.
the class ITJdbcTest method data.
@Parameters(name = "preferQueryMode = {0}, useDomainSocket = {1}")
public static List<Object[]> data() {
OptionsMetadata options = new OptionsMetadata(new String[] { "-p p", "-i i" });
boolean[] useDomainSockets;
if (options.isDomainSocketEnabled()) {
useDomainSockets = new boolean[] { true, false };
} else {
useDomainSockets = new boolean[] { false };
}
String[] queryModes = { "extended", "simple" };
List<Object[]> parameters = new ArrayList<>();
for (String queryMode : queryModes) {
for (boolean useDomainSocket : useDomainSockets) {
parameters.add(new Object[] { queryMode, useDomainSocket });
}
}
return parameters;
}
Aggregations