Search in sources :

Example 11 with DatabaseAdminClient

use of com.google.cloud.spanner.DatabaseAdminClient in project spanner-jdbc by olavloite.

the class CustomStatementsTest method testShowDDLOperations.

@Test
public void testShowDDLOperations() throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    final String ddl = "CREATE TABLE FOO (ID INT64 NOT NULL, NAME STRING(100) NOT NULL) PRIMARY KEY (ID)";
    Field adminClientField = CloudSpannerConnection.class.getDeclaredField("adminClient");
    adminClientField.setAccessible(true);
    DatabaseAdminClient adminClient = mock(DatabaseAdminClient.class);
    @SuppressWarnings("unchecked") Operation<Void, UpdateDatabaseDdlMetadata> operation = mock(Operation.class);
    when(operation.reload()).then(new Returns(operation));
    when(operation.getName()).then(new Returns("test"));
    when(adminClient.updateDatabaseDdl(any(), any(), any(), any())).then(new Returns(operation));
    adminClientField.set(connection, adminClient);
    Statement statement = connection.createStatement();
    assertFalse(statement.execute("SET_CONNECTION_PROPERTY AsyncDdlOperations=true"));
    assertEquals(1, statement.getUpdateCount());
    try (ResultSet rs = statement.executeQuery("SHOW_DDL_OPERATIONS")) {
        assertFalse(rs.next());
    }
    statement.execute(ddl);
    try (ResultSet rs = statement.executeQuery("SHOW_DDL_OPERATIONS")) {
        assertTrue(rs.next());
        assertEquals("test", rs.getString("NAME"));
        assertNotNull(rs.getTimestamp("TIME_STARTED"));
        assertEquals(ddl, rs.getString("STATEMENT"));
        assertFalse(rs.getBoolean("DONE"));
        assertNull(rs.getString("EXCEPTION"));
        assertFalse(rs.next());
    }
}
Also used : Field(java.lang.reflect.Field) Returns(org.mockito.internal.stubbing.answers.Returns) UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) Test(org.junit.Test) UnitTest(nl.topicus.jdbc.test.category.UnitTest)

Aggregations

DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)11 SpannerException (com.google.cloud.spanner.SpannerException)6 DatabaseClient (com.google.cloud.spanner.DatabaseClient)4 Database (com.google.cloud.spanner.Database)3 Spanner (com.google.cloud.spanner.Spanner)3 SpannerOptions (com.google.cloud.spanner.SpannerOptions)3 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)3 After (org.junit.After)3 Before (org.junit.Before)3 Mutation (com.google.cloud.spanner.Mutation)2 TransactionContext (com.google.cloud.spanner.TransactionContext)2 TransactionRunner (com.google.cloud.spanner.TransactionRunner)2 Nullable (javax.annotation.Nullable)2 RetrySettings (com.google.api.gax.retrying.RetrySettings)1 ServerStreamingCallSettings (com.google.api.gax.rpc.ServerStreamingCallSettings)1 UnaryCallSettings (com.google.api.gax.rpc.UnaryCallSettings)1 BatchClient (com.google.cloud.spanner.BatchClient)1 DatabaseId (com.google.cloud.spanner.DatabaseId)1 UpdateDatabaseDdlMetadata (com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata)1 CommitRequest (com.google.spanner.v1.CommitRequest)1