Documentation ¶
Index ¶
- Variables
- func Expr(sql string, values ...interface{}) *expr
- func Interpolate(sql string, vals []interface{}) (string, error)
- type Connection
- type DeleteBuilder
- func (b *DeleteBuilder) Exec() (sql.Result, error)
- func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder
- func (b *DeleteBuilder) Offset(offset uint64) *DeleteBuilder
- func (b *DeleteBuilder) OrderBy(ord string) *DeleteBuilder
- func (b *DeleteBuilder) OrderDir(ord string, isAsc bool) *DeleteBuilder
- func (b *DeleteBuilder) ToSql() (string, []interface{})
- func (b *DeleteBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *DeleteBuilder
- type Eq
- type EventReceiver
- type InsertBuilder
- func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder
- func (b *InsertBuilder) Exec() (sql.Result, error)
- func (b *InsertBuilder) Pair(column string, value interface{}) *InsertBuilder
- func (b *InsertBuilder) Record(record interface{}) *InsertBuilder
- func (b *InsertBuilder) ToSql() (string, []interface{})
- func (b *InsertBuilder) Values(vals ...interface{}) *InsertBuilder
- type MysqlQuoter
- type NullBool
- type NullEventReceiver
- func (n *NullEventReceiver) Event(eventName string)
- func (n *NullEventReceiver) EventErr(eventName string, err error) error
- func (n *NullEventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error
- func (n *NullEventReceiver) EventKv(eventName string, kvs map[string]string)
- func (n *NullEventReceiver) Timing(eventName string, nanoseconds int64)
- func (n *NullEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string)
- type NullFloat64
- type NullInt64
- type NullString
- type NullTime
- type PostgresQuoter
- type SelectBuilder
- func (b *SelectBuilder) Distinct() *SelectBuilder
- func (b *SelectBuilder) From(from string) *SelectBuilder
- func (b *SelectBuilder) GroupBy(group string) *SelectBuilder
- func (b *SelectBuilder) Having(whereSqlOrMap interface{}, args ...interface{}) *SelectBuilder
- func (b *SelectBuilder) Limit(limit uint64) *SelectBuilder
- func (b *SelectBuilder) LoadStruct(dest interface{}) error
- func (b *SelectBuilder) LoadStructs(dest interface{}) (int, error)
- func (b *SelectBuilder) LoadValue(dest interface{}) error
- func (b *SelectBuilder) LoadValues(dest interface{}) (int, error)
- func (b *SelectBuilder) Offset(offset uint64) *SelectBuilder
- func (b *SelectBuilder) OrderBy(ord string) *SelectBuilder
- func (b *SelectBuilder) OrderDir(ord string, isAsc bool) *SelectBuilder
- func (b *SelectBuilder) Paginate(page, perPage uint64) *SelectBuilder
- func (b *SelectBuilder) ReturnInt64() (int64, error)
- func (b *SelectBuilder) ReturnInt64s() ([]int64, error)
- func (b *SelectBuilder) ReturnString() (string, error)
- func (b *SelectBuilder) ReturnStrings() ([]string, error)
- func (b *SelectBuilder) ReturnUint64() (uint64, error)
- func (b *SelectBuilder) ReturnUint64s() ([]uint64, error)
- func (b *SelectBuilder) ToSql() (string, []interface{})
- func (b *SelectBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *SelectBuilder
- type Session
- func (sess *Session) Begin() (*Tx, error)
- func (sess *Session) DeleteFrom(from string) *DeleteBuilder
- func (sess *Session) InsertInto(into string) *InsertBuilder
- func (sess *Session) Select(cols ...string) *SelectBuilder
- func (sess *Session) SelectBySql(sql string, args ...interface{}) *SelectBuilder
- func (sess *Session) Update(table string) *UpdateBuilder
- func (sess *Session) UpdateBySql(sql string, args ...interface{}) *UpdateBuilder
- type SessionRunner
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) DeleteFrom(from string) *DeleteBuilder
- func (tx *Tx) InsertInto(into string) *InsertBuilder
- func (tx *Tx) Rollback() error
- func (tx *Tx) RollbackUnlessCommitted()
- func (tx *Tx) Select(cols ...string) *SelectBuilder
- func (tx *Tx) SelectBySql(sql string, args ...interface{}) *SelectBuilder
- func (tx *Tx) Update(table string) *UpdateBuilder
- func (tx *Tx) UpdateBySql(sql string, args ...interface{}) *UpdateBuilder
- type UpdateBuilder
- func (b *UpdateBuilder) Exec() (sql.Result, error)
- func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
- func (b *UpdateBuilder) Offset(offset uint64) *UpdateBuilder
- func (b *UpdateBuilder) OrderBy(ord string) *UpdateBuilder
- func (b *UpdateBuilder) OrderDir(ord string, isAsc bool) *UpdateBuilder
- func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder
- func (b *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder
- func (b *UpdateBuilder) ToSql() (string, []interface{})
- func (b *UpdateBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *UpdateBuilder
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrNotUTF8 = errors.New("invalid UTF-8") ErrInvalidSliceLength = errors.New("length of slice is 0. length must be >= 1") ErrInvalidSliceValue = errors.New("trying to interpolate invalid slice value into query") ErrInvalidValue = errors.New("trying to interpolate invalid value into query") ErrArgumentMismatch = errors.New("mismatch between ? (placeholders) and arguments") )
var NameMapping = camelCaseToSnakeCase
NameMapping is the routine to use when mapping column names to struct properties
var Now = nowSentinel{}
Now is a value that serializes to the curren time
var Quoter quoter = MysqlQuoter{}
Quoter is the quoter to use for quoting text; use Mysql quoting by default
Functions ¶
func Expr ¶
func Expr(sql string, values ...interface{}) *expr
Expr is a SQL fragment with placeholders, and a slice of args to replace them with
func Interpolate ¶
Interpolate takes a SQL string with placeholders and a list of arguments to replace them with. Returns a blank string and error if the number of placeholders does not match the number of arguments.
Types ¶
type Connection ¶
type Connection struct { Db *sql.DB EventReceiver }
Connection is a connection to the database with an EventReceiver to send events, errors, and timings to
func NewConnection ¶
func NewConnection(db *sql.DB, log EventReceiver) *Connection
NewConnection instantiates a Connection for a given database/sql connection and event receiver
func (*Connection) NewSession ¶
func (cxn *Connection) NewSession(log EventReceiver) *Session
NewSession instantiates a Session for the Connection
type DeleteBuilder ¶
type DeleteBuilder struct { *Session From string WhereFragments []*whereFragment OrderBys []string LimitCount uint64 LimitValid bool OffsetCount uint64 OffsetValid bool // contains filtered or unexported fields }
DeleteBuilder contains the clauses for a DELETE statement
func (*DeleteBuilder) Exec ¶
func (b *DeleteBuilder) Exec() (sql.Result, error)
Exec executes the statement represented by the DeleteBuilder It returns the raw database/sql Result and an error if there was one
func (*DeleteBuilder) Limit ¶
func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder
Limit sets a LIMIT clause for the statement; overrides any existing LIMIT
func (*DeleteBuilder) Offset ¶
func (b *DeleteBuilder) Offset(offset uint64) *DeleteBuilder
Offset sets an OFFSET clause for the statement; overrides any existing OFFSET
func (*DeleteBuilder) OrderBy ¶
func (b *DeleteBuilder) OrderBy(ord string) *DeleteBuilder
OrderBy appends an ORDER BY clause to the statement
func (*DeleteBuilder) OrderDir ¶
func (b *DeleteBuilder) OrderDir(ord string, isAsc bool) *DeleteBuilder
OrderDir appends an ORDER BY clause with a direction to the statement
func (*DeleteBuilder) ToSql ¶
func (b *DeleteBuilder) ToSql() (string, []interface{})
ToSql serialized the DeleteBuilder to a SQL string It returns the string with placeholders and a slice of query arguments
func (*DeleteBuilder) Where ¶
func (b *DeleteBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *DeleteBuilder
Where appends a WHERE clause to the statement whereSqlOrMap can be a string or map. If it's a string, args wil replaces any places holders
type Eq ¶
type Eq map[string]interface{}
Eq is a map column -> value pairs which must be matched in a query
type EventReceiver ¶
type EventReceiver interface { Event(eventName string) EventKv(eventName string, kvs map[string]string) EventErr(eventName string, err error) error EventErrKv(eventName string, err error, kvs map[string]string) error Timing(eventName string, nanoseconds int64) TimingKv(eventName string, nanoseconds int64, kvs map[string]string) }
EventReceiver gets events from dbr methods for logging purposes
type InsertBuilder ¶
type InsertBuilder struct { *Session Into string Cols []string Vals [][]interface{} Recs []interface{} // contains filtered or unexported fields }
InsertBuilder contains the clauses for an INSERT statement
func (*InsertBuilder) Columns ¶
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder
Columns appends columns to insert in the statement
func (*InsertBuilder) Exec ¶
func (b *InsertBuilder) Exec() (sql.Result, error)
Exec executes the statement represented by the InsertBuilder It returns the raw database/sql Result and an error if there was one
func (*InsertBuilder) Pair ¶
func (b *InsertBuilder) Pair(column string, value interface{}) *InsertBuilder
Pair adds a key/value pair to the statement
func (*InsertBuilder) Record ¶
func (b *InsertBuilder) Record(record interface{}) *InsertBuilder
Record pulls in values to match Columns from the record
func (*InsertBuilder) ToSql ¶
func (b *InsertBuilder) ToSql() (string, []interface{})
ToSql serialized the InsertBuilder to a SQL string It returns the string with placeholders and a slice of query arguments
func (*InsertBuilder) Values ¶
func (b *InsertBuilder) Values(vals ...interface{}) *InsertBuilder
Values appends a set of values to the statement
type NullBool ¶
NullBool is a type that can be null or a bool
func (*NullBool) MarshalJSON ¶
MarshalJSON correctly serializes a NullBool to JSON
func (*NullBool) UnmarshalJSON ¶
UnmarshalJSON correctly deserializes a NullBool from JSON
type NullEventReceiver ¶
type NullEventReceiver struct{}
NullEventReceiver is a sentinel EventReceiver; use it if the caller doesn't supply one
func (*NullEventReceiver) Event ¶
func (n *NullEventReceiver) Event(eventName string)
Event receives a simple notification when various events occur
func (*NullEventReceiver) EventErr ¶
func (n *NullEventReceiver) EventErr(eventName string, err error) error
EventErr receives a notification of an error if one occurs
func (*NullEventReceiver) EventErrKv ¶
EventErrKv receives a notification of an error if one occurs along with optional key/value data
func (*NullEventReceiver) EventKv ¶
func (n *NullEventReceiver) EventKv(eventName string, kvs map[string]string)
EventKv receives a notification when various events occur along with optional key/value data
func (*NullEventReceiver) Timing ¶
func (n *NullEventReceiver) Timing(eventName string, nanoseconds int64)
Timing receives the time an event took to happen
type NullFloat64 ¶
type NullFloat64 struct {
sql.NullFloat64
}
NullFloat64 is a type that can be null or a float64
func (*NullFloat64) MarshalJSON ¶
func (n *NullFloat64) MarshalJSON() ([]byte, error)
MarshalJSON correctly serializes a NullFloat64 to JSON
func (*NullFloat64) UnmarshalJSON ¶
func (n *NullFloat64) UnmarshalJSON(b []byte) error
UnmarshalJSON correctly deserializes a NullFloat64 from JSON
type NullInt64 ¶
NullInt64 is a type that can be null or an int
func (*NullInt64) MarshalJSON ¶
MarshalJSON correctly serializes a NullInt64 to JSON
func (*NullInt64) UnmarshalJSON ¶
UnmarshalJSON correctly deserializes a NullInt64 from JSON
type NullString ¶
type NullString struct {
sql.NullString
}
NullString is a type that can be null or a string
func (*NullString) MarshalJSON ¶
func (n *NullString) MarshalJSON() ([]byte, error)
MarshalJSON correctly serializes a NullString to JSON
func (*NullString) UnmarshalJSON ¶
func (n *NullString) UnmarshalJSON(b []byte) error
UnmarshalJSON correctly deserializes a NullString from JSON
type NullTime ¶
NullTime is a type that can be null or a time
func (*NullTime) MarshalJSON ¶
MarshalJSON correctly serializes a NullTime to JSON
func (*NullTime) UnmarshalJSON ¶
UnmarshalJSON correctly deserializes a NullTime from JSON
type PostgresQuoter ¶
type PostgresQuoter struct{}
PostgresQuoter implements PostgreSQL-specific quoting
type SelectBuilder ¶
type SelectBuilder struct { *Session RawFullSql string RawArguments []interface{} IsDistinct bool Columns []string FromTable string WhereFragments []*whereFragment GroupBys []string HavingFragments []*whereFragment OrderBys []string LimitCount uint64 LimitValid bool OffsetCount uint64 OffsetValid bool // contains filtered or unexported fields }
SelectBuilder contains the clauses for a SELECT statement
func (*SelectBuilder) Distinct ¶
func (b *SelectBuilder) Distinct() *SelectBuilder
Distinct marks the statement as a DISTINCT SELECT
func (*SelectBuilder) From ¶
func (b *SelectBuilder) From(from string) *SelectBuilder
From sets the table to SELECT FROM
func (*SelectBuilder) GroupBy ¶
func (b *SelectBuilder) GroupBy(group string) *SelectBuilder
GroupBy appends a column to group the statement
func (*SelectBuilder) Having ¶
func (b *SelectBuilder) Having(whereSqlOrMap interface{}, args ...interface{}) *SelectBuilder
Having appends a HAVING clause to the statement
func (*SelectBuilder) Limit ¶
func (b *SelectBuilder) Limit(limit uint64) *SelectBuilder
Limit sets a limit for the statement; overrides any existing LIMIT
func (*SelectBuilder) LoadStruct ¶
func (b *SelectBuilder) LoadStruct(dest interface{}) error
LoadStruct executes the SelectBuilder and loads the resulting data into a struct dest must be a pointer to a struct Returns ErrNotFound if nothing was found
func (*SelectBuilder) LoadStructs ¶
func (b *SelectBuilder) LoadStructs(dest interface{}) (int, error)
LoadStructs executes the SelectBuilder and loads the resulting data into a slice of structs dest must be a pointer to a slice of pointers to structs Returns the number of items found (which is not necessarily the # of items set)
func (*SelectBuilder) LoadValue ¶
func (b *SelectBuilder) LoadValue(dest interface{}) error
LoadValue executes the SelectBuilder and loads the resulting data into a primitive value Returns ErrNotFound if no value was found, and it was therefore not set.
func (*SelectBuilder) LoadValues ¶
func (b *SelectBuilder) LoadValues(dest interface{}) (int, error)
LoadValues executes the SelectBuilder and loads the resulting data into a slice of primitive values Returns ErrNotFound if no value was found, and it was therefore not set.
func (*SelectBuilder) Offset ¶
func (b *SelectBuilder) Offset(offset uint64) *SelectBuilder
Offset sets an offset for the statement; overrides any existing OFFSET
func (*SelectBuilder) OrderBy ¶
func (b *SelectBuilder) OrderBy(ord string) *SelectBuilder
OrderBy appends a column to ORDER the statement by
func (*SelectBuilder) OrderDir ¶
func (b *SelectBuilder) OrderDir(ord string, isAsc bool) *SelectBuilder
OrderDir appends a column to ORDER the statement by with a given direction
func (*SelectBuilder) Paginate ¶
func (b *SelectBuilder) Paginate(page, perPage uint64) *SelectBuilder
Paginate sets LIMIT/OFFSET for the statement based on the given page/perPage Assumes page/perPage are valid. Page and perPage must be >= 1
func (*SelectBuilder) ReturnInt64 ¶
func (b *SelectBuilder) ReturnInt64() (int64, error)
ReturnInt64 executes the SelectBuilder and returns the value as an int64
func (*SelectBuilder) ReturnInt64s ¶
func (b *SelectBuilder) ReturnInt64s() ([]int64, error)
ReturnInt64s executes the SelectBuilder and returns the value as a slice of int64s
func (*SelectBuilder) ReturnString ¶
func (b *SelectBuilder) ReturnString() (string, error)
ReturnString executes the SelectBuilder and returns the value as a string
func (*SelectBuilder) ReturnStrings ¶
func (b *SelectBuilder) ReturnStrings() ([]string, error)
ReturnStrings executes the SelectBuilder and returns the value as a slice of strings
func (*SelectBuilder) ReturnUint64 ¶
func (b *SelectBuilder) ReturnUint64() (uint64, error)
ReturnUint64 executes the SelectBuilder and returns the value as an uint64
func (*SelectBuilder) ReturnUint64s ¶
func (b *SelectBuilder) ReturnUint64s() ([]uint64, error)
ReturnUint64s executes the SelectBuilder and returns the value as a slice of uint64s
func (*SelectBuilder) ToSql ¶
func (b *SelectBuilder) ToSql() (string, []interface{})
ToSql serialized the SelectBuilder to a SQL string It returns the string with placeholders and a slice of query arguments
func (*SelectBuilder) Where ¶
func (b *SelectBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *SelectBuilder
Where appends a WHERE clause to the statement for the given string and args or map of column/value pairs
type Session ¶
type Session struct { EventReceiver // contains filtered or unexported fields }
Session represents a business unit of execution for some connection
func (*Session) DeleteFrom ¶
func (sess *Session) DeleteFrom(from string) *DeleteBuilder
DeleteFrom creates a new DeleteBuilder for the given table
func (*Session) InsertInto ¶
func (sess *Session) InsertInto(into string) *InsertBuilder
InsertInto instantiates a InsertBuilder for the given table
func (*Session) Select ¶
func (sess *Session) Select(cols ...string) *SelectBuilder
Select creates a new SelectBuilder that select that given columns
func (*Session) SelectBySql ¶
func (sess *Session) SelectBySql(sql string, args ...interface{}) *SelectBuilder
SelectBySql creates a new SelectBuilder for the given SQL string and arguments
func (*Session) Update ¶
func (sess *Session) Update(table string) *UpdateBuilder
Update creates a new UpdateBuilder for the given table
func (*Session) UpdateBySql ¶
func (sess *Session) UpdateBySql(sql string, args ...interface{}) *UpdateBuilder
UpdateBySql creates a new UpdateBuilder for the given SQL string and arguments
type SessionRunner ¶
type SessionRunner interface { Select(cols ...string) *SelectBuilder SelectBySql(sql string, args ...interface{}) *SelectBuilder InsertInto(into string) *InsertBuilder Update(table string) *UpdateBuilder UpdateBySql(sql string, args ...interface{}) *UpdateBuilder DeleteFrom(from string) *DeleteBuilder }
SessionRunner can do anything that a Session can except start a transaction.
type Tx ¶
Tx is a transaction for the given Session
func (*Tx) DeleteFrom ¶
func (tx *Tx) DeleteFrom(from string) *DeleteBuilder
DeleteFrom creates a new DeleteBuilder for the given table in the context for a transaction
func (*Tx) InsertInto ¶
func (tx *Tx) InsertInto(into string) *InsertBuilder
InsertInto instantiates a InsertBuilder for the given table bound to a transaction
func (*Tx) RollbackUnlessCommitted ¶
func (tx *Tx) RollbackUnlessCommitted()
RollbackUnlessCommitted rollsback the transaction unless it has already been committed or rolled back. Useful to defer tx.RollbackUnlessCommitted() -- so you don't have to handle N failure cases Keep in mind the only way to detect an error on the rollback is via the event log.
func (*Tx) Select ¶
func (tx *Tx) Select(cols ...string) *SelectBuilder
Select creates a new SelectBuilder that select that given columns bound to the transaction
func (*Tx) SelectBySql ¶
func (tx *Tx) SelectBySql(sql string, args ...interface{}) *SelectBuilder
SelectBySql creates a new SelectBuilder for the given SQL string and arguments bound to the transaction
func (*Tx) Update ¶
func (tx *Tx) Update(table string) *UpdateBuilder
Update creates a new UpdateBuilder for the given table bound to a transaction
func (*Tx) UpdateBySql ¶
func (tx *Tx) UpdateBySql(sql string, args ...interface{}) *UpdateBuilder
UpdateBySql creates a new UpdateBuilder for the given SQL string and arguments bound to a transaction
type UpdateBuilder ¶
type UpdateBuilder struct { *Session RawFullSql string RawArguments []interface{} Table string SetClauses []*setClause WhereFragments []*whereFragment OrderBys []string LimitCount uint64 LimitValid bool OffsetCount uint64 OffsetValid bool // contains filtered or unexported fields }
UpdateBuilder contains the clauses for an UPDATE statement
func (*UpdateBuilder) Exec ¶
func (b *UpdateBuilder) Exec() (sql.Result, error)
Exec executes the statement represented by the UpdateBuilder It returns the raw database/sql Result and an error if there was one
func (*UpdateBuilder) Limit ¶
func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
Limit sets a limit for the statement; overrides any existing LIMIT
func (*UpdateBuilder) Offset ¶
func (b *UpdateBuilder) Offset(offset uint64) *UpdateBuilder
Offset sets an offset for the statement; overrides any existing OFFSET
func (*UpdateBuilder) OrderBy ¶
func (b *UpdateBuilder) OrderBy(ord string) *UpdateBuilder
OrderBy appends a column to ORDER the statement by
func (*UpdateBuilder) OrderDir ¶
func (b *UpdateBuilder) OrderDir(ord string, isAsc bool) *UpdateBuilder
OrderDir appends a column to ORDER the statement by with a given direction
func (*UpdateBuilder) Set ¶
func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder
Set appends a column/value pair for the statement
func (*UpdateBuilder) SetMap ¶
func (b *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder
SetMap appends the elements of the map as column/value pairs for the statement
func (*UpdateBuilder) ToSql ¶
func (b *UpdateBuilder) ToSql() (string, []interface{})
ToSql serialized the UpdateBuilder to a SQL string It returns the string with placeholders and a slice of query arguments
func (*UpdateBuilder) Where ¶
func (b *UpdateBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *UpdateBuilder
Where appends a WHERE clause to the statement