sqlpgx

package
v0.0.0-...-73c04d8 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxParamPerInsert  = 15000
	AbsoluteMaxParamPerInsert = 65535

	ECode090701 = e.Code0907 + "01"
	ECode090702 = e.Code0907 + "02"
	ECode090703 = e.Code0907 + "03"
	ECode090704 = e.Code0907 + "04"
	ECode090705 = e.Code0907 + "05"
	ECode090706 = e.Code0907 + "06"
	ECode090707 = e.Code0907 + "07"
	ECode090708 = e.Code0907 + "08"
	ECode090709 = e.Code0907 + "09"
	ECode09070A = e.Code0907 + "0A"
	ECode09070B = e.Code0907 + "0B"
)
View Source
const (
	DefaultMaxParamPerUpdate  = 15000
	AbsoluteMaxParamPerUpdate = 65535

	ECode090901 = e.Code0909 + "01"
	ECode090902 = e.Code0909 + "02"
	ECode090903 = e.Code0909 + "03"
	ECode090904 = e.Code0909 + "04"
	ECode090905 = e.Code0909 + "05"
	ECode090906 = e.Code0909 + "06"
	ECode090907 = e.Code0909 + "07"
	ECode090908 = e.Code0909 + "08"
	ECode090909 = e.Code0909 + "09"
)
View Source
const (
	// FieldPlaceHolder TODO: move to more generic location
	FieldPlaceHolder = "<FIELD_PLACE_HOLDER>"
	// FieldCount TODO: move to more generic location
	FieldCount = "count(*) AS cnt"

	ECode090101 = e.Code0901 + "01"
	ECode090102 = e.Code0901 + "02"
)
View Source
const (
	ECode090201 = e.Code0902 + "01"
	ECode090202 = e.Code0902 + "02"
)
View Source
const (
	ECode090601 = e.Code0906 + "01"
	ECode090602 = e.Code0906 + "02"
	ECode090603 = e.Code0906 + "03"
)
View Source
const (
	ECode090301 = e.Code0903 + "01"
	ECode090302 = e.Code0903 + "02"
	ECode090303 = e.Code0903 + "03"
	ECode090304 = e.Code0903 + "04"
	ECode090305 = e.Code0903 + "05"
	ECode090306 = e.Code0903 + "06"
	ECode090307 = e.Code0903 + "07"
	ECode090308 = e.Code0903 + "08"
	ECode090309 = e.Code0903 + "09"
	ECode09030A = e.Code0903 + "0A"
	ECode09030B = e.Code0903 + "0B"
	ECode09030C = e.Code0903 + "0C"
	ECode09030D = e.Code0903 + "0D"
	ECode09030E = e.Code0903 + "0E"
	ECode09030F = e.Code0903 + "0F"
	ECode09030G = e.Code0903 + "0G"
	ECode09030H = e.Code0903 + "0H"
	ECode09030I = e.Code0903 + "0I"
	ECode09030J = e.Code0903 + "0J"
	ECode09030K = e.Code0903 + "0K"
	ECode09030L = e.Code0903 + "0L"
	ECode09030M = e.Code0903 + "0M"
	ECode09030N = e.Code0903 + "0N"
	ECode09030O = e.Code0903 + "0O"
	ECode09030P = e.Code0903 + "0P"
	ECode09030Q = e.Code0903 + "0Q"
	ECode09030R = e.Code0903 + "0R"
	ECode09030S = e.Code0903 + "0S"
	ECode09030T = e.Code0903 + "0T"
	ECode09030U = e.Code0903 + "0U"
	ECode09030V = e.Code0903 + "0V"
	ECode09030W = e.Code0903 + "0W"
	ECode09030X = e.Code0903 + "0X"
	ECode09030Y = e.Code0903 + "0Y"
	ECode09030Z = e.Code0903 + "0Z"
)
View Source
const (
	PQErr23505UniqueViolation = "23505" // Postgres code for unique_violation
	PQErr58030IOError         = "58030" // Postgres code for i/o error ("could not write to temporary file")
	PQErr23503IOError         = "23503" // Postgres code for foreign_key_violation
)
View Source
const (
	ECode020401 = e.Code0204 + "01"
	ECode020402 = e.Code0204 + "02"
	ECode020403 = e.Code0204 + "03"
	ECode020404 = e.Code0204 + "04"
	ECode020405 = e.Code0204 + "05"
	ECode020406 = e.Code0204 + "06"
	ECode020407 = e.Code0204 + "07"
	ECode020408 = e.Code0204 + "08"
	ECode020409 = e.Code0204 + "09"
	ECode02040A = e.Code0204 + "0A"
)
View Source
const (
	ECode090501 = e.Code0905 + "01"
	ECode090502 = e.Code0905 + "02"
	ECode090503 = e.Code0905 + "03"
	ECode090504 = e.Code0905 + "04"
	ECode090505 = e.Code0905 + "05"
	ECode090506 = e.Code0905 + "06"
	ECode090507 = e.Code0905 + "07"
)

