sqlx

package module
v0.0.0-...-a6e79c1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2018 License: MIT Imports: 4 Imported by: 0

README

sqlx-transactionmanager

Transaction handling for database it extends https://github.com/jmoiron/sqlx

GoDoc Build Status Coverage Status Go Report Card

Synopsis

Standard
db := sqlx.MustOpen("mysql", dsn())

// starts transaction statements
tx, err := db.BeginTxm()
if err != nil {
    panic(err)
}
// Do rollbacks if fail something in transaction.
// But do not commits if already commits in transaction.
defer func() {
    if err := tx.Rollback(); err != nil {
        // Actually, you should do something...
        panic(err)
    }
}()

tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "Code", "Hex", "x00.x7f@gmail.com")
tx.MustExec("UPDATE person SET email = ? WHERE first_name = ? AND last_name = ?", "a@b.com", "Code", "Hex")

var p Person
if err := tx.Get(&p, "SELECT * FROM person LIMIT 1"); err != nil {
    return err
}

// transaction commits
if err := tx.Commit(); err != nil {
    return err
}

fmt.Println(p)
Nested Transaction
db := sqlx.MustOpen("mysql", dsn())

func() {
    // We should prepare to recover from panic.
    defer func() {
        if r := recover(); r != nil {
            // Do something recover process
        }
    }()
    // Start nested transaction.
    // To be simple, we will cause panic if something sql process if failed.
    func() {
        // starts transaction statements
        tx, err := db.BeginTxm()
        if err != nil {
            panic(err)
        }
        // Do rollbacks if fail something in nested transaction.
        defer tx.Rollback()
        func() {
            // You don't need error handle in already began transaction.
            tx2, _ := db.BeginTxm()
            defer tx2.Rollback()
            tx2.MustExec("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "Code", "Hex", "x00.x7f@gmail.com")
            // Do something processing.
            // You should cause panic() if something failed.
            if err := tx2.Commit(); err != nil {
                panic(err)
            }
        }()
        tx.MustExec("UPDATE person SET email = ? WHERE first_name = ? AND last_name = ?", "a@b.com", "Code", "Hex")
        if err := tx.Commit(); err != nil {
            panic(err)
        }
    }()
}()

var p Person
if err := tx.Get(&p, "SELECT * FROM person LIMIT 1"); err != nil {
    return err
}

fmt.Println(p)
Transaction block
var p Person
if err := tm.Run(db, func(tx tm.Executor) error {
    _, err := tx.Exec("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)", "Al", "Paca", "x00.x7f@gmail.com")
    if err != nil {
        return err
    }
    _, err = tx.Exec("UPDATE person SET email = ? WHERE first_name = ? AND last_name = ?", "x@h.com", "Al", "Paca")
    if err != nil {
        return err
    }

    return tx.QueryRow("SELECT * FROM person LIMIT 1").Scan(&p.FirstName, &p.LastName, &p.Email, &p.AddedAt)
}); err != nil {
    panic(err)
}
println(&p)

if err := tm.Runx(db, func(tx tm.Executorx) error {
    tx.MustExec(tx.Rebind("INSERT INTO person (first_name, last_name, email) VALUES (?, ?, ?)"), "Code", "Hex", "x00.x7f@gmail.com")
    tx.MustExec(tx.Rebind("UPDATE person SET email = ? WHERE first_name = ? AND last_name = ?"), "a@b.com", "Code", "Hex")
    if err := tx.Get(&p, "SELECT * FROM person ORDER BY first_name DESC LIMIT 1"); err != nil {
        return err
    }
    return nil
}); err != nil {
    panic(err)
}
println(&p)

Description

sqlx-transactionmanager is a simple transaction manager. This package provides nested transaction management on multi threads.

See more details example for extends sqlx or example for transaction block if you want to know how to use this.

Install

go get github.com/Code-Hex/sqlx-transactionmanager

Contributing

I'm looking forward you to send pull requests or reporting issues.

License

MIT

Author

CodeHex

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func In

func In(query string, args ...interface{}) (string, []interface{}, error)

In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.

Types

type DB

type DB struct {
	*sqlxx.DB
	// contains filtered or unexported fields
}

DB is a wrapper around *github.com/jmoiron/sqlx.DB which manages transaction.

func MustOpen

func MustOpen(driverName, dataSourceName string) *DB

MustOpen returns only pointer of DB struct to manage transaction. But If you cause something error, It will do panic.

func Open

func Open(driverName, dataSourceName string) (*DB, error)

Open returns pointer of DB struct to manage transaction. It struct wrapped *github.com/jmoiron/sqlx.DB So we can use some methods of *github.com/jmoiron/sqlx.DB.

func (*DB) BeginTxm

func (db *DB) BeginTxm() (*Txm, error)

BeginTxm begins a transaction and returns pointer of transaction manager. Actually, This method will invoke *github.com/jmoiron/sqlx.Beginx(). but returns error if failed it.

func (*DB) BeginTxmx

func (db *DB) BeginTxmx(ctx context.Context, opts *sql.TxOptions) (*Txm, error)

BeginTxmx begins a transaction and returns pointer of transaction manager.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginxContext is canceled.

func (*DB) Close

func (db *DB) Close() error

Close closes *github.com/jmoiron/sqlx.DB

func (*DB) MustBeginTxm

func (db *DB) MustBeginTxm() *Txm

MustBeginTxm is like BeginTxm but panics if BeginTxm cannot begin transaction.

func (*DB) MustBeginTxmx

func (db *DB) MustBeginTxmx(ctx context.Context, opts *sql.TxOptions) (*Txm, error)

MustBeginTxmx is like BeginTxmx but panics if BeginTxmx cannot begin transaction.

func (*DB) SQL

func (db *DB) SQL() *sql.DB

SQL returns *sql.DB The reason for writing this method is that it needs to be written as *db.DB.DB to access *sql.DB.

type NestedCommitErr

type NestedCommitErr struct{}

NestedCommitErr is an error type to notice that commit in nested transaction.

func (*NestedCommitErr) Error

func (n *NestedCommitErr) Error() string

type Txm

type Txm struct {
	*sqlxx.Tx
	// contains filtered or unexported fields
}

Txm is a wrapper around *github.com/jmoiron/sqlx.DB with extra functionality and manages transaction.

func (*Txm) Commit

func (t *Txm) Commit() error

Commit commits the transaction.

func (*Txm) Rollback

func (t *Txm) Rollback() error

Rollback rollbacks the transaction.

Directories

Path Synopsis
eg
tm

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL