Documentation
¶
Index ¶
- Constants
- func HTTPError(err error) error
- func IsDatabaseError(err error) bool
- func NormalizeError(err error) error
- func SQLState(err error) string
- 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) 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(name string) string
- func (bind *Bind) Replace(query string) string
- func (bind *Bind) Set(key string, value any) string
- func (bind *Bind) String() string
- type Conn
- type DatabaseError
- type Err
- type ListReader
- type Listener
- type Notification
- type OffsetLimit
- type Op
- type Opt
- func WithAddr(addr string) Opt
- func WithApplicationName(name string) Opt
- func WithBind(k string, v any) Opt
- func WithCredentials(user, password string) Opt
- func WithDatabase(name string) Opt
- func WithHostPort(host, port string) Opt
- func WithPassword(password string) Opt
- func WithSSLMode(mode string) Opt
- func WithSchemaSearchPath(schemas ...string) Opt
- func WithTrace(fn TraceFn) Opt
- func WithTracer(t trace.Tracer) Opt
- func WithURL(value string) Opt
- type PoolConn
- type Queries
- type Reader
- type Row
- type Selector
- type TraceFn
- type Writer
Constants ¶
const (
DefaultPort = "5432"
)
const (
TraceSpanNameArg = "otelspan"
)
Variables ¶
This section is empty.
Functions ¶
func HTTPError ¶ added in v1.2.0
HTTPError returns the appropriate HTTP status code for the given error.
func IsDatabaseError ¶ added in v1.1.4
IsDatabaseError reports whether err is a PostgreSQL error with a SQLSTATE code.
func NormalizeError ¶ added in v1.1.4
NormalizeError maps driver-specific PostgreSQL errors to package errors.
func SQLState ¶ added in v1.1.4
SQLState returns the PostgreSQL SQLSTATE code for err, if one is available.
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) Query ¶
Query looks up a named query, sets the span name for OpenTelemetry tracing, and returns the resolved SQL string. The name should match a query key that was loaded via withQueries (e.g., "pgqueue.get").
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 new connection with bound queries
WithQueries(...*Queries) 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
// Subscribe to a PostgreSQL notification channel. Subscribe returns setup
// errors only; after successful registration the subscription runs in the
// background and delivers notifications on the returned channel. The channel
// is closed when the context is cancelled, the pool is closed, or the
// listener stops because WaitForNotification returns an error.
Subscribe(context.Context, string) (<-chan Notification, 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 DatabaseError ¶ added in v1.1.4
type DatabaseError struct {
// contains filtered or unexported fields
}
func (*DatabaseError) Error ¶ added in v1.1.4
func (e *DatabaseError) Error() string
Error returns the wrapped database error string.
func (*DatabaseError) Is ¶ added in v1.1.4
func (e *DatabaseError) Is(target error) bool
Is supports errors.Is checks against broad and specific package errors.
func (*DatabaseError) Message ¶ added in v1.1.4
func (e *DatabaseError) Message() string
Message returns the PostgreSQL error message.
func (*DatabaseError) SQLState ¶ added in v1.1.4
func (e *DatabaseError) SQLState() string
SQLState returns the PostgreSQL SQLSTATE code.
func (*DatabaseError) Unwrap ¶ added in v1.1.4
func (e *DatabaseError) Unwrap() error
Unwrap returns the underlying driver 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" help:"Offset within the result set" jsonschema:"Offset for pagination" example:"0"`
Limit *uint64 `json:"limit,omitempty" help:"Number of items to return" jsonschema:"Limit for pagination" example:"100"`
}
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(total uint64)
Clamp restricts the limit to the number of rows available after offset.
func (*OffsetLimit) Query ¶ added in v1.4.0
func (r *OffsetLimit) Query(params url.Values)
Query sets offset and limit query parameters on the provided map.
type Opt ¶
type Opt func(*opt) error
Opt is a function which applies options for a connection pool
func WithApplicationName ¶ added in v1.1.0
WithApplicationName sets the application name for the connection. This appears in pg_stat_activity and helps identify connections.
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 WithPassword ¶ added in v1.1.3
WithPassword sets the connection password without affecting the username or database name.
func WithSSLMode ¶
WithSSLMode sets the PostgreSQL SSL mode. Valid values are "disable", "allow", "prefer", "require", "verify-ca", "verify-full".
func WithSchemaSearchPath ¶ added in v1.1.0
WithSchemaSearchPath sets the schema search path for the connection. If no schemas are provided, the search_path parameter is removed.
func WithTracer ¶ added in v1.1.1
WithTracer sets the OTEL tracer for the connection pool.
type PoolConn ¶
type Queries ¶ added in v1.1.0
type Queries struct {
// contains filtered or unexported fields
}
Queries represents a collection of named SQL statements parsed from a reader. SQL statements are separated by comment lines in the format: -- <identifier> or -- <identifier>.<identifier>, with any number of dots between them. Each statement is stored with its key.
func NewQueries ¶ added in v1.1.0
NewQueries parses SQL statements from a reader and returns a Queries collection. SQL statements must be separated by comment lines with a key: -- <identifier> or -- <identifier>.<identifier>, with any number of dots, where an identifier consists of letters, digits and underscores, and doesn't start with a digit. Other comment lines, such as those which describe a statement, or one which ends a sentence with a period, such as "-- form.", and comments at the end of a line, are removed from the statement, except within strings, quoted identifiers and dollar-quoted bodies, such as those of functions. Returns an error if duplicate keys are found or if reading fails.
Example input format:
-- user.select SELECT * FROM users WHERE id = $1; -- user.insert INSERT INTO users (name, email) VALUES ($1, $2);
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
|
|
|
pgqueue
command
|
|
|
Package frontend is the web UI, which make builds into dist, and which a command built with the "frontend" tag embeds, as EmbedFS.
|
Package frontend is the web UI, which make builds into dist, and which a command built with the "frontend" tag embeds, as EmbedFS. |
|
cmd
Package cmd serves the web UI, and the server's configuration, which it reads, beside the API, when the server is built with the "frontend" tag, which embeds the UI, once it's built, with npm, into frontend/dist, which make does.
|
Package cmd serves the web UI, and the server's configuration, which it reads, beside the API, when the server is built with the "frontend" tag, which embeds the UI, once it's built, with npm, into frontend/dist, which make does. |
|
Package broadcaster listens for the database's notifications, on a channel, and publishes them, as changes, to a go-server broadcaster, so the server's modules, and its clients, which watch the broadcaster, are told of them, with one connection which listens, rather than one each.
|
Package broadcaster listens for the database's notifications, on a channel, and publishes them, as changes, to a go-server broadcaster, so the server's modules, and its clients, which watch the broadcaster, are told of them, with one connection which listens, rather than one each. |
|
cmd
Package cmd has the broadcaster's flags for a server.
|
Package cmd has the broadcaster's flags for a server. |
|
pgmanager
|
|
|
wasm/pgmanager
command
|
|
|
pkg
|
|
|
test
Package test provides utilities for integration testing with PostgreSQL using testcontainers.
|
Package test provides utilities for integration testing with PostgreSQL using testcontainers. |