sql

package
v0.0.0-...-bf83fb3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
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"
)
View Source
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"
)
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"

	ECode020101 = e.Code0201 + "01"
	ECode020102 = e.Code0201 + "02"
)
View Source
const (
	ECode020201 = e.Code0202 + "01"
	ECode020202 = e.Code0202 + "02"
)
View Source
const (
	ECode020601 = e.Code0206 + "01"
	ECode020602 = e.Code0206 + "02"
	ECode020603 = e.Code0206 + "03"
)
View Source
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"
)
View Source
const (
	// PQErr23505UniqueViolation Postgres code for unique violation
	PQErr23505UniqueViolation = "23505"
	// PQErr58030IOError Postgres code for i/o error ("could not write to temporary file")
	PQErr58030IOError = "58030"
)
View Source
const (
	ECode020801 = e.Code0208 + "01"
	ECode020802 = e.Code0208 + "02"
)
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 (
	ECode020501 = e.Code0205 + "01"
	ECode020502 = e.Code0205 + "02"
	ECode020503 = e.Code0205 + "03"
	ECode020504 = e.Code0205 + "04"
	ECode020505 = e.Code0205 + "05"
	ECode020506 = e.Code0205 + "06"
	ECode020507 = e.Code0205 + "07"
)

Variables

This section is empty.

Functions

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(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

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   *sql.DB
	Slug *Slug
	// contains filtered or unexported fields
}

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

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 - returns extended error instead

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 Statement

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

Statement a prepared statement

func (*Statement) Close

func (s *Statement) Close() (err error)

Close closes the prepared statement

func (*Statement) Exec

func (s *Statement) Exec(params ...interface{}) (res sql.Result, err error)

Exec runs the prepared statement with the passed parameters

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() (err error)

Commit attempts to commit the txn

func (*Txn) Exec

func (t *Txn) Exec(query string, args ...interface{}) (res sql.Result, err error)

Exec executes the query in the txn

func (*Txn) Prepare

func (t *Txn) Prepare(query string) (stmt *sql.Stmt, err error)

Prepare prepares the query in the txn

func (*Txn) Query

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

Query runs the query in the txn

func (*Txn) QueryRow

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

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

func (*Txn) Rollback

func (t *Txn) Rollback() (err error)

Rollback attempts to roll back the 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

func (*Txn) Stmt

func (t *Txn) Stmt(stmt *sql.Stmt) *sql.Stmt

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