Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code snippet to delete rows. #1269

Merged
merged 6 commits into from
Nov 26, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,30 @@ static void writeExampleData(DatabaseClient dbClient) {
}
// [END spanner_insert_data]

// [START spanner_delete_data]
static void deleteExampleData(DatabaseClient dbClient) {
List<Mutation> mutations = new ArrayList<>();

// KeySet.all() can be used to delete all the rows in a table.
mutations.add(Mutation.delete("Albums", KeySet.all()));

// KeySet.singleKey() can be used to delete one row at a time.
for (Singer singer : SINGERS) {
mutations.add(
Mutation.delete("Singers",
KeySet.singleKey(Key.newBuilder().append(singer.singerId).build())));
}

// Because Albums is created with ON DELETE CASCADE,
// you can delete all the rows in both tables
// by deleting the entire Singers table.
// mutations.add(Mutation.delete("Singers", KeySet.all()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentionally commented out?

It looks like it'll be a no-op since both tables are already removed, but it it's valid to include, uncommenting it will ensure the sample syntax is correct and it compiles.

Alternatively, remove this whole comment block. It's a bit confusing to see two ways to do the same thing in the same sample.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deleted it. It's another way of doing it and it was intentionally commented out. I can write about it in the topic text instead. It's the easier way to delete both tables, but the more difficult part of deletion is setting up the keys correctly. I wanted to show how to set up the keys.


dbClient.write(mutations);
System.out.printf("Records deleted.\n");
}
// [END spanner_delete_data]

// [START spanner_query_data]
static void query(DatabaseClient dbClient) {
// singleUse() can be used to execute a single read or query against Cloud Spanner.
Expand Down Expand Up @@ -1077,6 +1101,9 @@ static void run(
case "write":
writeExampleData(dbClient);
break;
case "delete":
deleteExampleData(dbClient);
break;
case "query":
query(dbClient);
break;
Expand Down Expand Up @@ -1194,6 +1221,7 @@ static void printUsageAndExit() {
System.err.println("Examples:");
System.err.println(" SpannerExample createdatabase my-instance example-db");
System.err.println(" SpannerExample write my-instance example-db");
System.err.println(" SpannerExample delete my-instance example-db");
System.err.println(" SpannerExample query my-instance example-db");
System.err.println(" SpannerExample read my-instance example-db");
System.err.println(" SpannerExample addmarketingbudget my-instance example-db");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public void testSample() throws Exception {

runSample("write");

out = runSample("delete");
assertThat(out).contains("Records deleted.");

runSample("write");

out = runSample("read");
assertThat(out).contains("1 1 Total Junk");

Expand Down