impl

package
v0.0.0-...-ab31a86 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connection

func Connection(ctx context.Context, db *sql.DB, config *sqldb.Config, validateColumnName func(string) error, argFmt string) sqldb.Connection

Connection returns a generic sqldb.Connection implementation for an existing sql.DB connection. argFmt is the format string for argument placeholders like "?" or "$%d" that will be replaced error messages to format a complete query.

func ForEachRowCallFunc

func ForEachRowCallFunc(ctx context.Context, callback any) (f func(sqldb.RowScanner) error, err error)

ForEachRowCallFunc will call the passed callback with scanned values or a struct for every row. If the callback function has a single struct or struct pointer argument, then RowScanner.ScanStruct will be used per row, else RowScanner.Scan will be used for all arguments of the callback. If the function has a context.Context as first argument, then the passed ctx will be passed on. The callback can have no result or a single error result value. If a non nil error is returned from the callback, then this error is returned immediately by this function without scanning further rows. In case of zero rows, no error will be returned.

func FormatQuery

func FormatQuery(query, argFmt string, args ...any) string

func FormatValue

func FormatValue(val any) (string, error)

FormatValue formats a value for debugging or logging SQL statements.

func Insert

func Insert(conn sqldb.Connection, table, argFmt string, values sqldb.Values) error

Insert a new row into table using the values.

func InsertReturning

func InsertReturning(conn sqldb.Connection, table, argFmt string, values sqldb.Values, returning string) sqldb.RowScanner

InsertReturning inserts a new row into table using values and returns values from the inserted row listed in returning.

func InsertStruct

func InsertStruct(conn sqldb.Connection, table string, rowStruct any, namer sqldb.StructFieldMapper, argFmt string, ignoreColumns []sqldb.ColumnFilter) error

InsertStruct inserts a new row into table using the connection's StructFieldMapper to map struct fields to column names. Optional ColumnFilter can be passed to ignore mapped columns.

func InsertStructs

func InsertStructs(conn sqldb.Connection, table string, rowStructs any, ignoreColumns ...sqldb.ColumnFilter) error

InsertStructs is a helper that calls InsertStruct for every struct in the rowStructs slice or array. The inserts are performed within a new transaction if the passed conn is not already a transaction.

func InsertUnique

func InsertUnique(conn sqldb.Connection, table, argFmt string, values sqldb.Values, onConflict string) (inserted bool, err error)

InsertUnique inserts a new row into table using the passed values or does nothing if the onConflict statement applies. Returns if a row was inserted.

func InsertUniqueStruct

func InsertUniqueStruct(conn sqldb.Connection, table string, rowStruct any, onConflict string, namer sqldb.StructFieldMapper, argFmt string, ignoreColumns []sqldb.ColumnFilter) (inserted bool, err error)

InsertUniqueStruct inserts a new row into table using the connection's StructFieldMapper to map struct fields to column names. Optional ColumnFilter can be passed to ignore mapped columns. Does nothing if the onConflict statement applies and returns if a row was inserted.

func Now

func Now(conn sqldb.Connection) (now time.Time, err error)

func QuoteLiteral

func QuoteLiteral(literal string) string

QuoteLiteral quotes a 'literal' (e.g. a parameter, often used to pass literal to DDL and other statements that do not accept parameters) to be used as part of an SQL statement. For example:

exp_date := pq.QuoteLiteral("2023-01-05 15:00:00Z")
err := db.Exec(fmt.Sprintf("CREATE ROLE my_user VALID UNTIL %s", exp_date))

