postgres

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	Serializable    = pgx.Serializable
	RepeatableRead  = pgx.RepeatableRead
	ReadCommitted   = pgx.ReadCommitted
	ReadUncommitted = pgx.ReadUncommitted
)

Variables

This section is empty.

Functions

func AddToWhereClause

func AddToWhereClause(whereClause, newClause string) string

func BatchInsertQuery

func BatchInsertQuery(sqlQueryTemplate string, objectsToInsertSlice any, dbColumns ...string) (string, []any)

BatchInsertQuery takes an sqlQueryTemplate of the form 'INSERT INTO table_name %s VALUES %s', a slice of objects to insert as well as the dbColumns which should map 1-to-1 with the object's db tags. It will return a query and an array of params that can be used directly with db.Exec(query, params) or tx.Exec(query, params). This method will panic if one of the dbColumns is not a valid tag of an object to insert.

func GetDBColumns

func GetDBColumns(object any, opts ...GetDBColumnsOption) []string

GetDBColumns returns all db tags of a struct passed by value.

func GetParams

func GetParams(object any, dbColumns ...string) []any

GetParams returns the params for the given db columns.

func InsertQuery

func InsertQuery(sqlQueryTemplate string, objectToInsert any, dbColumns ...string) (string, []any)

InsertQuery takes an sqlQueryTemplate of the form 'INSERT INTO table_name %s VALUES %s', an object to insert as well as the dbColumns which should map 1-to-1 with the object's db tags. It will return a query and an array of params that can be used directly with db.Exec(query, params) or tx.Exec(query, params). This method will panic if one of the dbColumns is not a valid tag of an object to insert.

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation returns true if the error is a PostgreSQL unique constraint violation (SQLSTATE 23505).

func NewNullString

func NewNullString(s string) sql.NullString

NewNullString returns a sql.NullString value from an input string

func QualifyColumns

func QualifyColumns(columns []string, alias string) string

QualifyColumns returns a comma-separated string of columns prefixed with the given table alias.

func SelectQuery

func SelectQuery(sqlQueryTemplate string, dbColumns []string) string

SelectQuery injects dbColumns into a sqlQuery template

Types

type Client

type Client struct {
	*pgxpool.Pool
	// contains filtered or unexported fields
}

Client is a wrapper around sqlx db to avoid importing it in core packages.

func NewClient

func NewClient(opts *Opts) *Client

func (*Client) Close

func (c *Client) Close()

func (*Client) ExecuteTransaction

func (c *Client) ExecuteTransaction(ctx context.Context, isolationLevel pgx.TxIsoLevel, fn func(pgx.Tx) error) error

ExecuteTransaction executes a transaction and retries serialization failures.

func (*Client) Start

func (c *Client) Start(ctx context.Context) error

NewClient instantiates and returns a new Postgres Client. Returns an error if it fails to ping server.

func (*Client) WithLogger

func (c *Client) WithLogger(logger *slog.Logger) *Client

type Dictionary

type Dictionary map[string]string

Dictionary is a postgres safe alias of map[string]string. This object can be inserted and retrieved from the database without the user having to marshal or unmarshal a dictionary.

func (*Dictionary) Scan

func (d *Dictionary) Scan(src any) error

Scan implements the sql.scanner interface, unmarshalling a json byte representation of a dictionary into this dictionary. (Used implicitely during SELECT)

func (Dictionary) Value

func (d Dictionary) Value() (driver.Value, error)

Value implements the driver.Value interface. It returns the json bytes representation of this dictionary, which is a supported type of postgres. (Used implicitely during INSERT)

type GetDBColumnsOption

type GetDBColumnsOption func(*GetDBColumnsOpts)

func ExceptColumns

func ExceptColumns(columns ...string) GetDBColumnsOption

func ExceptTagMatch

func ExceptTagMatch(key, value string) GetDBColumnsOption

type GetDBColumnsOpts

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

type Opts

type Opts struct {
	Host     string `long:"host"     env:"HOST"     default:"localhost" description:"Postgres host"`
	Port     int    `long:"port"     env:"PORT"     default:"5432"      description:"Postgres port"`
	User     string `long:"user"     env:"USER"     default:"postgres"  description:"Postgres username"`
	Password string `long:"password" env:"PASSWORD" default:"postgres"  description:"Postgres password"`
	Database string `long:"database" env:"DATABASE" default:"postgres"  description:"Postgres database"`
	MaxConns int    `long:"maxconns" env:"MAXCONNS" default:"10"        description:"Max number of connections"`
	SSLMode  string `long:"sslmode"  env:"SSLMODE"  default:"disable"   description:"Postgres SSL mode"`
}

Opts is the Client config containing the host, port, user and password.

func (*Opts) Endpoint

func (o *Opts) Endpoint() string

type Tx

type Tx = pgx.Tx

Directories

Path Synopsis
Package testserver runs a lightweight Postgres server for testing.
Package testserver runs a lightweight Postgres server for testing.

Jump to

Keyboard shortcuts

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