mirror of
https://github.com/jackc/pgx.git
synced 2025-09-04 19:37:10 +00:00
QueryExecModeCacheDescribe actually is safe even when the schema or search_path is modified. It may return an error on the first execution but it should never silently encode or decode a value incorrectly. Add a test to demonstrate and ensure this behavior. Update documentation of QueryExecModeCacheDescribe to remove warning of undetected result decoding errors. Update documentation of QueryExecModeCacheStatement and QueryExecModeCacheDescribe to indicate that the first execution of an invalidated statement may fail.
41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
// Package stmtcache is a cache for statement descriptions.
|
|
package stmtcache
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
// StatementName returns a statement name that will be stable for sql across multiple connections and program
|
|
// executions.
|
|
func StatementName(sql string) string {
|
|
digest := sha256.Sum256([]byte(sql))
|
|
return "stmtcache_" + hex.EncodeToString(digest[0:24])
|
|
}
|
|
|
|
// Cache caches statement descriptions.
|
|
type Cache interface {
|
|
// Get returns the statement description for sql. Returns nil if not found.
|
|
Get(sql string) *pgconn.StatementDescription
|
|
|
|
// Put stores sd in the cache. Put panics if sd.SQL is "". Put does nothing if sd.SQL already exists in the cache.
|
|
Put(sd *pgconn.StatementDescription)
|
|
|
|
// Invalidate invalidates statement description identified by sql. Does nothing if not found.
|
|
Invalidate(sql string)
|
|
|
|
// InvalidateAll invalidates all statement descriptions.
|
|
InvalidateAll()
|
|
|
|
// HandleInvalidated returns a slice of all statement descriptions invalidated since the last call to HandleInvalidated.
|
|
HandleInvalidated() []*pgconn.StatementDescription
|
|
|
|
// Len returns the number of cached prepared statement descriptions.
|
|
Len() int
|
|
|
|
// Cap returns the maximum number of cached prepared statement descriptions.
|
|
Cap() int
|
|
}
|