Skip to content

Commit

Permalink
chore(tx): improved naming on stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Apr 9, 2024
1 parent 102bb55 commit 09e3726
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (db *Context) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error
return nil, err
}

return &Tx{Tx: tx, cachedStmts: make(map[string]*sql.Stmt)}, nil
return &Tx{Tx: tx, stmts: make(map[string]*sql.Stmt)}, nil
}

func (db *Context) Transaction(ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context, tx *Tx) error) error {
Expand Down
20 changes: 10 additions & 10 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

type Tx struct {
*sql.Tx
noCopy //nolint
cachedStmts map[string]*sql.Stmt
noCopy //nolint
stmts map[string]*sql.Stmt
}

func (tx *Tx) prepareStmt(ctx context.Context, query string) (*sql.Stmt, error) {
if tx.cachedStmts == nil {
tx.cachedStmts = make(map[string]*sql.Stmt)
if tx.stmts == nil {
tx.stmts = make(map[string]*sql.Stmt)
}
s, ok := tx.cachedStmts[query]
s, ok := tx.stmts[query]
if ok {
return s, nil
}
Expand All @@ -25,13 +25,13 @@ func (tx *Tx) prepareStmt(ctx context.Context, query string) (*sql.Stmt, error)
return nil, err
}

tx.cachedStmts[query] = s
tx.stmts[query] = s

return s, nil
}

func (tx *Tx) closeCachedStmts() {
for _, stmt := range tx.cachedStmts {
func (tx *Tx) closeStmts() {
for _, stmt := range tx.stmts {
stmt.Close()
}
}
Expand Down Expand Up @@ -147,11 +147,11 @@ func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.R
}

func (tx *Tx) Rollback() error {
defer tx.closeCachedStmts()
defer tx.closeStmts()
return tx.Tx.Rollback()
}

func (tx *Tx) Commit() error {
defer tx.closeCachedStmts()
defer tx.closeStmts()
return tx.Tx.Commit()
}

0 comments on commit 09e3726

Please sign in to comment.