Variables

This section is empty.

Functions

func CheckPQError

func CheckPQError(err error) error

CheckPQError checks if it's a known postgres error and returns a proper message

func GetConnectionStr

func GetConnectionStr(cp *ConnParam) (connStr string)

GetConnectionStr returns a connection string

func IsPQError

func IsPQError(err error, errorCode string) bool

IsPQError checks if the passed error is the specified Postgres error code

Types

type BulkInsert

type BulkInsert struct {
	Table   string // The name of the table to bulk insert into
	Columns string // The column list to bulk insert
	Suffix  string // Optional suffix to append to a bulk insert (e.g. ON CONFLICT DO NOTHING)
	// contains filtered or unexported fields
}

BulkInsert allows for multiple inserts to be ran in a single query, speeding up inserts into a table.

func NewBulkInsert

func NewBulkInsert(ctx context.Context, db *Connection, table, columns, suffix string, txName string) (bi *BulkInsert, err error)

NewBulkInsert initializes a new BulkInsert, specifying the table, columns and optional suffix to use.

func (*BulkInsert) Add

func (bi *BulkInsert) Add(ctx context.Context, values ...interface{}) (rowsInserted int, err error)

Add adds the values to be sent as a bulk insert. If the number of parameters exceeds the max param per insert, then it will run the current build statement and then reset itself for more values to be added. If it executed a statement, it will return the current count as the number of rows inserted. This will not track actual rows inserted, e.g. if duplicates are ignored.

func (*BulkInsert) Close

func (bi *BulkInsert) Close() (errList []error)

Close the batch

func (*BulkInsert) Flush

func (bi *BulkInsert) Flush(ctx context.Context) (err error)

Flush if there is a remaining statement to run, it will execute the query

func (*BulkInsert) GetColumnCount

func (bi *BulkInsert) GetColumnCount() (colCount int)

GetColumnCount returns the number of columns in the bulk insert

func (*BulkInsert) GetCount

func (bi *BulkInsert) GetCount() (count int)

GetCount returns the number of rows that have been added to the bulk insert

func (*BulkInsert) GetMaxRowPerInsert

func (bi *BulkInsert) GetMaxRowPerInsert() (maxRows uint)

GetMaxRowPerInsert gets the current max rows per insert (maximum params per insert divided by the params per statement).

func (*BulkInsert) GetTotal

func (bi *BulkInsert) GetTotal() (total int)

GetTotal returns the total number of rows that have been added to the bulk insert since it was initialized

func (*BulkInsert) SetMaxParamPerInsert

func (bi *BulkInsert) SetMaxParamPerInsert(i int)

SetMaxParamPerInsert sets the max param per insert. If this value is greater than the absolute maximum, then it will silently set it to the absolute maximum instead

func (*BulkInsert) SetMaxRowPerInsert

func (bi *BulkInsert) SetMaxRowPerInsert(maxRows uint) (actualMaxRows uint)

SetMaxRowPerInsert sets the max rows per insert. If the specified number of rows makes the parameters per insert exceed the absolute max, then the max rows will be decremented until it falls into the allowed range. The number of parameters is equal to the maxRows multiplied by the params per statement (number of columns in the insert)

func (*BulkInsert) SetPostInsert

func (bi *BulkInsert) SetPostInsert(f func(rowCount int) error)

SetPreInsert sets the pre insert func, called right before an insert is executed

func (*BulkInsert) SetPreInsert

func (bi *BulkInsert) SetPreInsert(f func() error)

SetPreInsert sets the pre insert func, called right before an insert is executed

type BulkUpdate

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

BulkUpdate allows for multiple updates to be ran in a single query