Any single quotes in name will be escaped. Any backslashes (i.e. "\") will be replaced by two backslashes (i.e. "\\") and the C-style escape identifier that PostgreSQL provides ('E') will be prepended to the string.

func ReflectStructColumnPointers

func ReflectStructColumnPointers(structVal reflect.Value, namer sqldb.StructFieldMapper, columns []string) (pointers []any, err error)

func ReflectStructValues

func ReflectStructValues(structVal reflect.Value, namer sqldb.StructFieldMapper, ignoreColumns []sqldb.ColumnFilter) (columns []string, pkCols []int, values []any)

func ScanRowsAsSlice

func ScanRowsAsSlice(ctx context.Context, srcRows Rows, dest any, structFieldNamer sqldb.StructFieldMapper) error

ScanRowsAsSlice scans all srcRows as slice into dest. The rows must either have only one column compatible with the element type of the slice, or if multiple columns are returned then the slice element type must me a struct or struction pointer so that every column maps on exactly one struct field using structFieldNamer. In case of single column rows, nil must be passed for structFieldNamer. ScanRowsAsSlice calls srcRows.Close().

func ScanStrings

func ScanStrings(src Row) ([]string, error)

ScanStrings scans the values of a row as strings. Byte slices will be interpreted as strings, nil (SQL NULL) will be converted to an empty string, all other types are converted with fmt.Sprint.

func ScanStruct

func ScanStruct(srcRow Row, destStruct any, namer sqldb.StructFieldMapper) error

func ScanValues

func ScanValues(src Row) ([]any, error)

ScanValues returns the values of a row exactly how they are passed from the database driver to an sql.Scanner. Byte slices will be copied.

func ShouldWrapForArray

func ShouldWrapForArray(v reflect.Value) bool

func Update

func Update(conn sqldb.Connection, table string, values sqldb.Values, where, argFmt string, args []any) error

Update table rows(s) with values using the where statement with passed in args starting at $1.

func UpdateReturningRow

func UpdateReturningRow(conn sqldb.Connection, table string, values sqldb.Values, returning, where string, args ...any) sqldb.RowScanner

UpdateReturningRow updates a table row with values using the where statement with passed in args starting at $1 and returning a single row with the columns specified in returning argument.

func UpdateReturningRows

func UpdateReturningRows(conn sqldb.Connection, table string, values sqldb.Values, returning, where string, args ...any) sqldb.RowsScanner

UpdateReturningRows updates table rows with values using the where statement with passed in args starting at $1 and returning multiple rows with the columns specified in returning argument.

func UpdateStruct

func UpdateStruct(conn sqldb.Connection, table string, rowStruct any, namer sqldb.StructFieldMapper, argFmt string, ignoreColumns []sqldb.ColumnFilter) error

UpdateStruct updates a row of table using the exported fields of rowStruct which have a `db` tag that is not "-". Struct fields with a `db` tag matching any of the passed ignoreColumns will not be used. If restrictToColumns are provided, then only struct fields with a `db` tag matching any of the passed column names will be used.

func UpsertStruct

func UpsertStruct(conn sqldb.Connection, table string, rowStruct any, namer sqldb.StructFieldMapper, argFmt string, ignoreColumns []sqldb.ColumnFilter) error

UpsertStruct upserts a row to table using the exported fields of rowStruct which have a `db` tag that is not "-". Struct fields with a `db` tag matching any of the passed ignoreColumns will not be used. If restrictToColumns are provided, then only struct fields with a `db` tag matching any of the passed column names will be used. If inserting conflicts on pkColumn, then an update of the existing row is performed.

func WrapForArray

func WrapForArray(a any) interface {
	driver.Valuer
	sql.Scanner
}

func WrapNonNilErrorWithQuery

func WrapNonNilErrorWithQuery(err error, query, argFmt string, args []any) error

WrapNonNilErrorWithQuery wraps non nil errors with a formatted query if the error was not already wrapped with a query. If the passed error is nil, then nil will be returned.

Types

type CurrentRowScanner

type CurrentRowScanner struct {
	Rows              Rows
	StructFieldMapper sqldb.StructFieldMapper
}

CurrentRowScanner calls Rows.Scan without Rows.Next and Rows.Close

func (CurrentRowScanner) Columns

func (s CurrentRowScanner) Columns() ([]string, error)

func (CurrentRowScanner) Scan

func (s CurrentRowScanner) Scan(dest ...any) error

func (CurrentRowScanner) ScanStrings

func (s CurrentRowScanner) ScanStrings() ([]string, error)

func (CurrentRowScanner) ScanStruct

func (s CurrentRowScanner) ScanStruct(dest any) error

func (CurrentRowScanner) ScanValues

func (s CurrentRowScanner) ScanValues() ([]any, error)

type Row

type Row interface {
	// Columns returns the column names.
	Columns() ([]string, error)
	// Scan copies the columns in the current row into the values pointed
	// at by dest. The number of values in dest must be the same as the
	// number of columns in Rows.
	Scan(dest ...any) error
}

Row is an interface with the methods of sql.Rows that are needed for ScanStruct. Allows mocking for tests without an SQL driver.

type RowScanner

type RowScanner struct {
	// contains filtered or unexported fields
}

RowScanner implements sqldb.RowScanner for a sql.Row

func NewRowScanner

func NewRowScanner(rows Rows, structFieldNamer sqldb.StructFieldMapper, query, argFmt string, args []any) *RowScanner

func (*RowScanner) Columns

func (s *RowScanner) Columns() ([]string, error)

func (*RowScanner) Scan

func (s *RowScanner) Scan(dest ...any) (err error)

func (*RowScanner) ScanStrings

func (s *RowScanner) ScanStrings() ([]string, error)

func (*RowScanner) ScanStruct

func (s *RowScanner) ScanStruct(dest any) (err error)

func (*RowScanner) ScanValues

func (s *RowScanner) ScanValues() ([]any, error)

type Rows

type Rows interface {
	Row

	// Close closes the Rows, preventing further enumeration. If Next is called
	// and returns false and there are no further result sets,
	// the Rows are closed automatically and it will suffice to check the
	// result of Err. Close is idempotent and does not affect the result of Err.
	Close() error

	// Next prepares the next result row for reading with the Scan method. It
	// returns true on success, or false if there is no next result row or an error
	// happened while preparing it. Err should be consulted to distinguish between
	// the two cases.
	//
	// Every call to Scan, even the first one, must be preceded by a call to Next.
	Next() bool

	// Err returns the error, if any, that was encountered during iteration.
	// Err may be called after an explicit or implicit Close.
	Err() error
}

Rows is an interface with the methods of sql.Rows that are needed for ScanSlice. Allows mocking for tests without an SQL driver.

func RowAsRows

func RowAsRows(row Row) Rows

RowAsRows implements the methods of Rows for a Row as no-ops. Note that Next() always returns true leading to an endless loop if used to scan multiple rows.

type RowsScanner

type RowsScanner struct {
	// contains filtered or unexported fields
}

RowsScanner implements sqldb.RowsScanner with Rows

func NewRowsScanner

func NewRowsScanner(ctx context.Context, rows Rows, structFieldNamer sqldb.StructFieldMapper, query, argFmt string, args []any) *RowsScanner

func (*RowsScanner) Columns

func (s *RowsScanner) Columns() ([]string, error)

func (*RowsScanner) ForEachRow

func (s *RowsScanner) ForEachRow(callback func(sqldb.RowScanner) error) (err error)

func (*RowsScanner) ForEachRowCall

func (s *RowsScanner) ForEachRowCall(callback any) error

func (*RowsScanner) ScanAllRowsAsStrings

func (s *RowsScanner) ScanAllRowsAsStrings(headerRow bool) (rows [][]string, err error)

func (*RowsScanner) ScanSlice

func (s *RowsScanner) ScanSlice(dest any) error

func (*RowsScanner) ScanStructSlice

func (s *RowsScanner) ScanStructSlice(dest any) error

type SingleRowScanner

type SingleRowScanner struct {
	Row               Row
	StructFieldMapper sqldb.StructFieldMapper
}

SingleRowScanner always uses the same Row

func (SingleRowScanner) Columns

func (s SingleRowScanner) Columns() ([]string, error)

func (SingleRowScanner) Scan

func (s SingleRowScanner) Scan(dest ...any) error

func (SingleRowScanner) ScanStrings

func (s SingleRowScanner) ScanStrings() ([]string, error)

func (SingleRowScanner) ScanStruct

func (s SingleRowScanner) ScanStruct(dest any) error

func (SingleRowScanner) ScanValues

func (s SingleRowScanner) ScanValues() ([]any, error)

Jump to

Keyboard shortcuts

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