pg

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Example (EncodeQuery)
buf := fast.NewStringBuffer(256)
queryArgs := make([]any, 0, 5)

err := encodeQuery(buf, "SELECT * FROM table WHERE foo = %[1]d AND bar != %[1]d AND baz != %d", []any{123, 456}, &queryArgs)

if err != nil {
	panic(err)
}

fmt.Println(buf.String(), queryArgs)
Output:

SELECT * FROM table WHERE foo = $1 AND bar != $2 AND baz != $3 [123 123 456]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrefixSearch added in v0.1.0

func PrefixSearch(str string) string
Example
fmt.Println(PrefixSearch("  hello      world    "))
Output:

hello:* world:*

Types

type Alias added in v0.5.0

type Alias [2]StringEncoder

func (Alias) Col added in v0.5.0

func (t Alias) Col(col string) ChainedIdentifier

func (Alias) EncodeString added in v0.5.0

func (t Alias) EncodeString(b *fast.StringBuffer)

EncodeString implements StringEncoder.

type ChainedIdentifier added in v0.4.0

type ChainedIdentifier [2]StringEncoder

func (ChainedIdentifier) Alias added in v0.5.0

func (t ChainedIdentifier) Alias(col string) Alias

func (ChainedIdentifier) Col added in v0.4.0

func (ChainedIdentifier) EncodeString added in v0.4.0

func (t ChainedIdentifier) EncodeString(b *fast.StringBuffer)

EncodeString implements StringEncoder.

type Cond

type Cond func(buf *fast.StringBuffer, queryArgs *[]any)

func (Cond) EncodeQuery

func (c Cond) EncodeQuery(buf *fast.StringBuffer, queryArgs *[]any)

type DB

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

func New added in v0.2.0

func New(ctx context.Context, connString string, alterConfig ...func(*pgxpool.Config)) (db *DB, err error)

func (*DB) AcquireValues

func (db *DB) AcquireValues() *Values

func (*DB) Close added in v0.0.2

func (db *DB) Close()

func (*DB) Delete

func (db *DB) Delete(ctx context.Context, table Identifier, cond QueryEncoder) (count int64, err error)

func (*DB) Exec

func (db *DB) Exec(ctx context.Context, query string, args ...any) (cmd pgconn.CommandTag, err error)

Execute a query that doesn't return any rows. Arguments are passed in printf syntax.

func (*DB) InsertValues

func (db *DB) InsertValues(ctx context.Context, table Identifier, vals *Values, options ...InsertOption) (count int64, err error)

func (*DB) Query

func (db *DB) Query(ctx context.Context, query string, args ...any) (rows pgx.Rows, err error)

Query sends a query to the server and returns a Rows to read the results. Only errors encountered sending the query and initializing Rows will be returned. Err() on the returned Rows must be checked after the Rows is closed to determine if the query executed successfully.

The returned Rows must be closed before the connection can be used again. It is safe to attempt to read from the returned Rows even if an error is returned. The error will be the available in rows.Err() after rows are closed. It is allowed to ignore the error returned from Query and handle it in Rows.

It is possible for a call of FieldDescriptions on the returned Rows to return nil even if the Query call did not return an error.

It is possible for a query to return one or more rows before encountering an error. In most cases the rows should be collected before processing rather than processed while receiving each row. This avoids the possibility of the application processing rows from a query that the server rejected. The CollectRows function is useful here.

func (*DB) QueryRow

func (db *DB) QueryRow(ctx context.Context, query string, args ...any) (row pgx.Row)

QueryRow is a convenience wrapper over Query. Any error that occurs while querying is deferred until calling Scan on the returned Row. That Row will error with ErrNoRows if no rows are returned.

func (*DB) ReleaseValues

func (db *DB) ReleaseValues(v *Values)

func (*DB) Transaction

func (db *DB) Transaction(ctx context.Context, readOnly ...bool) (tx *Tx, err error)

func (*DB) UpdateValues

