Search in sources :

Example 11 with Connection

use of com.google.cloud.spanner.connection.Connection in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method setupQueryInformationSchemaResults.

private void setupQueryInformationSchemaResults() {
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    ResultSet spannerType = mock(ResultSet.class);
    when(spannerType.getString("column_name")).thenReturn("key", "value");
    when(spannerType.getString("data_type")).thenReturn("bigint", "character varying");
    when(spannerType.next()).thenReturn(true, true, false);
    when(connection.executeQuery(ArgumentMatchers.argThat(statement -> statement != null && statement.getSql().startsWith("SELECT column_name")))).thenReturn(spannerType);
    ResultSet countResult = mock(ResultSet.class);
    when(countResult.getLong(0)).thenReturn(2L);
    when(countResult.next()).thenReturn(true, false);
    when(connection.executeQuery(ArgumentMatchers.argThat(statement -> statement != null && statement.getSql().startsWith("SELECT COUNT(*)")))).thenReturn(countResult);
}
Also used : OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Dialect(com.google.cloud.spanner.Dialect) FunctionCallMessage(com.google.cloud.spanner.pgadapter.wireprotocol.FunctionCallMessage) CopyDoneMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CopyDoneMessage) ByteConverter(org.postgresql.util.ByteConverter) DatabaseClient(com.google.cloud.spanner.DatabaseClient) QueryMessage(com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage) Connection(com.google.cloud.spanner.connection.Connection) ResultSet(com.google.cloud.spanner.ResultSet) BootstrapMessage(com.google.cloud.spanner.pgadapter.wireprotocol.BootstrapMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) Map(java.util.Map) AbstractStatementParser(com.google.cloud.spanner.connection.AbstractStatementParser) ConnectionStatus(com.google.cloud.spanner.pgadapter.ConnectionHandler.ConnectionStatus) StartupMessage(com.google.cloud.spanner.pgadapter.wireprotocol.StartupMessage) Mockito.doReturn(org.mockito.Mockito.doReturn) AfterClass(org.junit.AfterClass) DescribeMessage(com.google.cloud.spanner.pgadapter.wireprotocol.DescribeMessage) JSONParser(org.json.simple.parser.JSONParser) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) Mockito.doNothing(org.mockito.Mockito.doNothing) ControlMessage(com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) Bytes(com.google.common.primitives.Bytes) StandardCharsets(java.nio.charset.StandardCharsets) TerminateMessage(com.google.cloud.spanner.pgadapter.wireprotocol.TerminateMessage) List(java.util.List) JSONObject(org.json.simple.JSONObject) ResultType(com.google.cloud.spanner.connection.StatementResult.ResultType) BindMessage(com.google.cloud.spanner.pgadapter.wireprotocol.BindMessage) Assert.assertFalse(org.junit.Assert.assertFalse) StatementResult(com.google.cloud.spanner.connection.StatementResult) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) CancelMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CancelMessage) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DataInputStream(java.io.DataInputStream) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) CopyFailMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CopyFailMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CopyDataMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CopyDataMessage) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) MatcherStatement(com.google.cloud.spanner.pgadapter.statements.MatcherStatement) PreparedType(com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage.PreparedType) SSLMessage(com.google.cloud.spanner.pgadapter.wireprotocol.SSLMessage) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) IntermediatePortalStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement) MockitoJUnit(org.mockito.junit.MockitoJUnit) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ConnectionMetadata(com.google.cloud.spanner.pgadapter.metadata.ConnectionMetadata) QueryMode(com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode) IntermediatePreparedStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement) Files(java.nio.file.Files) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) FlushMessage(com.google.cloud.spanner.pgadapter.wireprotocol.FlushMessage) JUnit4(org.junit.runners.JUnit4) Field(java.lang.reflect.Field) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) ErrorCode(com.google.cloud.spanner.ErrorCode) SpannerException(com.google.cloud.spanner.SpannerException) Statement(com.google.cloud.spanner.Statement) ExecuteMessage(com.google.cloud.spanner.pgadapter.wireprotocol.ExecuteMessage) SyncMessage(com.google.cloud.spanner.pgadapter.wireprotocol.SyncMessage) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) Paths(java.nio.file.Paths) CloseMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CloseMessage) ParseMessage(com.google.cloud.spanner.pgadapter.wireprotocol.ParseMessage) MockitoRule(org.mockito.junit.MockitoRule) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ResultSet(com.google.cloud.spanner.ResultSet)

