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

Updated SpannerSample to highlight bound parameters. #910

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Changes from all 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 @@ -306,16 +306,19 @@ static void addIndex(DatabaseAdminClient adminClient, DatabaseId dbId) {
// "CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)".
// [START query_index]
static void queryUsingIndex(DatabaseClient dbClient) {
ResultSet resultSet =
dbClient
.singleUse()
.executeQuery(
// We use FORCE_INDEX hint to specify which index to use. For more details see
// https://cloud.google.com/spanner/docs/query-syntax#from-clause
Statement.of(
"SELECT AlbumId, AlbumTitle, MarketingBudget\n"
+ "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle}\n"
+ "WHERE AlbumTitle >= 'Aardvark' AND AlbumTitle < 'Goo'"));
Statement statement = Statement
// We use FORCE_INDEX hint to specify which index to use. For more details see
// https://cloud.google.com/spanner/docs/query-syntax#from-clause
.newBuilder("SELECT AlbumId, AlbumTitle, MarketingBudget\n"
+ "FROM Albums@{FORCE_INDEX=AlbumsByAlbumTitle}\n"
+ "WHERE AlbumTitle >= @StartTitle AND AlbumTitle < @EndTitle")
// We use @BoundParameters to help speed up frequently executed queries.
// For more details see https://cloud.google.com/spanner/docs/sql-best-practices
.bind("StartTitle").to("Aardvark")
.bind("EndTitle").to("Goo")
.build();

ResultSet resultSet = dbClient.singleUse().executeQuery(statement);
while (resultSet.next()) {
System.out.printf(
"%d %s %s\n",
Expand Down