gormgorm.io/gorm/internal/stmt_store Index | Files

package stmt_store

import "gorm.io/gorm/internal/stmt_store"

Index

Types

type ConnPool

type ConnPool interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

type Stmt

type Stmt struct {
	*sql.Stmt
	Transaction bool
	// contains filtered or unexported fields
}

func (*Stmt) Close

func (stmt *Stmt) Close() error

func (*Stmt) Error

func (stmt *Stmt) Error() error

type Store

type Store interface {
	// New creates a new Stmt object and caches it.
	// Parameters:
	//   ctx: The context for the request, which can carry deadlines, cancellation signals, etc.
	//   key: The key representing the SQL query, used for caching and preparing the statement.
	//   isTransaction: Indicates whether this operation is part of a transaction, which may affect the caching strategy.
	//   connPool: A connection pool that provides database connections.
	//   locker: A synchronization lock that is unlocked after initialization to avoid deadlocks.
	// Returns:
	//   *Stmt: A newly created statement object for executing SQL operations.
	//   error: An error if the statement preparation fails.
	New(ctx context.Context, key string, isTransaction bool, connPool ConnPool, locker sync.Locker) (*Stmt, error)

	// Keys returns a slice of all cache keys in the store.
	Keys() []string

	// Get retrieves a Stmt object from the store based on the given key.
	// Parameters:
	//   key: The key used to look up the Stmt object.
	// Returns:
	//   *Stmt: The found Stmt object, or nil if not found.
	//   bool: Indicates whether the corresponding Stmt object was successfully found.
	Get(key string) (*Stmt, bool)

	// Set stores the given Stmt object in the store and associates it with the specified key.
	// Parameters:
	//   key: The key used to associate the Stmt object.
	//   value: The Stmt object to be stored.
	Set(key string, value *Stmt)

	// Delete removes the Stmt object corresponding to the specified key from the store.
	// Parameters:
	//   key: The key associated with the Stmt object to be deleted.
	Delete(key string)
}

Store defines an interface for managing the caching operations of SQL statements (Stmt). This interface provides methods for creating new statements, retrieving all cache keys, getting cached statements, setting cached statements, and deleting cached statements.

func New

func New(size int, ttl time.Duration) Store

New creates and returns a new Store instance.

Parameters:

This function defines an onEvicted callback that is invoked when a cache entry is evicted. The callback ensures that if the evicted value (v) is not nil, its Close method is called asynchronously to release associated resources.

Returns:

Source Files

stmt_store.go

Version
v1.26.1 (latest)
Published
May 7, 2025
Platform
linux/amd64
Imports
6 packages
Last checked
1 week ago

Tools for package owners.