Documentation
¶
Overview ¶
Example ¶
// connect to your database/sql of choice db, err := sql.Open("sqlite3", "file::memory:?mode=memory&cache=shared") if err != nil { panic(err) } defer db.Close() // create a SqlStmtCache to automatically prepare your statements dbsc, err := New(db) if err != nil { panic(err) } defer dbsc.Close() // now, instead of querying the database like usual: // res, err := db.QueryContext(context.Background(), "SELECT * FROM mytable WHERE id = ?", 1) // you do (notice we are using the SqlStmtCache): res, err := dbsc.QueryContext(context.Background(), "SELECT * FROM mytable WHERE id = ?", 1) if err != nil { panic(err) } defer res.Close()
Output:
Index ¶
- Constants
- type SQLStmtCache
- func (c *SQLStmtCache) Close()
- func (c *SQLStmtCache) ExecContext(ctx context.Context, sql string, values ...interface{}) (sql.Result, error)
- func (c *SQLStmtCache) ExecContextTx(ctx context.Context, tx *sql.Tx, sql string, values ...interface{}) (sql.Result, error)
- func (c *SQLStmtCache) GetStats() SQLStmtCacheStats
- func (c *SQLStmtCache) QueryContext(ctx context.Context, sql string, values ...interface{}) (*sql.Rows, error)
- func (c *SQLStmtCache) QueryContextTx(ctx context.Context, tx *sql.Tx, sql string, values ...interface{}) (*sql.Rows, error)
- func (c *SQLStmtCache) QueryRowContext(ctx context.Context, sql string, values ...interface{}) *sql.Row
- func (c *SQLStmtCache) QueryRowContextTx(ctx context.Context, tx *sql.Tx, sql string, values ...interface{}) *sql.Row
- type SQLStmtCacheOpt
- type SQLStmtCacheStats
Examples ¶
Constants ¶
const ( DefaultMaxQueryLen = 4096 DefaultMaxPreparedStmt = 16 DefaultMaxStmt = 1024 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type SQLStmtCache ¶
type SQLStmtCache struct {
// contains filtered or unexported fields
}
SQLStmtCache transparently caches and uses prepared SQL statements.
func New ¶
func New(db *sql.DB, opts ...SQLStmtCacheOpt) (*SQLStmtCache, error)
New creates a new SQLStmtCache, with the provided options, that wraps the provided *sql.DB instance.
func (*SQLStmtCache) Close ¶
func (c *SQLStmtCache) Close()
Close closes and frees all resources associated with the prepared statement cache. The SQLStmtCache should not be used after Close() has been called.
func (*SQLStmtCache) ExecContext ¶
func (c *SQLStmtCache) ExecContext(ctx context.Context, sql string, values ...interface{}) (sql.Result, error)
ExecContext is equivalent to (*sql.DB).ExecContext, but it transparently creates and uses prepared statements for the most frequently-executed queries.
func (*SQLStmtCache) ExecContextTx ¶
func (c *SQLStmtCache) ExecContextTx(ctx context.Context, tx *sql.Tx, sql string, values ...interface{}) (sql.Result, error)
ExecContextTx is equivalent to tx.ExecContext, but it transparently creates and uses prepared statements for the most frequently-executed queries.
func (*SQLStmtCache) GetStats ¶
func (c *SQLStmtCache) GetStats() SQLStmtCacheStats
GetStats returns statistics about the state and effectiveness of the prepared statements cache.
func (*SQLStmtCache) QueryContext ¶
func (c *SQLStmtCache) QueryContext(ctx context.Context, sql string, values ...interface{}) (*sql.Rows, error)
QueryContext is equivalent to (*sql.DB).QueryContext, but it transparently creates and uses prepared statements for the most frequently-executed queries.
func (*SQLStmtCache) QueryContextTx ¶
func (c *SQLStmtCache) QueryContextTx(ctx context.Context, tx *sql.Tx, sql string, values ...interface{}) (*sql.Rows, error)
QueryContextTx is equivalent to tx.QueryContext, but it transparently creates and uses prepared statements for the most frequently-executed queries.
func (*SQLStmtCache) QueryRowContext ¶
func (c *SQLStmtCache) QueryRowContext(ctx context.Context, sql string, values ...interface{}) *sql.Row
QueryRowContext is equivalent to (*sql.DB).QueryRowContext, but it transparently creates and uses prepared statements for the most frequently-executed queries.
func (*SQLStmtCache) QueryRowContextTx ¶
func (c *SQLStmtCache) QueryRowContextTx(ctx context.Context, tx *sql.Tx, sql string, values ...interface{}) *sql.Row
QueryRowContextTx is equivalent to tx.QueryRowContext, but it transparently creates and uses prepared statements for the most frequently-executed queries.
type SQLStmtCacheOpt ¶
type SQLStmtCacheOpt func(*SQLStmtCache) error
func WithMaxPreparedStmt ¶
func WithMaxPreparedStmt(max int) SQLStmtCacheOpt
WithMaxPreparedStmt specifies the maximum number of prepared statements that will exist at any one time. It defaults to DefaultMaxPreparedStmt. Some databases (e.g. mysql) have limits to how many statements can be prepared at any one time, across all clients and connections: be sure not to set this number too high, or to use too many concurrent connections, or to use too many concurrent clients. Setting this value to 0 disables the SQLStmtCache.
func WithMaxQueryLen ¶
func WithMaxQueryLen(max int) SQLStmtCacheOpt
WithMaxQueryLen specifies the maximum length of a SQL statement to be considered by autoprepare. Statements longer than this number are executed as-is and no prepared statements are ever cached. It defaults to DefaultMaxQueryLen.
func WithMaxStmt ¶
func WithMaxStmt(max int) SQLStmtCacheOpt
WithMaxStmt specifies a soft upper limit on how many different SQL statements to track to be able to pick the most frequently used one, that will be promoted to a prepared statement. It defaults to DefaultMaxStmt.
type SQLStmtCacheStats ¶
type SQLStmtCacheStats struct { Prepared uint64 // number of autoprepared statements created (Prepare() calls issued) Unprepared uint64 // number of autoprepared statements deleted (sql.(*Stmt).Close() calls issued) Hits uint64 // number of SQL queries that used automatically-prepared statements Misses uint64 // number of SQL queries executed raw Skips uint64 // number of SQL queries that do not qualify for caching }