Spansqlx - Spanner sql pkgs with golang

spansqlx

spanner sql pkgs

install

go get github.com/reiot777/spansqlx

usage

Below is an example which shows some common use cases for spansqlx. Check example_test.go for more usage.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/reiot777/spansqlx"
)

func main() {
	var database = "projects/sandbox/instances/sandbox/databases/sandbox"
	// this Pings the spanner database trying to connect
	// use spansqlx.Open() for spanner.NewClient() semantics
	db, err := spansqlx.Open(context.Background(), spansqlx.WithDatabase(database))
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// use TxPipeline (spanner.ReadWriteTransation), exec multi statements.
	if err := db.TxPipeline(context.Background(), func(ctx context.Context) error {
		var sqlInsertSingers = `INSERT INTO Singers (SingerId, FirstName, LastName) VALUES(@singer_id, @first_name, @last_name)`
		for _, singer := range allSingers {
			if err := db.Exec(ctx, sqlInsertSingers,
				singer.SingerID,
				singer.FirstName,
				singer.LastName,
			); err != nil {
				return err
			}
		}

		var sqlInsertAlbums = `INSERT INTO Albums (SingerID, AlbumID, AlbumTitle) VALUES (@SingerID, @AlbumID, @AlbumTitle)`
		for _, album := range allAlbums {
			if err := db.NamedExec(ctx, sqlInsertAlbums, album); err != nil {
				return err
			}
		}

		return nil
	}); err != nil {
		log.Fatalf("failed to spansqlx.TxPipeline %v", err)
	}

	// Query the database, storing results in a []Singer (wrapped in []interface)
	if err := db.Select(context.Background(), nil, `SELECT * FROM Singers ORDER BY FirstName DESC`); err != nil {
		log.Fatal(err)
	}

	// You can also get a a single result
	var david Singer
	db.Get(context.Background(), &david, `SELECT * FROM Singers WHERE FirstName=first_name`, "David")
	fmt.Printf("%#v\n", david)
}
Similar Resources

Golang REST Layer SQL Storage Handler

This REST Layer resource storage backend stores data in a SQL Database using database/sql.

Feb 15, 2022

Go package for sharding databases ( Supports every ORM or raw SQL )

Go package for sharding databases ( Supports every ORM or raw SQL )

Octillery Octillery is a Go package for sharding databases. It can use with every OR Mapping library ( xorm , gorp , gorm , dbr ...) implementing data

Dec 16, 2022

Prep finds all SQL statements in a Go package and instruments db connection with prepared statements

Prep Prep finds all SQL statements in a Go package and instruments db connection with prepared statements. It allows you to benefit from the prepared

Dec 10, 2022

pggen - generate type safe Go methods from Postgres SQL queries

pggen - generate type safe Go methods from Postgres SQL queries pggen is a tool that generates Go code to provide a typesafe wrapper around Postgres q

Jan 3, 2023

🐳 A most popular sql audit platform for mysql

🐳 A most popular sql audit platform for mysql

🐳 A most popular sql audit platform for mysql

Jan 6, 2023

sqlx is a library which provides a set of extensions on go's standard database/sql library

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

Jan 7, 2023

A tool to run queries in defined frequency and expose the count as prometheus metrics. Supports MongoDB and SQL

A tool to run queries in defined frequency and expose the count as prometheus metrics. Supports MongoDB and SQL

query2metric A tool to run db queries in defined frequency and expose the count as prometheus metrics. Why ? Product metrics play an important role in

Jul 1, 2022

Dumpling is a fast, easy-to-use tool written by Go for dumping data from the database(MySQL, TiDB...) to local/cloud(S3, GCP...) in multifarious formats(SQL, CSV...).

🥟 Dumpling Dumpling is a tool and a Go library for creating SQL dump from a MySQL-compatible database. It is intended to replace mysqldump and mydump

Nov 9, 2022

Universal command-line interface for SQL databases

usql A universal command-line interface for PostgreSQL, MySQL, Oracle Database, SQLite3, Microsoft SQL Server, and many other databases including NoSQ

Jan 9, 2023
write APIs using direct SQL queries with no hassle, let's rethink about SQL

SQLer SQL-er is a tiny portable server enables you to write APIs using SQL query to be executed when anyone hits it, also it enables you to define val

Jan 7, 2023
Parses a file and associate SQL queries to a map. Useful for separating SQL from code logic

goyesql This package is based on nleof/goyesql but is not compatible with it any more. This package introduces support for arbitrary tag types and cha

Oct 20, 2021
Go-sql-reader - Go utility to read the externalised sql with predefined tags

go-sql-reader go utility to read the externalised sql with predefined tags Usage

Jan 25, 2022
a golang library for sql builder

Gendry gendry is a Go library that helps you operate database. Based on go-sql-driver/mysql, it provides a series of simple but useful tools to prepar

Dec 26, 2022
Fluent SQL generation for golang

Squirrel is "complete". Bug fixes will still be merged (slowly). Bug reports are welcome, but I will not necessarily respond to them. If another fork

Dec 29, 2022
convert sql to elasticsearch DSL in golang(go)

_____ _ _ ____ _____ ___ ____ ____ ___ _ | ____| | / \ / ___|_ _|_ _|/ ___|/ ___| / _ \ | | | _| | | / _ \ \___ \ |

Jan 7, 2023
GoTSQL : A Better Way to Organize SQL codebase using Templates in Golang

GoTSQL - A Better Way to Organize SQL codebase using Templates in Golang Installation through Go Get command $ go get github.com/migopsrepos/gotsql In

Aug 17, 2022
SQL transaction wrapper on golang

TxWrapper TxWrapper is a sql transaction wrapper. It helps to exclude writing code for rollback and commit commands. Usage import ( "context"

Mar 14, 2022
Simple SQL escape and format for golang

sqlstring Simple SQL escape and format Escaping sql values //Format sql := sqlstring.Format("select * from users where name=? and age=? limit ?,?", "t

Sep 4, 2022
BigQuery database/sql golang driver

BigQuery SQL Driver This library is compatible with Go 1.17+ Please refer to CHA

Dec 7, 2022