Skip to content

Commit e1ddf59

Browse files
committed
Rename StmtCacher to StmtCache and deprecate NewStmtCacher
1 parent 67ef988 commit e1ddf59

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

stmtcacher.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ type DBProxy interface {
2121
Preparer
2222
}
2323

24-
// NOTE: NewStmtCacher is defined in stmtcacher_ctx.go (Go >= 1.8) or stmtcacher_noctx.go (Go < 1.8).
24+
// NOTE: NewStmtCache is defined in stmtcacher_ctx.go (Go >= 1.8) or stmtcacher_noctx.go (Go < 1.8).
2525

26-
// StmtCacher wraps and delegates down to a Preparer type
26+
// StmtCache wraps and delegates down to a Preparer type
2727
//
2828
// It also automatically prepares all statements sent to the underlying Preparer calls
2929
// for Exec, Query and QueryRow and caches the returns *sql.Stmt using the provided
3030
// query as the key. So that it can be automatically re-used.
31-
type StmtCacher struct {
31+
type StmtCache struct {
3232
prep Preparer
3333
cache map[string]*sql.Stmt
3434
mu sync.Mutex
3535
}
3636

3737
// Prepare delegates down to the underlying Preparer and caches the result
3838
// using the provided query as a key
39-
func (sc *StmtCacher) Prepare(query string) (*sql.Stmt, error) {
39+
func (sc *StmtCache) Prepare(query string) (*sql.Stmt, error) {
4040
sc.mu.Lock()
4141
defer sc.mu.Unlock()
4242

@@ -52,7 +52,7 @@ func (sc *StmtCacher) Prepare(query string) (*sql.Stmt, error) {
5252
}
5353

5454
// Exec delegates down to the underlying Preparer using a prepared statement
55-
func (sc *StmtCacher) Exec(query string, args ...interface{}) (res sql.Result, err error) {
55+
func (sc *StmtCache) Exec(query string, args ...interface{}) (res sql.Result, err error) {
5656
stmt, err := sc.Prepare(query)
5757
if err != nil {
5858
return
@@ -61,7 +61,7 @@ func (sc *StmtCacher) Exec(query string, args ...interface{}) (res sql.Result, e
6161
}
6262

6363
// Query delegates down to the underlying Preparer using a prepared statement
64-
func (sc *StmtCacher) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
64+
func (sc *StmtCache) Query(query string, args ...interface{}) (rows *sql.Rows, err error) {
6565
stmt, err := sc.Prepare(query)
6666
if err != nil {
6767
return
@@ -70,7 +70,7 @@ func (sc *StmtCacher) Query(query string, args ...interface{}) (rows *sql.Rows,
7070
}
7171

7272
// QueryRow delegates down to the underlying Preparer using a prepared statement
73-
func (sc *StmtCacher) QueryRow(query string, args ...interface{}) RowScanner {
73+
func (sc *StmtCache) QueryRow(query string, args ...interface{}) RowScanner {
7474
stmt, err := sc.Prepare(query)
7575
if err != nil {
7676
return &Row{err: err}
@@ -79,7 +79,7 @@ func (sc *StmtCacher) QueryRow(query string, args ...interface{}) RowScanner {
7979
}
8080

8181
// Clear removes and closes all the currently cached prepared statements
82-
func (sc *StmtCacher) Clear() (err error) {
82+
func (sc *StmtCache) Clear() (err error) {
8383
sc.mu.Lock()
8484
defer sc.mu.Unlock()
8585

@@ -113,7 +113,7 @@ type stmtCacheProxy struct {
113113
}
114114

115115
func NewStmtCacherProxy(db *sql.DB) DBProxyBeginner {
116-
return &stmtCacheProxy{DBProxy: NewStmtCacher(db), db: db}
116+
return &stmtCacheProxy{DBProxy: NewStmtCache(db), db: db}
117117
}
118118

119119
func (sp *stmtCacheProxy) Begin() (*sql.Tx, error) {

stmtcacher_ctx.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,23 @@ type DBProxyContext interface {
2424
PreparerContext
2525
}
2626

27-
// NewStmtCacher returns a DBProxy wrapping prep that caches Prepared Stmts.
27+
// NewStmtCache returns a *StmtCache wrapping a PreparerContext that caches Prepared Stmts.
2828
//
2929
// Stmts are cached based on the string value of their queries.
30+
func NewStmtCache(prep PreparerContext) *StmtCache {
31+
return &StmtCache{prep: prep, cache: make(map[string]*sql.Stmt)}
32+
}
33+
34+
// NewStmtCacher is deprecated
35+
//
36+
// Use NewStmtCache instead
3037
func NewStmtCacher(prep PreparerContext) DBProxyContext {
31-
return &StmtCacher{prep: prep, cache: make(map[string]*sql.Stmt)}
38+
return NewStmtCache(prep)
3239
}
3340

3441
// PrepareContext delegates down to the underlying PreparerContext and caches the result
3542
// using the provided query as a key
36-
func (sc *StmtCacher) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
43+
func (sc *StmtCache) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
3744
ctxPrep, ok := sc.prep.(PreparerContext)
3845
if !ok {
3946
return nil, NoContextSupport
@@ -52,7 +59,7 @@ func (sc *StmtCacher) PrepareContext(ctx context.Context, query string) (*sql.St
5259
}
5360

5461
// ExecContext delegates down to the underlying PreparerContext using a prepared statement
55-
func (sc *StmtCacher) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error) {
62+
func (sc *StmtCache) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error) {
5663
stmt, err := sc.PrepareContext(ctx, query)
5764
if err != nil {
5865
return
@@ -61,7 +68,7 @@ func (sc *StmtCacher) ExecContext(ctx context.Context, query string, args ...int
6168
}
6269

6370
// QueryContext delegates down to the underlying PreparerContext using a prepared statement
64-
func (sc *StmtCacher) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
71+
func (sc *StmtCache) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
6572
stmt, err := sc.PrepareContext(ctx, query)
6673
if err != nil {
6774
return
@@ -70,7 +77,7 @@ func (sc *StmtCacher) QueryContext(ctx context.Context, query string, args ...in
7077
}
7178

7279
// QueryRowContext delegates down to the underlying PreparerContext using a prepared statement
73-
func (sc *StmtCacher) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
80+
func (sc *StmtCache) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
7481
stmt, err := sc.PrepareContext(ctx, query)
7582
if err != nil {
7683
return &Row{err: err}

stmtcacher_ctx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestStmtCacherPrepareContext(t *testing.T) {
1212
db := &DBStub{}
13-
sc := NewStmtCacher(db)
13+
sc := NewStmtCache(db)
1414
query := "SELECT 1"
1515

1616
sc.PrepareContext(ctx, query)

stmtcacher_noctx.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import (
99
// NewStmtCacher returns a DBProxy wrapping prep that caches Prepared Stmts.
1010
//
1111
// Stmts are cached based on the string value of their queries.
12-
func NewStmtCacher(prep Preparer) DBProxy {
12+
func NewStmtCache(prep Preparer) *StmtCache {
1313
return &StmtCacher{prep: prep, cache: make(map[string]*sql.Stmt)}
1414
}
15+
16+
// NewStmtCacher is deprecated
17+
//
18+
// Use NewStmtCache instead
19+
func NewStmtCacher(prep Preparer) DBProxy {
20+
return NewStmtCache(prep)
21+
}

stmtcacher_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7-
"github.com/stretchr/testify/require"
87
)
98

10-
func TestStmtCacherPrepare(t *testing.T) {
9+
func TestStmtCachePrepare(t *testing.T) {
1110
db := &DBStub{}
12-
sc := NewStmtCacher(db)
11+
sc := NewStmtCache(db)
1312
query := "SELECT 1"
1413

1514
sc.Prepare(query)
@@ -19,9 +18,7 @@ func TestStmtCacherPrepare(t *testing.T) {
1918
assert.Equal(t, 1, db.PrepareCount, "expected 1 Prepare, got %d", db.PrepareCount)
2019

2120
// clear statement cache
22-
clearer, ok := sc.(*StmtCacher)
23-
require.True(t, ok)
24-
assert.Nil(t, clearer.Clear())
21+
assert.Nil(t, sc.Clear())
2522

2623
// should prepare the query again
2724
sc.Prepare(query)

0 commit comments

Comments
 (0)