stmt

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package stmt builds on top of package bsq to make programming with prepared statements and sqlize more convenient.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(stmts ...*Stmt) error

func Create

func Create(tx *Tx, stmt *Stmt, args ...interface{}) (id int64, err error)

func CreateNamed

func CreateNamed(tx *Tx, stmt *Stmt, args ...sql.NamedArg) (id int64, err error)

func Xaction

func Xaction(q sqlize.Querier, recovr bool, do func(*Tx) error) (err error)

Xaction is the variant of sqlize.Xaction that provides a stmt.Tx ready for use with prepared statements.

Example
db, _ := sql.Open("driver", "data source")
db.Exec(`CREATE TABLE t (id integer, name text)`)
sqlSelect := New(0, `SELECT * from t`).MustPrepare(db, nil)
err := Xaction(db, false, func(tx *Tx) (err error) {
	_, err = sqlSelect.Query(tx)
	return err
})
fmt.Println(err)
Output:

Types

type CRUD

type CRUD struct {
	CreateStmt *Stmt
	ReadStmt   *Stmt
	UpdateStmt *Stmt
	DeleteStmt *Stmt
	// contains filtered or unexported fields
}

func NewCRUD

func NewCRUD(id bsq.Columner, g *Group) *CRUD

func (*CRUD) Close

func (crud *CRUD) Close() error

func (*CRUD) Create

func (crud *CRUD) Create(tx *Tx, args ...interface{}) (id int64, err error)

func (*CRUD) CreateNamed

func (crud *CRUD) CreateNamed(tx *Tx, args ...sql.NamedArg) (id int64, err error)

func (*CRUD) Delete

func (crud *CRUD) Delete(tx *Tx, id interface{}) (sql.Result, error)

func (*CRUD) Prepare

func (crud *CRUD) Prepare(db sqlize.DB, d Dialect) error

func (*CRUD) Read

func (crud *CRUD) Read(tx *Tx, id interface{}) *sql.Row

func (*CRUD) Update

func (crud *CRUD) Update(tx *Tx, id interface{}, args ...interface{}) (sql.Result, error)

func (*CRUD) UpdateNamed

func (crud *CRUD) UpdateNamed(tx *Tx, id interface{}, args ...sql.NamedArg) (sql.Result, error)

type CloseError

type CloseError []error

func (CloseError) Error

func (e CloseError) Error() string

func (CloseError) Unwrap

func (e CloseError) Unwrap() error

type Dialect

type Dialect interface {
	bsq.Dialect
	StmtCreate(q *Tx, stmt *Stmt, args ...interface{}) (id int64, err error)
}

func DefaultDialect

func DefaultDialect() (Dialect, error)

func GetDialect

func GetDialect(name string) Dialect

func MustDefaultDialect

func MustDefaultDialect() Dialect

type Group

type Group struct {
	// contains filtered or unexported fields
}

func (*Group) Add

func (g *Group) Add(defn *Stmt) *Stmt

func (*Group) Close

func (g *Group) Close() error

func (*Group) ForEach

func (g *Group) ForEach(do func(*Stmt) error) error

func (*Group) Prepare

func (g *Group) Prepare(db sqlize.DB, d Dialect) error

type RepoBase added in v0.3.2

type RepoBase struct {
	StmtCreate *Stmt
	StmtRead   *Stmt
	StmtUpdate *Stmt
	StmtDelete *Stmt
}

func NewRepoBase added in v0.3.2

func NewRepoBase(db sqlize.DB, d Dialect, idColumn *bsq.Column, cfg *sqlize.RepoConfig) (*RepoBase, error)

type Stmt

type Stmt struct {
	// contains filtered or unexported fields
}

func New

func New(flags bsq.WriteFlag, query ...interface{}) *Stmt

func (*Stmt) Bind

func (stmt *Stmt) Bind(args ...sql.NamedArg) []interface{}

func (*Stmt) Close

func (stmt *Stmt) Close() error

func (*Stmt) Dialect

func (stmt *Stmt) Dialect() Dialect

func (*Stmt) Exec

