Documentation ¶
Overview ¶
Package stmt builds on top of package bsq to make programming with prepared statements and sqlize more convenient.
Index ¶
- func Close(stmts ...*Stmt) error
- func Create(tx *Tx, stmt *Stmt, args ...interface{}) (id int64, err error)
- func CreateNamed(tx *Tx, stmt *Stmt, args ...sql.NamedArg) (id int64, err error)
- func Xaction(q sqlize.Querier, recovr bool, do func(*Tx) error) (err error)
- type CRUD
- func (crud *CRUD) Close() error
- func (crud *CRUD) Create(tx *Tx, args ...interface{}) (id int64, err error)
- func (crud *CRUD) CreateNamed(tx *Tx, args ...sql.NamedArg) (id int64, err error)
- func (crud *CRUD) Delete(tx *Tx, id interface{}) (sql.Result, error)
- func (crud *CRUD) Prepare(db sqlize.DB, d Dialect) error
- func (crud *CRUD) Read(tx *Tx, id interface{}) *sql.Row
- func (crud *CRUD) Update(tx *Tx, id interface{}, args ...interface{}) (sql.Result, error)
- func (crud *CRUD) UpdateNamed(tx *Tx, id interface{}, args ...sql.NamedArg) (sql.Result, error)
- type CloseError
- type Dialect
- type Group
- type RepoBase
- type Stmt
- func (stmt *Stmt) Bind(args ...sql.NamedArg) []interface{}
- func (stmt *Stmt) Close() error
- func (stmt *Stmt) Dialect() Dialect
- func (stmt *Stmt) Exec(tx *Tx, args ...interface{}) (res sql.Result, err error)
- func (stmt *Stmt) ExecContext(ctx context.Context, tx *Tx, args ...interface{}) (res sql.Result, err error)
- func (stmt *Stmt) ExecNamed(tx *Tx, args ...sql.NamedArg) (sql.Result, error)
- func (stmt *Stmt) ExecNamedContext(ctx context.Context, tx *Tx, args ...sql.NamedArg) (sql.Result, error)
- func (stmt *Stmt) MustPrepare(db sqlize.DB, d Dialect) *Stmt
- func (stmt *Stmt) MustSQL(d Dialect) (string, interface{})
- func (stmt *Stmt) Prepare(db sqlize.DB, d Dialect) error
- func (stmt *Stmt) Query(tx *Tx, args ...interface{}) (*sql.Rows, error)
- func (stmt *Stmt) QueryContext(ctx context.Context, tx *Tx, args ...interface{}) (*sql.Rows, error)
- func (stmt *Stmt) QueryNamed(tx *Tx, args ...sql.NamedArg) (*sql.Rows, error)
- func (stmt *Stmt) QueryNamedContext(ctx context.Context, tx *Tx, args ...sql.NamedArg) (*sql.Rows, error)
- func (stmt *Stmt) QueryRow(q *Tx, args ...interface{}) *sql.Row
- func (stmt *Stmt) QueryRowContext(ctx context.Context, q *Tx, args ...interface{}) *sql.Row
- func (stmt *Stmt) QueryRowNamedContext(ctx context.Context, tx *Tx, args ...sql.NamedArg) *sql.Row
- func (stmt *Stmt) SQL(d Dialect) (string, interface{}, error)
- type Tx
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Xaction ¶
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 (*CRUD) CreateNamed ¶
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 GetDialect ¶
func MustDefaultDialect ¶
func MustDefaultDialect() Dialect
type Stmt ¶
type Stmt struct {
// contains filtered or unexported fields
}
func (*Stmt) ExecContext ¶
func (*Stmt) ExecNamedContext ¶
func (*Stmt) QueryContext ¶
func (*Stmt) QueryNamed ¶
func (*Stmt) QueryNamedContext ¶
func (*Stmt) QueryRowContext ¶
func (*Stmt) QueryRowNamedContext ¶
type Tx ¶
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:
Click to show internal directories.
Click to hide internal directories.