Documentation ¶
Overview ¶
Package dbr provides additions to Go's database/sql for super fast performance and convenience.
Index ¶
- Variables
- func ForceIndex(index ...string) indexHint
- func IgnoreIndex(index ...string) indexHint
- func InterfaceLoader(value interface{}, concreteType interface{}) interface{}
- func InterpolateForDialect(query string, value []interface{}, d Dialect) (string, error)
- func Load(rows *sql.Rows, value interface{}, d Dialect) (int, error)
- func Union(builder ...Builder) interface{ ... }
- func UnionAll(builder ...Builder) interface{ ... }
- func UseIndex(index ...string) indexHint
- type Buffer
- type BuildFunc
- type Builder
- func And(cond ...Builder) Builder
- func Eq(column string, value interface{}) Builder
- func Expr(query string, value ...interface{}) Builder
- func Gt(column string, value interface{}) Builder
- func Gte(column string, value interface{}) Builder
- func Like(column, value string, escape ...string) Builder
- func Lt(column string, value interface{}) Builder
- func Lte(column string, value interface{}) Builder
- func Neq(column string, value interface{}) Builder
- func NotLike(column, value string, escape ...string) Builder
- func Or(cond ...Builder) Builder
- type Comments
- type Connection
- type DeleteBuilder
- type DeleteStmt
- func (b *DeleteStmt) Build(d Dialect, buf Buffer) error
- func (b *DeleteStmt) Comment(comment string) *DeleteStmt
- func (b *DeleteStmt) Exec() (sql.Result, error)
- func (b *DeleteStmt) ExecContext(ctx context.Context) (sql.Result, error)
- func (b *DeleteStmt) Limit(n uint64) *DeleteStmt
- func (b *DeleteStmt) Where(query interface{}, value ...interface{}) *DeleteStmt
- type Dialect
- type EventReceiver
- type I
- type InsertBuilder
- type InsertStmt
- func (b *InsertStmt) Build(d Dialect, buf Buffer) error
- func (b *InsertStmt) Columns(column ...string) *InsertStmt
- func (b *InsertStmt) Comment(comment string) *InsertStmt
- func (b *InsertStmt) Exec() (sql.Result, error)
- func (b *InsertStmt) ExecContext(ctx context.Context) (sql.Result, error)
- func (b *InsertStmt) Ignore() *InsertStmt
- func (b *InsertStmt) Load(value interface{}) error
- func (b *InsertStmt) LoadContext(ctx context.Context, value interface{}) error
- func (b *InsertStmt) Pair(column string, value interface{}) *InsertStmt
- func (b *InsertStmt) Record(structValue interface{}) *InsertStmt
- func (b *InsertStmt) Returning(column ...string) *InsertStmt
- func (b *InsertStmt) Values(value ...interface{}) *InsertStmt
- type Iterator
- 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 Runner
- type SelectBuilder
- type SelectStmt
- func (b *SelectStmt) As(alias string) Builder
- func (b *SelectStmt) Build(d Dialect, buf Buffer) error
- func (b *SelectStmt) Comment(comment string) *SelectStmt
- func (b *SelectStmt) Distinct() *SelectStmt
- func (b *SelectStmt) From(table interface{}) *SelectStmt
- func (b *SelectStmt) FullJoin(table, on interface{}, indexHints ...Builder) *SelectStmt
- func (b *SelectStmt) GroupBy(col ...string) *SelectStmt
- func (b *SelectStmt) Having(query interface{}, value ...interface{}) *SelectStmt
- func (b *SelectStmt) IndexHint(hints ...interface{}) *SelectStmt
- func (b *SelectStmt) Iterate() (Iterator, error)
- func (b *SelectStmt) IterateContext(ctx context.Context) (Iterator, error)
- func (b *SelectStmt) Join(table, on interface{}, indexHints ...Builder) *SelectStmt
- func (b *SelectStmt) LeftJoin(table, on interface{}, indexHints ...Builder) *SelectStmt
- func (b *SelectStmt) Limit(n uint64) *SelectStmt
- func (b *SelectStmt) Load(value interface{}) (int, error)
- func (b *SelectStmt) LoadContext(ctx context.Context, value interface{}) (int, error)
- func (b *SelectStmt) LoadOne(value interface{}) error
- func (b *SelectStmt) LoadOneContext(ctx context.Context, value interface{}) error
- func (b *SelectStmt) Offset(n uint64) *SelectStmt
- func (b *SelectStmt) OrderAsc(col string) *SelectStmt
- func (b *SelectStmt) OrderBy(col string) *SelectStmt
- func (b *SelectStmt) OrderDesc(col string) *SelectStmt
- func (b *SelectStmt) OrderDir(col string, isAsc bool) *SelectStmt
- func (b *SelectStmt) Paginate(page, perPage uint64) *SelectStmt
- func (b *SelectStmt) ReturnInt64() (int64, error)
- func (b *SelectStmt) ReturnInt64s() ([]int64, error)
- func (b *SelectStmt) ReturnString() (string, error)
- func (b *SelectStmt) ReturnStrings() ([]string, error)
- func (b *SelectStmt) ReturnUint64() (uint64, error)
- func (b *SelectStmt) ReturnUint64s() ([]uint64, error)
- func (b *SelectStmt) RightJoin(table, on interface{}, indexHints ...Builder) *SelectStmt
- func (b *SelectStmt) Rows() (*sql.Rows, error)
- func (b *SelectStmt) RowsContext(ctx context.Context) (*sql.Rows, error)
- func (b *SelectStmt) Suffix(suffix string, value ...interface{}) *SelectStmt
- func (b *SelectStmt) Where(query interface{}, value ...interface{}) *SelectStmt
- type Session
- func (sess *Session) Begin() (*Tx, error)
- func (sess *Session) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (sess *Session) DeleteBySql(query string, value ...interface{}) *DeleteStmt
- func (sess *Session) DeleteFrom(table string) *DeleteStmt
- func (sess *Session) GetTimeout() time.Duration
- func (sess *Session) InsertBySql(query string, value ...interface{}) *InsertStmt
- func (sess *Session) InsertInto(table string) *InsertStmt
- func (sess *Session) Select(column ...string) *SelectStmt
- func (sess *Session) SelectBySql(query string, value ...interface{}) *SelectStmt
- func (sess *Session) Update(table string) *UpdateStmt
- func (sess *Session) UpdateBySql(query string, value ...interface{}) *UpdateStmt
- type SessionRunner
- type TracingEventReceiver
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) DeleteBySql(query string, value ...interface{}) *DeleteStmt
- func (tx *Tx) DeleteFrom(table string) *DeleteStmt
- func (tx *Tx) GetTimeout() time.Duration
- func (tx *Tx) InsertBySql(query string, value ...interface{}) *InsertStmt
- func (tx *Tx) InsertInto(table string) *InsertStmt
- func (tx *Tx) Rollback() error
- func (tx *Tx) RollbackUnlessCommitted()
- func (tx *Tx) Select(column ...string) *SelectStmt
- func (tx *Tx) SelectBySql(query string, value ...interface{}) *SelectStmt
- func (tx *Tx) Update(table string) *UpdateStmt
- func (tx *Tx) UpdateBySql(query string, value ...interface{}) *UpdateStmt
- type UpdateBuilder
- type UpdateStmt
- func (b *UpdateStmt) Build(d Dialect, buf Buffer) error
- func (b *UpdateStmt) Comment(comment string) *UpdateStmt
- func (b *UpdateStmt) DecrBy(column string, value interface{}) *UpdateStmt
- func (b *UpdateStmt) Exec() (sql.Result, error)
- func (b *UpdateStmt) ExecContext(ctx context.Context) (sql.Result, error)
- func (b *UpdateStmt) IncrBy(column string, value interface{}) *UpdateStmt
- func (b *UpdateStmt) IndexHint(hints ...interface{}) *UpdateStmt
- func (b *UpdateStmt) Limit(n uint64) *UpdateStmt
- func (b *UpdateStmt) Load(value interface{}) error
- func (b *UpdateStmt) LoadContext(ctx context.Context, value interface{}) error
- func (b *UpdateStmt) Returning(column ...string) *UpdateStmt
- func (b *UpdateStmt) Set(column string, value interface{}) *UpdateStmt
- func (b *UpdateStmt) SetMap(m map[string]interface{}) *UpdateStmt
- func (b *UpdateStmt) Where(query interface{}, value ...interface{}) *UpdateStmt
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("dbr: not found") ErrNotSupported = errors.New("dbr: not supported") ErrTableNotSpecified = errors.New("dbr: table not specified") ErrColumnNotSpecified = errors.New("dbr: column not specified") ErrInvalidPointer = errors.New("dbr: attempt to load into an invalid pointer") ErrPlaceholderCount = errors.New("dbr: wrong placeholder count") ErrInvalidSliceLength = errors.New("dbr: length of slice is 0. length must be >= 1") ErrCantConvertToTime = errors.New("dbr: can't convert to time.Time") ErrInvalidTimestring = errors.New("dbr: invalid time string") )
package errors
var ACINameMapping = camelCaseToUpperSnakeCase
var NameMapping = camelCaseToSnakeCase
var Now = nowSentinel{}
Now is a value that serializes to the current time in UTC.
Functions ¶
func ForceIndex ¶
func ForceIndex(index ...string) indexHint
func IgnoreIndex ¶
func IgnoreIndex(index ...string) indexHint
func InterfaceLoader ¶
func InterfaceLoader(value interface{}, concreteType interface{}) interface{}
func InterpolateForDialect ¶
InterpolateForDialect replaces placeholder in query with corresponding value in dialect.
It can be also used for debugging custom Builder.
Every time you call database/sql's db.Query("SELECT ...") method, under the hood, the mysql driver will create a prepared statement, execute it, and then throw it away. This has a big performance cost.
gocraft/dbr doesn't use prepared statements. We ported mysql's query escape functionality directly into our package, which means we interpolate all of those question marks with their arguments before they get to MySQL. The result of this is that it's way faster, and just as secure.
Check out these benchmarks from https://github.com/tyler-smith/golang-sql-benchmark.
func Load ¶
Load loads any value from sql.Rows.
value can be:
1. simple type like int64, string, etc.
2. sql.Scanner, which allows loading with custom types.
3. map; the first column from SQL result loaded to the key, and the rest of columns will be loaded into the value. This is useful to dedup SQL result with first column.
4. map of slice; like map, values with the same key are collected with a slice.
Types ¶
type Buffer ¶
type Buffer interface { WriteString(string) (int, error) String() string WriteValue(v ...interface{}) (err error) Value() []interface{} }
Buffer collects strings, and values that are ready to be interpolated. This is used internally to efficiently build SQL statement.
type Builder ¶
Builder builds SQL in Dialect like MySQL, and PostgreSQL. The raw SQL and values are stored in Buffer.
The core of gocraft/dbr is interpolation, which can expand ? with arbitrary SQL. If you need a feature that is not currently supported, you can build it on your own (or use Expr).
To do that, the value that you wish to be expanded with ? needs to implement Builder.
func Eq ¶
Eq is `=`. When value is nil, it will be translated to `IS NULL`. When value is a slice, it will be translated to `IN`. Otherwise it will be translated to `=`.
func Expr ¶
Expr allows raw expression to be used when current SQL syntax is not supported by gocraft/dbr.
func Neq ¶
Neq is `!=`. When value is nil, it will be translated to `IS NOT NULL`. When value is a slice, it will be translated to `NOT IN`. Otherwise it will be translated to `!=`.
type Comments ¶
type Comments []string
Comments represents a set of sql comments
type Connection ¶
type Connection struct { *sql.DB Dialect EventReceiver }
Connection wraps sql.DB with an EventReceiver to send events, errors, and timings.
func Open ¶
func Open(driver, dsn string, log EventReceiver) (*Connection, error)
Open creates a Connection. log can be nil to ignore logging.
func (*Connection) NewSession ¶
func (conn *Connection) NewSession(log EventReceiver) *Session
NewSession instantiates a Session from Connection. If log is nil, Connection EventReceiver is used.
type DeleteBuilder ¶
type DeleteBuilder = DeleteStmt
type DeleteStmt ¶
type DeleteStmt struct { Runner EventReceiver Dialect Table string WhereCond []Builder LimitCount int64 // contains filtered or unexported fields }
DeleteStmt builds `DELETE ...`.
func DeleteBySql ¶
func DeleteBySql(query string, value ...interface{}) *DeleteStmt
DeleteBySql creates a DeleteStmt from raw query.
func (*DeleteStmt) Comment ¶
func (b *DeleteStmt) Comment(comment string) *DeleteStmt
func (*DeleteStmt) ExecContext ¶
func (*DeleteStmt) Limit ¶
func (b *DeleteStmt) Limit(n uint64) *DeleteStmt
func (*DeleteStmt) Where ¶
func (b *DeleteStmt) Where(query interface{}, value ...interface{}) *DeleteStmt
Where adds a where condition. query can be Builder or string. value is used only if query type is string.
type Dialect ¶
type Dialect interface { QuoteIdent(id string) string EncodeString(s string) string EncodeBool(b bool) string EncodeTime(t time.Time) string EncodeBytes(b []byte) string Placeholder(n int) string }
Dialect abstracts database driver differences in encoding types, and placeholders.
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 = InsertStmt
type InsertStmt ¶
type InsertStmt struct { Runner EventReceiver Dialect Table string Column []string Value [][]interface{} Ignored bool ReturnColumn []string RecordID *int64 // contains filtered or unexported fields }
InsertStmt builds `INSERT INTO ...`.
func InsertBySql ¶
func InsertBySql(query string, value ...interface{}) *InsertStmt
InsertBySql creates an InsertStmt from raw query.
func (*InsertStmt) Columns ¶
func (b *InsertStmt) Columns(column ...string) *InsertStmt
func (*InsertStmt) Comment ¶
func (b *InsertStmt) Comment(comment string) *InsertStmt
Comment adds a comment to prepended. All multi-line sql comment characters are stripped
func (*InsertStmt) ExecContext ¶
func (*InsertStmt) Load ¶
func (b *InsertStmt) Load(value interface{}) error
func (*InsertStmt) LoadContext ¶
func (b *InsertStmt) LoadContext(ctx context.Context, value interface{}) error
func (*InsertStmt) Pair ¶
func (b *InsertStmt) Pair(column string, value interface{}) *InsertStmt
Pair adds (column, value) to be inserted. It is an error to mix Pair with Values and Record.
func (*InsertStmt) Record ¶
func (b *InsertStmt) Record(structValue interface{}) *InsertStmt
Record adds a tuple for columns from a struct.
If there is a field called "Id" or "ID" in the struct, it will be set to LastInsertId.
func (*InsertStmt) Returning ¶
func (b *InsertStmt) Returning(column ...string) *InsertStmt
Returning specifies the returning columns for postgres/mssql.
func (*InsertStmt) Values ¶
func (b *InsertStmt) Values(value ...interface{}) *InsertStmt
Values adds a tuple to be inserted. The order of the tuple should match Columns.
type Iterator ¶
Iterator is an interface to iterate over the result of a sql query and scan each row one at a time instead of getting all into one slice. The principe is similar to the standard sql.Rows type.
type NullBool ¶
NullBool is a type that can be null or a bool.
func NewNullBool ¶
func NewNullBool(v interface{}) (n NullBool)
NewNullBool creates a NullBool with Scan().
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 NewNullFloat64 ¶
func NewNullFloat64(v interface{}) (n NullFloat64)
NewNullFloat64 creates a NullFloat64 with Scan().
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 NewNullInt64 ¶
func NewNullInt64(v interface{}) (n NullInt64)
NewNullInt64 creates a NullInt64 with Scan().
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 NewNullString ¶
func NewNullString(v interface{}) (n NullString)
NewNullString creates a NullString with Scan().
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 NewNullTime ¶
func NewNullTime(v interface{}) (n NullTime)
NewNullTime creates a NullTime with Scan().
func (NullTime) MarshalJSON ¶
MarshalJSON correctly serializes a NullTime to JSON.
func (*NullTime) Scan ¶
Scan implements the Scanner interface. The value type must be time.Time or string / []byte (formatted time-string), otherwise Scan fails.
func (*NullTime) UnmarshalJSON ¶
UnmarshalJSON correctly deserializes a NullTime from JSON.
type SelectBuilder ¶
type SelectBuilder = SelectStmt
type SelectStmt ¶
type SelectStmt struct { Runner EventReceiver Dialect IsDistinct bool Column []interface{} Table interface{} JoinTable []Builder WhereCond []Builder Group []Builder HavingCond []Builder Order []Builder Suffixes []Builder LimitCount int64 OffsetCount int64 // contains filtered or unexported fields }
SelectStmt builds `SELECT ...`.
func SelectBySql ¶
func SelectBySql(query string, value ...interface{}) *SelectStmt
SelectBySql creates a SelectStmt from raw query.
func (*SelectStmt) As ¶
func (b *SelectStmt) As(alias string) Builder
As creates alias for select statement.
func (*SelectStmt) Comment ¶
func (b *SelectStmt) Comment(comment string) *SelectStmt
func (*SelectStmt) Distinct ¶
func (b *SelectStmt) Distinct() *SelectStmt
func (*SelectStmt) From ¶
func (b *SelectStmt) From(table interface{}) *SelectStmt
From specifies table to select from. table can be Builder like SelectStmt, or string.
func (*SelectStmt) FullJoin ¶
func (b *SelectStmt) FullJoin(table, on interface{}, indexHints ...Builder) *SelectStmt
FullJoin add full-join. on can be Builder or string.
func (*SelectStmt) GroupBy ¶
func (b *SelectStmt) GroupBy(col ...string) *SelectStmt
GroupBy specifies columns for grouping.
func (*SelectStmt) Having ¶
func (b *SelectStmt) Having(query interface{}, value ...interface{}) *SelectStmt
Having adds a having condition. query can be Builder or string. value is used only if query type is string.
func (*SelectStmt) IndexHint ¶
func (b *SelectStmt) IndexHint(hints ...interface{}) *SelectStmt
IndexHint adds a index hint. hint can be Builder or string.
func (*SelectStmt) Iterate ¶
func (b *SelectStmt) Iterate() (Iterator, error)
Iterate executes the query and returns the Iterator, or any error encountered.
func (*SelectStmt) IterateContext ¶
func (b *SelectStmt) IterateContext(ctx context.Context) (Iterator, error)
IterateContext executes the query and returns the Iterator, or any error encountered.
func (*SelectStmt) Join ¶
func (b *SelectStmt) Join(table, on interface{}, indexHints ...Builder) *SelectStmt
Join add inner-join. on can be Builder or string.
func (*SelectStmt) LeftJoin ¶
func (b *SelectStmt) LeftJoin(table, on interface{}, indexHints ...Builder) *SelectStmt
LeftJoin add left-join. on can be Builder or string.
func (*SelectStmt) Limit ¶
func (b *SelectStmt) Limit(n uint64) *SelectStmt
func (*SelectStmt) Load ¶
func (b *SelectStmt) Load(value interface{}) (int, error)
Load loads multi-row SQL result into a slice of go variables.
func (*SelectStmt) LoadContext ¶
func (b *SelectStmt) LoadContext(ctx context.Context, value interface{}) (int, error)
func (*SelectStmt) LoadOne ¶
func (b *SelectStmt) LoadOne(value interface{}) error
LoadOne loads SQL result into go variable that is not a slice. Unlike Load, it returns ErrNotFound if the SQL result row count is 0.
func (*SelectStmt) LoadOneContext ¶
func (b *SelectStmt) LoadOneContext(ctx context.Context, value interface{}) error
func (*SelectStmt) Offset ¶
func (b *SelectStmt) Offset(n uint64) *SelectStmt
func (*SelectStmt) OrderAsc ¶
func (b *SelectStmt) OrderAsc(col string) *SelectStmt
func (*SelectStmt) OrderBy ¶
func (b *SelectStmt) OrderBy(col string) *SelectStmt
OrderBy specifies columns for ordering.
func (*SelectStmt) OrderDesc ¶
func (b *SelectStmt) OrderDesc(col string) *SelectStmt
func (*SelectStmt) OrderDir ¶
func (b *SelectStmt) OrderDir(col string, isAsc bool) *SelectStmt
OrderDir is a helper for OrderAsc and OrderDesc.
func (*SelectStmt) Paginate ¶
func (b *SelectStmt) Paginate(page, perPage uint64) *SelectStmt
Paginate fetches a page in a naive way for a small set of data.
func (*SelectStmt) ReturnInt64 ¶
func (b *SelectStmt) ReturnInt64() (int64, error)
ReturnInt64 executes the SelectStmt and returns the value as an int64.
func (*SelectStmt) ReturnInt64s ¶
func (b *SelectStmt) ReturnInt64s() ([]int64, error)
ReturnInt64s executes the SelectStmt and returns the value as a slice of int64s.
func (*SelectStmt) ReturnString ¶
func (b *SelectStmt) ReturnString() (string, error)
ReturnString executes the SelectStmt and returns the value as a string.
func (*SelectStmt) ReturnStrings ¶
func (b *SelectStmt) ReturnStrings() ([]string, error)
ReturnStrings executes the SelectStmt and returns the value as a slice of strings.
func (*SelectStmt) ReturnUint64 ¶
func (b *SelectStmt) ReturnUint64() (uint64, error)
ReturnUint64 executes the SelectStmt and returns the value as an uint64.
func (*SelectStmt) ReturnUint64s ¶
func (b *SelectStmt) ReturnUint64s() ([]uint64, error)
ReturnUint64s executes the SelectStmt and returns the value as a slice of uint64s.
func (*SelectStmt) RightJoin ¶
func (b *SelectStmt) RightJoin(table, on interface{}, indexHints ...Builder) *SelectStmt
RightJoin add right-join. on can be Builder or string.
func (*SelectStmt) Rows ¶
func (b *SelectStmt) Rows() (*sql.Rows, error)
Rows executes the query and returns the rows returned, or any error encountered.
func (*SelectStmt) RowsContext ¶
func (*SelectStmt) Suffix ¶
func (b *SelectStmt) Suffix(suffix string, value ...interface{}) *SelectStmt
Suffix adds an expression to the end of the query. This is useful to add dialect-specific clauses like FOR UPDATE
func (*SelectStmt) Where ¶
func (b *SelectStmt) Where(query interface{}, value ...interface{}) *SelectStmt
Where adds a where condition. query can be Builder or string. value is used only if query type is string.
type Session ¶
type Session struct { *Connection EventReceiver Timeout time.Duration }
Session represents a business unit of execution.
All queries in gocraft/dbr are made in the context of a session. This is because when instrumenting your app, it's important to understand which business action the query took place in.
A custom EventReceiver can be set.
Timeout specifies max duration for an operation like Select.
func (*Session) DeleteBySql ¶
func (sess *Session) DeleteBySql(query string, value ...interface{}) *DeleteStmt
DeleteBySql creates a DeleteStmt from raw query.
func (*Session) DeleteFrom ¶
func (sess *Session) DeleteFrom(table string) *DeleteStmt
DeleteFrom creates a DeleteStmt.
func (*Session) GetTimeout ¶
GetTimeout returns current timeout enforced in session.
func (*Session) InsertBySql ¶
func (sess *Session) InsertBySql(query string, value ...interface{}) *InsertStmt
InsertBySql creates an InsertStmt from raw query.
func (*Session) InsertInto ¶
func (sess *Session) InsertInto(table string) *InsertStmt
InsertInto creates an InsertStmt.
func (*Session) Select ¶
func (sess *Session) Select(column ...string) *SelectStmt
Select creates a SelectStmt.
func (*Session) SelectBySql ¶
func (sess *Session) SelectBySql(query string, value ...interface{}) *SelectStmt
SelectBySql creates a SelectStmt from raw query.
func (*Session) Update ¶
func (sess *Session) Update(table string) *UpdateStmt
Update creates an UpdateStmt.
func (*Session) UpdateBySql ¶
func (sess *Session) UpdateBySql(query string, value ...interface{}) *UpdateStmt
UpdateBySql creates an UpdateStmt with raw query.
type SessionRunner ¶
type SessionRunner interface { Select(column ...string) *SelectBuilder SelectBySql(query string, value ...interface{}) *SelectBuilder InsertInto(table string) *InsertBuilder InsertBySql(query string, value ...interface{}) *InsertBuilder Update(table string) *UpdateBuilder UpdateBySql(query string, value ...interface{}) *UpdateBuilder DeleteFrom(table string) *DeleteBuilder DeleteBySql(query string, value ...interface{}) *DeleteBuilder }
SessionRunner can do anything that a Session can except start a transaction. Both Session and Tx implements this interface.
type TracingEventReceiver ¶
type TracingEventReceiver interface { SpanStart(ctx context.Context, eventName, query string) context.Context SpanError(ctx context.Context, err error) SpanFinish(ctx context.Context) }
TracingEventReceiver is an optional interface an EventReceiver type can implement to allow tracing instrumentation
type Tx ¶
Tx is a transaction created by Session.
func (*Tx) DeleteBySql ¶
func (tx *Tx) DeleteBySql(query string, value ...interface{}) *DeleteStmt
DeleteBySql creates a DeleteStmt from raw query.
func (*Tx) DeleteFrom ¶
func (tx *Tx) DeleteFrom(table string) *DeleteStmt
DeleteFrom creates a DeleteStmt.
func (*Tx) GetTimeout ¶
GetTimeout returns timeout enforced in Tx.
func (*Tx) InsertBySql ¶
func (tx *Tx) InsertBySql(query string, value ...interface{}) *InsertStmt
InsertBySql creates an InsertStmt from raw query.
func (*Tx) InsertInto ¶
func (tx *Tx) InsertInto(table string) *InsertStmt
InsertInto creates an InsertStmt.
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) SelectBySql ¶
func (tx *Tx) SelectBySql(query string, value ...interface{}) *SelectStmt
SelectBySql creates a SelectStmt from raw query.
func (*Tx) UpdateBySql ¶
func (tx *Tx) UpdateBySql(query string, value ...interface{}) *UpdateStmt
UpdateBySql creates an UpdateStmt with raw query.
type UpdateBuilder ¶
type UpdateBuilder = UpdateStmt
type UpdateStmt ¶
type UpdateStmt struct { Runner EventReceiver Dialect Table string Value map[string]interface{} WhereCond []Builder ReturnColumn []string LimitCount int64 // contains filtered or unexported fields }
UpdateStmt builds `UPDATE ...`.
func UpdateBySql ¶
func UpdateBySql(query string, value ...interface{}) *UpdateStmt
UpdateBySql creates an UpdateStmt with raw query.
func (*UpdateStmt) Comment ¶
func (b *UpdateStmt) Comment(comment string) *UpdateStmt
func (*UpdateStmt) DecrBy ¶
func (b *UpdateStmt) DecrBy(column string, value interface{}) *UpdateStmt
DecrBy decreases column by value
func (*UpdateStmt) ExecContext ¶
func (*UpdateStmt) IncrBy ¶
func (b *UpdateStmt) IncrBy(column string, value interface{}) *UpdateStmt
IncrBy increases column by value
func (*UpdateStmt) IndexHint ¶
func (b *UpdateStmt) IndexHint(hints ...interface{}) *UpdateStmt
IndexHint adds a index hint. hint can be Builder or string.
func (*UpdateStmt) Limit ¶
func (b *UpdateStmt) Limit(n uint64) *UpdateStmt
func (*UpdateStmt) Load ¶
func (b *UpdateStmt) Load(value interface{}) error
func (*UpdateStmt) LoadContext ¶
func (b *UpdateStmt) LoadContext(ctx context.Context, value interface{}) error
func (*UpdateStmt) Returning ¶
func (b *UpdateStmt) Returning(column ...string) *UpdateStmt
Returning specifies the returning columns for postgres.
func (*UpdateStmt) Set ¶
func (b *UpdateStmt) Set(column string, value interface{}) *UpdateStmt
Set updates column with value.
func (*UpdateStmt) SetMap ¶
func (b *UpdateStmt) SetMap(m map[string]interface{}) *UpdateStmt
SetMap specifies a map of (column, value) to update in bulk.
func (*UpdateStmt) Where ¶
func (b *UpdateStmt) Where(query interface{}, value ...interface{}) *UpdateStmt
Where adds a where condition. query can be Builder or string. value is used only if query type is string.