Documentation ¶
Index ¶
- type BatchQuerier
- type BatchRecorder
- type MemoryQueryCacher
- type Querier
- func (x *Querier) Begin(ctx context.Context) (pgx.Tx, error)
- func (x *Querier) Close()
- func (x *Querier) CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, ...) (int64, error)
- func (x *Querier) Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)
- func (x *Querier) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)
- func (x *Querier) QueryRow(ctx context.Context, query string, args ...any) pgx.Row
- func (x *Querier) SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults
- type QuerierTx
- func (q *QuerierTx) Begin(ctx context.Context) (pgx.Tx, error)
- func (q *QuerierTx) Commit(ctx context.Context) error
- func (q *QuerierTx) Conn() *pgx.Conn
- func (q *QuerierTx) CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, ...) (int64, error)
- func (q *QuerierTx) Exec(ctx context.Context, query string, args ...any) (commandTag pgconn.CommandTag, err error)
- func (q *QuerierTx) LargeObjects() pgx.LargeObjects
- func (q *QuerierTx) Prepare(ctx context.Context, name string, query string) (*pgconn.StatementDescription, error)
- func (q *QuerierTx) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)
- func (q *QuerierTx) QueryRow(ctx context.Context, query string, args ...any) pgx.Row
- func (q *QuerierTx) Queryable() Queryable
- func (q *QuerierTx) Rollback(ctx context.Context) error
- func (q *QuerierTx) SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults
- type QueryCacher
- type QueryItem
- type QueryKey
- type QueryOptions
- type Queryable
- type Row
- type RowError
- type RowRecorder
- type RowScanner
- type Rows
- func (r *Rows) Close()
- func (r *Rows) CommandTag() pgconn.CommandTag
- func (r *Rows) Conn() *pgx.Conn
- func (r *Rows) Err() error
- func (r *Rows) FieldDescriptions() []pgconn.FieldDescription
- func (r *Rows) Next() bool
- func (r *Rows) RawValues() [][]byte
- func (r *Rows) Scan(values ...any) error
- func (r *Rows) Values() ([]any, error)
- type RowsError
- func (r *RowsError) Close()
- func (r *RowsError) CommandTag() pgconn.CommandTag
- func (r *RowsError) Conn() *pgx.Conn
- func (r *RowsError) Err() error
- func (r *RowsError) FieldDescriptions() []pgconn.FieldDescription
- func (r *RowsError) Next() bool
- func (r *RowsError) RawValues() [][]byte
- func (r *RowsError) Scan(dest ...any) error
- func (r *RowsError) Values() ([]any, error)
- type RowsRecorder
- func (r *RowsRecorder) Close()
- func (r *RowsRecorder) CommandTag() pgconn.CommandTag
- func (r *RowsRecorder) Conn() *pgx.Conn
- func (r *RowsRecorder) Err() error
- func (r *RowsRecorder) FieldDescriptions() []pgconn.FieldDescription
- func (r *RowsRecorder) Next() bool
- func (r *RowsRecorder) RawValues() [][]byte
- func (r *RowsRecorder) Scan(values ...any) error
- func (r *RowsRecorder) Values() ([]any, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BatchQuerier ¶
type BatchQuerier struct {
// contains filtered or unexported fields
}
BatchQuerier is a wrapper around pgx.BatchResults that records the batch results.
func (*BatchQuerier) Close ¶
func (b *BatchQuerier) Close() error
Close implements pgx.BatchResults.
func (*BatchQuerier) Exec ¶
func (b *BatchQuerier) Exec() (pgconn.CommandTag, error)
Exec implements pgx.BatchResults.
func (*BatchQuerier) Query ¶
func (b *BatchQuerier) Query() (pgx.Rows, error)
Query implements pgx.BatchResults.
func (*BatchQuerier) QueryRow ¶
func (b *BatchQuerier) QueryRow() pgx.Row
QueryRow implements pgx.BatchResults.
type BatchRecorder ¶
type BatchRecorder struct {
// contains filtered or unexported fields
}
BatchRecorder is a wrapper around pgx.BatchResults that records the batch results.
func (*BatchRecorder) Close ¶
func (b *BatchRecorder) Close() error
Close implements pgx.BatchResults.
func (*BatchRecorder) Exec ¶
func (b *BatchRecorder) Exec() (pgconn.CommandTag, error)
Exec implements pgx.BatchResults.
func (*BatchRecorder) Query ¶
func (b *BatchRecorder) Query() (pgx.Rows, error)
Query implements pgx.BatchResults.
func (*BatchRecorder) QueryRow ¶
func (b *BatchRecorder) QueryRow() pgx.Row
QueryRow implements pgx.BatchResults.
type MemoryQueryCacher ¶
type MemoryQueryCacher struct {
// contains filtered or unexported fields
}
MemoryQueryCacher is a simple in-memory cache implementation.
func NewMemoryQueryCacher ¶
func NewMemoryQueryCacher() *MemoryQueryCacher
NewMemoryQueryCacher creates a new MemoryCacher.
type Querier ¶
type Querier struct { // Options is the cache options. Options *QueryOptions // Querier is the query executor. Querier Queryable // Cacher is the cache store. Cacher QueryCacher }
Querier is a wrapper around pgx.Conn that caches prepared statements.
Example ¶
config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL")) if err != nil { panic(err) } // create a new connection pool conn, err := pgxpool.NewWithConfig(context.TODO(), config) if err != nil { panic(err) } // close the connection when the function returns defer conn.Close() // create a new querier querier := &pgxcache.Querier{ // set the default query options, which can be overridden by the query // -- @cache-min-rows 1 // -- @cache-max-rows 100 // -- @cache-max-lifetime 30s Options: &pgxcache.QueryOptions{ MinRows: 1, MaxRows: 100, MaxLifetime: 30 * time.Second, }, Cacher: pgxcache.NewMemoryQueryCacher(), Querier: conn, } // create a new organization struct type Organization struct { Name string `db:"name"` } // fetch all the organizations rows, err := querier.Query(context.TODO(), "SELECT * FROM organization") if err != nil { panic(err) } // close the rows when the function returns defer rows.Close() for rows.Next() { organization, err := pgx.RowToStructByName[Organization](rows) if err != nil { panic(err) } fmt.Println(organization.Name) }
Output:
func (*Querier) CopyFrom ¶
func (x *Querier) CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, source pgx.CopyFromSource) (int64, error)
CopyFrom implements Queryable.
func (*Querier) Exec ¶
Exec is a helper that makes it easy to execute a query that doesn't return rows.
type QuerierTx ¶
type QuerierTx struct { // Querier is the query executor. Tx pgx.Tx // Options is the cache options. Options *QueryOptions // Cacher is the cache store. Cacher QueryCacher }
Querier is a wrapper around pgx.Conn that caches prepared statements.
func (*QuerierTx) CopyFrom ¶
func (q *QuerierTx) CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, source pgx.CopyFromSource) (int64, error)
CopyFrom implements pgx.Tx.
func (*QuerierTx) Exec ¶
func (q *QuerierTx) Exec(ctx context.Context, query string, args ...any) (commandTag pgconn.CommandTag, err error)
Exec implements pgx.Tx.
func (*QuerierTx) LargeObjects ¶
func (q *QuerierTx) LargeObjects() pgx.LargeObjects
LargeObjects implements pgx.Tx.
func (*QuerierTx) Prepare ¶
func (q *QuerierTx) Prepare(ctx context.Context, name string, query string) (*pgconn.StatementDescription, error)
Prepare implements pgx.Tx.
type QueryCacher ¶
type QueryCacher interface { // Get must return a pointer to the item, a boolean representing whether // item is present or not, and an error (must be nil when key is not // present). Get(context.Context, *QueryKey) (*QueryItem, error) // Set sets the item into cache with the given TTL. Set(context.Context, *QueryKey, *QueryItem, time.Duration) error // Reset resets the cache Reset(context.Context) error }
QueryCacher represents a backend cache that can be used by sqlcache package.
type QueryItem ¶
type QueryItem struct { // CommandTag is the command tag returned by the query. CommandTag string // Fields is the field descriptions of the query result. Fields []pgconn.FieldDescription // Rows is the query result. Rows [][][]byte }
QueryItem represents a query result.
func (*QueryItem) MarshalText ¶
MarshalText implements encoding.TextMarshaler.
func (*QueryItem) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler.
type QueryKey ¶
type QueryKey struct { // SQL is the SQL query. SQL string // Args are the arguments to the query. Args []any }
QueryKey is a unique identifier for a query.
type QueryOptions ¶
type QueryOptions struct { // MinRows is the minimum number of rows that the query should return. MinRows int // MaxRows is the maximum number of rows that the query should return. MaxRows int // MaxLifetime is the duration that the query result should be cached. MaxLifetime time.Duration }
QueryOptions represents the options that can be specified in a SQL query.
func ParseQueryOptions ¶
func ParseQueryOptions(query string) (*QueryOptions, error)
ParseQueryOptions parses query options from a SQL query.
type Queryable ¶
type Queryable interface { // Begin starts a pseudo nested transaction. Begin(ctx context.Context) (pgx.Tx, error) // Exec executes a query that doesn't return rows. Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error) // Query executes a query that returns rows, typically a SELECT. Query(ctx context.Context, query string, args ...any) (pgx.Rows, error) // QueryRow executes a query that is expected to return at most one row. QueryRow will always return a non-nil value. QueryRow(ctx context.Context, query string, args ...any) pgx.Row // SendBatch sends a batch of queries to the server. Use Batch.Send to create a batch. The returned Batch SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults // CopyFrom copies data from reader to table. CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, source pgx.CopyFromSource) (int64, error) }
Queryable is an interface that wraps the pgx.Executor interface.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row is a wrapper around pgx.Row.
type RowError ¶
type RowError struct {
// contains filtered or unexported fields
}
RowError is a wrapper around pgx.Row that returns an error.
type RowRecorder ¶
type RowRecorder struct {
// contains filtered or unexported fields
}
RowRecorder is a wrapper around pgx.Row that records the error.
func (*RowRecorder) RawValues ¶
func (r *RowRecorder) RawValues() [][]byte
RawValues returns the raw values.
type RowScanner ¶
type RowScanner struct {
// contains filtered or unexported fields
}
RowScanner is a wrapper around pgx.Row that scans the values.
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows is a wrapper around pgx.Rows that caches the rows.
func (*Rows) CommandTag ¶
func (r *Rows) CommandTag() pgconn.CommandTag
CommandTag implements pgx.Rows.
func (*Rows) FieldDescriptions ¶
func (r *Rows) FieldDescriptions() []pgconn.FieldDescription
FieldDescriptions implements pgx.Rows.
type RowsError ¶
type RowsError struct {
// contains filtered or unexported fields
}
RowsError is a wrapper around pgx.Rows that returns an error.
func (*RowsError) CommandTag ¶
func (r *RowsError) CommandTag() pgconn.CommandTag
CommandTag implements pgx.Rows.
func (*RowsError) FieldDescriptions ¶
func (r *RowsError) FieldDescriptions() []pgconn.FieldDescription
FieldDescriptions implements pgx.Rows.
type RowsRecorder ¶
type RowsRecorder struct {
// contains filtered or unexported fields
}
RowsRecorder is a wrapper around pgx.Rows that records the rows.
func (*RowsRecorder) CommandTag ¶
func (r *RowsRecorder) CommandTag() pgconn.CommandTag
CommandTag implements pgx.Rows.
func (*RowsRecorder) FieldDescriptions ¶
func (r *RowsRecorder) FieldDescriptions() []pgconn.FieldDescription
FieldDescriptions implements pgx.Rows.
func (*RowsRecorder) RawValues ¶
func (r *RowsRecorder) RawValues() [][]byte
RawValues implements pgx.Rows.
func (*RowsRecorder) Scan ¶
func (r *RowsRecorder) Scan(values ...any) error
Scan implements pgx.Rows.
func (*RowsRecorder) Values ¶
func (r *RowsRecorder) Values() ([]any, error)
Values implements pgx.Rows.