psql

package module
v0.4.180 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: ISC Imports: 10 Imported by: 1

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

Constants

This section is empty.

Variables

View Source
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.

func TrimSql

func TrimSql(sql string) (trimmedSql string)

TrimSql removes leading and trailing newlines and replaces all infix newlines with space

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) Close

func (d *DBMap) Close() (err error)

Close shuts down the statement cache and the data source

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

func (*DBMap) QueryRow

func (d *DBMap) QueryRow(
	partition parl.DBPartition, query string, ctx context.Context,
	args ...any) (sqlRow *sql.Row, err error)

Query executes a query known to return exactly one row

func (*DBMap) QueryString

func (d *DBMap) QueryString(
	partition parl.DBPartition, query string, ctx context.Context,
	args ...any) (value string, err error)

Query executes a query known to return exactly one row and returns its string value

type ResultSetIterator

type ResultSetIterator[T any] struct {
	// contains filtered or unexported fields
}

ResultSetIterator is an iterator for a sql result-set

type ScanFunc

type ScanFunc[T any] func(sqlRows *sql.Rows) (t T, err error)

ScanFunc scans a record into type T

Directories

Path Synopsis
Package psql2 contains types supporting psql functions.
Package psql2 contains types supporting psql functions.

Jump to

Keyboard shortcuts

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