Example 12 with Connection

use of com.google.cloud.spanner.connection.Connection 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 13 with Connection

use of com.google.cloud.spanner.connection.Connection in project java-spanner-jdbc by googleapis.

the class JdbcStatementTest method createStatement.

@SuppressWarnings("unchecked")
private JdbcStatement createStatement() throws SQLException {
    Connection spanner = mock(Connection.class);
    when(spanner.getDialect()).thenReturn(dialect);
    com.google.cloud.spanner.ResultSet resultSet = mock(com.google.cloud.spanner.ResultSet.class);
    when(resultSet.next()).thenReturn(true, false);
    when(resultSet.getColumnType(0)).thenReturn(Type.int64());
    when(resultSet.getLong(0)).thenReturn(1L);
    StatementResult selectResult = mock(StatementResult.class);
    when(selectResult.getResultType()).thenReturn(ResultType.RESULT_SET);
    when(selectResult.getResultSet()).thenReturn(resultSet);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(SELECT))).thenReturn(selectResult);
    StatementResult updateResult = mock(StatementResult.class);
    when(updateResult.getResultType()).thenReturn(ResultType.UPDATE_COUNT);
    when(updateResult.getUpdateCount()).thenReturn(1L);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(UPDATE))).thenReturn(updateResult);
    StatementResult largeUpdateResult = mock(StatementResult.class);
    when(largeUpdateResult.getResultType()).thenReturn(ResultType.UPDATE_COUNT);
    when(largeUpdateResult.getUpdateCount()).thenReturn(Integer.MAX_VALUE + 1L);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(LARGE_UPDATE))).thenReturn(largeUpdateResult);
    StatementResult ddlResult = mock(StatementResult.class);
    when(ddlResult.getResultType()).thenReturn(ResultType.NO_RESULT);
    when(spanner.execute(com.google.cloud.spanner.Statement.of(DDL))).thenReturn(ddlResult);
    when(spanner.executeQuery(com.google.cloud.spanner.Statement.of(SELECT))).thenReturn(resultSet);
    when(spanner.executeQuery(com.google.cloud.spanner.Statement.of(UPDATE))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not a query"));
    when(spanner.executeQuery(com.google.cloud.spanner.Statement.of(DDL))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not a query"));
    when(spanner.executeUpdate(com.google.cloud.spanner.Statement.of(UPDATE))).thenReturn(1L);
    when(spanner.executeUpdate(com.google.cloud.spanner.Statement.of(SELECT))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not an update"));
    when(spanner.executeUpdate(com.google.cloud.spanner.Statement.of(DDL))).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "not an update"));
    when(spanner.executeBatchUpdate(anyList())).thenAnswer((Answer<long[]>) invocation -> {
        List<com.google.cloud.spanner.Statement> statements = (List<com.google.cloud.spanner.Statement>) invocation.getArguments()[0];
        if (statements.isEmpty() || AbstractStatementParser.getInstance(dialect).isDdlStatement(statements.get(0).getSql())) {
            return new long[0];
        }
        long[] res = new long[((List<com.google.cloud.spanner.Statement>) invocation.getArguments()[0]).size()];
        Arrays.fill(res, 1L);
        return res;
    });
    JdbcConnection connection = mock(JdbcConnection.class);
    when(connection.getDialect()).thenReturn(dialect);
    when(connection.getParser()).thenReturn(AbstractStatementParser.getInstance(dialect));
    when(connection.getSpannerConnection()).thenReturn(spanner);
    return new JdbcStatement(connection);
}
Also used : Arrays(java.util.Arrays) Dialect(com.google.cloud.spanner.Dialect) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) SpannerExceptionFactory(com.google.cloud.spanner.SpannerExceptionFactory) Connection(com.google.cloud.spanner.connection.Connection) Answer(org.mockito.stubbing.Answer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Assert.fail(org.junit.Assert.fail) AbstractStatementParser(com.google.cloud.spanner.connection.AbstractStatementParser) Code(com.google.rpc.Code) Parameterized(org.junit.runners.Parameterized) Parameter(org.junit.runners.Parameterized.Parameter) Type(com.google.cloud.spanner.Type) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) ErrorCode(com.google.cloud.spanner.ErrorCode) TimeUnit(java.util.concurrent.TimeUnit) JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Mockito.anyList(org.mockito.Mockito.anyList) ResultType(com.google.cloud.spanner.connection.StatementResult.ResultType) StatementResult(com.google.cloud.spanner.connection.StatementResult) Statement(java.sql.Statement) Mockito.mock(org.mockito.Mockito.mock) StatementResult(com.google.cloud.spanner.connection.StatementResult) Statement(java.sql.Statement) Connection(com.google.cloud.spanner.connection.Connection) List(java.util.List) Mockito.anyList(org.mockito.Mockito.anyList)

