Documentation ¶
Overview ¶
Package psql provides cached, shared, partitioned database objects with cached prepared statements cancelable by context.
- NewDBMap provides cached, shared database implementation objects. database access is using cached prepared statements and access using application name and partition name like year.
- NewResultSetIterator provides a Go for-statements abstract result-set iterator
- ScanFunc is the signature for preparing custom result-set iterators
- seamless statement-retry, remedying concurrency-deficient databases such as SQLite3
- —
- TrimSql trims SQL statements in Go raw string literals, multi-line strings enclosed by the back-tick ‘`’ character
- ColumnType describes columns of a result-set
- convenience method for single-value queries: DBMap.QueryInt DBMap.QueryString
- SqlExec pprovides statement execution prior to obtaining a cached database, ie. for seamlessly preparing the schema
Index ¶
- Variables
- func ColumnType(typ *sql.ColumnType) (s string)
- func NewResultSetIterator[T any](sqlRows *sql.Rows, scanFunc ScanFunc[T]) (iterator iters.Iterator[T])
- func SqlExec(label string, ctx context.Context, dataSource parl.DataSource, query string, ...) (err error)
- func TrimSql(sql string) (trimmedSql string)
- type DBMap
- func (d *DBMap) Close() (err error)
- func (d *DBMap) Exec(partition parl.DBPartition, query string, ctx context.Context, args ...any) (execResult parl.ExecResult, err error)
- func (d *DBMap) Query(partition parl.DBPartition, query string, ctx context.Context, args ...any) (sqlRows *sql.Rows, err error)
- func (d *DBMap) QueryInt(partition parl.DBPartition, query string, ctx context.Context, args ...any) (value int, err error)
- func (d *DBMap) QueryRow(partition parl.DBPartition, query string, ctx context.Context, args ...any) (sqlRow *sql.Row, err error)
- func (d *DBMap) QueryString(partition parl.DBPartition, query string, ctx context.Context, args ...any) (value string, err error)
- type ResultSetIterator
- type ScanFunc
Constants ¶
This section is empty.
Variables ¶
var DBFactory = &dbFactory{}
DBFactory implements parl.DBFactory providing:
- cached database access
- cached prepared statement queries
Functions ¶
func ColumnType ¶
func ColumnType(typ *sql.ColumnType) (s string)
func NewResultSetIterator ¶
func NewResultSetIterator[T any](sqlRows *sql.Rows, scanFunc ScanFunc[T]) (iterator iters.Iterator[T])
NewResultSetIterator returns a result-set iterator for type T
- scanFunc invokes sql.Rows.Scan and returns result
- to return multiple values, use a tuple value-container similar to github.com/haraldrudell/parl/pfs.ResultEntry
- note that scanFunc is self-contained. Providing a method value as scanFunc causes allocation 20 ns M1 Max. Providing a top-level function is allocation-free.
Usage:
var sqlRows *sql.Rows if sqlRows, err = o.Query(parl.NoPartition, myQuery, o.ctx); perrors.IsPF(&err, "query %w", err) { return } iterator = psql.NewResultSetIterator(sqlRows, scanFunc) defer iterator.Cancel(&err) for item, _ := iterator.Init(); iterator.Cond(&item); { … func scanFunc(sqlRows *sql.Rows) (item Item, err error) { err = sqlRows.Scan(&item)
func SqlExec ¶
func SqlExec(label string, ctx context.Context, dataSource parl.DataSource, query string, args ...any) (err error)
SqlExec is used when parl.DB is not available, for example to implement the schema function provided to DBFactory.NewDB(). query is an sql statement that does not return any rows. label is used in error messages. SqlExec uses parl.DataSource obtained from DataSourceNamer.DataSource(). Because the cached prepared statements and the partitioning of parl.DB are not available, SqlExec uses any sql.DB method.
Types ¶
type DBMap ¶
type DBMap struct {
// contains filtered or unexported fields
}
DBMap provides:
- caching of SQL-implementation-specific database objects
- a cache for prepared statements via methods Exec Query QueryRow QueryString QueryInt
func NewDBMap ¶
func NewDBMap( dsnr parl.DataSourceNamer, schema func(dataSource parl.DataSource, ctx context.Context) (err error), ) (dbMap *DBMap)
NewDBMap returns a database connection and prepared statement cache for dsnr that implements parl.DB
func NewDBMap2 ¶
func NewDBMap2( dsnr parl.DataSourceNamer, schema func(dataSource parl.DataSource, ctx context.Context) (err error), getp *func(dataSourceName parl.DataSourceName, ctx context.Context) (dbStatementCache *psql2.StatementCache, err error), ) (dbMap *DBMap)
func (*DBMap) Exec ¶
func (d *DBMap) Exec( partition parl.DBPartition, query string, ctx context.Context, args ...any) (execResult parl.ExecResult, err error)
Exec executes a query not returning any rows
func (*DBMap) Query ¶
func (d *DBMap) Query( partition parl.DBPartition, query string, ctx context.Context, args ...any) (sqlRows *sql.Rows, err error)
Query executes a query returning zero or more rows
func (*DBMap) QueryInt ¶
func (d *DBMap) QueryInt( partition parl.DBPartition, query string, ctx context.Context, args ...any) (value int, err error)
Query executes a query known to return exactly one row and returns its int value
type ResultSetIterator ¶
type ResultSetIterator[T any] struct { // contains filtered or unexported fields }
ResultSetIterator is an iterator for a sql result-set
type ScanFunc ¶
ScanFunc scans a record into type T
- invoked after sql.Rows.Next returned true
- intended to execute sql.Rows.Scan and return result