Google Cloud Spanner driver for Go's database/sql package.
THIS IS A WORK-IN-PROGRESS, DON'T USE IT IN PRODUCTION YET.
import _ "github.com/rakyll/go-sql-driver-spanner"
db, err := sql.Open("spanner", "projects/PROJECT/instances/INSTANCE/databases/DATABASE")
if err != nil {
log.Fatal(err)
}
// Print tweets with more than 500 likes.
rows, err := db.QueryContext(ctx, "SELECT id, text FROM tweets WHERE likes > @likes", 500)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var (
id int64
text string
)
for rows.Next() {
if err := rows.Scan(&id, &text); err != nil {
log.Fatal(err)
}
fmt.Println(id, text)
}
Statements support follows the official Google Cloud Spanner Go client style arguments.
db.QueryContext(ctx, "SELECT id, text FROM tweets WHERE likes > @likes", 500)
db.ExecContext(ctx, "INSERT INTO tweets (id, text, rts) VALUES (@id, @text, @rts)", id, text, 10000)
db.ExecContext(ctx, "DELETE FROM tweets WHERE id = @id", 14544498215374)
- Read-only transactions do strong-reads only.
- Read-write transactions always uses the strongest isolation level and ignore the user-specified level.
tx, err := db.BeginTx(ctx, &sql.TxOptions{
ReadOnly: true, // Read-only transaction.
})
tx, err := db.BeginTx(ctx, &sql.TxOptions{}) // Read-write transaction.
See the Google Cloud Spanner Emulator support to learn how to start the emulator. Once the emulator is started and the host environmental flag is set, the driver should just work.
$ gcloud beta emulators spanner start
$ export SPANNER_EMULATOR_HOST=localhost:9010
The driver will propagate any Aborted error that is returned by Cloud Spanner during a read/write transaction, and it will currently not automatically retry the transaction.
gorm cannot use the driver as it-is but @rakyll has been working on a dialect. She doesn't have bandwidth to ship a fully featured dialect right now but contact her if you would like to contribute.
DDLs are not supported in the transactions per Cloud Spanner restriction. Instead, run them against the database:
db.ExecContext(ctx, "CREATE TABLE ...")
This is not an officially supported Google Cloud product.