Documentation ¶
Overview ¶
Package db is the base package for database access at paydex. 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 ¶
- Variables
- type BatchInsertBuilder
- 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 MockSession
- func (m *MockSession) Begin() error
- func (m *MockSession) Clone() *Session
- func (m *MockSession) Close() error
- func (m *MockSession) Exec(query squirrel.Sqlizer) (sql.Result, error)
- func (m *MockSession) ExecRaw(query string, args ...interface{}) (sql.Result, error)
- func (m *MockSession) Get(dest interface{}, query sq.Sqlizer) error
- func (m *MockSession) GetRaw(dest interface{}, query string, args ...interface{}) error
- func (m *MockSession) GetTable(name string) *Table
- func (m *MockSession) NoRows(err error) bool
- func (m *MockSession) Rollback() error
- func (m *MockSession) Select(dest interface{}, query squirrel.Sqlizer) error
- func (m *MockSession) SelectRaw(dest interface{}, query string, args ...interface{}) error
- func (m *MockSession) TruncateTables(tables []string) error
- 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) BeginTx(opts *sql.TxOptions) error
- func (s *Session) Clone() *Session
- func (s *Session) Close() error
- 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) GetTx() *sqlx.Tx
- 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
- func (s *Session) TruncateTables(tables []string) error
- type SessionInterface
- 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) SetStruct(s interface{}, ignored []string) *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 ¶
var ( // ErrCancelled is an error returned by Session methods when request has // been cancelled (ex. context cancelled). ErrCancelled = errors.New("canceling statement due to user request") )
Functions ¶
This section is empty.
Types ¶
type BatchInsertBuilder ¶
type BatchInsertBuilder struct { Table *Table // MaxBatchSize defines the maximum size of a batch. If this number is // reached after calling Row() it will call Exec() immediately inserting // all rows to a DB. // Zero (default) will not add rows until explicitly calling Exec. MaxBatchSize int // contains filtered or unexported fields }
BatchInsertBuilder works like sq.InsertBuilder but has a better support for batching large number of rows. It is NOT safe for concurrent use.
func (*BatchInsertBuilder) Exec ¶
func (b *BatchInsertBuilder) Exec() error
Exec inserts rows in batches. In case of errors it's possible that some batches were added so this should be run in a DB transaction for easy rollbacks.
func (*BatchInsertBuilder) Row ¶
func (b *BatchInsertBuilder) Row(row map[string]interface{}) error
Row adds a new row to the batch. All rows must have exactly the same columns (map keys). Otherwise, error will be returned. Please note that rows are not added one by one but in batches when `Exec` is called (or `MaxBatchSize` is reached).
type Conn ¶
type Conn interface { ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error Rebind(sql string) string QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) SelectContext(ctx context.Context, 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. NOTE: InsertBuilder will use "zero" value of a type in case of nil pointer values. If you need to insert `NULL` use sql.Null* or build your own type that implements database/sql/driver.Valuer.
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) IgnoreCols ¶
func (ib *InsertBuilder) IgnoreCols(cols ...string) *InsertBuilder
IgnoreCols adds colums to ignore list (will not be inserted)
func (*InsertBuilder) Rows ¶
func (ib *InsertBuilder) Rows(rows ...interface{}) *InsertBuilder
Rows appends more rows onto the insert statement
type MockSession ¶
func (*MockSession) Begin ¶
func (m *MockSession) Begin() error
func (*MockSession) Clone ¶
func (m *MockSession) Clone() *Session
func (*MockSession) Close ¶
func (m *MockSession) Close() error
func (*MockSession) ExecRaw ¶
func (m *MockSession) ExecRaw(query string, args ...interface{}) (sql.Result, error)
func (*MockSession) GetRaw ¶
func (m *MockSession) GetRaw(dest interface{}, query string, args ...interface{}) error
func (*MockSession) GetTable ¶
func (m *MockSession) GetTable(name string) *Table
func (*MockSession) NoRows ¶
func (m *MockSession) NoRows(err error) bool
func (*MockSession) Rollback ¶
func (m *MockSession) Rollback() error
func (*MockSession) Select ¶
func (m *MockSession) Select(dest interface{}, query squirrel.Sqlizer) error
func (*MockSession) SelectRaw ¶
func (m *MockSession) SelectRaw( dest interface{}, query string, args ...interface{}, ) error
func (*MockSession) TruncateTables ¶
func (m *MockSession) TruncateTables(tables []string) error
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 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) BeginTx ¶
BeginTx binds this session to a new transaction which is configured with the given transaction options
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) Close ¶
Close delegates to the underlying database Close method, closing the database and releasing any resources. It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.
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.
func (*Session) SelectRaw ¶
SelectRaw runs `query` with `args`, setting the results found on `dest`.
func (*Session) TruncateTables ¶
type SessionInterface ¶
type SessionInterface interface { Begin() error Rollback() error TruncateTables(tables []string) error Clone() *Session Close() error Get(dest interface{}, query squirrel.Sqlizer) error GetRaw(dest interface{}, query string, args ...interface{}) error Select(dest interface{}, query squirrel.Sqlizer) error SelectRaw(dest interface{}, query string, args ...interface{}) error GetTable(name string) *Table Exec(query squirrel.Sqlizer) (sql.Result, error) ExecRaw(query string, args ...interface{}) (sql.Result, error) NoRows(err error) bool }
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.Set
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.SetMap
func (*UpdateBuilder) SetStruct ¶
func (ub *UpdateBuilder) SetStruct(s interface{}, ignored []string) *UpdateBuilder
SetStruct is using `db` tag on the struct and updates the query with struct values for each field (except `ignored` fields).
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 |