From eefdf2b42491a37bb15ac9147350b0b57b11618e Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent Date: Mon, 6 Nov 2017 14:32:28 -0800 Subject: [PATCH] Updated SpannerSample to highlight bound parameters. --- .../com/example/spanner/SpannerSample.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java index 7b83efb1d9d..bae2b2d6965 100644 --- a/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java +++ b/spanner/cloud-client/src/main/java/com/example/spanner/SpannerSample.java @@ -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",