All Projects → rakyll → go-sql-driver-spanner

rakyll / go-sql-driver-spanner

Licence: Apache-2.0 license
Google Cloud Spanner driver for Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-sql-driver-spanner

spanner-bench
Google Cloud Spanner Query Planner Benchmarking
Stars: ✭ 24 (-72.73%)
Mutual labels:  google-cloud-spanner
spannerz
Google Cloud Spanner Query Planner Visualizer
Stars: ✭ 60 (-31.82%)
Mutual labels:  google-cloud-spanner
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 2,315 (+2530.68%)
Mutual labels:  google-cloud-spanner
Migrate
Database migrations. CLI and Golang library.
Stars: ✭ 7,712 (+8663.64%)
Mutual labels:  google-cloud-spanner
hammer
🛠 hammer is a command-line tool to schema management for Google Cloud Spanner.
Stars: ✭ 38 (-56.82%)
Mutual labels:  google-cloud-spanner
spanner-schema-diff-tool
Compare two Cloud Spanner Schema (DDL) files, determine the differences and generate the required ALTER statements to convert one schema to the other.
Stars: ✭ 20 (-77.27%)
Mutual labels:  google-cloud-spanner
google-cloud-cpp-spanner
C++ client library for Google Cloud Spanner
Stars: ✭ 26 (-70.45%)
Mutual labels:  google-cloud-spanner

go-sql-driver-spanner

CircleCI go.dev reference

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

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)

Transactions

  • 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.

Emulator

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

Troubleshooting

This driver shouldn't automatically retry the transactions but it does. It causes unwanted results. Don't use this library in production yet.


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.


error = <use T(nil), not nil>: Use a typed nil, instead of just nil.

The following query returns rows with NULL likes:

var nilInt64 *int64
db.QueryContext(ctx, "SELECT id, text FROM tweets WHERE likes = @likes LIMIT 10", nilInt64)

When querying and executing with emails, pass them as arguments and don't hardcode them in the query:

db.QueryContext(ctx, "SELECT id, name ... WHERE email = @email", "[email protected]")

The driver will relax this requirement in the future but it is a work-in-progress for now.


DDLs are not supported in the transactions per Cloud Spanner restriction. Instead, run them against the database:

db.ExecContext(ctx, "CREATE TABLE ...")

Disclaimer

This is not an officially supported Google Cloud product.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].