Documentation
¶
Index ¶
- Constants
- func CheckPQError(err error) error
- func GetConnectionStr(cp *ConnParam) (connStr string)
- func IsPQError(err error, errorCode string) bool
- type BulkInsert
- func (bi *BulkInsert) Add(ctx context.Context, values ...interface{}) (rowsInserted int, err error)
- func (bi *BulkInsert) Close() (errList []error)
- func (bi *BulkInsert) Flush(ctx context.Context) (err error)
- func (bi *BulkInsert) GetColumnCount() (colCount int)
- func (bi *BulkInsert) GetCount() (count int)
- func (bi *BulkInsert) GetMaxRowPerInsert() (maxRows uint)
- func (bi *BulkInsert) GetTotal() (total int)
- func (bi *BulkInsert) SetMaxParamPerInsert(i int)
- func (bi *BulkInsert) SetMaxRowPerInsert(maxRows uint) (actualMaxRows uint)
- func (bi *BulkInsert) SetPostInsert(f func(rowCount int) error)
- func (bi *BulkInsert) SetPreInsert(f func() error)
- type BulkUpdate
- func (bu *BulkUpdate) Add(ctx context.Context, values ...interface{}) (rowsUpdated int, err error)
- func (bu *BulkUpdate) Close() (errList []error)
- func (bu *BulkUpdate) Flush(ctx context.Context) (err error)
- func (bu *BulkUpdate) GetAbsoluteMaxParamPerUpdate() (count int)
- func (bu *BulkUpdate) GetCount() (count int)
- func (bu *BulkUpdate) GetParamPerStatement() (count int)
- func (bu *BulkUpdate) GetTotal() (total int)
- func (bu *BulkUpdate) SetMaxParamPerUpdate(i int)
- type BulkUpdateCol
- type ConnParam
- type Connection
- func (c *Connection) Begin(ctx context.Context) (err error)
- func (c *Connection) BeginReturnDB(ctx context.Context) (db *Connection, err error)
- func (c *Connection) BeginUseDefaultTxn(ctx context.Context) (err error)
- func (c *Connection) Close()
- func (c *Connection) Commit(ctx context.Context) (err error)
- func (c *Connection) Delete(from string) sq.DeleteBuilder
- func (c *Connection) Exec(ctx context.Context, query string, args ...interface{}) (res *pgconn.CommandTag, err error)
- func (c *Connection) ExecDelete(ctx context.Context, delB sq.DeleteBuilder) (err error)
- func (c *Connection) ExecInsert(ctx context.Context, ib sq.InsertBuilder) (err error)
- func (c *Connection) ExecInsertReturningID(ctx context.Context, ib sq.InsertBuilder) (id int, err error)
- func (c *Connection) ExecUpdate(ctx context.Context, ub sq.UpdateBuilder) (err error)
- func (c *Connection) Expr(sql string, args ...interface{}) sq.Sqlizer
- func (c *Connection) Insert(table string) sq.InsertBuilder
- func (c *Connection) Ping(ctx context.Context) error
- func (c *Connection) Prepare(ctx context.Context, query string, name string) (stmt *pgconn.StatementDescription, err error)
- func (c *Connection) Query(ctx context.Context, query string, args ...interface{}) (rows *Rows, err error)
- func (c *Connection) QueryCount(ctx context.Context, sb sq.SelectBuilder) (count int, err error)
- func (c *Connection) QueryRow(ctx context.Context, query string, args ...interface{}) (rows *Row)
- func (c *Connection) Rollback(ctx context.Context)
- func (c *Connection) RollbackIfInTxn(ctx context.Context)
- func (c *Connection) Select(columns ...string) sq.SelectBuilder
- func (db *Connection) SetStatusLoad(f func(*Connection) ([]*Status, error))
- func (db *Connection) StatusGetByCode(table, column, code string) (s *Status, err error)
- func (db *Connection) StatusGetByID(table, column string, id int) (s *Status, err error)
- func (db *Connection) StatusGetListByTblAndCol(table, column string) (sList []*Status, err error)
- func (c *Connection) ToSQLAndQuery(ctx context.Context, sb sq.SelectBuilder) (rows *Rows, err error)
- func (c *Connection) ToSQLAndQueryRow(ctx context.Context, sb sq.SelectBuilder) (row *Row, err error)
- func (c *Connection) ToSQLWFieldAndQuery(ctx context.Context, sb sq.SelectBuilder, fields string) (rows *Rows, err error)
- func (c *Connection) Txn() pgx.Tx
- func (c *Connection) Update(table string) sq.UpdateBuilder
- type Row
- type Rows
- type Slug
- type Status
- type Txn
- func (t *Txn) Commit(ctx context.Context) (err error)
- func (t *Txn) Exec(ctx context.Context, query string, args ...interface{}) (commandTag *pgconn.CommandTag, err error)
- func (t *Txn) Prepare(ctx context.Context, query string, name string) (stmt *pgconn.StatementDescription, err error)
- func (t *Txn) Query(ctx context.Context, query string, args ...interface{}) (rows *Rows, err error)
- func (t *Txn) QueryRow(ctx context.Context, query string, args ...interface{}) (row *Row)
- func (t *Txn) Rollback(ctx context.Context) (err error)
- func (t *Txn) RollbackIfInTxn(ctx context.Context)
- func (t *Txn) Stmt(ctx context.Context, stmt string, name string) (*pgconn.StatementDescription, error)
Constants ¶
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" )
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" )
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" )
const ( ECode090201 = e.Code0902 + "01" ECode090202 = e.Code0902 + "02" )
const ( ECode090601 = e.Code0906 + "01" ECode090602 = e.Code0906 + "02" ECode090603 = e.Code0906 + "03" )
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" )
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 )
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" )
Variables ¶
This section is empty.
Functions ¶
func CheckPQError ¶
CheckPQError checks if it's a known postgres error and returns a proper message
func GetConnectionStr ¶
GetConnectionStr returns a connection string
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) 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) 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 ¶
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 ¶
GetConnParamFromJSONConfig get connection params from a JSON config
type Connection ¶
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) 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
type Rows ¶
type Rows struct {
// contains filtered or unexported fields
}
Rows wrapper struct for sql.Rows, so error handling can happen
type Slug ¶
type Slug struct {
// contains filtered or unexported fields
}
Slug ...
func (*Slug) SetReplacements ¶
SetReplacements defines the replacement map, if not specified, it uses the default mapping defined in getDefaultMap
type Status ¶
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) 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) RollbackIfInTxn ¶
RollbackIfInTxn same as Rollback, except if it is in a txn, it will not return an error