func NewBulkUpdate

func NewBulkUpdate(ctx context.Context, db *Connection, table string,
	columns []BulkUpdateCol, whereColumns []string,
	useCache bool) (bu *BulkUpdate, err error)

NewBulkUpdate initializes a new BulkUpdate, specifying the table, columns to update, columns to use as filters and whether to use caching or not

func (*BulkUpdate) Add

func (bu *BulkUpdate) Add(ctx context.Context, values ...interface{}) (rowsUpdated int, err error)

Add adds the values to be sent as a bulk update. If the number of parameters exceeds the max param per update, then it will run the currently build statement and then reset itself for more values to be added. It will return the number of rows that were updated, or zero if the query was not executed

func (*BulkUpdate) Close

func (bu *BulkUpdate) Close() (errList []error)

Close the batch

func (*BulkUpdate) Flush

func (bu *BulkUpdate) Flush(ctx context.Context) (err error)

Flush if there is a remaining statement to run, it will execute the query

func (*BulkUpdate) GetAbsoluteMaxParamPerUpdate

func (bu *BulkUpdate) GetAbsoluteMaxParamPerUpdate() (count int)

GetAbsoluteMaxParamPerUpdate returns the maximum number of columns that is allowed to be added to the bulk update

func (*BulkUpdate) GetCount

func (bu *BulkUpdate) GetCount() (count int)

GetCount returns the number of rows that have been added to the bulk update since initialization or the last Flush call

func (*BulkUpdate) GetParamPerStatement

func (bu *BulkUpdate) GetParamPerStatement() (count int)

GetParamPerStatement returns the number of params that have been assigned to each statement

func (*BulkUpdate) GetTotal

func (bu *BulkUpdate) GetTotal() (total int)

GetTotal returns the total number of rows that have been added to the bulk update since it was initialized

func (*BulkUpdate) SetMaxParamPerUpdate

func (bu *BulkUpdate) SetMaxParamPerUpdate(i int)

SetMaxParamPerUpdate sets the max params to use per update. If this value is greater than the absolute maximum, then it will silently set it to the absolute maximum instead

type BulkUpdateCol

type BulkUpdateCol struct {
	Name string
	Type string
}

BulkUpdateCol defines the column name and type. If type is left empty, it will not be specified in the update query. If it is specified, it must be a valid Postgres type in the database and inserted values will automatically be cast to that type

type ConnParam

type ConnParam struct {
	Host       string `json:"host"`
	Port       string `json:"port"`
	User       string `json:"user"`
	Password   string `json:"password"`
	DBName     string `json:"dbname"`
	SSLMode    string `json:"sslmode"`
	SearchPath string `json:"searchpath"`
}

ConnParam connection parameters used to initialize a connection

func GetConnParamFromENV

func GetConnParamFromENV() (cp *ConnParam)

GetConnParamFromENV initializes new connection parameters and populates from ENV variables

func GetConnParamFromJSONConfig

func GetConnParamFromJSONConfig(configPath string) (cp *ConnParam, err error)

GetConnParamFromJSONConfig get connection params from a JSON config

type Connection

type Connection struct {
	DB   *pgxpool.Pool
	Slug *Slug
	// contains filtered or unexported fields
}

Connection wrapper of the *pgxpool.Pool

func NewPostgresConn

func NewPostgresConn(ctx context.Context, cp *ConnParam) (conn *Connection, err error)

NewPostgresConn initializes a new Postgres connection

func (*Connection) Begin

func (c *Connection) Begin(ctx context.Context) (err error)

Begin wrapper for sql.Begin. It doesn't return the txn object, but stores it internally and it will be used automatically for subsequent query/exec/select calls until commit/rollback is called. This is not thread safe and shouldn't be called within a go routine

func (*Connection) BeginReturnDB

func (c *Connection) BeginReturnDB(ctx context.Context) (db *Connection, err error)

BeginReturnDB begins a new transaction, returning a copy of the database connection with the txn already set. This copy should be used to call all txn commands and then discarded.

func (*Connection) BeginUseDefaultTxn

func (c *Connection) BeginUseDefaultTxn(ctx context.Context) (err error)

BeginUseDefaultTxn begins a txn, storing it in the txn property If txn is not nil (already in a txn), it will return an error

func (*Connection) Close

func (c *Connection) Close()

