Documentation ¶
Overview ¶
Package querybuilder provides a fluent SQL generator. This package is derived from Squirrel, an open source fluent SQL generator for Go
Index ¶
- Variables
- func IsUUID(value interface{}) bool
- func Placeholders(count int) string
- type And
- type DeleteBuilder
- func (b DeleteBuilder) From(from string) DeleteBuilder
- func (b DeleteBuilder) GetData() StatementAccessor
- func (b DeleteBuilder) IsCAS() bool
- func (b DeleteBuilder) PlaceholderFormat(f PlaceholderFormat) DeleteBuilder
- func (b DeleteBuilder) StmtType() StmtType
- func (b DeleteBuilder) ToSQL() (string, []interface{}, error)
- func (b DeleteBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
- func (b DeleteBuilder) Using(sql string, args ...interface{}) DeleteBuilder
- func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) DeleteBuilder
- type Eq
- type Gt
- type GtOrEq
- type InsertBuilder
- func (b InsertBuilder) AddSTApplyMetadata(value []byte) InsertBuilder
- func (b InsertBuilder) Columns(columns ...string) InsertBuilder
- func (b InsertBuilder) GetData() StatementAccessor
- func (b InsertBuilder) IfNotExist() InsertBuilder
- func (b InsertBuilder) Into(from string) InsertBuilder
- func (b InsertBuilder) IsCAS() bool
- func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertBuilder
- func (b InsertBuilder) SetMap(clauses map[string]interface{}) InsertBuilder
- func (b InsertBuilder) StmtType() StmtType
- func (b InsertBuilder) ToSQL() (string, []interface{}, error)
- func (b InsertBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
- func (b InsertBuilder) Using(sql string, args ...interface{}) InsertBuilder
- func (b InsertBuilder) Values(values ...interface{}) InsertBuilder
- type Lt
- type LtOrEq
- type NotEq
- type Or
- type PlaceholderFormat
- type SelectBuilder
- func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectBuilder
- func (b SelectBuilder) Columns(columns ...string) SelectBuilder
- func (b SelectBuilder) Distinct() SelectBuilder
- func (b SelectBuilder) From(from string) SelectBuilder
- func (b SelectBuilder) FromSelect(from SelectBuilder, aliasString string) SelectBuilder
- func (b SelectBuilder) GetData() StatementAccessor
- func (b SelectBuilder) GetPageSize() int
- func (b SelectBuilder) GetPagingState() []byte
- func (b SelectBuilder) GroupBy(groupBys ...string) SelectBuilder
- func (b SelectBuilder) Having(pred interface{}, rest ...interface{}) SelectBuilder
- func (b SelectBuilder) IsCAS() bool
- func (b SelectBuilder) IsDisableAutoPaging() bool
- func (b SelectBuilder) Join(join string, rest ...interface{}) SelectBuilder
- func (b SelectBuilder) JoinClause(pred interface{}, args ...interface{}) SelectBuilder
- func (b SelectBuilder) LeftJoin(join string, rest ...interface{}) SelectBuilder
- func (b SelectBuilder) Limit(limit uint64) SelectBuilder
- func (b SelectBuilder) Offset(offset uint64) SelectBuilder
- func (b SelectBuilder) OrderBy(orderBys ...string) SelectBuilder
- func (b SelectBuilder) PageSize(pageSize int) SelectBuilder
- func (b SelectBuilder) PagingState(pagingState []byte) SelectBuilder
- func (b SelectBuilder) PlaceholderFormat(f PlaceholderFormat) SelectBuilder
- func (b SelectBuilder) RightJoin(join string, rest ...interface{}) SelectBuilder
- func (b SelectBuilder) StmtType() StmtType
- func (b SelectBuilder) ToSQL() (string, []interface{}, error)
- func (b SelectBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
- func (b SelectBuilder) Where(pred interface{}, args ...interface{}) SelectBuilder
- type Sqlizer
- type StatementAccessor
- type StatementBuilderType
- func (b StatementBuilderType) Delete(from string) DeleteBuilder
- func (b StatementBuilderType) Insert(into string) InsertBuilder
- func (b StatementBuilderType) PlaceholderFormat(f PlaceholderFormat) StatementBuilderType
- func (b StatementBuilderType) Select(columns ...string) SelectBuilder
- func (b StatementBuilderType) Update(table string) UpdateBuilder
- type StmtType
- type UUID
- type UpdateBuilder
- func (b UpdateBuilder) Add(column string, value interface{}) UpdateBuilder
- func (b UpdateBuilder) AddSTApplyMetadata(value []byte) UpdateBuilder
- func (b UpdateBuilder) GetData() StatementAccessor
- func (b UpdateBuilder) IfOnly(pred interface{}, rest ...interface{}) UpdateBuilder
- func (b UpdateBuilder) IsCAS() bool
- func (b UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) UpdateBuilder
- func (b UpdateBuilder) Remove(column string, value interface{}) UpdateBuilder
- func (b UpdateBuilder) Set(column string, value interface{}) UpdateBuilder
- func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateBuilder
- func (b UpdateBuilder) StmtType() StmtType
- func (b UpdateBuilder) Table(table string) UpdateBuilder
- func (b UpdateBuilder) ToSQL() (string, []interface{}, error)
- func (b UpdateBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
- func (b UpdateBuilder) Using(sql string, args ...interface{}) UpdateBuilder
- func (b UpdateBuilder) Where(pred interface{}, args ...interface{}) UpdateBuilder
Constants ¶
This section is empty.
Variables ¶
var ( // Question is a PlaceholderFormat instance that leaves placeholders as // question marks. Question = questionFormat{} // Dollar is a PlaceholderFormat instance that replaces placeholders with // dollar-prefixed positional placeholders (e.g. $1, $2, $3). Dollar = dollarFormat{} )
var ( // ErrMalformedSetClause indicates that the update is missing a set clause ErrMalformedSetClause = errors.New("update statements must have at least one Set clause") // ErrMissingTable indicates that the update is missing a target table ErrMissingTable = errors.New("update statements must specify a table") )
var StatementBuilder = StatementBuilderType(builder.EmptyBuilder).PlaceholderFormat(Question)
StatementBuilder is a parent builder for other builders, e.g. SelectBuilder.
Functions ¶
func Placeholders ¶
Placeholders returns a string with count ? placeholders joined with commas.
Types ¶
type DeleteBuilder ¶
DeleteBuilder builds SQL DELETE statements.
func Delete ¶
func Delete(from string) DeleteBuilder
Delete returns a new DeleteBuilder with the given table name.
See DeleteBuilder.Table.
func (DeleteBuilder) From ¶
func (b DeleteBuilder) From(from string) DeleteBuilder
From sets the table to be deleted from.
func (DeleteBuilder) GetData ¶
func (b DeleteBuilder) GetData() StatementAccessor
GetData returns the underlying struct as an interface
func (DeleteBuilder) PlaceholderFormat ¶
func (b DeleteBuilder) PlaceholderFormat(f PlaceholderFormat) DeleteBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the delete.
func (DeleteBuilder) StmtType ¶
func (b DeleteBuilder) StmtType() StmtType
StmtType returns type of the statement
func (DeleteBuilder) ToSQL ¶
func (b DeleteBuilder) ToSQL() (string, []interface{}, error)
ToSQL builds the delete into a SQL string and bound args.
func (DeleteBuilder) ToUql ¶
func (b DeleteBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
ToUql builds the delete into a UQL string and bound args. it also returns simple query options
func (DeleteBuilder) Using ¶
func (b DeleteBuilder) Using(sql string, args ...interface{}) DeleteBuilder
Using adds an expression to the end of the query
func (DeleteBuilder) Where ¶
func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) DeleteBuilder
Where adds WHERE expressions to the delete.
See SelectBuilder.Where for more information.
type Eq ¶
type Eq map[string]interface{}
Eq is syntactic sugar for use with Where/Having/Set methods. Ex:
.Where(Eq{"id": 1})
type Gt ¶
type Gt Lt
Gt is syntactic sugar for use with Where/Having/Set methods. Ex:
.Where(Gt{"id": 1}) == "id > 1"
type GtOrEq ¶
type GtOrEq Lt
GtOrEq is syntactic sugar for use with Where/Having/Set methods. Ex:
.Where(GtOrEq{"id": 1}) == "id >= 1"
type InsertBuilder ¶
InsertBuilder builds SQL INSERT statements.
func Insert ¶
func Insert(into string) InsertBuilder
Insert returns a new InsertBuilder with the given table name.
See InsertBuilder.Into.
func (InsertBuilder) AddSTApplyMetadata ¶
func (b InsertBuilder) AddSTApplyMetadata(value []byte) InsertBuilder
AddSTApplyMetadata adds a value for the special st_apply_metadata column.
func (InsertBuilder) Columns ¶
func (b InsertBuilder) Columns(columns ...string) InsertBuilder
Columns adds insert columns to the query.
func (InsertBuilder) GetData ¶
func (b InsertBuilder) GetData() StatementAccessor
GetData returns the underlying struct as an interface
func (InsertBuilder) IfNotExist ¶
func (b InsertBuilder) IfNotExist() InsertBuilder
IfNotExist performs the insert only if the value does not exist.
func (InsertBuilder) Into ¶
func (b InsertBuilder) Into(from string) InsertBuilder
Into sets the INTO clause of the query.
func (InsertBuilder) IsCAS ¶
func (b InsertBuilder) IsCAS() bool
IsCAS returns true is the insert statement has a compare-and-set part
func (InsertBuilder) PlaceholderFormat ¶
func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.
func (InsertBuilder) SetMap ¶
func (b InsertBuilder) SetMap(clauses map[string]interface{}) InsertBuilder
SetMap set columns and values for insert builder from a map of column name and value note that it will reset all previous columns and values was set if any
func (InsertBuilder) StmtType ¶
func (b InsertBuilder) StmtType() StmtType
StmtType returns type of the statement
func (InsertBuilder) ToSQL ¶
func (b InsertBuilder) ToSQL() (string, []interface{}, error)
ToSQL builds the query into a SQL string and bound args.
func (InsertBuilder) ToUql ¶
func (b InsertBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
ToUql builds the query into a UQL string and bound args. As an runtime optimization, it also returns query options
func (InsertBuilder) Using ¶
func (b InsertBuilder) Using(sql string, args ...interface{}) InsertBuilder
Using adds an expression to the end of the query
func (InsertBuilder) Values ¶
func (b InsertBuilder) Values(values ...interface{}) InsertBuilder
Values adds a single row's values to the query.
type Lt ¶
type Lt map[string]interface{}
Lt is syntactic sugar for use with Where/Having/Set methods. Ex:
.Where(Lt{"id": 1})
type LtOrEq ¶
type LtOrEq Lt
LtOrEq is syntactic sugar for use with Where/Having/Set methods. Ex:
.Where(LtOrEq{"id": 1}) == "id <= 1"
type NotEq ¶
type NotEq Eq
NotEq is syntactic sugar for use with Where/Having/Set methods. Ex:
.Where(NotEq{"id": 1}) == "id <> 1"
type PlaceholderFormat ¶
PlaceholderFormat is the interface that wraps the ReplacePlaceholders method.
ReplacePlaceholders takes a SQL statement and replaces each question mark placeholder with a (possibly different) SQL placeholder.
type SelectBuilder ¶
SelectBuilder builds SQL SELECT statements.
func Select ¶
func Select(columns ...string) SelectBuilder
Select returns a new SelectBuilder, optionally setting some result columns.
See SelectBuilder.Columns.
func (SelectBuilder) Column ¶
func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectBuilder
Column adds a result column to the query. Unlike Columns, Column accepts args which will be bound to placeholders in the columns string, for example:
Column("IF(col IN ("+squirrel.Placeholders(3)+"), 1, 0) as col", 1, 2, 3)
func (SelectBuilder) Columns ¶
func (b SelectBuilder) Columns(columns ...string) SelectBuilder
Columns adds result columns to the query.
func (SelectBuilder) Distinct ¶
func (b SelectBuilder) Distinct() SelectBuilder
Distinct adds a DISTINCT clause to the query.
func (SelectBuilder) From ¶
func (b SelectBuilder) From(from string) SelectBuilder
From sets the FROM clause of the query.
func (SelectBuilder) FromSelect ¶
func (b SelectBuilder) FromSelect(from SelectBuilder, aliasString string) SelectBuilder
FromSelect sets a subquery into the FROM clause of the query.
func (SelectBuilder) GetData ¶
func (b SelectBuilder) GetData() StatementAccessor
GetData returns the underlying struct as an interface
func (SelectBuilder) GetPageSize ¶
func (b SelectBuilder) GetPageSize() int
GetPageSize returns true the size of the page
func (SelectBuilder) GetPagingState ¶
func (b SelectBuilder) GetPagingState() []byte
GetPagingState returns true the continuation token
func (SelectBuilder) GroupBy ¶
func (b SelectBuilder) GroupBy(groupBys ...string) SelectBuilder
GroupBy adds GROUP BY expressions to the query.
func (SelectBuilder) Having ¶
func (b SelectBuilder) Having(pred interface{}, rest ...interface{}) SelectBuilder
Having adds an expression to the HAVING clause of the query.
See Where.
func (SelectBuilder) IsDisableAutoPaging ¶
func (b SelectBuilder) IsDisableAutoPaging() bool
IsDisableAutoPaging returns true if the select statement disable auto-paging
func (SelectBuilder) Join ¶
func (b SelectBuilder) Join(join string, rest ...interface{}) SelectBuilder
Join adds a JOIN clause to the query.
func (SelectBuilder) JoinClause ¶
func (b SelectBuilder) JoinClause(pred interface{}, args ...interface{}) SelectBuilder
JoinClause adds a join clause to the query.
func (SelectBuilder) LeftJoin ¶
func (b SelectBuilder) LeftJoin(join string, rest ...interface{}) SelectBuilder
LeftJoin adds a LEFT JOIN clause to the query.
func (SelectBuilder) Limit ¶
func (b SelectBuilder) Limit(limit uint64) SelectBuilder
Limit sets a LIMIT clause on the query.
func (SelectBuilder) Offset ¶
func (b SelectBuilder) Offset(offset uint64) SelectBuilder
Offset sets a OFFSET clause on the query.
func (SelectBuilder) OrderBy ¶
func (b SelectBuilder) OrderBy(orderBys ...string) SelectBuilder
OrderBy adds ORDER BY expressions to the query.
func (SelectBuilder) PageSize ¶
func (b SelectBuilder) PageSize(pageSize int) SelectBuilder
PageSize sets size of rows the query should fetch a time
func (SelectBuilder) PagingState ¶
func (b SelectBuilder) PagingState(pagingState []byte) SelectBuilder
PagingState sets a continuation token on where the query will resume for the next page.
func (SelectBuilder) PlaceholderFormat ¶
func (b SelectBuilder) PlaceholderFormat(f PlaceholderFormat) SelectBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.
func (SelectBuilder) RightJoin ¶
func (b SelectBuilder) RightJoin(join string, rest ...interface{}) SelectBuilder
RightJoin adds a RIGHT JOIN clause to the query.
func (SelectBuilder) StmtType ¶
func (b SelectBuilder) StmtType() StmtType
StmtType returns type of the statement
func (SelectBuilder) ToSQL ¶
func (b SelectBuilder) ToSQL() (string, []interface{}, error)
ToSQL builds the query into a SQL string and bound args.
func (SelectBuilder) ToUql ¶
func (b SelectBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
ToUql builds the query into a UQL string and bound args. As an runtime optimization, it also returns query options
func (SelectBuilder) Where ¶
func (b SelectBuilder) Where(pred interface{}, args ...interface{}) SelectBuilder
Where adds an expression to the WHERE clause of the query.
Expressions are ANDed together in the generated SQL.
Where accepts several types for its pred argument:
nil OR "" - ignored.
string - SQL expression. If the expression has SQL placeholders then a set of arguments must be passed as well, one for each placeholder.
map[string]interface{} OR Eq - map of SQL expressions to values. Each key is transformed into an expression like "<key> = ?", with the corresponding value bound to the placeholder. If the value is nil, the expression will be "<key> IS NULL". If the value is an array or slice, the expression will be "<key> IN (?,?,...)", with one placeholder for each item in the value. These expressions are ANDed together.
Where will panic if pred isn't any of the above types.
type Sqlizer ¶
Sqlizer is the interface that wraps the ToSql method. ToSQL returns a SQL representation of the Sqlizer, along with a slice of args as passed to e.g. database/sql.Exec. It can also return an error.
type StatementAccessor ¶
type StatementAccessor interface { // GetWhereParts gets the where clause of this statement GetWhereParts() []Sqlizer // GetResource gets the resource type (that is, the table) on which this statement operates GetResource() string // GetColumns returns the selected columns and is applicable for select stmt only. Other types // of statements will return nil GetColumns() []Sqlizer }
StatementAccessor provides an interface to access statement internals
type StatementBuilderType ¶
StatementBuilderType is the type of StatementBuilder.
func (StatementBuilderType) Delete ¶
func (b StatementBuilderType) Delete(from string) DeleteBuilder
Delete returns a DeleteBuilder for this StatementBuilderType.
func (StatementBuilderType) Insert ¶
func (b StatementBuilderType) Insert(into string) InsertBuilder
Insert returns a InsertBuilder for this StatementBuilderType.
func (StatementBuilderType) PlaceholderFormat ¶
func (b StatementBuilderType) PlaceholderFormat(f PlaceholderFormat) StatementBuilderType
PlaceholderFormat sets the PlaceholderFormat field for any child builders.
func (StatementBuilderType) Select ¶
func (b StatementBuilderType) Select(columns ...string) SelectBuilder
Select returns a SelectBuilder for this StatementBuilderType.
func (StatementBuilderType) Update ¶
func (b StatementBuilderType) Update(table string) UpdateBuilder
Update returns a UpdateBuilder for this StatementBuilderType.
type UpdateBuilder ¶
UpdateBuilder builds SQL UPDATE statements.
func Update ¶
func Update(table string) UpdateBuilder
Update returns a new UpdateBuilder with the given table name.
See UpdateBuilder.Table.
func (UpdateBuilder) Add ¶
func (b UpdateBuilder) Add(column string, value interface{}) UpdateBuilder
Add appends a value to a list column.
func (UpdateBuilder) AddSTApplyMetadata ¶
func (b UpdateBuilder) AddSTApplyMetadata(value []byte) UpdateBuilder
AddSTApplyMetadata adds a value for the special st_apply_metadata column.
func (UpdateBuilder) GetData ¶
func (b UpdateBuilder) GetData() StatementAccessor
GetData returns the underlying struct as an interface
func (UpdateBuilder) IfOnly ¶
func (b UpdateBuilder) IfOnly(pred interface{}, rest ...interface{}) UpdateBuilder
IfOnly represents a LWT
func (UpdateBuilder) IsCAS ¶
func (b UpdateBuilder) IsCAS() bool
IsCAS returns true is the update statement has a compare-and-set part
func (UpdateBuilder) PlaceholderFormat ¶
func (b UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) UpdateBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the update.
func (UpdateBuilder) Remove ¶
func (b UpdateBuilder) Remove(column string, value interface{}) UpdateBuilder
Remove discards a value from a list column
func (UpdateBuilder) Set ¶
func (b UpdateBuilder) Set(column string, value interface{}) UpdateBuilder
Set adds SET clauses to the update.
func (UpdateBuilder) SetMap ¶
func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateBuilder
SetMap is a convenience method which calls .Set for each key/value pair in clauses.
func (UpdateBuilder) StmtType ¶
func (b UpdateBuilder) StmtType() StmtType
StmtType returns type of the statement
func (UpdateBuilder) Table ¶
func (b UpdateBuilder) Table(table string) UpdateBuilder
Table sets the table to be updated.
func (UpdateBuilder) ToSQL ¶
func (b UpdateBuilder) ToSQL() (string, []interface{}, error)
ToSQL builds the update into a SQL string and bound args.
func (UpdateBuilder) ToUql ¶
func (b UpdateBuilder) ToUql() (query string, args []interface{}, options map[string]interface{}, err error)
ToUql builds the query into a UQL string and bound args. As an runtime optimization, it also returns query options
func (UpdateBuilder) Using ¶
func (b UpdateBuilder) Using(sql string, args ...interface{}) UpdateBuilder
Using adds an expression to the end of the update.
func (UpdateBuilder) Where ¶
func (b UpdateBuilder) Where(pred interface{}, args ...interface{}) UpdateBuilder
Where adds WHERE expressions to the update.
See SelectBuilder.Where for more information.