Search in sources :

Example 11 with OptionsMetadata

use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.

the class ConnectionHandler method connectToSpanner.

@InternalApi
public void connectToSpanner(String database) {
    OptionsMetadata options = getServer().getOptions();
    String uri = options.hasDefaultConnectionUrl() ? options.getDefaultConnectionUrl() : options.buildConnectionURL(database);
    if (uri.startsWith("jdbc:")) {
        uri = uri.substring("jdbc:".length());
    }
    uri = appendPropertiesToUrl(uri, getServer().getProperties());
    if (System.getProperty(CHANNEL_PROVIDER_PROPERTY) != null) {
        uri = uri + ";" + ConnectionOptions.CHANNEL_PROVIDER_PROPERTY_NAME + "=" + System.getProperty(CHANNEL_PROVIDER_PROPERTY);
        // This forces the connection to use NoCredentials.
        uri = uri + ";usePlainText=true";
        try {
            Class.forName(System.getProperty(CHANNEL_PROVIDER_PROPERTY));
        } catch (ClassNotFoundException e) {
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Unknown or invalid channel provider: " + System.getProperty(CHANNEL_PROVIDER_PROPERTY));
        }
    }
    ConnectionOptions connectionOptions = ConnectionOptions.newBuilder().setUri(uri).build();
    Connection spannerConnection = connectionOptions.getConnection();
    try {
        // invalid, for example as a result of the credentials being wrong.
        if (spannerConnection.getDialect() != Dialect.POSTGRESQL) {
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, String.format("The database uses dialect %s. Currently PGAdapter only supports connections to PostgreSQL dialect databases. " + "These can be created using https://cloud.google.com/spanner/docs/quickstart-console#postgresql", spannerConnection.getDialect()));
        }
    } catch (SpannerException e) {
        spannerConnection.close();
        throw e;
    }
    this.spannerConnection = spannerConnection;
}
Also used : Connection(com.google.cloud.spanner.connection.Connection) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) SpannerException(com.google.cloud.spanner.SpannerException) InternalApi(com.google.api.core.InternalApi)

Example 12 with OptionsMetadata

use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.

the class Server method main.

/**
 * Main method for running a Spanner PostgreSQL Adaptor {@link Server} as a stand-alone
 * application. Here we call for parameter parsing and start the Proxy Server.
 */
public static void main(String[] args) {
    try {
        System.out.printf("-- Starting PGAdapter version %s --\n", getVersion());
        OptionsMetadata optionsMetadata = new OptionsMetadata(args);
        ProxyServer server = new ProxyServer(optionsMetadata);
        server.startServer();
    } catch (Exception e) {
        System.err.println("The server could not be started because an error occurred: " + (e.getMessage() == null ? e.toString() : e.getMessage()));
        System.out.println("Run with option -h or --help to get help");
        System.out.printf("Version: %s\n", getVersion());
    }
}
Also used : OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)

Example 13 with OptionsMetadata

use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.

the class AbstractMockServerTest method startMockSpannerAndPgAdapterServers.

@BeforeClass
public static void startMockSpannerAndPgAdapterServers() 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.update(UPDATE_STATEMENT, UPDATE_COUNT));
    mockSpanner.putStatementResult(StatementResult.update(INSERT_STATEMENT, INSERT_COUNT));
    mockSpanner.putStatementResult(MockSpannerServiceImpl.StatementResult.detectDialectResult(Dialect.POSTGRESQL));
    mockOperationsService = new MockOperationsServiceImpl();
    mockDatabaseAdmin = new MockDatabaseAdminServiceImpl(mockOperationsService);
    InetSocketAddress address = new InetSocketAddress("localhost", 0);
    spannerServer = NettyServerBuilder.forAddress(address).addService(mockSpanner).addService(mockDatabaseAdmin).addService(mockOperationsService).build().start();
    // Create the test database on the mock server. This should be replaced by a simple feature in
    // the mock server to just add a database instead of having to simulate the creation of it.
    createDatabase();
    ImmutableList.Builder<String> argsListBuilder = ImmutableList.<String>builder().add("-p", "p", "-i", "i", "-d", "d", "-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;");
    String[] args = argsListBuilder.build().toArray(new String[0]);
    pgServer = new ProxyServer(new OptionsMetadata(args));
    pgServer.startServer();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ImmutableList(com.google.common.collect.ImmutableList) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) MockOperationsServiceImpl(com.google.cloud.spanner.MockOperationsServiceImpl) MockDatabaseAdminServiceImpl(com.google.cloud.spanner.MockDatabaseAdminServiceImpl) MockSpannerServiceImpl(com.google.cloud.spanner.MockSpannerServiceImpl) BeforeClass(org.junit.BeforeClass)

Aggregations

OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)13 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 DataInputStream (java.io.DataInputStream)5 JSONObject (org.json.simple.JSONObject)5 JSONParser (org.json.simple.parser.JSONParser)5 QueryMode (com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode)4 Type (com.google.cloud.spanner.Type)3 ImmutableList (com.google.common.collect.ImmutableList)3 ArrayList (java.util.ArrayList)3 MockSpannerServiceImpl (com.google.cloud.spanner.MockSpannerServiceImpl)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 Parameters (org.junit.runners.Parameterized.Parameters)2 InternalApi (com.google.api.core.InternalApi)1 Database (com.google.cloud.spanner.Database)1 MockDatabaseAdminServiceImpl (com.google.cloud.spanner.MockDatabaseAdminServiceImpl)1 MockOperationsServiceImpl (com.google.cloud.spanner.MockOperationsServiceImpl)1 SpannerException (com.google.cloud.spanner.SpannerException)1