Close wrapper for close

func (*Connection) Commit

func (c *Connection) Commit(ctx context.Context) (err error)

Commit wrapper for sql.Commit. If successfull, will unset the txn object

func (*Connection) Delete

func (c *Connection) Delete(from string) sq.DeleteBuilder

Delete wrapper for github.com/Masterminds/squirrel.Delete

func (*Connection) Exec

func (c *Connection) Exec(ctx context.Context, query string, args ...interface{}) (res *pgconn.CommandTag, err error)

Exec wrapper for sql.Exec with automatic txn handling

func (*Connection) ExecDelete

func (c *Connection) ExecDelete(ctx context.Context, delB sq.DeleteBuilder) (err error)

ExecDelete wrapper to generate SQL/bind list and then execute delete query

func (*Connection) ExecInsert

func (c *Connection) ExecInsert(ctx context.Context, ib sq.InsertBuilder) (err error)

ExecInsert wrapper to generate SQL/bind list and then execute insert query

func (*Connection) ExecInsertReturningID

func (c *Connection) ExecInsertReturningID(ctx context.Context, ib sq.InsertBuilder) (id int, err error)

ExecInsertReturningID wrapper to generate SQL/bind list and then execute insert query

func (*Connection) ExecUpdate

func (c *Connection) ExecUpdate(ctx context.Context, ub sq.UpdateBuilder) (err error)

ExecUpdate wrapper to generate SQL/bind list and then execute update query

func (*Connection) Expr

func (c *Connection) Expr(sql string, args ...interface{}) sq.Sqlizer

Expr wrapper for github.com/Masterminds/squirrel.Expr

func (*Connection) Insert

func (c *Connection) Insert(table string) sq.InsertBuilder

Insert wrapper for github.com/Masterminds/squirrel.Insert

func (*Connection) Ping

func (c *Connection) Ping(ctx context.Context) error

Ping wrapper for ping

func (*Connection) Prepare

func (c *Connection) Prepare(ctx context.Context, query string, name string) (stmt *pgconn.StatementDescription, err error)

Prepare creates a prepared statement from the query

func (*Connection) Query

func (c *Connection) Query(ctx context.Context, query string, args ...interface{}) (rows *Rows, err error)

Query wrapper for sql.Query with automatic txn handling

func (*Connection) QueryCount

func (c *Connection) QueryCount(ctx context.Context, sb sq.SelectBuilder) (count int, err error)

QueryCount gets the count from a select builder query. Would prefer being able to generate the same query with different fields, but that doesn't seem possible with the current library being used. TODO: research alternatives or maybe fork/enhance as needed

func (*Connection) QueryRow

func (c *Connection) QueryRow(ctx context.Context, query string, args ...interface{}) (rows *Row)

QueryRow wrapper for sql.QueryRow with automatic txn handling

func (*Connection) Rollback

func (c *Connection) Rollback(ctx context.Context)

Rollback wrapper for sql.Rollback - no matter what the transaction will be cancelled. So, we will log errors here, but will always assume the txn is rolled back and now unavailable

func (*Connection) RollbackIfInTxn

func (c *Connection) RollbackIfInTxn(ctx context.Context)

RollbackIfInTxn same as Rollback, except if it is in a txn, it will not return an error

func (*Connection) Select

func (c *Connection) Select(columns ...string) sq.SelectBuilder

Select wrapper for github.com/Masterminds/squirrel.Select

func (*Connection) SetStatusLoad

func (db *Connection) SetStatusLoad(f func(*Connection) ([]*Status, error))

SetStatusLoader sets the status loader. This should load all statuses for the application presumably from the db, but could be defined in elsewhere, and return them as an array. The Connection will call this method when a status is first requested and cache the array into a map for access to the statuses per table/column combination

func (*Connection) StatusGetByCode

func (db *Connection) StatusGetByCode(table, column, code string) (s *Status, err error)

StatusGetByCode returns the status record associated with the table, column and code combination

func (*Connection) StatusGetByID

func (db *Connection) StatusGetByID(table, column string, id int) (s *Status, err error)

StatusGetByID returns the status record associated with the table, column and id combination

func (*Connection) StatusGetListByTblAndCol

func (db *Connection) StatusGetListByTblAndCol(table, column string) (sList []*Status, err error)

StatusGetListByTblAndCol returns all status records associated with the table/column

