Documentation ¶
Overview ¶
Package repo provide a mechanism for accessing data in a single database table
Index ¶
- Constants
- func IsErrNoRows(err error) bool
- type Base
- func (r *Base) AddCustom(name, stmt, alias string) error
- func (r *Base) Count() (int, error)
- func (r *Base) CountByParam(params []Restriction) (int, error)
- func (r *Base) CreateRec(createSQL string, rec interface{}) error
- func (r *Base) DeleteRec(deleteSQL string, recID interface{}) error
- func (r *Base) ErrEmptyParam(param string) *Error
- func (r *Base) ErrOperatorNotSupported(op string) *Error
- func (r *Base) GenerateStatement(statementType string, columns map[string]Columns) (stmt string)
- func (r *Base) GetByAltIDSQL() string
- func (r *Base) GetByIDSQL() string
- func (r *Base) GetByParamSQL() string
- func (r *Base) GetColumnList(name string) []string
- func (r *Base) GetCreateSQL() string
- func (r *Base) GetDeleteSQL() string
- func (r *Base) GetRecByAltID(querySQL string, recID interface{}, rec interface{}, forUpdate bool) error
- func (r *Base) GetRecByID(querySQL string, recID interface{}, rec interface{}, forUpdate bool) error
- func (r *Base) GetRemoveSQL() string
- func (r *Base) GetRowByID(querySQL string, recID interface{}, forUpdate bool) (*sqlx.Row, error)
- func (r *Base) GetRowsByParam(querySQL string, params []Restriction, options *Options) (rows *sqlx.Rows, err error)
- func (r *Base) GetRowsCustom(queryName string, params map[string]interface{}) (*sqlx.Rows, error)
- func (r *Base) GetUpdateSQL() string
- func (r *Base) Init(rec interface{}) error
- func (r *Base) InitTx(tx *sqlx.Tx) error
- func (r *Base) OrderBy() string
- func (r *Base) RemoveRec(removeSQL string, recID interface{}) error
- func (r *Base) SetLockOption(lock string) error
- func (r *Base) UpdateRec(updateSQL string, rec interface{}) error
- type BaseMock
- func (r *BaseMock) CompareParams(params []Restriction, rec interface{}) (bool, error)
- func (r *BaseMock) GetByParamSQL() string
- func (r *BaseMock) GetRowsByParam(querySQL string, params []Restriction, options *Options) (rows *sqlx.Rows, err error)
- func (r *BaseMock) Init() error
- func (r *BaseMock) InitTx(tx *sqlx.Tx) error
- func (r *BaseMock) SetLockOption(lock string) error
- type ColumnFilters
- type Columns
- type Error
- type Options
- type Ordering
- type QueryParams
- type Restriction
Constants ¶
const ( // Operators - OperatorEqual = "=" // default operator for non-array values OperatorIn = "in" // default operator for array values OperatorNotIn = "not in" OperatorBetween = "between" OperatorLike = "like" OperatorIsNull = "is null" OperatorIsNotNull = "is not null" OperatorNotEqualTo = "!=" OperatorLessThanEqualTo = "<=" OperatorLessThan = "<" OperatorGreaterThanEqualTo = ">=" OperatorGreaterThan = ">" OperatorArrayContains = "@>" OperatorArrayContainedIn = "<@" // Clause Options - ClauseOrderByAscending = "order_by_asc" ClauseOrderByDescending = "order_by_desc" ClauseOrderBy = "order_by" ClauseForUpdate = "for_update" ClauseLimit = "limit" ClauseOffset = "offset" // for update lock options LockOptionSkip = "skip" LockOptionNoWait = "nowait" // error details ErrBetweenStringHasTwoVals = "String value for between operator on >%s< should have 2 values separated by a comma" ErrBetweenArrayHasTwoVals = "Array value for between operator on >%s< should have exactly 2 values" ErrBetweenBounds = "Upper bound must not be less than lower bound for between operator" )
package constants for builder
const ( TitleAccess string = "Access Denied" TitleIntegrity string = "Data Integrity Error" TitleInvalid string = "Invalid Request" TitleNoData string = "Data Not Found" TitleData string = "Data Exception" TitleDatabase string = "Database Error" TitleQuery string = "Query Error" TitleRepo string = "Repo Setup Error" TitleSyntax string = "Syntax Error" TitleService string = "Service Not Available" TitleSystem string = "System Error" QueryNoRows string = "Query returned no records" NilRecord string = "Nil record supplied" ErrInvalidColumn string = "Unrecognised column name for table" )
Database errors -
const ( StatementCreate string = "create" StatementUpdate string = "update" StatementDelete string = "delete" StatementRemove string = "remove" StatementSelectByID string = "select by id" StatementSelectByIDs string = "select by ids" StatementSelectByAltID string = "select by alt id" StatementSelectByParam string = "select by param" StatementSelectForUpdate string = "select for update" StatementSelectByAltForUpdate string = "select by alt for update" StatementCount string = "select count" ColumnsCreate string = "create" ColumnsUpdate string = "update" ColumnsReturn string = "return" ForUpdateNowait string = " for no key update nowait" ForUpdateSkip string = " for no key update skip locked" )
package constants
Variables ¶
This section is empty.
Functions ¶
func IsErrNoRows ¶ added in v0.1.5
IsErrNoRows checks if a given error is a database no rows found error
Types ¶
type Base ¶
type Base struct { // Services Logger zerolog.Logger Tx *sqlx.Tx TableName string OrderByFunc func() string LockOption string // bare SQL statements VarGetByIDSQL string VarGetByAltIDSQL string VarGetByParamSQL string VarCreateRecordSQL string VarUpdateRecordSQL string VarDeleteRecordSQL string VarRemoveRecordSQL string // lists of table column names to be used in statments CreateColumns Columns UpdateColumns Columns ReturnColumns Columns ColumnMap map[string]Columns Filters ColumnFilters // columns to exclude from lists LogicalDeleteColumn string // name of column used for logical deletes AltIDColumn string // alternative unique key field name // contains filtered or unexported fields }
Base is the base set of properties for a repository
func (*Base) AddCustom ¶ added in v0.1.8
AddCustom adds a custom query to rebp. As any custom query is intended to return a record
(or array of records) of repo table type, this allows the column list to be specified by '%s' and substituted in from parsed table columns as in standard queries
func (*Base) CountByParam ¶
func (r *Base) CountByParam(params []Restriction) (int, error)
CountByParam returns a count of the number of records in the table that match the given params
func (*Base) DeleteRec ¶
DeleteRec logically deletes a record in the database for the given primary key value
func (*Base) ErrEmptyParam ¶
ErrEmptyParam is the error for a missing repo.Restriction Value when one is expected
func (*Base) ErrOperatorNotSupported ¶
ErrOperatorNotSupported is the error for an unrecognised operator specification in repo.Restriction
func (*Base) GenerateStatement ¶
GenerateStatement generates the standard SQL statements statements requiring non-standard column lists must be explicitly generated by the user
func (*Base) GetByAltIDSQL ¶ added in v0.1.5
GetByAltIDSQL generates the SQL statement for fetching records by unique key
func (*Base) GetByIDSQL ¶ added in v0.1.2
GetByIDSQL generates the SQL statement for fetching records by primary key
func (*Base) GetByParamSQL ¶ added in v0.1.2
GetByParamSQL generates the base SQL statement for fetching records by provided parameters
func (*Base) GetColumnList ¶ added in v0.1.2
GetColumnList - Colunm list getter
func (*Base) GetCreateSQL ¶ added in v0.1.2
GetCreateSQL generates the SQL statement for creating a record
func (*Base) GetDeleteSQL ¶ added in v0.1.2
GetDeleteSQL generates the SQL statement for logically deleting a record
func (*Base) GetRecByAltID ¶ added in v0.1.5
func (r *Base) GetRecByAltID(querySQL string, recID interface{}, rec interface{}, forUpdate bool) error
GetRecByAltID fetchs a table record by the alternate unique key value
func (*Base) GetRecByID ¶
func (r *Base) GetRecByID(querySQL string, recID interface{}, rec interface{}, forUpdate bool) error
GetRecByID fetches a table record by the primary key value
func (*Base) GetRemoveSQL ¶ added in v0.1.2
GetRemoveSQL generates the SQL statement for deleting a record
func (*Base) GetRowByID ¶
GetRowByID fetches a table row by the primary key value
func (*Base) GetRowsByParam ¶
func (r *Base) GetRowsByParam( querySQL string, params []Restriction, options *Options, ) (rows *sqlx.Rows, err error)
GetRowsByParam - fetch set of table rows filtered by the provided parameters and options
func (*Base) GetRowsCustom ¶ added in v0.1.8
GetRowsCustom fetches a set of table rows filtered by the custom query
func (*Base) GetUpdateSQL ¶ added in v0.1.2
GetUpdateSQL generates the SQL statement for updating a record
func (*Base) SetLockOption ¶ added in v0.1.14
SetLockOption allows the default lock option to be set on the repo. This will clear any cached 'for update' statemens. Valid option are "skip", "nowait", "default"
type BaseMock ¶
BaseMock - Base properties for all repo's
func (*BaseMock) CompareParams ¶
func (r *BaseMock) CompareParams(params []Restriction, rec interface{}) (bool, error)
CompareParams iterates over parameters and struct column to find matches, to mimic real query filtering. Not all possible options are necessarily covered, so some restrictions may not give the expected dataset.
func (*BaseMock) GetRowsByParam ¶
func (r *BaseMock) GetRowsByParam(querySQL string, params []Restriction, options *Options) (rows *sqlx.Rows, err error)
GetRowsByParam - satisfy interface requirements
func (*BaseMock) SetLockOption ¶ added in v0.1.14
SetLockOption - satisfy interface requirements
type ColumnFilters ¶ added in v0.1.2
type ColumnFilters struct { Create map[string]*string Update map[string]*string Return map[string]*string }
ColumnFilters specifies columns to exclude from lists and statements
type Columns ¶
type Columns []string
Columns is an array of string representing the column names for a table
func GetColumns ¶
func GetColumns(rec interface{}) Columns
GetColumns extracts column names from a given struct based on `db` tags
func (Columns) ToPrefixedList ¶
ToPrefixedList returns Colunms as a comma separated list in string, with each item prepended with the given prefix
func (Columns) ToUpdateList ¶
ToUpdateList formats Colunms for a SET clause of a UPDATE statement with each column set to a bind variable of the same name
type Error ¶
Error - struct for error details
func NewAccessError ¶
NewAccessError returns a generic error wrapped in standard Error struct
func NewDatabaseError ¶
NewDatabaseError provides standard format for database error details
func NewFromDatabaseError ¶
NewFromDatabaseError converts a pq.Error to a Error
func NewRepoError ¶
NewRepoError provides standard format for non-database errors
type Options ¶ added in v0.1.5
Options specifies options for non-restriction clauses to apply to a query
type QueryParams ¶ added in v0.1.5
type QueryParams = map[string]interface{}
QueryParams is the format in which parameters are provided to the database layer for queries
type Restriction ¶
Restriction specifies a column restriction to apply to a query