stmtcache

package
v2.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2025 License: Apache-2.0, BSD-3-Clause, MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB is a wrapper for sql.DB, providing an additional layer for caching prepared statements to optimize database interactions and improve performance.

func New

func New(db *sql.DB) *DB

New creates new DB wrapper with statements caching enabled

func (*DB) Begin

func (d *DB) Begin() (*Tx, error)

Begin starts a new SQL transaction and returns a Tx object with statement caching capabilities.

func (*DB) BeginTx

func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTx starts a new SQL transaction and returns a Tx object with statement caching capabilities.

func (*DB) CacheSize

func (d *DB) CacheSize() int

CacheSize returns the current number of prepared statements stored in the cache.

func (*DB) CachingEnabled

func (d *DB) CachingEnabled() bool

CachingEnabled returns true if statements caching is enabled

func (*DB) ClearCache

func (d *DB) ClearCache() error

ClearCache will close all cached prepared statements and clear statements cache map

func (*DB) Close

func (d *DB) Close() error

Close will clear the statements cache and close the underlying db connection

func (*DB) Exec

func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error)

Exec executes a query that doesn't return rows. Exec delegates call to ExecContext with contex.Background() as parameter.

func (*DB) ExecContext

func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes a query that doesn't return rows. If statement caching is enabled, ExecContext will first call PrepareContext to retrieve a prepared statement, and then execute a query using a prepared statement. If statement caching is disabled, this method delegates the call to the *sql.DB ExecContext method.

func (*DB) Prepare

func (d *DB) Prepare(query string) (*sql.Stmt, error)

Prepare delegates call to PrepareContext using context.Background as a parameter.

func (*DB) PrepareContext

func (d *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext returns database prepared statement for a query. When statement caching is enabled, it returns a cached prepared statement if available; otherwise, it creates a new prepared statement and adds it to the cache. Invoking this method directly is unnecessary, as wrapper methods like Exec/ExecContext and Query/QueryContext will call PrepareContext before executing a query on it. If statement caching is disabled, this method delegates the call to the *sql.DB PrepareContext method.

There's no need to manually close the returned statement; it operates within the transaction scope and will be closed automatically upon the completion of the transaction, whether it's committed or rolled back.

func (*DB) Query

func (d *DB) Query(query string, args ...interface{}) (*sql.Rows, error)

Query delegates call to QueryContext using context.Background() as parameter.

func (*DB) QueryContext

func (d *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext executes a query that returns rows. If statement caching is enabled, QueryContext will first call PrepareContext to retrieve a prepared statement, and then execute a query using a prepared statement. If statement caching is disabled, this method delegates the call to the *sql.DB QueryContext method.

func (*DB) SetCaching

func (d *DB) SetCaching(enabled bool) *DB

SetCaching returns *DB wrapper with prepared statements caching enabled or disabled. This method should be called only once. It is not concurrency-safe.

type Tx

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

Tx is a wrapper around *sql.Tx, adding prepared statement caching capability. Tx is not thread safe and should not be shared between goroutines.

func (*Tx) Exec

func (t *Tx) Exec(query string, args ...interface{}) (sql.Result, error)

Exec executes a query that doesn't return rows. Exec delegates call to ExecContext with contex.Background() as parameter.

func (*Tx) ExecContext

func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext executes a query that doesn't return rows. If statement caching is enabled, ExecContext will first call PrepareContext to retrieve a prepared statement, and then execute a query using a prepared statement. If statement caching is disabled, this method delegates the call to the *sql.Tx ExecContext method.

func (*Tx) Prepare

func (t *Tx) Prepare(query string) (*sql.Stmt, error)

Prepare delegates call to PrepareContext using context.Background as a parameter.

func (*Tx) PrepareContext

func (t *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext returns database prepared statement for a query. When statement caching is enabled, it returns a cached prepared statement if available; otherwise, it creates a new prepared statement and adds it to the cache. Invoking this method directly is unnecessary, as wrapper methods like Exec/ExecContext and Query/QueryContext will call PrepareContext before executing a query on it. If statement caching is disabled, this method delegates the call to the *sql.Tx PrepareContext method.

There's no need to manually close the returned statement; it operates within the transaction scope and will be closed automatically upon the completion of the transaction, whether it's committed or rolled back.

func (*Tx) Query

func (t *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)

Query delegates call to QueryContext using context.Background() as parameter.

func (*Tx) QueryContext

func (t *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext executes a query that returns rows. If statement caching is enabled, QueryContext will first call PrepareContext to retrieve a prepared statement, and then execute a query using a prepared statement. If statement caching is disabled, this method delegates the call to the *sql.Tx QueryContext method.

Jump to

Keyboard shortcuts

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