Example 14 with Connection

use of com.google.cloud.spanner.connection.Connection in project java-spanner-jdbc by googleapis.

the class JdbcStatementTest method testInternalExecuteUpdate.

@Test
public void testInternalExecuteUpdate() throws SQLException {
    JdbcConnection connection = mock(JdbcConnection.class);
    when(connection.getDialect()).thenReturn(dialect);
    Connection spannerConnection = mock(Connection.class);
    when(connection.getSpannerConnection()).thenReturn(spannerConnection);
    com.google.cloud.spanner.Statement updateStatement = com.google.cloud.spanner.Statement.of(UPDATE);
    com.google.cloud.spanner.Statement largeUpdateStatement = com.google.cloud.spanner.Statement.of(LARGE_UPDATE);
    when(spannerConnection.executeUpdate(updateStatement)).thenReturn(1L);
    when(spannerConnection.executeUpdate(largeUpdateStatement)).thenReturn(Integer.MAX_VALUE + 1L);
    try (JdbcStatement statement = new JdbcStatement(connection)) {
        assertThat(statement.executeUpdate(updateStatement)).isEqualTo(1);
        try {
            statement.executeUpdate(largeUpdateStatement);
            fail("missing expected exception");
        } catch (JdbcSqlExceptionImpl e) {
            assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE);
        }
    }
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) Connection(com.google.cloud.spanner.connection.Connection) Test(org.junit.Test)

Example 15 with Connection

use of com.google.cloud.spanner.connection.Connection in project java-spanner by googleapis.

the class ITAsyncTransactionRetryTest method testAbortWithConcurrentUpdate.

