Documentation
¶
Overview ¶
Package pg provides PostgreSQL support for Go, built on top of pgx.
It provides binding SQL statements to named arguments, mapping Go structures to SQL tables, and easy semantics for Insert, Delete, Update, Get and List operations. The package also supports bulk insert operations, transactions, and tracing for observability.
Connection Pool ¶
Create a connection pool using NewPool:
pool, err := pg.NewPool(ctx,
pg.WithURL("postgres://user:pass@localhost:5432/dbname"),
)
if err != nil {
panic(err)
}
defer pool.Close()
Executing Queries ¶
Use bind variables with the With method:
err := pool.With("table", "users").Exec(ctx, `SELECT * FROM ${"table"}`)
CRUD Operations ¶
Implement the Reader, Writer, and Selector interfaces on your types to enable Insert, Update, Delete, Get, and List operations:
err := pool.Insert(ctx, &obj, obj) // Insert err := pool.Get(ctx, &obj, selector) // Get err := pool.List(ctx, &list, request) // List err := pool.Update(ctx, &obj, selector, writer) // Update err := pool.Delete(ctx, &obj, selector) // Delete
Index ¶
- Constants
- func NewTracer(fn TraceFn) *tracer
- func SchemaCreate(ctx context.Context, conn Conn, name string) error
- func SchemaDrop(ctx context.Context, conn Conn, name string) error
- func SchemaExists(ctx context.Context, conn Conn, name string) (bool, error)
- type Bind
- func (bind *Bind) Append(key string, value any) bool
- func (bind *Bind) Copy(pairs ...any) *Bind
- func (bind *Bind) Del(key string)
- func (bind *Bind) Exec(ctx context.Context, conn pgx.Tx, query string) error
- func (bind *Bind) Get(key string) any
- func (bind *Bind) Has(key string) bool
- func (bind *Bind) Join(key, sep string) string
- func (bind *Bind) MarshalJSON() ([]byte, error)
- func (bind *Bind) Query(ctx context.Context, conn pgx.Tx, query string) (pgx.Rows, error)
- func (bind *Bind) QueryRow(ctx context.Context, conn pgx.Tx, query string) pgx.Row
- func (bind *Bind) Replace(query string) string
- func (bind *Bind) Set(key string, value any) string
- func (bind *Bind) String() string
- type Conn
- type Err
- type ListReader
- type Listener
- type Notification
- type OffsetLimit
- type Op
- type Opt
- type PoolConn
- type Reader
- type Row
- type Selector
- type TraceFn
- type Writer
Constants ¶
const (
DefaultPort = "5432"
)
Variables ¶
This section is empty.
Functions ¶
func SchemaCreate ¶
SchemaCreate creates a schema with the given name if it does not exist.
func SchemaDrop ¶
SchemaDrop drops a schema with the given name if it exists.
Types ¶
type Bind ¶
Bind represents a set of variables and arguments to be used in a query. The vars are substituted in the query string itself, while the args are passed as arguments to the query.
func NewBind ¶
NewBind creates a new Bind object with the given name/value pairs. Returns nil if the number of arguments is not even.
func (*Bind) Append ¶
Append appends a bind var to a list. Returns false if the key is not a list, or the value is not a list.
func (*Bind) Join ¶
Join joins a bind var with a separator when it is a []any and returns the result as a string. Returns an empty string if the key does not exist.
func (*Bind) MarshalJSON ¶
func (*Bind) Replace ¶
Replace returns a query string with ${subtitution} replaced by the values:
- ${key} => value
- ${'key'} => 'value'
- ${"key"} => "value"
- $1 => $1
- $$ => $$
type Conn ¶
type Conn interface {
// Return a new connection with bound parameters
With(...any) Conn
// Return a connection to a remote database
Remote(database string) Conn
// Perform a transaction within a function
Tx(context.Context, func(Conn) error) error
// Perform a bulk operation within a function (and indicate whether this
// should be in a transaction)
Bulk(context.Context, func(Conn) error) error
// Execute a query
Exec(context.Context, string) error
// Perform an insert
Insert(context.Context, Reader, Writer) error
// Perform an update
Update(context.Context, Reader, Selector, Writer) error
// Perform a delete
Delete(context.Context, Reader, Selector) error
// Perform a get
Get(context.Context, Reader, Selector) error
// Perform a list. If the reader is a ListReader, then the
// count of items is also calculated
List(context.Context, Reader, Selector) error
}
type Err ¶
type Err int
type ListReader ¶
ListReader scans database rows and counts total results.
type Listener ¶
type Listener interface {
// Listen to a topic
Listen(context.Context, string) error
// Unlisten from a topic
Unlisten(context.Context, string) error
// Wait for a notification and return it
WaitForNotification(context.Context) (*Notification, error)
// Free resources
Close(context.Context) error
}
Listener is an interface for listening to notifications
type Notification ¶
type OffsetLimit ¶
type OffsetLimit struct {
Offset uint64 `json:"offset,omitempty"`
Limit *uint64 `json:"limit,omitempty"`
}
func (*OffsetLimit) Bind ¶
func (r *OffsetLimit) Bind(bind *Bind, max uint64)
Bind sets the offset and limit SQL fragment on the bind object.
func (*OffsetLimit) Clamp ¶
func (r *OffsetLimit) Clamp(len uint64)
Clamp restricts the limit to the maximum length.
type Opt ¶
type Opt func(*opt) error
Opt is a function which applies options for a connection pool
func WithCredentials ¶
WithCredentials sets the connection pool username and password. If the database name is not set, then the username will be used as the default database name.
func WithDatabase ¶
WithDatabase sets the database name for the connection. If the user name is not set, then the database name will be used as the user name.
func WithHostPort ¶
WithHostPort sets the hostname and port for the connection. If the port is not set, then the default port 5432 will be used.
func WithSSLMode ¶
WithSSLMode sets the PostgreSQL SSL mode. Valid values are "disable", "allow", "prefer", "require", "verify-ca", "verify-full".
type PoolConn ¶
type Selector ¶
type Selector interface {
// Set bind parameters for getting, updating or deleting
Select(*Bind, Op) (string, error)
}
Selector binds parameters for get, update, or delete operations.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
example
command
|
|
|
example2
command
|
|
|
pgmanager
command
|
|
|
pkg
|
|
|
manager
Package manager provides a comprehensive API for managing PostgreSQL server resources including roles, databases, schemas, tables, connections, and more.
|
Package manager provides a comprehensive API for managing PostgreSQL server resources including roles, databases, schemas, tables, connections, and more. |
|
manager/httpclient
Package httpclient provides a typed Go client for consuming the PostgreSQL management REST API.
|
Package httpclient provides a typed Go client for consuming the PostgreSQL management REST API. |
|
manager/httphandler
Package httphandler provides REST API endpoints for PostgreSQL management operations.
|
Package httphandler provides REST API endpoints for PostgreSQL management operations. |
|
manager/schema
Package schema defines all data types, request/response structures, and SQL queries for PostgreSQL management resources.
|
Package schema defines all data types, request/response structures, and SQL queries for PostgreSQL management resources. |
|
test
Package test provides utilities for integration testing with PostgreSQL using testcontainers.
|
Package test provides utilities for integration testing with PostgreSQL using testcontainers. |
|
types
Package types provides utility functions for pointer conversions and string formatting.
|
Package types provides utility functions for pointer conversions and string formatting. |
|
version
Package version provides build version information.
|
Package version provides build version information. |
|
wasm
|
|
|
pgmanager
command
|