func (db *DB) UpdateValues(ctx context.Context, table Identifier, vals *Values, cond QueryEncoder) (count int64, err error)

type EncodeQuery added in v0.2.0

type EncodeQuery func(buf *fast.StringBuffer, queryArgs *[]any)

func Multiple added in v0.2.0

func Multiple(enc ...QueryEncoder) EncodeQuery

func (EncodeQuery) EncodeQuery added in v0.2.0

func (fn EncodeQuery) EncodeQuery(buf *fast.StringBuffer, queryArgs *[]any)

type EncodeString added in v0.2.0

type EncodeString func(buf *fast.StringBuffer)

func (EncodeString) EncodeString added in v0.2.0

func (fn EncodeString) EncodeString(buf *fast.StringBuffer)

type Error

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

func (Error) Args

func (err Error) Args() []any

func (Error) Error

func (err Error) Error() string

func (Error) Query

func (err Error) Query() string

func (Error) String

func (err Error) String() string

type Identifier

type Identifier string

func (Identifier) Alias added in v0.5.0

func (t Identifier) Alias(col string) Alias

func (Identifier) Col added in v0.4.0

func (t Identifier) Col(col string) ChainedIdentifier

func (Identifier) EncodeString

func (t Identifier) EncodeString(b *fast.StringBuffer)

EncodeString implements StringEncoder.

type InsertOption added in v0.6.0

type InsertOption func(vals *Values) EncodeQuery

func Upsert added in v0.6.0

func Upsert(numConflictingColumns int, ignoreColumns ...string) InsertOption

type MultiAnd

type MultiAnd interface {
	QueryEncoder
	And(QueryEncoder) MultiAnd
}

func And

func And(ops ...QueryEncoder) MultiAnd

type MultiOr

type MultiOr interface {
	QueryEncoder
	Or(QueryEncoder) MultiOr
}

func Or

func Or(ops ...QueryEncoder) MultiOr

type QueryEncoder

type QueryEncoder interface {
	EncodeQuery(buf *fast.StringBuffer, queryArgs *[]any)
}

func Eq

func Eq(col any, val any) QueryEncoder

func Gt

func Gt(col any, val any) QueryEncoder

func Gte

func Gte(col any, val any) QueryEncoder

func In

func In(col any, val any) QueryEncoder

func Lt

func Lt(col any, val any) QueryEncoder

func Lte

func Lte(col any, val any) QueryEncoder

func NotEq

func NotEq(col any, val any) QueryEncoder

func NotIn

func NotIn(col any, val any) QueryEncoder

func Order added in v0.2.0

func Order(column string, order ...string) QueryEncoder

func Raw

func Raw(s string, args ...any) QueryEncoder
func Search(col any, val string, options ...SearchOptions) QueryEncoder

type SearchOptions added in v0.1.0

type SearchOptions struct {
	Dictionary   string
	Preprocessor func(string) string
}

type StringEncoder added in v0.2.0

type StringEncoder = fast.StringEncoder

type Tx

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

func (*Tx) Commit

func (tx *Tx) Commit(ctx ...context.Context) (err error)

func (*Tx) Deadline

func (tx *Tx) Deadline() (deadline time.Time, ok bool)

Deadline implements context.Context.

func (*Tx) Done

func (tx *Tx) Done() <-chan struct{}

Done implements context.Context.

func (*Tx) Err

func (tx *Tx) Err() error

Err implements context.Context.

func (*Tx) Rollback

func (tx *Tx) Rollback(ctx ...context.Context) (err error)

func (*Tx) Value

func (tx *Tx) Value(key any) any

Value implements context.Context.

type Values

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

func (*Values) Empty

func (r *Values) Empty() bool

func (*Values) EncodeQuery added in v0.2.0

func (r *Values) EncodeQuery(buf *fast.StringBuffer, queryArgs *[]any)

EncodeQuery implements QueryEncoder.

func (*Values) Len

func (r *Values) Len() int

func (*Values) Value

func (r *Values) Value(column string, value any) *Values

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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