Documentation ¶
Overview ¶
Package squirrel provides a fluent SQL generator.
See https://github.com/MindMayhem/squirrel for examples.
Index ¶
- Variables
- func Alias(expr Sqlizer, alias string) aliasExpr
- func ConcatExpr(parts ...interface{}) concatExpr
- func DebugSqlizer(s Sqlizer) string
- func Exec(ctx context.Context, db ExecerContext, s Sqlizer) (res pgconn.CommandTag, err error)
- func ExecWithNoContext(db Execer, s Sqlizer) (res pgconn.CommandTag, err error)
- func Placeholders(count int) string
- func Query(ctx context.Context, db QueryerContext, s Sqlizer) (rows pgx.Rows, err error)
- func QueryRow(ctx context.Context, db QueryRowerContext, s Sqlizer) pgx.Row
- func QueryWithNoContext(db Queryer, s Sqlizer) (rows pgx.Rows, err error)
- type And
- type ArrayContains
- type BaseRunner
- type Between
- type CaseBuilder
- type DeleteBuilder
- func (b DeleteBuilder) Exec(ctx context.Context) (pgconn.CommandTag, error)
- func (b DeleteBuilder) From(from string) DeleteBuilder
- func (b DeleteBuilder) Limit(limit uint64) DeleteBuilder
- func (b DeleteBuilder) MustSql() (string, []interface{})
- func (b DeleteBuilder) Offset(offset uint64) DeleteBuilder
- func (b DeleteBuilder) OrderBy(orderBys ...string) DeleteBuilder
- func (b DeleteBuilder) PlaceholderFormat(f PlaceholderFormat) DeleteBuilder
- func (b DeleteBuilder) Prefix(sql string, args ...interface{}) DeleteBuilder
- func (b DeleteBuilder) PrefixExpr(expr Sqlizer) DeleteBuilder
- func (b DeleteBuilder) Query(ctx context.Context) (pgx.Rows, error)
- func (b DeleteBuilder) QueryRow(ctx context.Context) RowScanner
- func (b DeleteBuilder) RunWith(runner RunnerContext) DeleteBuilder
- func (b DeleteBuilder) Scan(ctx context.Context, dest ...interface{}) error
- func (b DeleteBuilder) Suffix(sql string, args ...interface{}) DeleteBuilder
- func (b DeleteBuilder) SuffixExpr(expr Sqlizer) DeleteBuilder
- func (b DeleteBuilder) ToSql() (string, []interface{}, error)
- func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) DeleteBuilder
- type Eq
- type Execer
- type ExecerContext
- type Gt
- type GtOrEq
- type ILike
- type InsertBuilder
- func (b InsertBuilder) Columns(columns ...string) InsertBuilder
- func (b InsertBuilder) Exec(ctx context.Context) (pgconn.CommandTag, error)
- func (b InsertBuilder) Into(from string) InsertBuilder
- func (b InsertBuilder) MustSql() (string, []interface{})
- func (b InsertBuilder) Options(options ...string) InsertBuilder
- func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertBuilder
- func (b InsertBuilder) Prefix(sql string, args ...interface{}) InsertBuilder
- func (b InsertBuilder) PrefixExpr(expr Sqlizer) InsertBuilder
- func (b InsertBuilder) Query(ctx context.Context) (pgx.Rows, error)
- func (b InsertBuilder) QueryRow(ctx context.Context) RowScanner
- func (b InsertBuilder) RunWith(runner RunnerContext) InsertBuilder
- func (b InsertBuilder) Scan(ctx context.Context, dest ...interface{}) error
- func (b InsertBuilder) Select(sb SelectBuilder) InsertBuilder
- func (b InsertBuilder) SetMap(clauses map[string]interface{}) InsertBuilder
- func (b InsertBuilder) Suffix(sql string, args ...interface{}) InsertBuilder
- func (b InsertBuilder) SuffixExpr(expr Sqlizer) InsertBuilder
- func (b InsertBuilder) ToSql() (string, []interface{}, error)
- func (b InsertBuilder) Values(values ...interface{}) InsertBuilder
- type Like
- type Lt
- type LtOrEq
- type NotEq
- type NotILike
- type NotLike
- type Or
- type PlaceholderFormat
- type QueryRower
- type QueryRowerContext
- type Queryer
- type QueryerContext
- type Row
- type RowScanner
- type Runner
- type RunnerContext
- 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(ctx context.Context) (pgconn.CommandTag, 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(ctx context.Context) (pgx.Rows, error)
- func (b SelectBuilder) QueryRow(ctx context.Context) 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 RunnerContext) SelectBuilder
- func (b SelectBuilder) Scan(ctx context.Context, 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) Where(pred interface{}, args ...interface{}) SelectBuilder
- type Sqlizer
- 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) Replace(into string) InsertBuilder
- func (b StatementBuilderType) RunWith(runner RunnerContext) StatementBuilderType
- func (b StatementBuilderType) Select(columns ...string) SelectBuilder
- func (b StatementBuilderType) Update(table string) UpdateBuilder
- func (b StatementBuilderType) Where(pred interface{}, args ...interface{}) StatementBuilderType
- type StdSql
- type StdSqlCtx
- type UpdateBuilder
- func (b UpdateBuilder) Exec(ctx context.Context) (pgconn.CommandTag, error)
- func (b UpdateBuilder) Limit(limit uint64) UpdateBuilder
- func (b UpdateBuilder) MustSql() (string, []interface{})
- func (b UpdateBuilder) Offset(offset uint64) UpdateBuilder
- func (b UpdateBuilder) OrderBy(orderBys ...string) UpdateBuilder
- func (b UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) UpdateBuilder
- func (b UpdateBuilder) Prefix(sql string, args ...interface{}) UpdateBuilder
- func (b UpdateBuilder) PrefixExpr(expr Sqlizer) UpdateBuilder
- func (b UpdateBuilder) Query(ctx context.Context) (pgx.Rows, error)
- func (b UpdateBuilder) QueryRow(ctx context.Context) pgx.Row
- func (b UpdateBuilder) RunWith(runner RunnerContext) UpdateBuilder
- func (b UpdateBuilder) Scan(ctx context.Context, dest ...interface{}) error
- func (b UpdateBuilder) Set(column string, value interface{}) UpdateBuilder
- func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateBuilder
- func (b UpdateBuilder) Suffix(sql string, args ...interface{}) UpdateBuilder
- func (b UpdateBuilder) SuffixExpr(expr Sqlizer) UpdateBuilder
- func (b UpdateBuilder) Table(table string) UpdateBuilder
- func (b UpdateBuilder) ToSql() (string, []interface{}, error)
- 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{} // Colon is a PlaceholderFormat instance that replaces placeholders with // colon-prefixed positional placeholders (e.g. :1, :2, :3). Colon = colonFormat{} // AtP is a PlaceholderFormat instance that replaces placeholders with // "@p"-prefixed positional placeholders (e.g. @p1, @p2, @p3). AtP = atpFormat{} )
var NoContextSupport = errors.New("DB does not support Context")
NoContextSupport is returned if a db doesn't support Context.
var RunnerNotQueryRunner = fmt.Errorf("cannot QueryRow; Runner is not a QueryRower")
RunnerNotQueryRunner is returned by QueryRow if the RunWith value doesn't implement QueryRower.
var RunnerNotSet = fmt.Errorf("cannot run; no Runner set (RunWith)")
RunnerNotSet is returned by methods that need a Runner if it isn't set.
var StatementBuilder = StatementBuilderType(builder.EmptyBuilder).PlaceholderFormat(Question)
StatementBuilder is a parent builder for other builders, e.g. SelectBuilder.
Functions ¶
func Alias ¶
Alias allows to define alias for column in SelectBuilder. Useful when column is defined as complex expression like IF or CASE Ex:
.Column(Alias(caseStmt, "case_column"))
func ConcatExpr ¶
func ConcatExpr(parts ...interface{}) concatExpr
ConcatExpr builds an expression by concatenating strings and other expressions.
Ex:
name_expr := Expr("CONCAT(?, ' ', ?)", firstName, lastName) ConcatExpr("COALESCE(full_name,", name_expr, ")")
func DebugSqlizer ¶
DebugSqlizer calls ToSql on s and shows the approximate SQL to be executed
If ToSql returns an error, the result of this method will look like: "[ToSql error: %s]" or "[DebugSqlizer error: %s]"
IMPORTANT: As its name suggests, this function should only be used for debugging. While the string result *might* be valid SQL, this function does not try very hard to ensure it. Additionally, executing the output of this function with any untrusted user input is certainly insecure.
func Exec ¶
func Exec(ctx context.Context, db ExecerContext, s Sqlizer) (res pgconn.CommandTag, err error)
Exec ExecContexts the SQL returned by s with db.
func ExecWithNoContext ¶
func ExecWithNoContext(db Execer, s Sqlizer) (res pgconn.CommandTag, err error)
ExecWith Execs the SQL returned by s with db.
func Placeholders ¶
Placeholders returns a string with count ? placeholders joined with commas.
func Query ¶
func Query(ctx context.Context, db QueryerContext, s Sqlizer) (rows pgx.Rows, err error)
Query QueryContexts the SQL returned by s with db.
func QueryRow ¶
func QueryRow(ctx context.Context, db QueryRowerContext, s Sqlizer) pgx.Row
QueryRow QueryRowContexts the SQL returned by s with db.
func QueryWithNoContext ¶
QueryWith Querys the SQL returned by s with db.
Types ¶
type ArrayContains ¶
type ArrayContains map[string]interface{}
ArrayContains is syntactic sugar for use with Arrays
func (ArrayContains) ToSql ¶
func (ac ArrayContains) ToSql() (sql string, args []interface{}, err error)
type BaseRunner ¶
BaseRunner groups the Execer and Queryer interfaces.
type CaseBuilder ¶
CaseBuilder builds SQL CASE construct which could be used as parts of queries.
func Case ¶
func Case(what ...interface{}) CaseBuilder
Case returns a new CaseBuilder "what" represents case value
func (CaseBuilder) Else ¶
func (b CaseBuilder) Else(expr interface{}) CaseBuilder
What sets optional "ELSE ..." part for CASE construct
func (CaseBuilder) MustSql ¶
func (b CaseBuilder) MustSql() (string, []interface{})
MustSql builds the query into a SQL string and bound args. It panics if there are any errors.
func (CaseBuilder) ToSql ¶
func (b CaseBuilder) ToSql() (string, []interface{}, error)
ToSql builds the query into a SQL string and bound args.
func (CaseBuilder) When ¶
func (b CaseBuilder) When(when interface{}, then interface{}) CaseBuilder
When adds "WHEN ... THEN ..." part to CASE construct
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) Exec ¶
func (b DeleteBuilder) Exec(ctx context.Context) (pgconn.CommandTag, error)
ExecContext builds and ExecContexts the query with the Runner set by RunWith.
func (DeleteBuilder) From ¶
func (b DeleteBuilder) From(from string) DeleteBuilder
From sets the table to be deleted from.
func (DeleteBuilder) Limit ¶
func (b DeleteBuilder) Limit(limit uint64) DeleteBuilder
Limit sets a LIMIT clause on the query.
func (DeleteBuilder) MustSql ¶
func (b DeleteBuilder) MustSql() (string, []interface{})
MustSql builds the query into a SQL string and bound args. It panics if there are any errors.
func (DeleteBuilder) Offset ¶
func (b DeleteBuilder) Offset(offset uint64) DeleteBuilder
Offset sets a OFFSET clause on the query.
func (DeleteBuilder) OrderBy ¶
func (b DeleteBuilder) OrderBy(orderBys ...string) DeleteBuilder
OrderBy adds ORDER BY expressions to the query.
func (DeleteBuilder) PlaceholderFormat ¶
func (b DeleteBuilder) PlaceholderFormat(f PlaceholderFormat) DeleteBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.
func (DeleteBuilder) Prefix ¶
func (b DeleteBuilder) Prefix(sql string, args ...interface{}) DeleteBuilder
Prefix adds an expression to the beginning of the query
func (DeleteBuilder) PrefixExpr ¶
func (b DeleteBuilder) PrefixExpr(expr Sqlizer) DeleteBuilder
PrefixExpr adds an expression to the very beginning of the query
func (DeleteBuilder) Query ¶
func (b DeleteBuilder) Query(ctx context.Context) (pgx.Rows, error)
QueryContext builds and QueryContexts the query with the Runner set by RunWith.
func (DeleteBuilder) QueryRow ¶
func (b DeleteBuilder) QueryRow(ctx context.Context) RowScanner
QueryRowContext builds and QueryRowContexts the query with the Runner set by RunWith.
func (DeleteBuilder) RunWith ¶
func (b DeleteBuilder) RunWith(runner RunnerContext) DeleteBuilder
RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
func (DeleteBuilder) Scan ¶
func (b DeleteBuilder) Scan(ctx context.Context, dest ...interface{}) error
ScanContext is a shortcut for QueryRowContext().Scan.
func (DeleteBuilder) Suffix ¶
func (b DeleteBuilder) Suffix(sql string, args ...interface{}) DeleteBuilder
Suffix adds an expression to the end of the query
func (DeleteBuilder) SuffixExpr ¶
func (b DeleteBuilder) SuffixExpr(expr Sqlizer) DeleteBuilder
SuffixExpr adds an expression to the end of the query
func (DeleteBuilder) ToSql ¶
func (b DeleteBuilder) ToSql() (string, []interface{}, error)
ToSql builds the query into a SQL string and bound args.
func (DeleteBuilder) Where ¶
func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) DeleteBuilder
Where adds WHERE expressions to the query.
See SelectBuilder.Where for more information.
type Eq ¶
type Eq map[string]interface{}
Eq is syntactic sugar for use with Where/Having/Set methods.
type Execer ¶
type Execer interface {
ExecNoContext(query string, args ...interface{}) (pgconn.CommandTag, error)
}
Execer is the interface that wraps the Exec method.
Exec executes the given query as implemented by database/sql.Exec.
type ExecerContext ¶
type ExecerContext interface {
Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)
}
ExecerContext is the interface that wraps the ExecContext method.
Exec executes the given query as implemented by database/sql.ExecContext.
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 ILike ¶
type ILike Like
ILike is syntactic sugar for use with ILIKE conditions. Ex:
.Where(ILike{"name": "sq%"})
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 Replace ¶
func Replace(into string) InsertBuilder
Replace returns a new InsertBuilder with the statement keyword set to "REPLACE" and with the given table name.
See InsertBuilder.Into.
func (InsertBuilder) Columns ¶
func (b InsertBuilder) Columns(columns ...string) InsertBuilder
Columns adds insert columns to the query.
func (InsertBuilder) Exec ¶
func (b InsertBuilder) Exec(ctx context.Context) (pgconn.CommandTag, error)
ExecContext builds and ExecContexts the query with the Runner set by RunWith.
func (InsertBuilder) Into ¶
func (b InsertBuilder) Into(from string) InsertBuilder
Into sets the INTO clause of the query.
func (InsertBuilder) MustSql ¶
func (b InsertBuilder) MustSql() (string, []interface{})
MustSql builds the query into a SQL string and bound args. It panics if there are any errors.
func (InsertBuilder) Options ¶
func (b InsertBuilder) Options(options ...string) InsertBuilder
Options adds keyword options before the INTO clause of the query.
func (InsertBuilder) PlaceholderFormat ¶
func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.
func (InsertBuilder) Prefix ¶
func (b InsertBuilder) Prefix(sql string, args ...interface{}) InsertBuilder
Prefix adds an expression to the beginning of the query
func (InsertBuilder) PrefixExpr ¶
func (b InsertBuilder) PrefixExpr(expr Sqlizer) InsertBuilder
PrefixExpr adds an expression to the very beginning of the query
func (InsertBuilder) Query ¶
func (b InsertBuilder) Query(ctx context.Context) (pgx.Rows, error)
QueryContext builds and QueryContexts the query with the Runner set by RunWith.
func (InsertBuilder) QueryRow ¶
func (b InsertBuilder) QueryRow(ctx context.Context) RowScanner
QueryRowContext builds and QueryRowContexts the query with the Runner set by RunWith.
func (InsertBuilder) RunWith ¶
func (b InsertBuilder) RunWith(runner RunnerContext) InsertBuilder
RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
func (InsertBuilder) Scan ¶
func (b InsertBuilder) Scan(ctx context.Context, dest ...interface{}) error
ScanContext is a shortcut for QueryRowContext().Scan.
func (InsertBuilder) Select ¶
func (b InsertBuilder) Select(sb SelectBuilder) InsertBuilder
Select set Select clause for insert query If Values and Select are used, then Select has higher priority
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) Suffix ¶
func (b InsertBuilder) Suffix(sql string, args ...interface{}) InsertBuilder
Suffix adds an expression to the end of the query
func (InsertBuilder) SuffixExpr ¶
func (b InsertBuilder) SuffixExpr(expr Sqlizer) InsertBuilder
SuffixExpr adds an expression to the end of the query
func (InsertBuilder) ToSql ¶
func (b InsertBuilder) ToSql() (string, []interface{}, error)
ToSql builds the query into a SQL string and bound args.
func (InsertBuilder) Values ¶
func (b InsertBuilder) Values(values ...interface{}) InsertBuilder
Values adds a single row's values to the query.
type Like ¶
type Like map[string]interface{}
Like is syntactic sugar for use with LIKE conditions. Ex:
.Where(Like{"name": "%irrel"})
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 NotILike ¶
type NotILike Like
NotILike is syntactic sugar for use with ILIKE conditions. Ex:
.Where(NotILike{"name": "sq%"})
type NotLike ¶
type NotLike Like
NotLike is syntactic sugar for use with LIKE conditions. Ex:
.Where(NotLike{"name": "%irrel"})
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 QueryRower ¶
type QueryRower interface {
QueryRowNoContext(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 QueryRowerContext ¶
QueryRowerContext is the interface that wraps the QueryRowContext method.
QueryRowContext executes the given query as implemented by database/sql.QueryRowContext.
type Queryer ¶
Queryer is the interface that wraps the Query method.
Query executes the given query as implemented by database/sql.Query.
type QueryerContext ¶
type QueryerContext interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
}
QueryerContext is the interface that wraps the QueryContext method.
QueryContext executes the given query as implemented by database/sql.QueryContext.
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.
func QueryRowWithNoContext ¶
func QueryRowWithNoContext(db QueryRower, s Sqlizer) RowScanner
QueryRowWith QueryRows the SQL returned by s with db.
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 RunnerContext ¶
type RunnerContext interface { QueryerContext QueryRowerContext ExecerContext }
RunnerContext groups the Runner interface, along with the Context versions of each of its methods
func WrapStdSqlCtx ¶
func WrapStdSqlCtx(stdSqlCtx StdSqlCtx) RunnerContext
WrapStdSqlCtx wraps a type implementing the standard SQL interface plus the context versions of the methods with methods that squirrel expects.
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) 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(ctx context.Context) (pgconn.CommandTag, error)
ExecContext builds and ExecContexts 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 subquery 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 a 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 a 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 adds 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(ctx context.Context) (pgx.Rows, error)
QueryContext builds and QueryContexts the query with the Runner set by RunWith.
func (SelectBuilder) QueryRow ¶
func (b SelectBuilder) QueryRow(ctx context.Context) RowScanner
QueryRowContext builds and QueryRowContexts the query with the Runner set by RunWith.
func (SelectBuilder) RemoveColumns ¶
func (b SelectBuilder) RemoveColumns() SelectBuilder
RemoveColumns remove all columns from query. Must add a new column with Column or Columns methods, otherwise return a error.
func (SelectBuilder) RemoveLimit ¶
func (b SelectBuilder) RemoveLimit() SelectBuilder
Limit ALL allows to access 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 RunnerContext) 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(ctx context.Context, dest ...interface{}) error
ScanContext is a shortcut for QueryRowContext().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 a SQL string and bound args.
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 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) Replace ¶
func (b StatementBuilderType) Replace(into string) InsertBuilder
Replace returns a InsertBuilder for this StatementBuilderType with the statement keyword set to "REPLACE".
func (StatementBuilderType) RunWith ¶
func (b StatementBuilderType) RunWith(runner RunnerContext) 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) UpdateBuilder
Update returns a 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.
type StdSql ¶
type StdSql interface { QueryNoContext(string, ...interface{}) (pgx.Rows, error) QueryRowNoContext(string, ...interface{}) pgx.Row ExecNoContext(string, ...interface{}) (pgconn.CommandTag, error) }
StdSql encompasses the standard methods of the *sql.DB type, and other types that wrap these methods.
type StdSqlCtx ¶
type StdSqlCtx interface { StdSql Query(context.Context, string, ...any) (pgx.Rows, error) QueryRow(context.Context, string, ...any) pgx.Row Exec(context.Context, string, ...any) (pgconn.CommandTag, error) }
StdSqlCtx encompasses the standard methods of the *sql.DB type, along with the Context versions of those methods, and other types that wrap these methods.
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) Exec ¶
func (b UpdateBuilder) Exec(ctx context.Context) (pgconn.CommandTag, error)
ExecContext builds and ExecContexts the query with the Runner set by RunWith.
func (UpdateBuilder) Limit ¶
func (b UpdateBuilder) Limit(limit uint64) UpdateBuilder
Limit sets a LIMIT clause on the query.
func (UpdateBuilder) MustSql ¶
func (b UpdateBuilder) MustSql() (string, []interface{})
MustSql builds the query into a SQL string and bound args. It panics if there are any errors.
func (UpdateBuilder) Offset ¶
func (b UpdateBuilder) Offset(offset uint64) UpdateBuilder
Offset sets a OFFSET clause on the query.
func (UpdateBuilder) OrderBy ¶
func (b UpdateBuilder) OrderBy(orderBys ...string) UpdateBuilder
OrderBy adds ORDER BY expressions to the query.
func (UpdateBuilder) PlaceholderFormat ¶
func (b UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) UpdateBuilder
PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.
func (UpdateBuilder) Prefix ¶
func (b UpdateBuilder) Prefix(sql string, args ...interface{}) UpdateBuilder
Prefix adds an expression to the beginning of the query
func (UpdateBuilder) PrefixExpr ¶
func (b UpdateBuilder) PrefixExpr(expr Sqlizer) UpdateBuilder
PrefixExpr adds an expression to the very beginning of the query
func (UpdateBuilder) Query ¶
func (b UpdateBuilder) Query(ctx context.Context) (pgx.Rows, error)
QueryContext builds and QueryContexts the query with the Runner set by RunWith.
func (UpdateBuilder) QueryRow ¶
func (b UpdateBuilder) QueryRow(ctx context.Context) pgx.Row
QueryRowContext builds and QueryRowContexts the query with the Runner set by RunWith.
func (UpdateBuilder) RunWith ¶
func (b UpdateBuilder) RunWith(runner RunnerContext) UpdateBuilder
RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
func (UpdateBuilder) Scan ¶
func (b UpdateBuilder) Scan(ctx context.Context, dest ...interface{}) error
ScanContext is a shortcut for QueryRowContext().Scan.
func (UpdateBuilder) Set ¶
func (b UpdateBuilder) Set(column string, value interface{}) UpdateBuilder
Set adds SET clauses to the query.
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) Suffix ¶
func (b UpdateBuilder) Suffix(sql string, args ...interface{}) UpdateBuilder
Suffix adds an expression to the end of the query
func (UpdateBuilder) SuffixExpr ¶
func (b UpdateBuilder) SuffixExpr(expr Sqlizer) UpdateBuilder
SuffixExpr adds an expression to the end of the query
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 query into a SQL string and bound args.
func (UpdateBuilder) Where ¶
func (b UpdateBuilder) Where(pred interface{}, args ...interface{}) UpdateBuilder
Where adds WHERE expressions to the query.
See SelectBuilder.Where for more information.