Documentation ¶
Overview ¶
Package squirrel provides a fluent SQL generator.
See https://github.com/Masterminds/squirrel for examples.
Index ¶
- Variables
- type BaseRunner
- type Execer
- type PlaceholderFormat
- type QueryRower
- type Queryer
- type Row
- type RowScanner
- type Runner
- type SelectBuilder
- func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectBuilder
- func (b SelectBuilder) Columns(columns ...string) SelectBuilder
- func (b SelectBuilder) CrossJoin(join string, rest ...interface{}) SelectBuilder
- func (b SelectBuilder) Distinct() SelectBuilder
- func (b SelectBuilder) Exec() (sql.Result, error)
- func (b SelectBuilder) From(from string) SelectBuilder
- func (b SelectBuilder) FromSelect(from SelectBuilder, alias string) SelectBuilder
- func (b SelectBuilder) GroupBy(groupBys ...string) SelectBuilder
- func (b SelectBuilder) Having(pred interface{}, rest ...interface{}) SelectBuilder
- func (b SelectBuilder) InnerJoin(join string, rest ...interface{}) SelectBuilder
- 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) MustSql() (string, []interface{})
- func (b SelectBuilder) Offset(offset uint64) SelectBuilder
- func (b SelectBuilder) Options(options ...string) SelectBuilder
- func (b SelectBuilder) OrderBy(orderBys ...string) SelectBuilder
- func (b SelectBuilder) OrderByClause(pred interface{}, args ...interface{}) SelectBuilder
- func (b SelectBuilder) PlaceholderFormat(f PlaceholderFormat) SelectBuilder
- func (b SelectBuilder) Prefix(sql string, args ...interface{}) SelectBuilder
- func (b SelectBuilder) PrefixExpr(expr Sqlizer) SelectBuilder
- func (b SelectBuilder) Query() (*sql.Rows, error)
- func (b SelectBuilder) QueryRow() RowScanner
- func (b SelectBuilder) RemoveColumns() SelectBuilder
- func (b SelectBuilder) RemoveLimit() SelectBuilder
- func (b SelectBuilder) RemoveOffset() SelectBuilder
- func (b SelectBuilder) RightJoin(join string, rest ...interface{}) SelectBuilder
- func (b SelectBuilder) RunWith(runner BaseRunner) SelectBuilder
- func (b SelectBuilder) Scan(dest ...interface{}) error
- func (b SelectBuilder) Suffix(sql string, args ...interface{}) SelectBuilder
- func (b SelectBuilder) SuffixExpr(expr Sqlizer) SelectBuilder
- func (b SelectBuilder) ToSql() (string, []interface{}, error)
- func (b SelectBuilder) Union(query interface{}, args ...interface{}) SelectBuilder
- func (b SelectBuilder) UnionAll(query interface{}, args ...interface{}) SelectBuilder
- func (b SelectBuilder) Where(pred interface{}, args ...interface{}) SelectBuilder
- type Sqlizer
- type StatementBuilderType
- func (b StatementBuilderType) Delete(from string) sq.DeleteBuilder
- func (b StatementBuilderType) PlaceholderFormat(f PlaceholderFormat) StatementBuilderType
- func (b StatementBuilderType) RunWith(runner BaseRunner) StatementBuilderType
- func (b StatementBuilderType) Select(columns ...string) SelectBuilder
- func (b StatementBuilderType) Update(table string) sq.UpdateBuilder
- func (b StatementBuilderType) Where(pred interface{}, args ...interface{}) StatementBuilderType
- type StdSql
Constants ¶
This section is empty.
Variables ¶
var ( // Dollar is a PlaceholderFormat instance that replaces placeholders with // dollar-prefixed positional placeholders (e.g. $1, $2, $3). Dollar = dollarFormat{} )
var StatementBuilder = StatementBuilderType(builder.EmptyBuilder).PlaceholderFormat(Dollar)
StatementBuilder is a parent builder for other builders, e.g. SelectBuilder.
Functions ¶
This section is empty.
Types ¶
type BaseRunner ¶
BaseRunner groups the Execer and Queryer interfaces.
type Execer ¶
Execer is the interface that wraps the Exec method.
Exec executes the given query as implemented by database/sql.Exec.
type PlaceholderFormat ¶
PlaceholderFormat is the interface that wraps the ReplacePlaceholders method.
ReplacePlaceholders takes an SQL statement and replaces each question mark placeholder with a (possibly different) SQL placeholder.
type QueryRower ¶
type QueryRower interface {
QueryRow(query string, args ...interface{}) RowScanner
}
QueryRower is the interface that wraps the QueryRow method.
QueryRow executes the given query as implemented by database/sql.QueryRow.
type Queryer ¶
Queryer is the interface that wraps the Query method.
Query executes the given query as implemented by database/sql.Query.
type Row ¶
type Row struct { RowScanner // contains filtered or unexported fields }
Row wraps database/sql.Row to let squirrel return new errors on Scan.
type RowScanner ¶
type RowScanner interface {
Scan(...interface{}) error
}
RowScanner is the interface that wraps the Scan method.
Scan behaves like database/sql.Row.Scan.
type Runner ¶
type Runner interface { Execer Queryer QueryRower }
Runner groups the Execer, Queryer, and QueryRower interfaces.
func WrapStdSql ¶
WrapStdSql wraps a type implementing the standard SQL interface with methods that squirrel expects.
type SelectBuilder ¶
SelectBuilder builds SQL SELECT statements.
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 add result columns to the query.
func (SelectBuilder) CrossJoin ¶
func (b SelectBuilder) CrossJoin(join string, rest ...interface{}) SelectBuilder
CrossJoin adds a CROSS JOIN clause to the query.
func (SelectBuilder) Distinct ¶
func (b SelectBuilder) Distinct() SelectBuilder
Distinct adds a DISTINCT clause to the query.
func (SelectBuilder) Exec ¶
func (b SelectBuilder) Exec() (sql.Result, error)
Exec builds and Execs the query with the Runner set by RunWith.
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, alias string) SelectBuilder
FromSelect sets a sub-query into the FROM clause of the query.
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) InnerJoin ¶
func (b SelectBuilder) InnerJoin(join string, rest ...interface{}) SelectBuilder
InnerJoin adds an INNER JOIN clause to the query.
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) MustSql ¶
func (b SelectBuilder) MustSql() (string, []interface{})
MustSql builds the query into an SQL string and bound args. It panics if there are any errors.
func (SelectBuilder) Offset ¶
func (b SelectBuilder) Offset(offset uint64) SelectBuilder
Offset sets a OFFSET clause on the query.
func (SelectBuilder) Options ¶
func (b SelectBuilder) Options(options ...string) SelectBuilder
Options add a select option to the query
func (SelectBuilder) OrderBy ¶
func (b SelectBuilder) OrderBy(orderBys ...string) SelectBuilder
OrderBy adds ORDER BY expressions to the query.
func (SelectBuilder) OrderByClause ¶
func (b SelectBuilder) OrderByClause(pred interface{}, args ...interface{}) SelectBuilder
OrderByClause adds ORDER BY clause to the query.
func (SelectBuilder) PlaceholderFormat ¶
func (b SelectBuilder) PlaceholderFormat(f PlaceholderFormat) SelectBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.
func (SelectBuilder) Prefix ¶
func (b SelectBuilder) Prefix(sql string, args ...interface{}) SelectBuilder
Prefix adds an expression to the beginning of the query
func (SelectBuilder) PrefixExpr ¶
func (b SelectBuilder) PrefixExpr(expr Sqlizer) SelectBuilder
PrefixExpr adds an expression to the very beginning of the query
func (SelectBuilder) Query ¶
func (b SelectBuilder) Query() (*sql.Rows, error)
Query builds and queries the query with the Runner set by RunWith.
func (SelectBuilder) QueryRow ¶
func (b SelectBuilder) QueryRow() RowScanner
QueryRow builds and QueryRows the query with the Runner set by RunWith.
func (SelectBuilder) RemoveColumns ¶
func (b SelectBuilder) RemoveColumns() SelectBuilder
RemoveColumns remove all columns from the query. Must add a new column with Column or Columns methods, otherwise return an error.
func (SelectBuilder) RemoveLimit ¶
func (b SelectBuilder) RemoveLimit() SelectBuilder
RemoveLimit ALL allows accessing all records with limit
func (SelectBuilder) RemoveOffset ¶
func (b SelectBuilder) RemoveOffset() SelectBuilder
RemoveOffset removes OFFSET clause.
func (SelectBuilder) RightJoin ¶
func (b SelectBuilder) RightJoin(join string, rest ...interface{}) SelectBuilder
RightJoin adds a RIGHT JOIN clause to the query.
func (SelectBuilder) RunWith ¶
func (b SelectBuilder) RunWith(runner BaseRunner) SelectBuilder
RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec. For most cases runner will be a database connection.
Internally, we use this to mock out the database connection for testing.
func (SelectBuilder) Scan ¶
func (b SelectBuilder) Scan(dest ...interface{}) error
Scan is a shortcut for QueryRow().Scan.
func (SelectBuilder) Suffix ¶
func (b SelectBuilder) Suffix(sql string, args ...interface{}) SelectBuilder
Suffix adds an expression to the end of the query
func (SelectBuilder) SuffixExpr ¶
func (b SelectBuilder) SuffixExpr(expr Sqlizer) SelectBuilder
SuffixExpr adds an expression to the end of the query
func (SelectBuilder) ToSql ¶
func (b SelectBuilder) ToSql() (string, []interface{}, error)
ToSql builds the query into an SQL string and bound args.
func (SelectBuilder) Union ¶
func (b SelectBuilder) Union(query interface{}, args ...interface{}) SelectBuilder
Union adds a UNION clause to the query
func (SelectBuilder) UnionAll ¶
func (b SelectBuilder) UnionAll(query interface{}, args ...interface{}) SelectBuilder
UnionAll adds a UNION ALL clause to the query
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 an 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 StatementBuilderType ¶
StatementBuilderType is the type of StatementBuilder.
func (StatementBuilderType) Delete ¶
func (b StatementBuilderType) Delete(from string) sq.DeleteBuilder
Delete returns a DeleteBuilder for this StatementBuilderType.
func (StatementBuilderType) PlaceholderFormat ¶
func (b StatementBuilderType) PlaceholderFormat(f PlaceholderFormat) StatementBuilderType
PlaceholderFormat sets the PlaceholderFormat field for any child builders.
func (StatementBuilderType) RunWith ¶
func (b StatementBuilderType) RunWith(runner BaseRunner) StatementBuilderType
RunWith sets the RunWith 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) sq.UpdateBuilder
Update returns an UpdateBuilder for this StatementBuilderType.
func (StatementBuilderType) Where ¶
func (b StatementBuilderType) Where(pred interface{}, args ...interface{}) StatementBuilderType
Where adds WHERE expressions to the query.
See SelectBuilder.Where for more information.