|
|
@ -22,6 +22,7 @@ type Tx struct { |
|
|
|
ctx context.Context |
|
|
|
ctx context.Context |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// BeginTx begin a transaction with option
|
|
|
|
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) { |
|
|
|
func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) { |
|
|
|
hookCtx := contexts.NewContextHook(ctx, "BEGIN TRANSACTION", nil) |
|
|
|
hookCtx := contexts.NewContextHook(ctx, "BEGIN TRANSACTION", nil) |
|
|
|
ctx, err := db.beforeProcess(hookCtx) |
|
|
|
ctx, err := db.beforeProcess(hookCtx) |
|
|
@ -36,10 +37,12 @@ func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) { |
|
|
|
return &Tx{tx, db, ctx}, nil |
|
|
|
return &Tx{tx, db, ctx}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Begin begins a transaction
|
|
|
|
func (db *DB) Begin() (*Tx, error) { |
|
|
|
func (db *DB) Begin() (*Tx, error) { |
|
|
|
return db.BeginTx(context.Background(), nil) |
|
|
|
return db.BeginTx(context.Background(), nil) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Commit submit the transaction
|
|
|
|
func (tx *Tx) Commit() error { |
|
|
|
func (tx *Tx) Commit() error { |
|
|
|
hookCtx := contexts.NewContextHook(tx.ctx, "COMMIT", nil) |
|
|
|
hookCtx := contexts.NewContextHook(tx.ctx, "COMMIT", nil) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
@ -48,12 +51,10 @@ func (tx *Tx) Commit() error { |
|
|
|
} |
|
|
|
} |
|
|
|
err = tx.Tx.Commit() |
|
|
|
err = tx.Tx.Commit() |
|
|
|
hookCtx.End(ctx, nil, err) |
|
|
|
hookCtx.End(ctx, nil, err) |
|
|
|
if err := tx.db.afterProcess(hookCtx); err != nil { |
|
|
|
return tx.db.afterProcess(hookCtx) |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Rollback rollback the transaction
|
|
|
|
func (tx *Tx) Rollback() error { |
|
|
|
func (tx *Tx) Rollback() error { |
|
|
|
hookCtx := contexts.NewContextHook(tx.ctx, "ROLLBACK", nil) |
|
|
|
hookCtx := contexts.NewContextHook(tx.ctx, "ROLLBACK", nil) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
@ -62,12 +63,10 @@ func (tx *Tx) Rollback() error { |
|
|
|
} |
|
|
|
} |
|
|
|
err = tx.Tx.Rollback() |
|
|
|
err = tx.Tx.Rollback() |
|
|
|
hookCtx.End(ctx, nil, err) |
|
|
|
hookCtx.End(ctx, nil, err) |
|
|
|
if err := tx.db.afterProcess(hookCtx); err != nil { |
|
|
|
return tx.db.afterProcess(hookCtx) |
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PrepareContext prepare the query
|
|
|
|
func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) { |
|
|
|
func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) { |
|
|
|
names := make(map[string]int) |
|
|
|
names := make(map[string]int) |
|
|
|
var i int |
|
|
|
var i int |
|
|
@ -89,19 +88,23 @@ func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) { |
|
|
|
return &Stmt{stmt, tx.db, names, query}, nil |
|
|
|
return &Stmt{stmt, tx.db, names, query}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare prepare the query
|
|
|
|
func (tx *Tx) Prepare(query string) (*Stmt, error) { |
|
|
|
func (tx *Tx) Prepare(query string) (*Stmt, error) { |
|
|
|
return tx.PrepareContext(context.Background(), query) |
|
|
|
return tx.PrepareContext(context.Background(), query) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// StmtContext creates Stmt with context
|
|
|
|
func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt { |
|
|
|
func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt { |
|
|
|
stmt.Stmt = tx.Tx.StmtContext(ctx, stmt.Stmt) |
|
|
|
stmt.Stmt = tx.Tx.StmtContext(ctx, stmt.Stmt) |
|
|
|
return stmt |
|
|
|
return stmt |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Stmt creates Stmt
|
|
|
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt { |
|
|
|
func (tx *Tx) Stmt(stmt *Stmt) *Stmt { |
|
|
|
return tx.StmtContext(context.Background(), stmt) |
|
|
|
return tx.StmtContext(context.Background(), stmt) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ExecMapContext executes query with args in a map
|
|
|
|
func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error) { |
|
|
|
func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{}) (sql.Result, error) { |
|
|
|
query, args, err := MapToSlice(query, mp) |
|
|
|
query, args, err := MapToSlice(query, mp) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -110,10 +113,12 @@ func (tx *Tx) ExecMapContext(ctx context.Context, query string, mp interface{}) |
|
|
|
return tx.ExecContext(ctx, query, args...) |
|
|
|
return tx.ExecContext(ctx, query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ExecMap executes query with args in a map
|
|
|
|
func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error) { |
|
|
|
func (tx *Tx) ExecMap(query string, mp interface{}) (sql.Result, error) { |
|
|
|
return tx.ExecMapContext(context.Background(), query, mp) |
|
|
|
return tx.ExecMapContext(context.Background(), query, mp) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ExecStructContext executes query with args in a struct
|
|
|
|
func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error) { |
|
|
|
func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{}) (sql.Result, error) { |
|
|
|
query, args, err := StructToSlice(query, st) |
|
|
|
query, args, err := StructToSlice(query, st) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -122,6 +127,7 @@ func (tx *Tx) ExecStructContext(ctx context.Context, query string, st interface{ |
|
|
|
return tx.ExecContext(ctx, query, args...) |
|
|
|
return tx.ExecContext(ctx, query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ExecContext executes a query with args
|
|
|
|
func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { |
|
|
|
func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) { |
|
|
|
hookCtx := contexts.NewContextHook(ctx, query, args) |
|
|
|
hookCtx := contexts.NewContextHook(ctx, query, args) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
@ -136,10 +142,12 @@ func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{} |
|
|
|
return res, err |
|
|
|
return res, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ExecStruct executes query with args in a struct
|
|
|
|
func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error) { |
|
|
|
func (tx *Tx) ExecStruct(query string, st interface{}) (sql.Result, error) { |
|
|
|
return tx.ExecStructContext(context.Background(), query, st) |
|
|
|
return tx.ExecStructContext(context.Background(), query, st) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryContext query with args
|
|
|
|
func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) { |
|
|
|
func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) { |
|
|
|
hookCtx := contexts.NewContextHook(ctx, query, args) |
|
|
|
hookCtx := contexts.NewContextHook(ctx, query, args) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
|
ctx, err := tx.db.beforeProcess(hookCtx) |
|
|
@ -157,10 +165,12 @@ func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{ |
|
|
|
return &Rows{rows, tx.db}, nil |
|
|
|
return &Rows{rows, tx.db}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Query query with args
|
|
|
|
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { |
|
|
|
func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { |
|
|
|
return tx.QueryContext(context.Background(), query, args...) |
|
|
|
return tx.QueryContext(context.Background(), query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryMapContext query with args in a map
|
|
|
|
func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error) { |
|
|
|
func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{}) (*Rows, error) { |
|
|
|
query, args, err := MapToSlice(query, mp) |
|
|
|
query, args, err := MapToSlice(query, mp) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -169,10 +179,12 @@ func (tx *Tx) QueryMapContext(ctx context.Context, query string, mp interface{}) |
|
|
|
return tx.QueryContext(ctx, query, args...) |
|
|
|
return tx.QueryContext(ctx, query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryMap query with args in a map
|
|
|
|
func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error) { |
|
|
|
func (tx *Tx) QueryMap(query string, mp interface{}) (*Rows, error) { |
|
|
|
return tx.QueryMapContext(context.Background(), query, mp) |
|
|
|
return tx.QueryMapContext(context.Background(), query, mp) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryStructContext query with args in struct
|
|
|
|
func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error) { |
|
|
|
func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface{}) (*Rows, error) { |
|
|
|
query, args, err := StructToSlice(query, st) |
|
|
|
query, args, err := StructToSlice(query, st) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -181,19 +193,23 @@ func (tx *Tx) QueryStructContext(ctx context.Context, query string, st interface |
|
|
|
return tx.QueryContext(ctx, query, args...) |
|
|
|
return tx.QueryContext(ctx, query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryStruct query with args in struct
|
|
|
|
func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error) { |
|
|
|
func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error) { |
|
|
|
return tx.QueryStructContext(context.Background(), query, st) |
|
|
|
return tx.QueryStructContext(context.Background(), query, st) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryRowContext query one row with args
|
|
|
|
func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row { |
|
|
|
func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row { |
|
|
|
rows, err := tx.QueryContext(ctx, query, args...) |
|
|
|
rows, err := tx.QueryContext(ctx, query, args...) |
|
|
|
return &Row{rows, err} |
|
|
|
return &Row{rows, err} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryRow query one row with args
|
|
|
|
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row { |
|
|
|
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row { |
|
|
|
return tx.QueryRowContext(context.Background(), query, args...) |
|
|
|
return tx.QueryRowContext(context.Background(), query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryRowMapContext query one row with args in a map
|
|
|
|
func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row { |
|
|
|
func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface{}) *Row { |
|
|
|
query, args, err := MapToSlice(query, mp) |
|
|
|
query, args, err := MapToSlice(query, mp) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -202,10 +218,12 @@ func (tx *Tx) QueryRowMapContext(ctx context.Context, query string, mp interface |
|
|
|
return tx.QueryRowContext(ctx, query, args...) |
|
|
|
return tx.QueryRowContext(ctx, query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryRowMap query one row with args in a map
|
|
|
|
func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row { |
|
|
|
func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row { |
|
|
|
return tx.QueryRowMapContext(context.Background(), query, mp) |
|
|
|
return tx.QueryRowMapContext(context.Background(), query, mp) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryRowStructContext query one row with args in struct
|
|
|
|
func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row { |
|
|
|
func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interface{}) *Row { |
|
|
|
query, args, err := StructToSlice(query, st) |
|
|
|
query, args, err := StructToSlice(query, st) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -214,6 +232,7 @@ func (tx *Tx) QueryRowStructContext(ctx context.Context, query string, st interf |
|
|
|
return tx.QueryRowContext(ctx, query, args...) |
|
|
|
return tx.QueryRowContext(ctx, query, args...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// QueryRowStruct query one row with args in struct
|
|
|
|
func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row { |
|
|
|
func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row { |
|
|
|
return tx.QueryRowStructContext(context.Background(), query, st) |
|
|
|
return tx.QueryRowStructContext(context.Background(), query, st) |
|
|
|
} |
|
|
|
} |
|
|
|