@Test
public void testAbortWithConcurrentUpdate() {
    assumeFalse("concurrent transactions are not supported on the emulator", isUsingEmulator());
    AbortInterceptor interceptor = new AbortInterceptor(0);
    // first insert two test records
    try (ITConnection connection = createConnection()) {
        connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (1, 'test 1')"));
        connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (2, 'test 2')"));
        get(connection.commitAsync());
    }
    // open a new connection and select the two test records
    try (ITConnection connection = createConnection(interceptor, new CountTransactionRetryListener())) {
        // select the test records and consume the entire result set
        try (AsyncResultSet rs = connection.executeQueryAsync(Statement.of("SELECT * FROM TEST ORDER BY ID"))) {
            get(rs.setCallback(executor, resultSet -> {
                while (true) {
                    switch(resultSet.tryNext()) {
                        case DONE:
                            return CallbackResponse.DONE;
                        case NOT_READY:
                            return CallbackResponse.CONTINUE;
                        case OK:
                            break;
                    }
                }
            }));
        }
        // open a new connection and transaction and update one of the test records
        try (ITConnection connection2 = createConnection()) {
            connection2.executeUpdateAsync(Statement.of("UPDATE TEST SET NAME='test updated' WHERE ID=2"));
            get(connection2.commitAsync());
        }
        // now try to do an insert that will abort. The retry should now fail as there has been a
        // concurrent modification
        interceptor.setProbability(1.0);
        interceptor.setOnlyInjectOnce(true);
        try {
            get(connection.executeUpdateAsync(Statement.of("INSERT INTO TEST (ID, NAME) VALUES (3, 'test 3')")));
            fail("Missing expected exception");
        } catch (AbortedDueToConcurrentModificationException e) {
            assertRetryStatistics(1, 1, 0);
        }
    }
}
Also used : Assume.assumeFalse(org.junit.Assume.assumeFalse) RunWith(org.junit.runner.RunWith) SpannerExceptionFactory(com.google.cloud.spanner.SpannerExceptionFactory) TransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener) Timestamp(com.google.cloud.Timestamp) Connection(com.google.cloud.spanner.connection.Connection) Level(java.util.logging.Level) ResultSet(com.google.cloud.spanner.ResultSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestName(org.junit.rules.TestName) After(org.junit.After) Assert.fail(org.junit.Assert.fail) AbortedDueToConcurrentModificationException(com.google.cloud.spanner.AbortedDueToConcurrentModificationException) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) AbortedException(com.google.cloud.spanner.AbortedException) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) AfterClass(org.junit.AfterClass) CallbackResponse(com.google.cloud.spanner.AsyncResultSet.CallbackResponse) SpannerApiFutures.get(com.google.cloud.spanner.SpannerApiFutures.get) EmulatorSpannerHelper.isUsingEmulator(com.google.cloud.spanner.testing.EmulatorSpannerHelper.isUsingEmulator) Test(org.junit.Test) Mutation(com.google.cloud.spanner.Mutation) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest) Logger(java.util.logging.Logger) Category(org.junit.experimental.categories.Category) Options(com.google.cloud.spanner.Options) Executors(java.util.concurrent.Executors) ApiFuture(com.google.api.core.ApiFuture) SettableApiFuture(com.google.api.core.SettableApiFuture) KeySet(com.google.cloud.spanner.KeySet) ErrorCode(com.google.cloud.spanner.ErrorCode) Statement(com.google.cloud.spanner.Statement) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Rule(org.junit.Rule) Struct(com.google.cloud.spanner.Struct) AsyncResultSet(com.google.cloud.spanner.AsyncResultSet) AbortedDueToConcurrentModificationException(com.google.cloud.spanner.AbortedDueToConcurrentModificationException) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test) ITAbstractSpannerTest(com.google.cloud.spanner.connection.ITAbstractSpannerTest)

Aggregations

Connection (com.google.cloud.spanner.connection.Connection)18 Test (org.junit.Test)16 TypeCode (com.google.spanner.v1.TypeCode)6 CSVFormat (org.apache.commons.csv.CSVFormat)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)5 ErrorCode (com.google.cloud.spanner.ErrorCode)5 Mutation (com.google.cloud.spanner.Mutation)5 ResultSet (com.google.cloud.spanner.ResultSet)5 RunWith (org.junit.runner.RunWith)5 SpannerException (com.google.cloud.spanner.SpannerException)4 SpannerExceptionFactory (com.google.cloud.spanner.SpannerExceptionFactory)4 Statement (com.google.cloud.spanner.Statement)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 ExecutorService (java.util.concurrent.ExecutorService)4 TimeUnit (java.util.concurrent.TimeUnit)4 AfterClass (org.junit.AfterClass)4 Assert.fail (org.junit.Assert.fail)4 Rule (org.junit.Rule)4 JUnit4 (org.junit.runners.JUnit4)4 ApiFuture (com.google.api.core.ApiFuture)3