func (stmt *Stmt) Exec(tx *Tx, args ...interface{}) (res sql.Result, err error)

func (*Stmt) ExecContext

func (stmt *Stmt) ExecContext(ctx context.Context, tx *Tx, args ...interface{}) (res sql.Result, err error)

func (*Stmt) ExecNamed

func (stmt *Stmt) ExecNamed(tx *Tx, args ...sql.NamedArg) (sql.Result, error)

func (*Stmt) ExecNamedContext

func (stmt *Stmt) ExecNamedContext(ctx context.Context, tx *Tx, args ...sql.NamedArg) (sql.Result, error)

func (*Stmt) MustPrepare

func (stmt *Stmt) MustPrepare(db sqlize.DB, d Dialect) *Stmt

func (*Stmt) MustSQL

func (stmt *Stmt) MustSQL(d Dialect) (string, interface{})

func (*Stmt) Prepare

func (stmt *Stmt) Prepare(db sqlize.DB, d Dialect) error

func (*Stmt) Query

func (stmt *Stmt) Query(tx *Tx, args ...interface{}) (*sql.Rows, error)

func (*Stmt) QueryContext

func (stmt *Stmt) QueryContext(ctx context.Context, tx *Tx, args ...interface{}) (*sql.Rows, error)

func (*Stmt) QueryNamed

func (stmt *Stmt) QueryNamed(tx *Tx, args ...sql.NamedArg) (*sql.Rows, error)

func (*Stmt) QueryNamedContext

func (stmt *Stmt) QueryNamedContext(ctx context.Context, tx *Tx, args ...sql.NamedArg) (*sql.Rows, error)

func (*Stmt) QueryRow

func (stmt *Stmt) QueryRow(q *Tx, args ...interface{}) *sql.Row

func (*Stmt) QueryRowContext

func (stmt *Stmt) QueryRowContext(ctx context.Context, q *Tx, args ...interface{}) *sql.Row

func (*Stmt) QueryRowNamedContext

func (stmt *Stmt) QueryRowNamedContext(ctx context.Context, tx *Tx, args ...sql.NamedArg) *sql.Row

func (*Stmt) SQL

func (stmt *Stmt) SQL(d Dialect) (string, interface{}, error)

type Tx

type Tx struct {
	sqlize.Tx
	// contains filtered or unexported fields
}

Tx is a transaction that manages its actual statements prepared from the Defns. I.e. Tx.Stmt is called at most once and only on demand for a Defn and all prepared statements are closed when Tx is Closed.

Example
db, _ := sql.Open("driver", "data source")
db.Exec(`CREATE TABLE t (id integer, name text)`)
sqlSelect := New(0, `SELECT * from t`).MustPrepare(db, nil)
// Select without transaction
_, err := sqlSelect.Query(nil)
fmt.Println(err)
// Select with transaction (see also stmt.Xaction)
tx, _ := db.Begin()
stx := WrapTx(tx)
_, err = sqlSelect.Query(stx)
stx.Close()
if err != nil {
	tx.Rollback()
} else {
	tx.Commit()
}
fmt.Println(err)
Output:

Example (WithSqlizeXaction)
db, _ := sql.Open("driver", "data source")
db.Exec(`CREATE TABLE t (id integer, name text)`)
sqlSelect := New(0, `SELECT * from t`).MustPrepare(db, nil)
// See also stmt.Xaction
err := sqlize.Xaction(db, false, func(tx sqlize.Tx) (err error) {
	stx := WrapTx(tx)
	defer stx.Close()
	_, err = sqlSelect.Query(stx)
	return err
})
fmt.Println(err)
Output:

func NewTx

func NewTx(db sqlize.DB) (*Tx, error)

func ToTx

func ToTx(q sqlize.Querier) *Tx

ToTx makes sure that it returns a *Tx if q is a transaction. Otherwise it returns nil.

func WrapTx

func WrapTx(tx sqlize.Tx) *Tx

func (*Tx) Close

func (xa *Tx) Close() error

Jump to

Keyboard shortcuts

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