use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class ITDatabaseAdminTest method databaseOperationsViaEntity.
@Test
public void databaseOperationsViaEntity() throws Exception {
String dbId = testHelper.getUniqueDatabaseId();
String instanceId = testHelper.getInstanceId().getInstance();
String statement1 = "CREATE TABLE T (\n" + " K STRING(MAX),\n" + ") PRIMARY KEY(K)";
Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
op = op.waitFor();
Database db = op.getResult();
dbs.add(db);
assertThat(db.getId().getDatabase()).isEqualTo(dbId);
db = db.reload();
assertThat(db.getId().getDatabase()).isEqualTo(dbId);
String statement2 = "CREATE TABLE T2 (\n" + " K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
Operation<?, ?> op2 = db.updateDdl(ImmutableList.of(statement2), null);
op2.waitFor();
Iterable<String> statementsInDb = db.getDdl();
assertThat(statementsInDb).containsExactly(statement1, statement2);
db.drop();
dbs.clear();
expectedException.expect(isSpannerException(ErrorCode.NOT_FOUND));
db.reload();
}
use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class ITDatabaseAdminTest method updateDdlRetry.
@Test
public void updateDdlRetry() throws Exception {
String dbId = testHelper.getUniqueDatabaseId();
String instanceId = testHelper.getInstanceId().getInstance();
String statement1 = "CREATE TABLE T (\n" + " K STRING(MAX),\n" + ") PRIMARY KEY(K)";
Operation<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
op = op.waitFor();
Database db = op.getResult();
dbs.add(db);
String statement2 = "CREATE TABLE T2 (\n" + " K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
Operation<Void, UpdateDatabaseDdlMetadata> op1 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
Operation<Void, UpdateDatabaseDdlMetadata> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
op1 = op1.waitFor();
op2 = op2.waitFor();
assertThat(op1.getMetadata()).isEqualTo(op2.getMetadata());
}
use of com.google.cloud.spanner.Database in project google-cloud-java by GoogleCloudPlatform.
the class ITQueryTest method queryRealTable.
@Test
public void queryRealTable() {
Database populatedDb = env.getTestHelper().createTestDatabase("CREATE TABLE T ( K STRING(MAX) NOT NULL, V STRING(MAX) ) PRIMARY KEY (K)");
DatabaseClient client = env.getTestHelper().getDatabaseClient(populatedDb);
client.writeAtLeastOnce(Arrays.asList(Mutation.newInsertBuilder("T").set("K").to("k1").set("V").to("v1").build(), Mutation.newInsertBuilder("T").set("K").to("k2").set("V").to("v2").build(), Mutation.newInsertBuilder("T").set("K").to("k3").set("V").to("v3").build(), Mutation.newInsertBuilder("T").set("K").to("k4").set("V").to("v4").build()));
Statement statement = Statement.newBuilder("SELECT K, V FROM T WHERE K >= @min AND K < @max ORDER BY K ASC").bind("min").to("k13").bind("max").to("k32").build();
ResultSet resultSet = statement.executeQuery(client.singleUse(TimestampBound.strong()));
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getType()).isEqualTo(Type.struct(StructField.of("K", Type.string()), StructField.of("V", Type.string())));
assertThat(resultSet.getString(0)).isEqualTo("k2");
assertThat(resultSet.getString(1)).isEqualTo("v2");
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getString("K")).isEqualTo("k3");
assertThat(resultSet.getString("V")).isEqualTo("v3");
assertThat(resultSet.next()).isFalse();
}
Aggregations