stmtcacher

package module
v0.0.0-...-98023a9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2017 License: MIT Imports: 3 Imported by: 0

README

stmtcacher

Prepared statement caching for go's sql.DB.

Godoc

CachingWrapper

Wrapper around DB with additional functions that explicitly reuse cached prepared statements. All sql.DB query functions are wrapped and available with the suffix "Prepared", e.g. QueryPrepared, ExecPrepared

db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
	// ...
}

wrapper := stmtcacher.NewCachingWrapper(db)

query := "SELECT * FROM table where id = ?"

// By default, sql.DB will perform 3 statements and roundtrips
db.Query(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
// CLOSE stmt1
db.Query(query, "bar")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt2, "bar"
// CLOSE

// With stmtcacher, they're cached
wrapper.QueryPrepared(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
wrapper.QueryPrepared(query, "bar")
// EXECUTE stmt1, "bar"
wrapper.QueryPrepared(query, "baz")
// EXECUTE stmt1, "baz"

// sql.DB is embedded and available to
// easily opt-in or out of the behavior
wrapper.Query(query, "bar")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt2, "bar"
// CLOSE

CachingProxy

Caching proxy and wrapper around sql.DB. Exec, Query, QueryRow, ExecContext, QueryContext, and QueryRowContext are each wrapped to cache and reuse prepared statements

db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
	// ...
}

proxy := stmtcacher.NewCachingProxy(db)

query := "SELECT * FROM table where name = ?"

// By default, sql.DB will perform 3 statements and roundtrips
db.Query(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
// CLOSE stmt1
db.Query(query, "bar")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt2, "bar"
// CLOSE

// With stmtcacher, they're cached
proxy.Query(query, "foo")
// PREPARE 'SELECT * FROM table where name = ?'
// EXECUTE stmt1, "foo"
proxy.Query(query, "bar")
// EXECUTE stmt1, "bar"
proxy.Query(query, "baz")
// EXECUTE stmt1, "baz"

Comparison

Inspired by: https://github.com/Masterminds/squirrel

Compared to squirrel, this pkg has:

  • Better error handling
  • Doesn't enforce squirrel-specific types like RowScanner
  • Easier use with other pkgs that wrap or work with sql.DB
  • Offers both a wrapper with additional fns, as well as caching proxy

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachingProxy

type CachingProxy struct {
	*sql.DB
	// contains filtered or unexported fields
}

Caching proxy and wrapper around sql.DB. Exec, Query, QueryRow, ExecContext, QueryContext, and QueryRowContext are each wrapped to cache and reuse prepared statements

func NewCachingProxy

func NewCachingProxy(db *sql.DB) *CachingProxy

Returns a CachingProxy

func (*CachingProxy) Exec

func (proxy *CachingProxy) Exec(query string, args ...interface{}) (res sql.Result, err error)

sql.DB.Exec with caching and reuse of the generated prepared statement

func (*CachingProxy) ExecContext

func (proxy *CachingProxy) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)

sql.DB.ExecContext with caching and reuse of the generated prepared statement

func (*CachingProxy) Prepare

func (proxy *CachingProxy) Prepare(query string) (*sql.Stmt, error)

sql.DB.Prepare that checks an object cache before creating a new prepared statement

func (*CachingProxy) PrepareContext

func (proxy *CachingProxy) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

sql.DB.PrepareContext that checks an object cache before creating a new prepared statement

func (*CachingProxy) Query

func (proxy *CachingProxy) Query(query string, args ...interface{}) (rows *sql.Rows, err error)

sql.DB.Query with caching and reuse of the generated prepared statement

func (*CachingProxy) QueryContext

func (proxy *CachingProxy) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)

sql.DB.QueryContext with caching and reuse of the generated prepared statement

func (*CachingProxy) QueryRow

func (proxy *CachingProxy) QueryRow(query string, args ...interface{}) *sql.Row

sql.DB.QueryRow with caching and reuse of the generated prepared statement

func (*CachingProxy) QueryRowContext

func (proxy *CachingProxy) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

sql.DB.QueryRowContext with caching and reuse of the generated prepared statement

type CachingWrapper

type CachingWrapper struct {
	*sql.DB
	// contains filtered or unexported fields
}

Wrapper around DB with additional functions that explicitly reuse cached prepared statements. All sql.DB query functions are wrapped and available with the suffix "Prepared", e.g. QueryPrepared, ExecPrepared

func NewCachingWrapper

func NewCachingWrapper(db *sql.DB) *CachingWrapper

Returns a CachingWrapper

func (*CachingWrapper) ExecContextPrepared

func (wrapper *CachingWrapper) ExecContextPrepared(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)

sql.DB.ExecContext with caching and reuse of the generated prepared statement

func (*CachingWrapper) ExecPrepared

func (wrapper *CachingWrapper) ExecPrepared(query string, args ...interface{}) (res sql.Result, err error)

sql.DB.Exec with caching and reuse of the generated prepared statement

func (*CachingWrapper) QueryContextPrepared

func (wrapper *CachingWrapper) QueryContextPrepared(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error)

sql.DB.QueryContext with caching and reuse of the generated prepared statement

func (*CachingWrapper) QueryPrepared

func (wrapper *CachingWrapper) QueryPrepared(query string, args ...interface{}) (rows *sql.Rows, err error)

sql.DB.Query with caching and reuse of the generated prepared statement

func (*CachingWrapper) QueryRowContextPrepared

func (wrapper *CachingWrapper) QueryRowContextPrepared(ctx context.Context, query string, args ...interface{}) *sql.Row

sql.DB.QueryRowContext with caching and reuse of the generated prepared statement

func (*CachingWrapper) QueryRowPrepared

func (wrapper *CachingWrapper) QueryRowPrepared(query string, args ...interface{}) *sql.Row

sql.DB.QueryRow with caching and reuse of the generated prepared statement

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL