Documentation
¶
Index ¶
- Constants
- Variables
- func CalculateFieldMap(recordType reflect.Type, columns []string, requireAllColumns bool) ([][]int, error)
- func Interpolate(sql string, vals []interface{}) (string, []interface{}, error)
- func PrepareHolderFor(record reflect.Value, fieldMap [][]int, holder []interface{}) ([]interface{}, error)
- func SQLMapFromFile(filename string) (map[string]string, error)
- func SQLMapFromReader(r io.Reader) (map[string]string, error)
- func SQLMapFromString(s string) (map[string]string, error)
- func SQLSliceFromFile(filename string) ([]string, error)
- func SQLSliceFromString(s string) ([]string, error)
- func SetVerbose(verbose bool)
- func ValuesFor(recordType reflect.Type, record reflect.Value, columns []string) ([]interface{}, error)
- type Builder
- type DeleteBuilder
- func (b *DeleteBuilder) Interpolate() (string, []interface{}, error)
- func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder
- func (b *DeleteBuilder) MustInterpolate() (string, []interface{})
- func (b *DeleteBuilder) Offset(offset uint64) *DeleteBuilder
- func (b *DeleteBuilder) OrderBy(ord string) *DeleteBuilder
- func (b *DeleteBuilder) ToSQL() (string, []interface{})
- func (b *DeleteBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *DeleteBuilder
- type Eq
- type EventReceiver
- type Execer
- type Expression
- type InsertBuilder
- func (b *InsertBuilder) Blacklist(columns ...string) *InsertBuilder
- func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder
- func (b *InsertBuilder) Interpolate() (string, []interface{}, error)
- func (b *InsertBuilder) MustInterpolate() (string, []interface{})
- func (b *InsertBuilder) Pair(column string, value interface{}) *InsertBuilder
- func (b *InsertBuilder) Record(record interface{}) *InsertBuilder
- func (b *InsertBuilder) Returning(columns ...string) *InsertBuilder
- func (b *InsertBuilder) ToSQL() (string, []interface{})
- func (b *InsertBuilder) Values(vals ...interface{}) *InsertBuilder
- func (b *InsertBuilder) Whitelist(columns ...string) *InsertBuilder
- type JSONText
- type LogEventReceiver
- func (ler *LogEventReceiver) Event(eventName string)
- func (ler *LogEventReceiver) EventErr(eventName string, err error) error
- func (ler *LogEventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error
- func (ler *LogEventReceiver) EventKv(eventName string, kvs map[string]string)
- func (ler *LogEventReceiver) Timing(eventName string, nanoseconds int64)
- func (ler *LogEventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string)
- 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 PostgresDialect
- type RawBuilder
- type Result
- type SQLDialect
- 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) Interpolate() (string, []interface{}, error)
- func (b *SelectBuilder) Limit(limit uint64) *SelectBuilder
- func (b *SelectBuilder) MustInterpolate() (string, []interface{})
- func (b *SelectBuilder) Offset(offset uint64) *SelectBuilder
- func (b *SelectBuilder) OrderBy(ord string) *SelectBuilder
- func (b *SelectBuilder) Paginate(page, perPage uint64) *SelectBuilder
- func (b *SelectBuilder) ToSQL() (string, []interface{})
- func (b *SelectBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *SelectBuilder
- type UnsafeString
- type UpdateBuilder
- func (b *UpdateBuilder) Interpolate() (string, []interface{}, error)
- func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
- func (b *UpdateBuilder) MustInterpolate() (string, []interface{})
- func (b *UpdateBuilder) Offset(offset uint64) *UpdateBuilder
- func (b *UpdateBuilder) OrderBy(ord string) *UpdateBuilder
- func (b *UpdateBuilder) Returning(columns ...string) *UpdateBuilder
- func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder
- func (b *UpdateBuilder) SetBlacklist(rec interface{}, columns ...string) *UpdateBuilder
- func (b *UpdateBuilder) SetMap(clauses map[string]interface{}) *UpdateBuilder
- func (b *UpdateBuilder) SetWhitelist(rec interface{}, columns ...string) *UpdateBuilder
- func (b *UpdateBuilder) ToSQL() (string, []interface{})
- func (b *UpdateBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *UpdateBuilder
Constants ¶
const DEFAULT = UnsafeString("DEFAULT")
DEFAULT SQL value
const NOW = UnsafeString("NOW()")
NOW SQL value
Variables ¶
var ( // ErrNotFound ... ErrNotFound = errors.New("not found") // ErrNotUTF8 ... ErrNotUTF8 = errors.New("invalid UTF-8") // ErrInvalidSliceLength ... ErrInvalidSliceLength = errors.New("length of slice is 0. length must be >= 1") // ErrInvalidSliceValue ... ErrInvalidSliceValue = errors.New("trying to interpolate invalid slice value into query") // ErrInvalidValue ... ErrInvalidValue = errors.New("trying to interpolate invalid value into query") // ErrArgumentMismatch ... ErrArgumentMismatch = errors.New("mismatch between ? (placeholders) and arguments") )
var EnableInterpolation = false
Whether to enable interpolation
var NameMapping = camelCaseToSnakeCase
NameMapping is the routine to use when mapping column names to struct properties
var Strict = false
Strict tells dat to raise errors
Functions ¶
func CalculateFieldMap ¶
func CalculateFieldMap(recordType reflect.Type, columns []string, requireAllColumns bool) ([][]int, error)
CalculateFieldMap recordType is the type of a structure
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.
func PrepareHolderFor ¶
func PrepareHolderFor(record reflect.Value, fieldMap [][]int, holder []interface{}) ([]interface{}, error)
PrepareHolderFor creates holders for a record.
TODO: fill this in
func SQLMapFromFile ¶
SQLMapFromFile loads a file containing special markers and loads the SQL statements into a map.
func SQLMapFromReader ¶
SQLMapFromReader creates a SQL map from an io.Reader.
This string
` --@selectUsers SELECT * FROM users; --@selectAccounts SELECT * FROM accounts; ` returns map[string]string{ "selectUsers": "SELECT * FROM users;", "selectACcounts": "SELECT * FROM accounts;", }
func SQLMapFromString ¶
SQLMapFromString creates a map of strings from s.
func SQLSliceFromFile ¶
SQLSliceFromFile reads a file's text then passes it to SQLSliceFromString.
func SQLSliceFromString ¶
SQLSliceFromString converts a multiline string marked by `^GO$` into a slice of SQL statements.
This string
SELECT * FROM users; GO SELECT * FROM accounts; returns []string{"SELECT *\nFROM users;", "SELECT *\nFROM accounts"}
func SetVerbose ¶
func SetVerbose(verbose bool)
SetVerbose sets the verbosity of logging which defaults to none
Types ¶
type Builder ¶
type Builder interface { ToSQL() (string, []interface{}) Interpolate() (string, []interface{}, error) MustInterpolate() (string, []interface{}) }
Builder interface is used to tie SQL generators to executors.
type DeleteBuilder ¶
type DeleteBuilder struct { Execer // contains filtered or unexported fields }
DeleteBuilder contains the clauses for a DELETE statement
func DeleteFrom ¶
func DeleteFrom(table string) *DeleteBuilder
DeleteFrom creates a new DeleteBuilder for the given table.
func NewDeleteBuilder ¶
func NewDeleteBuilder(table string) *DeleteBuilder
NewDeleteBuilder creates a new DeleteBuilder for the given table.
func (*DeleteBuilder) Interpolate ¶
func (b *DeleteBuilder) Interpolate() (string, []interface{}, error)
Interpolate interpolates this builder's SQL.
func (*DeleteBuilder) Limit ¶
func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder
Limit sets a LIMIT clause for the statement; overrides any existing LIMIT
func (*DeleteBuilder) MustInterpolate ¶
func (b *DeleteBuilder) MustInterpolate() (string, []interface{})
MustInterpolate interpolates this builder's SQL or panics.
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) 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
var Events EventReceiver
Events is the event receiver.
type Execer ¶
type Execer interface { Exec() (*Result, error) QueryScalar(destinations ...interface{}) error QuerySlice(dest interface{}) error QueryStruct(dest interface{}) error QueryStructs(dest interface{}) error }
Execer is an object that can be execute/query a database.
type Expression ¶
type Expression struct { Sql string Values []interface{} }
Expression holds a sub expression.
func Expr ¶
func Expr(sql string, values ...interface{}) *Expression
Expr is a SQL expression with placeholders, and a slice of args to replace them with
type InsertBuilder ¶
type InsertBuilder struct { Execer // contains filtered or unexported fields }
InsertBuilder contains the clauses for an INSERT statement
func InsertInto ¶
func InsertInto(table string) *InsertBuilder
InsertInto creates a new InsertBuilder for the given table.
func NewInsertBuilder ¶
func NewInsertBuilder(table string) *InsertBuilder
NewInsertBuilder creates a new InsertBuilder for the given table.
func (*InsertBuilder) Blacklist ¶
func (b *InsertBuilder) Blacklist(columns ...string) *InsertBuilder
Blacklist defines a blacklist of columns and should only be used in conjunction with Record.
func (*InsertBuilder) Columns ¶
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder
Columns appends columns to insert in the statement
func (*InsertBuilder) Interpolate ¶
func (b *InsertBuilder) Interpolate() (string, []interface{}, error)
Interpolate interpolates this builders sql.
func (*InsertBuilder) MustInterpolate ¶
func (b *InsertBuilder) MustInterpolate() (string, []interface{})
MustInterpolate must interpolate or panic.
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) Returning ¶
func (b *InsertBuilder) Returning(columns ...string) *InsertBuilder
Returning sets the columns for the RETURNING clause
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
func (*InsertBuilder) Whitelist ¶
func (b *InsertBuilder) Whitelist(columns ...string) *InsertBuilder
Whitelist defines a whitelist of columns to be inserted. To specify all columsn of a record use "*".
type JSONText ¶
type JSONText json.RawMessage
JSONText is a json.RawMessage, which is a []byte underneath. Value() validates the json format in the source, and returns an error if the json is not valid. Scan does no validation. JSONText additionally implements `Unmarshal`, which unmarshals the json within to an interface{}
func (*JSONText) MarshalJSON ¶
MarshalJSON returns the *j as the JSON encoding of j.
func (*JSONText) UnmarshalJSON ¶
UnmarshalJSON sets *j to a copy of data
type LogEventReceiver ¶
type LogEventReceiver struct {
// contains filtered or unexported fields
}
LogEventReceiver is a sentinel EventReceiver; use it if the caller doesn't supply one
func NewLogEventReceiver ¶
func NewLogEventReceiver(prefix string) *LogEventReceiver
NewLogEventReceiver creates a new LogEventReceiver.
func (*LogEventReceiver) Event ¶
func (ler *LogEventReceiver) Event(eventName string)
Event receives a simple notification when various events occur
func (*LogEventReceiver) EventErr ¶
func (ler *LogEventReceiver) EventErr(eventName string, err error) error
EventErr receives a notification of an error if one occurs
func (*LogEventReceiver) EventErrKv ¶
EventErrKv receives a notification of an error if one occurs along with optional key/value data
func (*LogEventReceiver) EventKv ¶
func (ler *LogEventReceiver) EventKv(eventName string, kvs map[string]string)
EventKv receives a notification when various events occur along with optional key/value data
func (*LogEventReceiver) Timing ¶
func (ler *LogEventReceiver) Timing(eventName string, nanoseconds int64)
Timing receives the time an event took to happen
type NullBool ¶
NullBool is a type that can be null or a bool
func (*NullBool) MarshalJSON ¶
MarshalJSON correctly serializes a NullBool to 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
type NullInt64 ¶
NullInt64 is a type that can be null or an int
func (*NullInt64) MarshalJSON ¶
MarshalJSON correctly serializes a NullInt64 to 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
type NullTime ¶
NullTime is a type that can be null or a time
func (*NullTime) MarshalJSON ¶
MarshalJSON correctly serializes a NullTime to JSON
type PostgresDialect ¶
type PostgresDialect struct{}
PostgresDialect is the PostgeSQL dialect.
func (*PostgresDialect) WriteIdentifier ¶
func (pd *PostgresDialect) WriteIdentifier(buf *bytes.Buffer, ident string)
WriteIdentifier is part of Dialect implementation.
func (*PostgresDialect) WriteStringLiteral ¶
func (pd *PostgresDialect) WriteStringLiteral(buf *bytes.Buffer, val string)
WriteStringLiteral is part of Dialect implementation.
Postgres 9.1+ does not allow any escape sequences by default. See http://www.postgresql.org/docs/9.3/interactive/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE In short, all backslashes are treated literally not as escape sequences.
type RawBuilder ¶
type RawBuilder struct { Execer // contains filtered or unexported fields }
RawBuilder builds SQL from raw SQL.
func NewRawBuilder ¶
func NewRawBuilder(sql string, args ...interface{}) *RawBuilder
NewRawBuilder creates a new RawBuilder for the given SQL string and arguments
func (*RawBuilder) Interpolate ¶
func (b *RawBuilder) Interpolate() (string, []interface{}, error)
Interpolate interpolates this builder's SQL.
func (*RawBuilder) MustInterpolate ¶
func (b *RawBuilder) MustInterpolate() (string, []interface{})
MustInterpolate interpolates this builder's SQL.
func (*RawBuilder) ToSQL ¶
func (b *RawBuilder) ToSQL() (string, []interface{})
ToSQL implements builder interface
type SQLDialect ¶
type SQLDialect interface { // WriteStringLiteral writes a string literal. WriteStringLiteral(buf *bytes.Buffer, value string) // WriteIdentifier writes a quoted identifer such as a column or table. WriteIdentifier(buf *bytes.Buffer, column string) }
SQLDialect represents a vendor specific SQL dialect.
var Dialect SQLDialect = &PostgresDialect{}
Dialect is the active SQLDialect.
type SelectBuilder ¶
type SelectBuilder struct { Execer // contains filtered or unexported fields }
SelectBuilder contains the clauses for a SELECT statement
func NewSelectBuilder ¶
func NewSelectBuilder(columns ...string) *SelectBuilder
NewSelectBuilder creates a new SelectBuilder for the given columns
func Select ¶
func Select(columns ...string) *SelectBuilder
Select creates a new SelectBuilder for the given columns.
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) Interpolate ¶
func (b *SelectBuilder) Interpolate() (string, []interface{}, error)
Interpolate interpolates this builders sql.
func (*SelectBuilder) Limit ¶
func (b *SelectBuilder) Limit(limit uint64) *SelectBuilder
Limit sets a limit for the statement; overrides any existing LIMIT
func (*SelectBuilder) MustInterpolate ¶
func (b *SelectBuilder) MustInterpolate() (string, []interface{})
MustInterpolate interpolates this builders sql or panics.
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) 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) 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 UnsafeString ¶
type UnsafeString string
UnsafeString is interpolated as an unescaped and unquoted value and should only be used to create constants.
type UpdateBuilder ¶
type UpdateBuilder struct { Execer // contains filtered or unexported fields }
UpdateBuilder contains the clauses for an UPDATE statement
func NewUpdateBuilder ¶
func NewUpdateBuilder(table string) *UpdateBuilder
NewUpdateBuilder creates a new UpdateBuilder for the given table
func Update ¶
func Update(table string) *UpdateBuilder
Update creates a new UpdateBuilder for the given table.
func (*UpdateBuilder) Interpolate ¶
func (b *UpdateBuilder) Interpolate() (string, []interface{}, error)
Interpolate interpolates this builders sql.
func (*UpdateBuilder) Limit ¶
func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
Limit sets a limit for the statement; overrides any existing LIMIT
func (*UpdateBuilder) MustInterpolate ¶
func (b *UpdateBuilder) MustInterpolate() (string, []interface{})
MustInterpolate interpolates this builders sql or panics.
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) Returning ¶
func (b *UpdateBuilder) Returning(columns ...string) *UpdateBuilder
Returning sets the columns for the RETURNING clause
func (*UpdateBuilder) Set ¶
func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder
Set appends a column/value pair for the statement
func (*UpdateBuilder) SetBlacklist ¶
func (b *UpdateBuilder) SetBlacklist(rec interface{}, columns ...string) *UpdateBuilder
SetBlacklist creates SET clause(s) using a record and blacklist of columns
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) SetWhitelist ¶
func (b *UpdateBuilder) SetWhitelist(rec interface{}, columns ...string) *UpdateBuilder
SetWhitelist creates SET clause(s) using a record and whitelist of columns. To specify all columns, use "*".
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