55[ Google Cloud Spanner] ( https://cloud.google.com/spanner ) driver for
66Go's [ database/sql] ( https://golang.org/pkg/database/sql/ ) package.
77
8+ This driver can be used with both Spanner GoogleSQL and Spanner PostgreSQL databases.
9+
810``` go
911import _ " github.com/googleapis/go-sql-spanner"
1012
@@ -13,8 +15,8 @@ if err != nil {
1315 log.Fatal (err)
1416}
1517
16- // Print tweets with more than 500 likes.
17- rows , err := db.QueryContext (ctx, " SELECT id, text FROM tweets WHERE likes > @likes " , sql. Named ( " likes " , 500 ) )
18+ // Print singers with more than 500 likes.
19+ rows , err := db.QueryContext (ctx, " SELECT id, name FROM singers WHERE likes > ? " , 500 )
1820if err != nil {
1921 log.Fatal (err)
2022}
@@ -55,6 +57,17 @@ db.ExecContext(ctx, "INSERT INTO tweets (id, text, rts) VALUES (@id, @text, @rts
5557 sql.Named (" id" , id), sql.Named (" text" , text), sql.Named (" rts" , 10000 ))
5658```
5759
60+ ### Using PostgreSQL-style positional arguments
61+
62+ When connected to a PostgreSQL-dialect Spanner database, you can also use PostgreSQL-style
63+ query parameters ` $1, $2, ..., $n ` . These query parameters are handled as positional parameters.
64+
65+ ``` go
66+ db.QueryContext (ctx, " SELECT id, text FROM tweets WHERE likes > $1" , 500 )
67+
68+ db.ExecContext (ctx, " INSERT INTO tweets (id, text, rts) VALUES ($1, $2, $3)" , id, text, 10000 )
69+ ```
70+
5871### Using named parameters with positional arguments (not recommended)
5972Named parameters can also be used in combination with positional arguments,
6073but this is __ not recommended__ , as the behavior can be hard to predict if
@@ -215,10 +228,19 @@ $ export SPANNER_EMULATOR_HOST=localhost:9010
215228
216229## Spanner PostgreSQL Interface
217230
218- This driver only works with the Spanner GoogleSQL dialect. For the
219- Spanner PostgreSQL dialect, any PostgreSQL driver that implements the
220- [ database/sql] ( https://golang.org/pkg/database/sql/ ) interface can be used
221- in combination with
231+ This driver works with both Spanner GoogleSQL and PostgreSQL dialects.
232+ The driver automatically detects the dialect of the database that it is connected to.
233+
234+ Note that the types of query parameters that are supported depends on the dialect of the
235+ database that the driver is connected to:
236+ 1 . Positional parameters (` ? ` ): Supported for both dialects.
237+ 2 . Named parameters (` @id ` , ` @name ` , ...): Supported for both dialects.
238+ 3 . PostgreSQL-style positional parameters (` $1 ` , ` $2 ` , ...): Only supported for PostgreSQL databases.
239+
240+ ### Alternatives
241+
242+ You can also use Spanner PostgreSQL dialect databases with any off-the-shelf PostgreSQL driver that
243+ implements the [ database/sql] ( https://golang.org/pkg/database/sql/ ) interface in combination with
222244[ PGAdapter] ( https://cloud.google.com/spanner/docs/pgadapter ) .
223245
224246For example, the [ pgx] ( https://github.com/jackc/pgx ) driver can be used in combination with
0 commit comments