func (*Connection) ToSQLAndQuery

func (c *Connection) ToSQLAndQuery(ctx context.Context, sb sq.SelectBuilder) (rows *Rows, err error)

ToSQLAndQuery converts the select build to a SQL statement and bind parameters, then attempts to execute the query, returning the rows

func (*Connection) ToSQLAndQueryRow

func (c *Connection) ToSQLAndQueryRow(ctx context.Context, sb sq.SelectBuilder) (row *Row, err error)

ToSQLAndQueryRow converts the select builder to a SQL statement and bind parameters, then attempts to execute the query, returning a single row

func (*Connection) ToSQLWFieldAndQuery

func (c *Connection) ToSQLWFieldAndQuery(ctx context.Context, sb sq.SelectBuilder, fields string) (rows *Rows, err error)

ToSQLWFieldAndQuery converts the select builder to a sql, replaces the fields in the statement with the passed fields (this assumes the fields that were used to build the select builder is the const FieldCount) and then attempts to query the statement

func (*Connection) Txn

func (c *Connection) Txn() pgx.Tx

Txn returns the underlying transaction, if currently in one

func (*Connection) Update

func (c *Connection) Update(table string) sq.UpdateBuilder

Update wrapper for github.com/Masterminds/squirrel.Update

type Row

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

Row a wrapper struct for pgx.Row, so error handling can happen

func (*Row) Err

func (r *Row) Err() error

Err wrapper for row's Err func

func (*Row) Scan

func (r *Row) Scan(dest ...interface{}) error

Scan wrapper for row's Scan, which returns an extended error instead

type Rows

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

Rows wrapper struct for sql.Rows, so error handling can happen

func (*Rows) Close

func (r *Rows) Close() error

Close wrapper for row's Close func Leaving return error for compatibility

func (*Rows) Err

func (r *Rows) Err() error

Err wrapper for row's Err func

func (*Rows) Next

func (r *Rows) Next() bool

Next wrapper for row's Next func

func (*Rows) Scan

func (r *Rows) Scan(dest ...interface{}) error

Scan wrapper for row's Scan, which returns an extended error instead

type Slug

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

Slug ...

func NewSlug

func NewSlug(m map[rune]string) (s *Slug)

NewSlug initializes and returns a new slug generator

func (*Slug) SetReplacements

func (s *Slug) SetReplacements(m map[rune]string)

SetReplacements defines the replacement map, if not specified, it uses the default mapping defined in getDefaultMap

func (*Slug) Slugify

func (s *Slug) Slugify(input string) (slug string)

Slugify takes a string input and converts it to a slug, removing special characters, normalizing accented characters and replacing space characters with dashes

type Status

type Status struct {
	ID     int
	Table  string
	Column string
	Code   string
	Name   string
}

Status defines a status reference for a table/column combination. The table/column/id should be unique (as well as the table/column/code).

type Txn

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

Txn wrapper of the *sql.Txn

func (*Txn) Commit

func (t *Txn) Commit(ctx context.Context) (err error)

Commit attempts to commit the txn

func (*Txn) Exec

func (t *Txn) Exec(ctx context.Context, query string, args ...interface{}) (commandTag *pgconn.CommandTag, err error)

Exec executes the query in the txn

func (*Txn) Prepare

func (t *Txn) Prepare(ctx context.Context, query string, name string) (stmt *pgconn.StatementDescription, err error)

Prepare prepares the query in the txn

func (*Txn) Query

func (t *Txn) Query(ctx context.Context, query string, args ...interface{}) (rows *Rows, err error)

Query runs the query in the txn

func (*Txn) QueryRow

func (t *Txn) QueryRow(ctx context.Context, query string, args ...interface{}) (row *Row)

QueryRow runs the query in the txn, returning the single row

func (*Txn) Rollback

func (t *Txn) Rollback(ctx context.Context) (err error)

Rollback attempts to roll back the txn

func (*Txn) RollbackIfInTxn

func (t *Txn) RollbackIfInTxn(ctx context.Context)

RollbackIfInTxn same as Rollback, except if it is in a txn, it will not return an error

func (*Txn) Stmt

func (t *Txn) Stmt(ctx context.Context, stmt string, name string) (*pgconn.StatementDescription, error)

Stmt prepares the statement in the txn

Jump to

Keyboard shortcuts

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