Update dependencies (#5518)

This commit is contained in:
hongming
2023-02-12 23:09:20 +08:00
committed by GitHub
parent d3b35fb2da
commit a979342f56
1486 changed files with 126660 additions and 71128 deletions

View File

@@ -2,6 +2,7 @@ package squirrel
import (
"database/sql"
"fmt"
"sync"
)
@@ -20,22 +21,25 @@ type DBProxy interface {
Preparer
}
type stmtCacher struct {
// NOTE: NewStmtCache is defined in stmtcacher_ctx.go (Go >= 1.8) or stmtcacher_noctx.go (Go < 1.8).
// StmtCache wraps and delegates down to a Preparer type
//
// It also automatically prepares all statements sent to the underlying Preparer calls
// for Exec, Query and QueryRow and caches the returns *sql.Stmt using the provided
// query as the key. So that it can be automatically re-used.
type StmtCache struct {
prep Preparer
cache map[string]*sql.Stmt
mu sync.Mutex
}
// NewStmtCacher returns a DBProxy wrapping prep that caches Prepared Stmts.
//
// Stmts are cached based on the string value of their queries.
func NewStmtCacher(prep Preparer) DBProxy {
return &stmtCacher{prep: prep, cache: make(map[string]*sql.Stmt)}
}
func (sc *stmtCacher) Prepare(query string) (*sql.Stmt, error) {
// Prepare delegates down to the underlying Preparer and caches the result
// using the provided query as a key
func (sc *StmtCache) Prepare(query string) (*sql.Stmt, error) {
sc.mu.Lock()
defer sc.mu.Unlock()
stmt, ok := sc.cache[query]
if ok {
return stmt, nil
@@ -47,7 +51,8 @@ func (sc *stmtCacher) Prepare(query string) (*sql.Stmt, error) {
return stmt, err
}
func (sc *stmtCacher) Exec(query string, args ...interface{}) (res sql.Result, err error) {
// Exec delegates down to the underlying Preparer using a prepared statement
func (sc *StmtCache) Exec(query string, args ...interface{}) (res sql.Result, err error) {
stmt, err := sc.Prepare(query)
if err != nil {
return
@@ -55,7 +60,8 @@ func (sc *stmtCacher) Exec(query string, args ...interface{}) (res sql.Result, e
return stmt.Exec(args...)
}
func (sc *stmtCacher) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
// Query delegates down to the underlying Preparer using a prepared statement
func (sc *StmtCache) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
stmt, err := sc.Prepare(query)
if err != nil {
return
@@ -63,7 +69,8 @@ func (sc *stmtCacher) Query(query string, args ...interface{}) (rows *sql.Rows,
return stmt.Query(args...)
}
func (sc *stmtCacher) QueryRow(query string, args ...interface{}) RowScanner {
// QueryRow delegates down to the underlying Preparer using a prepared statement
func (sc *StmtCache) QueryRow(query string, args ...interface{}) RowScanner {
stmt, err := sc.Prepare(query)
if err != nil {
return &Row{err: err}
@@ -71,6 +78,30 @@ func (sc *stmtCacher) QueryRow(query string, args ...interface{}) RowScanner {
return stmt.QueryRow(args...)
}
// Clear removes and closes all the currently cached prepared statements
func (sc *StmtCache) Clear() (err error) {
sc.mu.Lock()
defer sc.mu.Unlock()
for key, stmt := range sc.cache {
delete(sc.cache, key)
if stmt == nil {
continue
}
if cerr := stmt.Close(); cerr != nil {
err = cerr
}
}
if err != nil {
return fmt.Errorf("one or more Stmt.Close failed; last error: %v", err)
}
return
}
type DBProxyBeginner interface {
DBProxy
Begin() (*sql.Tx, error)
@@ -82,7 +113,7 @@ type stmtCacheProxy struct {
}
func NewStmtCacheProxy(db *sql.DB) DBProxyBeginner {
return &stmtCacheProxy{DBProxy: NewStmtCacher(db), db: db}
return &stmtCacheProxy{DBProxy: NewStmtCache(db), db: db}
}
func (sp *stmtCacheProxy) Begin() (*sql.Tx, error) {