Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Nested transactions for sqlx

License

NotificationsYou must be signed in to change notification settings

heetch/sqalx

Repository files navigation

sqalx

GoDocGo Report Card

sqalx (pronounced 'scale-x') is a library built on top ofsqlx that allows to seamlessly create nested transactions and to avoid thinking about whether or not a function is called within a transaction.With sqalx you can easily create reusable and composable functions that can be called within or out of transactions and that can create transactions themselves.

Getting started

$ go get github.com/heetch/sqalx

Import sqalx

import"github.com/heetch/sqalx"

Usage

package mainimport ("log""github.com/heetch/sqalx""github.com/jmoiron/sqlx"_"github.com/lib/pq")funcmain() {// Connect to PostgreSQL with sqlx.db,err:=sqlx.Connect("postgres","user=foo dbname=bar sslmode=disable")iferr!=nil {log.Fatal(err)}deferdb.Close()// Pass the db to sqalx.// It returns a sqalx.Node. A Node is a wrapper around sqlx.DB or sqlx.Tx.node,err:=sqalx.New(db)iferr!=nil {log.Fatal(err)}err=createUser(node)iferr!=nil {log.Fatal(err)}}funccreateUser(node sqalx.Node)error {// Exec a query_,_=node.Exec("INSERT INTO ....")// you can use a node as if it were a *sqlx.DB or a *sqlx.Tx// Let's create a transaction.// A transaction is also a sqalx.Node.tx,err:=node.Beginx()iferr!=nil {returnerr}defertx.Rollback()_,_=tx.Exec("UPDATE ...")// Now we call another function and pass it the transaction.err=updateGroups(tx)iferr!=nil {returnnil}returntx.Commit()}funcupdateGroups(node sqalx.Node)error {// Notice we are creating a new transaction.// This would normally cause a dead lock without sqalx.tx,err:=node.Beginx()iferr!=nil {returnerr}defertx.Rollback()_,_=tx.Exec("INSERT ...")_,_=tx.Exec("UPDATE ...")_,_=tx.Exec("DELETE ...")returntx.Commit()}

PostgreSQL Savepoints

When using the PostgreSQL driver, an option can be passed toNew to enable the use of PostgreSQLSavepoints for nested transactions.

node,err:=sqalx.New(db,sqalx.SavePoint(true))

Issue

Please open an issue if you encounter any problem.

Development

sqalx is covered by a go test suite. In order to test against specific databases we include a docker-compose file that runs Postgres and MySQL.

Running all tests

To run the tests, first rundocker-compose up to run both Postgres and MySQL in locally-exposed docker images. Then run your tests viamake test which sets up the above described data sources and runs all tests.

Running specific tests

To test against the Postgres instance be sure to export the following DSN:

export POSTGRESQL_DATASOURCE="postgresql://sqalx:sqalx@localhost:5432/sqalx?sslmode=disable"

To test against the MySQL instance be sure to export the following DSN:

export MYSQL_DATASOURCE="sqalx:sqalx@tcp(localhost:3306)/sqalx"

To test against SQlite export the following DSN:

export SQLITE_DATASOURCE=":memory:"

Note: If you are developing on an M1 Mac you will need to use the officially supported by Oracle image rather than the defaultmysql:tag image. It is commented out indocker-compose.yml.

License

The library is released under the MIT license. SeeLICENSE file.


[8]ページ先頭

©2009-2025 Movatter.jp