Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 99bf734

Browse files
author
Takashi Matsuo
authored
feat: add spanner_create_index example (#1020)
* feat: add spanner_create_index example * address review comment
1 parent b5b4702 commit 99bf734

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

google/cloud/spanner/samples/samples.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,36 @@ void CreateDatabase(std::vector<std::string> const& argv) {
427427
(argv[0], argv[1], argv[2]);
428428
}
429429

430+
void AddIndex(std::vector<std::string> const& argv) {
431+
if (argv.size() != 3) {
432+
throw std::runtime_error(
433+
"add-index <project-id> <instance-id> <database-id>");
434+
}
435+
// [START spanner_create_index]
436+
using google::cloud::future;
437+
using google::cloud::StatusOr;
438+
[](std::string const& project_id, std::string const& instance_id,
439+
std::string const& database_id) {
440+
google::cloud::spanner::DatabaseAdminClient client;
441+
google::cloud::spanner::Database database(project_id, instance_id,
442+
database_id);
443+
future<StatusOr<
444+
google::spanner::admin::database::v1::UpdateDatabaseDdlMetadata>>
445+
future = client.UpdateDatabase(
446+
database,
447+
{"CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)"});
448+
StatusOr<google::spanner::admin::database::v1::UpdateDatabaseDdlMetadata>
449+
result = future.get();
450+
if (!result) {
451+
throw std::runtime_error(result.status().message());
452+
}
453+
std::cout << "`AlbumsByAlbumTitle` Index successfully added, new DDL:\n"
454+
<< result->DebugString() << "\n";
455+
}
456+
// [END spanner_create_index]
457+
(argv[0], argv[1], argv[2]);
458+
}
459+
430460
//! [get-database]
431461
void GetDatabase(google::cloud::spanner::DatabaseAdminClient client,
432462
std::string const& project_id, std::string const& instance_id,
@@ -1289,6 +1319,7 @@ int RunOneCommand(std::vector<std::string> argv) {
12891319
{"remove-database-reader", &RemoveDatabaseReaderCommand},
12901320
{"instance-test-iam-permissions", &InstanceTestIamPermissionsCommand},
12911321
{"create-database", &CreateDatabase},
1322+
{"add-index", &AddIndex},
12921323
{"get-database", &GetDatabaseCommand},
12931324
{"get-database-ddl", &GetDatabaseCommandDdl},
12941325
{"add-column", &AddColumn},
@@ -1441,6 +1472,9 @@ void RunAll() {
14411472
std::cout << "\nRunning spanner_create_database sample\n";
14421473
RunOneCommand({"", "create-database", project_id, instance_id, database_id});
14431474

1475+
std::cout << "\nRunning spanner_create_index sample\n";
1476+
RunOneCommand({"", "add-index", project_id, instance_id, database_id});
1477+
14441478
std::cout << "\nRunning spanner get-database sample\n";
14451479
RunOneCommand({"", "get-database", project_id, instance_id, database_id});
14461480

0 commit comments

Comments
 (0)