Documentation ¶
Index ¶
- Constants
- Variables
- func Date(t time.Time) date
- func Expr(sql string, values ...interface{}) *expr
- func Preprocess(sql string, vals []interface{}) (string, error)
- type And
- type By
- type Connection
- func (db *Connection) Begin() (*Tx, error)
- func (c *Connection) Close() error
- func (db *Connection) DeleteFrom(from string) *DeleteBuilder
- func (db *Connection) InsertInto(into string) *InsertBuilder
- func (c *Connection) Ping() error
- func (db *Connection) Query(sql string, args ...interface{}) *Query
- func (db *Connection) Select(cols ...string) *SelectBuilder
- func (db *Connection) Update(table string) *UpdateBuilder
- type DeleteBuilder
- func (e DeleteBuilder) Exec() (sql.Result, error)
- func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder
- func (e DeleteBuilder) MustExec() sql.Result
- func (b *DeleteBuilder) Offset(offset uint64) *DeleteBuilder
- func (b *DeleteBuilder) Order(by By) *DeleteBuilder
- func (b *DeleteBuilder) OrderBy(expr string) *DeleteBuilder
- func (b *DeleteBuilder) String() string
- func (b *DeleteBuilder) ToSql() (string, []interface{})
- func (b *DeleteBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *DeleteBuilder
- type Dialect
- type EventReceiver
- type InsertBuilder
- func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder
- func (e InsertBuilder) Exec() (sql.Result, error)
- func (e InsertBuilder) MustExec() sql.Result
- func (b *InsertBuilder) Pair(column string, value interface{}) *InsertBuilder
- func (b *InsertBuilder) Record(record interface{}) *InsertBuilder
- func (b *InsertBuilder) String() string
- func (b *InsertBuilder) ToSql() (string, []interface{})
- func (b *InsertBuilder) Values(vals ...interface{}) *InsertBuilder
- 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 Query
- func (l Query) All(dest interface{}) (n int, err error)
- func (e Query) Exec() (sql.Result, error)
- func (e Query) MustExec() sql.Result
- func (l Query) One(dest interface{}) error
- func (l Query) ReturnInt64() (int64, error)
- func (l Query) ReturnInt64s() ([]int64, error)
- func (l Query) ReturnString() (string, error)
- func (l Query) ReturnStrings() ([]string, error)
- func (l Query) ReturnUint64() (uint64, error)
- func (l Query) ReturnUint64s() ([]uint64, error)
- func (q *Query) String() string
- func (q *Query) ToSql() (string, []interface{})
- type SelectBuilder
- func (l SelectBuilder) All(dest interface{}) (n int, err error)
- func (b *SelectBuilder) Distinct() *SelectBuilder
- func (b *SelectBuilder) From(from string) *SelectBuilder
- func (b *SelectBuilder) GroupBy(group string) *SelectBuilder
- func (b *SelectBuilder) Having(exprOrMap interface{}, args ...interface{}) *SelectBuilder
- func (b *SelectBuilder) Limit(limit uint64) *SelectBuilder
- func (b *SelectBuilder) Offset(offset uint64) *SelectBuilder
- func (b *SelectBuilder) One(dest interface{}) error
- func (b *SelectBuilder) Order(by By) *SelectBuilder
- func (b *SelectBuilder) OrderBy(expr string) *SelectBuilder
- func (l SelectBuilder) ReturnInt64() (int64, error)
- func (l SelectBuilder) ReturnInt64s() ([]int64, error)
- func (l SelectBuilder) ReturnString() (string, error)
- func (l SelectBuilder) ReturnStrings() ([]string, error)
- func (l SelectBuilder) ReturnUint64() (uint64, error)
- func (l SelectBuilder) ReturnUint64s() ([]uint64, error)
- func (b *SelectBuilder) String() string
- func (b *SelectBuilder) ToSql() (string, []interface{})
- func (b *SelectBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *SelectBuilder
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) DeleteFrom(from string) *DeleteBuilder
- func (tx *Tx) InsertInto(into string) *InsertBuilder
- func (tx *Tx) Query(sql string, args ...interface{}) *Query
- func (tx *Tx) Rollback() error
- func (tx *Tx) RollbackUnlessCommitted()
- func (tx *Tx) Select(cols ...string) *SelectBuilder
- func (tx *Tx) Update(table string) *UpdateBuilder
- type UpdateBuilder
- func (e UpdateBuilder) Exec() (sql.Result, error)
- func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
- func (e UpdateBuilder) MustExec() sql.Result
- func (b *UpdateBuilder) Offset(offset uint64) *UpdateBuilder
- func (b *UpdateBuilder) Order(by By) *UpdateBuilder
- func (b *UpdateBuilder) OrderBy(expr string) *UpdateBuilder
- func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder
- func (b *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder
- func (b *UpdateBuilder) String() string
- func (b *UpdateBuilder) ToSql() (string, []interface{})
- func (b *UpdateBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *UpdateBuilder
Constants ¶
const ( Asc direction = false Desc direction = true )
These constant are used to indicate a direction of an ORDER clause. They are used as a value in the column/direction map in Order method.
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") ErrInvalidSyntax = errors.New("SQL syntax error") )
var NameMapping = camelCaseToSnakeCase
NameMapping is the routine to use when mapping column names to struct properties
Functions ¶
func Date ¶
Date turns time into date type. It is supposed to be used in queries to use 2006-01-02 time format.
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 Preprocess ¶
Preprocess takes an SQL string with placeholders and a list of arguments to replace them with. It returns a blank string and error if the number of placeholders does not match the number of arguments.
Types ¶
type And ¶
type And map[string]interface{}
And is a map column -> value pairs which must be matched in a query.
type By ¶
type By map[string]direction
By is a map of columns and order directions used in builders' Order methods. Example of usage:
b.Order(ql.By{"col1": ql.Asc,"col2": ql.Desc})
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 MustOpen ¶
func MustOpen(driverName, dataSourceName string) *Connection
MustOpen is like Open but panics on error.
func MustOpenAndVerify ¶
func MustOpenAndVerify(driverName, dataSourceName string) *Connection
MustOpenAndVerify is like MustOpen but it verifies the connection and panics on error.
func NewConnection ¶
func NewConnection(db *sql.DB, log EventReceiver) *Connection
NewConnection instantiates a Connection for a given database/sql connection and event receiver.
func Open ¶
func Open(driverName, dataSourceName string) (*Connection, error)
Open opens a database by calling sql.Open. It returns new Connection with nil EventReceiver.
func (*Connection) Begin ¶
func (db *Connection) Begin() (*Tx, error)
Begin creates a transaction for the given connection.
func (*Connection) Close ¶
func (c *Connection) Close() error
Close closes the database, releasing any open resources.
func (*Connection) DeleteFrom ¶
func (db *Connection) DeleteFrom(from string) *DeleteBuilder
DeleteFrom creates a new DeleteBuilder for the given table.
func (*Connection) InsertInto ¶
func (db *Connection) InsertInto(into string) *InsertBuilder
InsertInto instantiates a InsertBuilder for the given table.
func (*Connection) Ping ¶
func (c *Connection) Ping() error
Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (*Connection) Query ¶
func (db *Connection) Query(sql string, args ...interface{}) *Query
Query creates Query by the raw SQL query and args.
func (*Connection) Select ¶
func (db *Connection) Select(cols ...string) *SelectBuilder
Select creates a new SelectBuilder that select that given columns.
func (*Connection) Update ¶
func (db *Connection) Update(table string) *UpdateBuilder
Update creates a new UpdateBuilder for the given table.
type DeleteBuilder ¶
type DeleteBuilder struct { From string // contains filtered or unexported fields }
DeleteBuilder contains the clauses for a DELETE statement.
func (DeleteBuilder) Exec ¶
Exec executes the query. It returns the raw database/sql Result and an error if there is 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) Order ¶
func (b *DeleteBuilder) Order(by By) *DeleteBuilder
Order accepts By map of columns and directions to ORDER the statement by.
func (*DeleteBuilder) OrderBy ¶
func (b *DeleteBuilder) OrderBy(expr string) *DeleteBuilder
OrderBy appends an ORDER BY clause to the statement.
func (*DeleteBuilder) String ¶
func (b *DeleteBuilder) String() string
String returns a string representing a preprocessed, interpolated, query.
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 Dialect ¶
type Dialect interface { EscapeIdent(w query.Writer, ident string) EscapeBool(w query.Writer, b bool) EscapeString(w query.Writer, s string) EscapeTime(w query.Writer, t time.Time) ApplyLimitAndOffset(w query.Writer, limit, offset uint64) }
Dialect is an interface that wraps the diverse properties of individual SQL drivers.
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 { 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 ¶
Exec executes the query. It returns the raw database/sql Result and an error if there is 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) String ¶
func (b *InsertBuilder) String() string
String returns a string representing a preprocessed, interpolated, query.
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 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 Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query represent an arbitrary SQL statement.
func (Query) All ¶
All executes the query and loads the resulting data into the dest, which can be a slice of either structs, or primitive values. It returns n found items (which is not necessarily the number of items set).
func (Query) Exec ¶
Exec executes the query. It returns the raw database/sql Result and an error if there is one.
func (Query) One ¶
func (l Query) One(dest interface{}) error
One executes the query and loads the resulting data into the dest, which can be either a struct, or a primitive value. Returns ErrNotFound if no item was found, and it was therefore not set.
func (Query) ReturnInt64 ¶
ReturnInt64 executes the SelectBuilder and returns the value as an int64.
func (Query) ReturnInt64s ¶
ReturnInt64s executes the SelectBuilder and returns the value as a slice of int64s.
func (Query) ReturnString ¶
ReturnString executes the SelectBuilder and returns the value as a string.
func (Query) ReturnStrings ¶
ReturnStrings executes the SelectBuilder and returns the value as a slice of strings.
func (Query) ReturnUint64 ¶
ReturnUint64 executes the SelectBuilder and returns the value as an uint64.
func (Query) ReturnUint64s ¶
ReturnUint64s executes the SelectBuilder and returns the value as a slice of uint64s.
type SelectBuilder ¶
type SelectBuilder struct { IsDistinct bool Columns []string FromTable string GroupBys []string HavingFragments []*whereFragment // contains filtered or unexported fields }
SelectBuilder contains the clauses for a SELECT statement.
func (SelectBuilder) All ¶
All executes the query and loads the resulting data into the dest, which can be a slice of either structs, or primitive values. It returns n found items (which is not necessarily the number of items set).
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(exprOrMap 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) Offset ¶
func (b *SelectBuilder) Offset(offset uint64) *SelectBuilder
Offset sets an offset for the statement; overrides any existing OFFSET.
func (*SelectBuilder) One ¶
func (b *SelectBuilder) One(dest interface{}) error
One executes the query and loads the resulting data into the dest, which can be either a struct, or a primitive value. Returns ErrNotFound if no item was found, and it was therefore not set.
func (*SelectBuilder) Order ¶
func (b *SelectBuilder) Order(by By) *SelectBuilder
Order accepts By map of columns and directions to ORDER the statement by.
func (*SelectBuilder) OrderBy ¶
func (b *SelectBuilder) OrderBy(expr string) *SelectBuilder
OrderBy appends a column to ORDER the statement by.
func (SelectBuilder) ReturnInt64 ¶
ReturnInt64 executes the SelectBuilder and returns the value as an int64.
func (SelectBuilder) ReturnInt64s ¶
ReturnInt64s executes the SelectBuilder and returns the value as a slice of int64s.
func (SelectBuilder) ReturnString ¶
ReturnString executes the SelectBuilder and returns the value as a string.
func (SelectBuilder) ReturnStrings ¶
ReturnStrings executes the SelectBuilder and returns the value as a slice of strings.
func (SelectBuilder) ReturnUint64 ¶
ReturnUint64 executes the SelectBuilder and returns the value as an uint64.
func (SelectBuilder) ReturnUint64s ¶
ReturnUint64s executes the SelectBuilder and returns the value as a slice of uint64s.
func (*SelectBuilder) String ¶
func (b *SelectBuilder) String() string
String returns a string representing a preprocessed, interpolated, query.
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 Tx ¶
type Tx struct { *Connection *sql.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) Query ¶
Query creates Query by the raw SQL query and args. Query is bound to the 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) Update ¶
func (tx *Tx) Update(table string) *UpdateBuilder
Update creates a new UpdateBuilder for the given table bound to a transaction.
type UpdateBuilder ¶
type UpdateBuilder struct { Table string SetClauses []*setClause // contains filtered or unexported fields }
UpdateBuilder contains the clauses for an UPDATE statement.
func (UpdateBuilder) Exec ¶
Exec executes the query. It returns the raw database/sql Result and an error if there is 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) Order ¶
func (b *UpdateBuilder) Order(by By) *UpdateBuilder
Order accepts By map of columns and directions to ORDER the statement by.
func (*UpdateBuilder) OrderBy ¶
func (b *UpdateBuilder) OrderBy(expr string) *UpdateBuilder
OrderBy appends a column to ORDER the statement by.
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) String ¶
func (b *UpdateBuilder) String() string
String returns a string representing a preprocessed, interpolated, query.
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.