Documentation ¶
Index ¶
- Constants
- func GetConnectionStr(cp *ConnParam) (connStr string)
- func IsPQError(err error, errorCode string) bool
- type BulkInsert
- func (bi *BulkInsert) Add(values ...interface{}) (rowsInserted int, err error)
- func (bi *BulkInsert) Close() (errList []error)
- func (bi *BulkInsert) DisableCache() (errList []error)
- func (bi *BulkInsert) EnableCache()
- func (bi *BulkInsert) Flush() (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(values ...interface{}) (rowsUpdated int, err error)
- func (bu *BulkUpdate) Close() (errList []error)
- func (bu *BulkUpdate) DisableCache() (errList []error)
- func (bu *BulkUpdate) EnableCache()
- func (bu *BulkUpdate) Flush() (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() (err error)
- func (c *Connection) BeginReturnDB() (db *Connection, err error)
- func (c *Connection) BeginUseDefaultTxn() (err error)
- func (c *Connection) Commit() (err error)
- func (c *Connection) Delete(from string) sq.DeleteBuilder
- func (c *Connection) Exec(query string, args ...interface{}) (res sql.Result, err error)
- func (c *Connection) ExecDelete(delB sq.DeleteBuilder) (err error)
- func (c *Connection) ExecInsert(ib sq.InsertBuilder) (err error)
- func (c *Connection) ExecInsertReturningID(ib sq.InsertBuilder) (id int, err error)
- func (c *Connection) ExecUpdate(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) Prepare(query string) (stmt *sql.Stmt, err error)
- func (c *Connection) Query(query string, args ...interface{}) (rows *Rows, err error)
- func (c *Connection) QueryCount(sb sq.SelectBuilder) (count int, err error)
- func (c *Connection) QueryRow(query string, args ...interface{}) (rows *Row)
- func (c *Connection) Rollback()
- func (c *Connection) RollbackIfInTxn()
- 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(sb sq.SelectBuilder) (rows *Rows, err error)
- func (c *Connection) ToSQLAndQueryRow(sb sq.SelectBuilder) (row *Row, err error)
- func (c *Connection) ToSQLWFieldAndQuery(sb sq.SelectBuilder, fields string) (rows *Rows, err error)
- func (c *Connection) Txn() *sql.Tx
- func (c *Connection) Update(table string) sq.UpdateBuilder
- type Row
- type Rows
- type Slug
- type Statement
- type Status
- type Txn
- func (t *Txn) Commit() (err error)
- func (t *Txn) Exec(query string, args ...interface{}) (res sql.Result, err error)
- func (t *Txn) Prepare(query string) (stmt *sql.Stmt, err error)
- func (t *Txn) Query(query string, args ...interface{}) (rows *Rows, err error)
- func (t *Txn) QueryRow(query string, args ...interface{}) (row *Row)
- func (t *Txn) Rollback() (err error)
- func (t *Txn) RollbackIfInTxn()
- func (t *Txn) Stmt(stmt *sql.Stmt) *sql.Stmt
Constants ¶
const ( DefaultMaxParamPerInsert = 15000 AbsoluteMaxParamPerInsert = 65535 ECode020701 = e.Code0207 + "01" ECode020702 = e.Code0207 + "02" ECode020703 = e.Code0207 + "03" ECode020704 = e.Code0207 + "04" ECode020705 = e.Code0207 + "05" ECode020706 = e.Code0207 + "06" ECode020707 = e.Code0207 + "07" ECode020708 = e.Code0207 + "08" ECode020709 = e.Code0207 + "09" ECode02070A = e.Code0207 + "0A" ECode02070B = e.Code0207 + "0B" )
const ( DefaultMaxParamPerUpdate = 15000 AbsoluteMaxParamPerUpdate = 65535 ECode020901 = e.Code0209 + "01" ECode020902 = e.Code0209 + "02" ECode020903 = e.Code0209 + "03" ECode020904 = e.Code0209 + "04" ECode020905 = e.Code0209 + "05" ECode020906 = e.Code0209 + "06" ECode020907 = e.Code0209 + "07" ECode020908 = e.Code0209 + "08" ECode020909 = e.Code0209 + "09" ECode02090A = e.Code0209 + "0A" )
const ( // FieldPlaceHolder TODO: move to more generic location FieldPlaceHolder = "<FIELD_PLACE_HOLDER>" // FieldCount TODO: move to more generic location FieldCount = "count(*) AS cnt" ECode020101 = e.Code0201 + "01" ECode020102 = e.Code0201 + "02" )
const ( ECode020201 = e.Code0202 + "01" ECode020202 = e.Code0202 + "02" )
const ( ECode020601 = e.Code0206 + "01" ECode020602 = e.Code0206 + "02" ECode020603 = e.Code0206 + "03" )
const ( ECode020301 = e.Code0203 + "01" ECode020302 = e.Code0203 + "02" ECode020303 = e.Code0203 + "03" ECode020304 = e.Code0203 + "04" ECode020305 = e.Code0203 + "05" ECode020306 = e.Code0203 + "06" ECode020307 = e.Code0203 + "07" ECode020308 = e.Code0203 + "08" ECode020309 = e.Code0203 + "09" ECode02030A = e.Code0203 + "0A" ECode02030B = e.Code0203 + "0B" ECode02030C = e.Code0203 + "0C" ECode02030D = e.Code0203 + "0D" ECode02030E = e.Code0203 + "0E" ECode02030F = e.Code0203 + "0F" ECode02030G = e.Code0203 + "0G" ECode02030H = e.Code0203 + "0H" ECode02030I = e.Code0203 + "0I" ECode02030J = e.Code0203 + "0J" ECode02030K = e.Code0203 + "0K" ECode02030L = e.Code0203 + "0L" ECode02030M = e.Code0203 + "0M" ECode02030N = e.Code0203 + "0N" ECode02030O = e.Code0203 + "0O" ECode02030P = e.Code0203 + "0P" ECode02030Q = e.Code0203 + "0Q" ECode02030R = e.Code0203 + "0R" ECode02030S = e.Code0203 + "0S" ECode02030T = e.Code0203 + "0T" ECode02030U = e.Code0203 + "0U" ECode02030V = e.Code0203 + "0V" )
const ( // PQErr23505UniqueViolation Postgres code for unique violation PQErr23505UniqueViolation = "23505" // PQErr58030IOError Postgres code for i/o error ("could not write to temporary file") PQErr58030IOError = "58030" )
const ( ECode020801 = e.Code0208 + "01" ECode020802 = e.Code0208 + "02" )
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 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(db *Connection, table, columns, suffix 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(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 if cache is enabled, then it closes all cached statements
func (*BulkInsert) DisableCache ¶
func (bi *BulkInsert) DisableCache() (errList []error)
DisableCache disables the cache and closes any open statements
func (*BulkInsert) EnableCache ¶
func (bi *BulkInsert) EnableCache()
EnableCache enables caching of bulk insert statements. If used, Close must be called when finished to properly clean up the sql connections
func (*BulkInsert) Flush ¶
func (bi *BulkInsert) Flush() (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(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(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 if cache is enabled, then it closes all cached statements
func (*BulkUpdate) DisableCache ¶
func (bu *BulkUpdate) DisableCache() (errList []error)
DisableCache disables the cache and closes any open statements
func (*BulkUpdate) EnableCache ¶
func (bu *BulkUpdate) EnableCache()
EnableCache enables caching of bulk update statements. If used, Close must be called when finished to properly clean up the sql connections
func (*BulkUpdate) Flush ¶
func (bu *BulkUpdate) Flush() (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 *sql.DB If a transaction is started, it is stored internally in the txn and automatically used when making DB calls until commit/rollback is executed. If during a txn, a call outside of the txn is needed, the DB property can be accessed directly and used to make a query/exec/select call.
func NewPostgresConn ¶
func NewPostgresConn(cp *ConnParam) (conn *Connection, err error)
NewPostgresConn initializes a new Postgres connection FIXME: use a pool?
func (*Connection) Begin ¶
func (c *Connection) Begin() (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() (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() (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() (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(query string, args ...interface{}) (res sql.Result, err error)
Exec wrapper for sql.Exec with automatic txn handling
func (*Connection) ExecDelete ¶
func (c *Connection) ExecDelete(delB sq.DeleteBuilder) (err error)
ExecDelete wrapper to generate SQL/bind list and then execute delete query
func (*Connection) ExecInsert ¶
func (c *Connection) ExecInsert(ib sq.InsertBuilder) (err error)
ExecInsert wrapper to generate SQL/bind list and then execute insert query
func (*Connection) ExecInsertReturningID ¶
func (c *Connection) ExecInsertReturningID(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(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) Prepare ¶
func (c *Connection) Prepare(query string) (stmt *sql.Stmt, err error)
Prepare creates a prepared statement from the query
func (*Connection) Query ¶
func (c *Connection) Query(query string, args ...interface{}) (rows *Rows, err error)
Query wrapper for sql.Query with automatic txn handling
func (*Connection) QueryCount ¶
func (c *Connection) QueryCount(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(query string, args ...interface{}) (rows *Row)
QueryRow wrapper for sql.QueryRow with automatic txn handling
func (*Connection) Rollback ¶
func (c *Connection) Rollback()
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()
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(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(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(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() *sql.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 sql.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 Statement ¶
type Statement struct {
// contains filtered or unexported fields
}
Statement a prepared statement
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) RollbackIfInTxn ¶
func (t *Txn) RollbackIfInTxn()
RollbackIfInTxn same as Rollback, except if it is in a txn, it will not return an error