psql

package
v0.4.135 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: ISC Imports: 7 Imported by: 1

Documentation

Overview

Package psql augments database/sql

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 added in v0.4.127

func ColumnType(typ *sql.ColumnType) (s string)

func DelFromContext

func DelFromContext(ctx context.Context, key string) (ok bool)

DelFromContext removes a value from context. Thread-safe

func DiscardDB

func DiscardDB(ctx context.Context)

DiscardDB removes db handle from context

func ExecValues added in v0.4.14

func ExecValues(execResult sql.Result, e error) (ID, rows int64, err error)

ExecValues parses the result from an Exec* method into its values. if LastInsertId or RowsAffected fails, the error is added to err.

func GetDB

func GetDB(ctx context.Context) (db *sql.DB)

GetDB obtains db handle from context

func NewExecResult added in v0.4.14

func NewExecResult(execResult sql.Result, e error) (result parl.ExecResult, err error)

func QueryString added in v0.4.26

func QueryString(label string, ctx context.Context, dataSource parl.DataSource,
	query string, args ...any) (value string, err error)

func ScanToInt added in v0.4.14

func ScanToInt(sqlRow *sql.Row, e error) (value int, err error)

func ScanToString added in v0.4.26

func ScanToString(sqlRow *sql.Row, e error) (value string, err error)

func SqlExec added in v0.4.26

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 StoreDB

func StoreDB(ctx context.Context, db *sql.DB)

StoreDB saves db handle in context

func StoreInContext

func StoreInContext(ctx context.Context, key string, value interface{}) (ok bool)

StoreInContext stores a value in context. Thread-safe

Types

type Context

type Context struct {
	context.Context
	Map sync.Map
}

Context is a context.Context with a Value() implementation

func NewContext

func NewContext(ctx ...context.Context) (c *Context)

NewContext provides a context.Context with a Value() implementation

func (*Context) Delete

func (c *Context) Delete(key string)

StoreInContext stores a value in context. Thread-safe

func (*Context) Store

func (c *Context) Store(key string, value interface{})

StoreInContext stores a value in context. Thread-safe

func (*Context) Value

func (c *Context) Value(key interface{}) (result interface{})

Value retrieves data from context. Thread-safe

func (*Context) Value2

func (c *Context) Value2(key interface{}) (result interface{}, ok bool)

Value2 retrieves data and was–present indicator from context. Thread-safe

type DBMap added in v0.4.14

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 added in v0.4.14

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 added in v0.4.127

func NewDBMap2(
	dsnr parl.DataSourceNamer,
	schema func(dataSource parl.DataSource, ctx context.Context) (err error),
	getp *func(dataSourceName parl.DataSourceName,
		ctx context.Context) (dbStatementCache *StatementCache, err error),
) (dbMap *DBMap)

func (*DBMap) Close added in v0.4.14

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

Close shuts down the statement cache and the data source

func (*DBMap) Exec added in v0.4.14

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 added in v0.4.14

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 added in v0.4.14

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 added in v0.4.14

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 added in v0.4.14

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 ExecResult added in v0.4.14

type ExecResult struct {
	ID int64 // last insert ID, 0 if none like UPDATE
	// contains filtered or unexported fields
}

ExecResult makes sql.Result printable. sql.Result are obtained by invoking Exec* methods of sql.DB sql.Stmt sql.Conn sql.Tx. sql.Result is an interface with methods LastInsertId and RowsAffected. each driver provides an sql.Result implementation.

func (*ExecResult) Get added in v0.4.14

func (r *ExecResult) Get() (ID int64, rows int64)

Get obtains last id and number of affected rows with errors separately

func (ExecResult) String added in v0.4.14

func (r ExecResult) String() (s string)

type StatementCache added in v0.4.127

type StatementCache struct {
	// the datasource containing SQL tables
	DataSource parl.DataSource
	// contains filtered or unexported fields
}

StatementCache caches prepared statements for a data source

func NewStatementCache added in v0.4.127

func NewStatementCache(dataSource parl.DataSource) (cache *StatementCache)

NewStatementCache returns a cache for prepared statements for a data source

func (*StatementCache) Close added in v0.4.127

func (c *StatementCache) Close() (err error)

Close shuts down the statement cache

func (*StatementCache) Stmt added in v0.4.127

func (c *StatementCache) Stmt(query string, ctx context.Context) (stmt *sql.Stmt, err error)

Stmt returns a cached prepared statement from the cache or prepares, caches and returns a new prepared statement

func (*StatementCache) WrapStmt added in v0.4.127

func (c *StatementCache) WrapStmt(stmt *sql.Stmt) (stm Stmt)

WrapStmt retruns a wrapped statement if the data source support it

  • wrapper is used for retries of databases like SQLite3 that may always return busy errors

type Stmt added in v0.4.26

type Stmt interface {
	ExecContext(ctx context.Context, args ...any) (sqlResult sql.Result, err error)
	QueryContext(ctx context.Context, args ...any) (sqlRows *sql.Rows, err error)
	QueryRowContext(ctx context.Context, args ...any) (sqlRow *sql.Row)
}

type StmtWrapper added in v0.4.26

type StmtWrapper interface {
	WrapStmt(stmt *sql.Stmt) (stm Stmt)
}

Jump to

Keyboard shortcuts

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