Documentation ¶
Overview ¶
Package db is the base package for database access at payshares. It primarily exposes Session which is a lightweight wrapper around a *sqlx.DB that provides utility methods (See the repo tests for examples).
In addition to the query methods, this package provides query logging and stateful transaction management.
In addition to the lower-level access facilities, this package exposes a system to build queries more dynamically using the help of https://github.com/Masterminds/squirrel. These builder method are access through the `Table` type.
Index ¶
- type Conn
- type DeleteBuilder
- type GetBuilder
- func (gb *GetBuilder) Exec() error
- func (gb *GetBuilder) Offset(offset uint64) *GetBuilder
- func (gb *GetBuilder) OrderBy(orderBys ...string) *GetBuilder
- func (gb *GetBuilder) Prefix(sql string, args ...interface{}) *GetBuilder
- func (gb *GetBuilder) Suffix(sql string, args ...interface{}) *GetBuilder
- func (gb *GetBuilder) Where(pred interface{}, args ...interface{}) *GetBuilder
- type InsertBuilder
- type NoRowsError
- type SelectBuilder
- func (sb *SelectBuilder) Exec() error
- func (sb *SelectBuilder) Limit(limit uint64) *SelectBuilder
- func (sb *SelectBuilder) Offset(offset uint64) *SelectBuilder
- func (sb *SelectBuilder) OrderBy(orderBys ...string) *SelectBuilder
- func (sb *SelectBuilder) Prefix(sql string, args ...interface{}) *SelectBuilder
- func (sb *SelectBuilder) Suffix(sql string, args ...interface{}) *SelectBuilder
- func (sb *SelectBuilder) Where(pred interface{}, args ...interface{}) *SelectBuilder
- type Session
- func (s *Session) Begin() error
- func (s *Session) Clone() *Session
- func (s *Session) Commit() error
- func (s *Session) DeleteRange(start, end int64, table string, idCol string) error
- func (s *Session) Dialect() string
- func (s *Session) Exec(query sq.Sqlizer) (sql.Result, error)
- func (s *Session) ExecAll(script string) error
- func (s *Session) ExecRaw(query string, args ...interface{}) (sql.Result, error)
- func (s *Session) Get(dest interface{}, query sq.Sqlizer) error
- func (s *Session) GetRaw(dest interface{}, query string, args ...interface{}) error
- func (s *Session) GetTable(name string) *Table
- func (s *Session) NoRows(err error) bool
- func (s *Session) Query(query sq.Sqlizer) (*sqlx.Rows, error)
- func (s *Session) QueryRaw(query string, args ...interface{}) (*sqlx.Rows, error)
- func (s *Session) ReplacePlaceholders(query string) (string, error)
- func (s *Session) Rollback() error
- func (s *Session) Select(dest interface{}, query sq.Sqlizer) error
- func (s *Session) SelectRaw(dest interface{}, query string, args ...interface{}) error
- type Table
- func (tbl *Table) Delete(pred interface{}, args ...interface{}) *DeleteBuilder
- func (tbl *Table) Get(dest interface{}, pred interface{}, args ...interface{}) *GetBuilder
- func (tbl *Table) Insert(rows ...interface{}) *InsertBuilder
- func (tbl *Table) Select(dest interface{}, pred interface{}, args ...interface{}) *SelectBuilder
- func (tbl *Table) Update(source interface{}, pred interface{}, args ...interface{}) *UpdateBuilder
- type UpdateBuilder
- func (ub *UpdateBuilder) Exec() (sql.Result, error)
- func (ub *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
- func (ub *UpdateBuilder) Offset(offset uint64) *UpdateBuilder
- func (ub *UpdateBuilder) OrderBy(orderBys ...string) *UpdateBuilder
- func (ub *UpdateBuilder) Prefix(sql string, args ...interface{}) *UpdateBuilder
- func (ub *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder
- func (ub *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder
- func (ub *UpdateBuilder) Suffix(sql string, args ...interface{}) *UpdateBuilder
- func (ub *UpdateBuilder) Where(pred interface{}, args ...interface{}) *UpdateBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn interface { Exec(query string, args ...interface{}) (sql.Result, error) Get(dest interface{}, query string, args ...interface{}) error Rebind(sql string) string Queryx(query string, args ...interface{}) (*sqlx.Rows, error) Select(dest interface{}, query string, args ...interface{}) error }
Conn represents a connection to a single database.
type DeleteBuilder ¶
type DeleteBuilder struct { Table *Table // contains filtered or unexported fields }
DeleteBuilder is a helper struct used to construct sql queries of the DELETE variety.
func (*DeleteBuilder) Exec ¶
func (delb *DeleteBuilder) Exec() (sql.Result, error)
Exec executes the query represented by the builder, deleting any rows that match the queries where clauses.
func (*DeleteBuilder) Where ¶
func (delb *DeleteBuilder) Where( pred interface{}, args ...interface{}, ) *DeleteBuilder
Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#DeleteBuilder.Where
type GetBuilder ¶
type GetBuilder struct { Table *Table // contains filtered or unexported fields }
GetBuilder is a helper struct used to construct sql queries of the SELECT variety.
func (*GetBuilder) Exec ¶
func (gb *GetBuilder) Exec() error
Exec executes the query represented by the builder, populating the destination with the results returned by running the query against the current database session.
func (*GetBuilder) Offset ¶
func (gb *GetBuilder) Offset(offset uint64) *GetBuilder
Offset is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Offset
func (*GetBuilder) OrderBy ¶
func (gb *GetBuilder) OrderBy( orderBys ...string, ) *GetBuilder
OrderBy is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.OrderBy
func (*GetBuilder) Prefix ¶
func (gb *GetBuilder) Prefix( sql string, args ...interface{}, ) *GetBuilder
Prefix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Prefix
func (*GetBuilder) Suffix ¶
func (gb *GetBuilder) Suffix( sql string, args ...interface{}, ) *GetBuilder
Suffix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Suffix
func (*GetBuilder) Where ¶
func (gb *GetBuilder) Where( pred interface{}, args ...interface{}, ) *GetBuilder
Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#GetBuilder.Where
type InsertBuilder ¶
type InsertBuilder struct { Table *Table // contains filtered or unexported fields }
InsertBuilder is a helper struct used to construct sql queries of the INSERT variety.
func (*InsertBuilder) Exec ¶
func (ib *InsertBuilder) Exec() (sql.Result, error)
Exec executes the query represented by the builder, inserting each val provided to the builder into the database.
func (*InsertBuilder) Rows ¶
func (ib *InsertBuilder) Rows(rows ...interface{}) *InsertBuilder
Rows appends more rows onto the insert statement
type NoRowsError ¶
type NoRowsError struct { }
NoRowsError is returned when an insert is attempted without providing any values to insert.
func (*NoRowsError) Error ¶
func (err *NoRowsError) Error() string
type SelectBuilder ¶
type SelectBuilder struct { Table *Table // contains filtered or unexported fields }
SelectBuilder is a helper struct used to construct sql queries of the SELECT variety.
func (*SelectBuilder) Exec ¶
func (sb *SelectBuilder) Exec() error
Exec executes the query represented by the builder, populating the destination with the results returned by running the query against the current database session.
func (*SelectBuilder) Limit ¶
func (sb *SelectBuilder) Limit(limit uint64) *SelectBuilder
Limit is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Limit
func (*SelectBuilder) Offset ¶
func (sb *SelectBuilder) Offset(offset uint64) *SelectBuilder
Offset is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Offset
func (*SelectBuilder) OrderBy ¶
func (sb *SelectBuilder) OrderBy( orderBys ...string, ) *SelectBuilder
OrderBy is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.OrderBy
func (*SelectBuilder) Prefix ¶
func (sb *SelectBuilder) Prefix( sql string, args ...interface{}, ) *SelectBuilder
Prefix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Prefix
func (*SelectBuilder) Suffix ¶
func (sb *SelectBuilder) Suffix( sql string, args ...interface{}, ) *SelectBuilder
Suffix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Suffix
func (*SelectBuilder) Where ¶
func (sb *SelectBuilder) Where( pred interface{}, args ...interface{}, ) *SelectBuilder
Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#SelectBuilder.Where
type Session ¶
type Session struct { // DB is the database connection that queries should be executed against. DB *sqlx.DB // Ctx is the optional context in which the repo is operating under. Ctx context.Context // contains filtered or unexported fields }
Session provides helper methods for making queries against `DB` and provides utilities such as automatic query logging and transaction management. NOTE: A Session is designed to be lightweight and temporarily lived (usually request scoped) which is one reason it is acceptable for it to store a context. It is not presently intended to cross goroutine boundaries and is not concurrency safe.
func Wrap ¶
Wrap wraps a bare *sql.DB (from the database/sql stdlib package) in a *db.Session instance. It is meant to be used in cases where you do not control the instantiation of the database connection, but would still like to leverage the facilities provided in Session.
func (*Session) Clone ¶
Clone clones the receiver, returning a new instance backed by the same context and db. The result will not be bound to any transaction that the source is currently within.
func (*Session) DeleteRange ¶
DeleteRange deletes a range of rows from a sql table between `start` and `end` (exclusive).
func (*Session) ExecAll ¶
ExecAll runs all sql commands in `script` against `r` within a single transaction.
func (*Session) GetRaw ¶
GetRaw runs `query` with `args`, setting the first result found on `dest`, if any.
func (*Session) NoRows ¶
NoRows returns true if the provided error resulted from a query that found no results.
func (*Session) ReplacePlaceholders ¶
ReplacePlaceholders replaces the '?' parameter placeholders in the provided sql query with a sql dialect appropriate version. Use '??' to escape a placeholder.
type Table ¶
Table helps to build sql queries against a given table. It logically represents a SQL table on the database that `Session` is connected to.
func (*Table) Delete ¶
func (tbl *Table) Delete( pred interface{}, args ...interface{}, ) *DeleteBuilder
Delete returns a new query builder configured to delete rows from the table.
func (*Table) Get ¶
func (tbl *Table) Get( dest interface{}, pred interface{}, args ...interface{}, ) *GetBuilder
Get returns a new query builder configured to select into the provided `dest`.
Get behaves the same was as Select, but automatically limits the query generated to a single value and only populates a single struct.
func (*Table) Insert ¶
func (tbl *Table) Insert(rows ...interface{}) *InsertBuilder
Insert returns a new query builder configured to insert structs into the table.
Insert takes one or more struct (or pointer to struct) values, each of which represents a single row to be created in the table. The first value provided in a call to this function will operate as the template for the insert and will determine what columns are populated in the query. For this reason, it is highly recommmended that you always use the same struct type for any single call this function.
An InsertBuilder uses the "db" struct tag to determine the column names that a given struct should be mapped to, and by default the unmofdified name of the field will be used. Similar to other struct tags, the value "-" will cause the field to be skipped.
NOTE: using the omitempty option, such as used with json struct tags, is not supported.
func (*Table) Select ¶
func (tbl *Table) Select( dest interface{}, pred interface{}, args ...interface{}, ) *SelectBuilder
Select returns a new query builder configured to select into the provided `dest`.
func (*Table) Update ¶
func (tbl *Table) Update( source interface{}, pred interface{}, args ...interface{}, ) *UpdateBuilder
Update returns a new query builder configured to update rows that match the predicate with the values of the provided source struct. See docs for `UpdateBuildeExec` for more documentation.
type UpdateBuilder ¶
type UpdateBuilder struct { Table *Table // contains filtered or unexported fields }
UpdateBuilder is a helper struct used to construct sql queries of the UPDATE variety.
func (*UpdateBuilder) Exec ¶
func (ub *UpdateBuilder) Exec() (sql.Result, error)
Exec executes the query that has been previously configured on the UpdateBuilder.
func (*UpdateBuilder) Limit ¶
func (ub *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
Limit is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Limit
func (*UpdateBuilder) Offset ¶
func (ub *UpdateBuilder) Offset(offset uint64) *UpdateBuilder
Offset is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Offset
func (*UpdateBuilder) OrderBy ¶
func (ub *UpdateBuilder) OrderBy( orderBys ...string, ) *UpdateBuilder
OrderBy is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.OrderBy
func (*UpdateBuilder) Prefix ¶
func (ub *UpdateBuilder) Prefix( sql string, args ...interface{}, ) *UpdateBuilder
Prefix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Prefix
func (*UpdateBuilder) Set ¶
func (ub *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder
Set is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Suffix
func (*UpdateBuilder) SetMap ¶
func (ub *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder
SetMap is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Suffix
func (*UpdateBuilder) Suffix ¶
func (ub *UpdateBuilder) Suffix( sql string, args ...interface{}, ) *UpdateBuilder
Suffix is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Suffix
func (*UpdateBuilder) Where ¶
func (ub *UpdateBuilder) Where( pred interface{}, args ...interface{}, ) *UpdateBuilder
Where is a passthrough call to the squirrel. See https://godoc.org/github.com/Masterminds/squirrel#UpdateBuilder.Where
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package dbtest is a package to ease the pain of developing test code that works against external databases.
|
Package dbtest is a package to ease the pain of developing test code that works against external databases. |
Package sqlutils contains utility functions for manipulating strings of SQL
|
Package sqlutils contains utility functions for manipulating strings of SQL |