Skip to content

Commit

Permalink
Updated SpannerSample to highlight bound parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtisvg committed Nov 6, 2017
1 parent c0b17fb commit 05903c4
Showing 1 changed file with 13 additions and 10 deletions.
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 @EndTitle < 'Goo'")
// 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

0 comments on commit 05903c4

Please sign in to comment.