Documentation ¶
Overview ¶
package sqrl provides a fluent SQL generator.
See https://github.com/elgris/sqrl for examples.
Index ¶
- Variables
- func Alias(expr Sqlizer, alias string) aliasExpr
- func ExecWith(db Execer, s Sqlizer) (res sql.Result, err error)
- func ExecWithContext(ctx context.Context, db ExecerContext, s Sqlizer) (res sql.Result, err error)
- func Expr(sql string, args ...interface{}) expr
- func LastInsertId(res sql.Result, err error) (int64, error)
- func Placeholders(count int) string
- func RowsAffected(res sql.Result, err error) (int64, error)
- type And
- type BaseRunner
- type CaseBuilder
- type DBProxy
- type DBProxyBeginner
- type DeleteBuilder
- func (r *DeleteBuilder) AppendToSql(w io.Writer, args []interface{}) ([]interface{}, error)
- func (b *DeleteBuilder) Exec() (sql.Result, error)
- func (b *DeleteBuilder) ExecContext(ctx context.Context) (sql.Result, error)
- func (b *DeleteBuilder) From(from string) *DeleteBuilder
- func (b *DeleteBuilder) Join(join string) *DeleteBuilder
- func (b *DeleteBuilder) JoinClause(join string) *DeleteBuilder
- func (b *DeleteBuilder) LeftJoin(join string) *DeleteBuilder
- func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder
- 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) Query() (RowsScanner, error)
- func (b *DeleteBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
- func (b *DeleteBuilder) QueryRow() RowScanner
- func (b *DeleteBuilder) QueryRowContext(ctx context.Context) RowScanner
- func (b *DeleteBuilder) Returning(columns ...string) *DeleteBuilder
- func (b *DeleteBuilder) ReturningSelect(from *SelectBuilder, alias string) *DeleteBuilder
- func (b *DeleteBuilder) RightJoin(join string) *DeleteBuilder
- func (b *DeleteBuilder) RunWith(runner BaseRunner) *DeleteBuilder
- func (b *DeleteBuilder) Scan(dest ...interface{}) error
- func (b *DeleteBuilder) Suffix(sql string, args ...interface{}) *DeleteBuilder
- func (b *DeleteBuilder) ToSql() (sqlStr string, args []interface{}, err error)
- func (b *DeleteBuilder) Using(tables ...string) *DeleteBuilder
- func (b *DeleteBuilder) UsingSelect(from *SelectBuilder, alias string) *DeleteBuilder
- func (b *DeleteBuilder) What(what ...string) *DeleteBuilder
- func (b *DeleteBuilder) Where(pred interface{}, args ...interface{}) *DeleteBuilder
- type Eq
- type Execer
- type ExecerContext
- type Gt
- type GtOrEq
- type InsertBuilder
- func (r *InsertBuilder) AppendToSql(w io.Writer, args []interface{}) ([]interface{}, error)
- func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder
- func (b *InsertBuilder) Exec() (sql.Result, error)
- func (b *InsertBuilder) ExecContext(ctx context.Context) (sql.Result, error)
- func (b *InsertBuilder) Into(into string) *InsertBuilder
- 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) Query() (RowsScanner, error)
- func (b *InsertBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
- func (b *InsertBuilder) QueryRow() RowScanner
- func (b *InsertBuilder) QueryRowContext(ctx context.Context) RowScanner
- func (b *InsertBuilder) Returning(columns ...string) *InsertBuilder
- func (b *InsertBuilder) ReturningSelect(from *SelectBuilder, alias string) *InsertBuilder
- func (b *InsertBuilder) RunWith(runner BaseRunner) *InsertBuilder
- func (b *InsertBuilder) Scan(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) ToSql() (sqlStr string, args []interface{}, err error)
- func (b *InsertBuilder) Values(values ...interface{}) *InsertBuilder
- type Lt
- type LtOrEq
- type NotEq
- type Or
- type PlaceholderFormat
- type Preparer
- type QueryRower
- type QueryRowerContext
- type Queryer
- type QueryerContext
- type Row
- type RowScanner
- type RowsScanner
- type Runner
- 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) Exec() (sql.Result, error)
- func (b *SelectBuilder) ExecContext(ctx context.Context) (sql.Result, error)
- func (b *SelectBuilder) From(tables ...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) 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) Options(options ...string) *SelectBuilder
- func (b *SelectBuilder) OrderBy(orderBys ...Sqlizer) *SelectBuilder
- func (b *SelectBuilder) PlaceholderFormat(f PlaceholderFormat) *SelectBuilder
- func (b *SelectBuilder) Prefix(sql string, args ...interface{}) *SelectBuilder
- func (b *SelectBuilder) Query() (RowsScanner, error)
- func (b *SelectBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
- func (b *SelectBuilder) QueryRow() RowScanner
- func (b *SelectBuilder) QueryRowContext(ctx context.Context) RowScanner
- 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) ToSql() (sqlStr string, args []interface{}, err error)
- func (b *SelectBuilder) Where(pred interface{}, args ...interface{}) *SelectBuilder
- type Sqlizer
- type StatementBuilderType
- func (b StatementBuilderType) Delete(what ...string) *DeleteBuilder
- func (b StatementBuilderType) Insert(into string) *InsertBuilder
- 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) *UpdateBuilder
- type UpdateBuilder
- func (r *UpdateBuilder) AppendToSql(w io.Writer, args []interface{}) ([]interface{}, error)
- func (b *UpdateBuilder) Exec() (sql.Result, error)
- func (b *UpdateBuilder) ExecContext(ctx context.Context) (sql.Result, error)
- func (b *UpdateBuilder) From(tables ...string) *UpdateBuilder
- func (b *UpdateBuilder) FromSelect(from *SelectBuilder, alias string) *UpdateBuilder
- func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
- 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) Query() (RowsScanner, error)
- func (b *UpdateBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
- func (b *UpdateBuilder) QueryRow() RowScanner
- func (b *UpdateBuilder) QueryRowContext(ctx context.Context) RowScanner
- func (b *UpdateBuilder) Returning(columns ...string) *UpdateBuilder
- func (b *UpdateBuilder) ReturningSelect(from *SelectBuilder, alias string) *UpdateBuilder
- func (b *UpdateBuilder) RunWith(runner BaseRunner) *UpdateBuilder
- func (b *UpdateBuilder) Scan(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) Table(table string) *UpdateBuilder
- func (b *UpdateBuilder) ToSql() (sqlStr string, args []interface{}, err 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{} )
var ErrRunnerNotQueryRunner = fmt.Errorf("cannot QueryRow; Runner is not a QueryRower")
ErrRunnerNotQueryRunner is returned by QueryRow if the RunWith value doesn't implement QueryRower.
var ErrRunnerNotQueryRunnerContext = fmt.Errorf("cannot QueryRow; Runner is not a QueryRowerContext")
ErrRunnerNotQueryRunnerContext is returned by QueryRowContext if the RunWith value doesn't implement QueryRowerContext.
var ErrRunnerNotQueryer = fmt.Errorf("cannot Query; Runner is not a Queryer")
ErrRunnerNotQueryer is returned by Query if the RunWith value doesn't implement Queryer.
var ErrRunnerNotQueryerContext = fmt.Errorf("cannot Query; Runner is not a QueryerContext")
ErrRunnerNotQueryerContext is returned by QueryContext if the RunWith value doesn't implement QueryerContext.
var ErrRunnerNotSet = fmt.Errorf("cannot run; no Runner set (RunWith)")
ErrRunnerNotSet is returned by methods that need a Runner if it isn't set.
var StatementBuilder = StatementBuilderType{/* contains filtered or unexported fields */}
StatementBuilder is a basic statement builder, holds global configuration options like placeholder format or SQL runner
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 ExecWithContext ¶
ExecWithContext Execs the SQL returned by s with db.
func Expr ¶
func Expr(sql string, args ...interface{}) expr
Expr builds value expressions for InsertBuilder and UpdateBuilder.
Ex:
.Values(Expr("FROM_UNIXTIME(?)", t))
func LastInsertId ¶
LastInsertId returns last insert id or error from query execution
func Placeholders ¶
Placeholders returns a string with count ? placeholders joined with commas.
Types ¶
type And ¶
type And conj
And is syntactic sugar that glues where/having parts with AND clause Ex:
.Where(And{Expr("a > ?", 15), Expr("b < ?", 20), Expr("c is TRUE")})
type BaseRunner ¶
type BaseRunner interface { Execer ExecerContext }
BaseRunner groups the Execer and Queryer interfaces.
type CaseBuilder ¶
type CaseBuilder struct {
// contains filtered or unexported fields
}
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
Else sets optional "ELSE ..." part for CASE construct
func (*CaseBuilder) ToSql ¶
func (b *CaseBuilder) ToSql() (sqlStr string, args []interface{}, err error)
ToSql implements Sqlizer
func (*CaseBuilder) When ¶
func (b *CaseBuilder) When(when interface{}, then interface{}) *CaseBuilder
When adds "WHEN ... THEN ..." part to CASE construct
type DBProxy ¶
type DBProxy interface { Execer Queryer QueryRower Preparer ExecerContext QueryerContext QueryRowerContext }
DBProxy groups the Execer, Queryer, QueryRower, and Preparer interfaces.
func NewStmtCacher ¶
NewStmtCacher returns a DBProxy wrapping prep that caches Prepared Stmts.
Stmts are cached based on the string value of their queries.
type DBProxyBeginner ¶
DBProxyBeginner describes a DBProxy that can start transactions
func NewStmtCacheProxy ¶
func NewStmtCacheProxy(db *sql.DB) DBProxyBeginner
NewStmtCacheProxy creates new cache proxy for statements
type DeleteBuilder ¶
type DeleteBuilder struct { StatementBuilderType // contains filtered or unexported fields }
DeleteBuilder builds SQL DELETE statements.
func Delete ¶
func Delete(what ...string) *DeleteBuilder
Delete returns a new DeleteBuilder for given table names.
See DeleteBuilder.Table.
func NewDeleteBuilder ¶
func NewDeleteBuilder(b StatementBuilderType) *DeleteBuilder
NewDeleteBuilder creates new instance of DeleteBuilder
func (*DeleteBuilder) AppendToSql ¶
func (*DeleteBuilder) Exec ¶
func (b *DeleteBuilder) Exec() (sql.Result, error)
Exec builds and Execs the query with the Runner set by RunWith.
func (*DeleteBuilder) ExecContext ¶
ExecContext builds and Execs the query with the Runner set by RunWith using given context.
func (*DeleteBuilder) From ¶
func (b *DeleteBuilder) From(from string) *DeleteBuilder
From sets the FROM clause of the query.
func (*DeleteBuilder) Join ¶
func (b *DeleteBuilder) Join(join string) *DeleteBuilder
Join adds a JOIN clause to the query.
func (*DeleteBuilder) JoinClause ¶
func (b *DeleteBuilder) JoinClause(join string) *DeleteBuilder
JoinClause adds a join clause to the query.
func (*DeleteBuilder) LeftJoin ¶
func (b *DeleteBuilder) LeftJoin(join string) *DeleteBuilder
LeftJoin adds a LEFT JOIN clause to the query.
func (*DeleteBuilder) Limit ¶
func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder
Limit sets a LIMIT clause on the query.
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) Query ¶
func (b *DeleteBuilder) Query() (RowsScanner, error)
Query builds and Querys the query with the Runner set by RunWith.
func (*DeleteBuilder) QueryContext ¶
func (b *DeleteBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
QueryContext builds and runs the query using given context and Query command.
func (*DeleteBuilder) QueryRow ¶
func (b *DeleteBuilder) QueryRow() RowScanner
QueryRow builds and QueryRows the query with the Runner set by RunWith.
func (*DeleteBuilder) QueryRowContext ¶
func (b *DeleteBuilder) QueryRowContext(ctx context.Context) RowScanner
QueryRowContext builds and runs the query using given context.
func (*DeleteBuilder) Returning ¶
func (b *DeleteBuilder) Returning(columns ...string) *DeleteBuilder
Returning adds columns to RETURNING clause of the query
DELETE ... RETURNING is PostgreSQL specific extension
func (*DeleteBuilder) ReturningSelect ¶
func (b *DeleteBuilder) ReturningSelect(from *SelectBuilder, alias string) *DeleteBuilder
ReturningSelect adds subquery to RETURNING clause of the query
DELETE ... RETURNING is PostgreSQL specific extension
func (*DeleteBuilder) RightJoin ¶
func (b *DeleteBuilder) RightJoin(join string) *DeleteBuilder
RightJoin adds a RIGHT JOIN clause to the query.
func (*DeleteBuilder) RunWith ¶
func (b *DeleteBuilder) RunWith(runner BaseRunner) *DeleteBuilder
RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
func (*DeleteBuilder) Scan ¶
func (b *DeleteBuilder) Scan(dest ...interface{}) error
Scan is a shortcut for QueryRow().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) ToSql ¶
func (b *DeleteBuilder) ToSql() (sqlStr string, args []interface{}, err error)
ToSql builds the query into a SQL string and bound args.
func (*DeleteBuilder) Using ¶
func (b *DeleteBuilder) Using(tables ...string) *DeleteBuilder
Using sets the USING clause of the query.
DELETE ... USING is an MySQL/PostgreSQL specific extension
func (*DeleteBuilder) UsingSelect ¶
func (b *DeleteBuilder) UsingSelect(from *SelectBuilder, alias string) *DeleteBuilder
UsingSelect sets a subquery into the USING clause of the query.
DELETE ... USING is an MySQL/PostgreSQL specific extension
func (*DeleteBuilder) What ¶
func (b *DeleteBuilder) What(what ...string) *DeleteBuilder
What sets names of tables to be used for deleting from
func (*DeleteBuilder) Where ¶
func (b *DeleteBuilder) Where(pred interface{}, args ...interface{}) *DeleteBuilder
Where adds WHERE expressions to the query.
type Eq ¶
type Eq map[string]interface{}
Eq is syntactic sugar for use with Where/Having/Set methods. Ex:
.Where(Eq{"id": 1})
type Execer ¶
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 {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
}
ExecerContext is the interface that wraps the Exec method.
ExecContext executes the given query using given context 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 InsertBuilder ¶
type InsertBuilder struct { StatementBuilderType // contains filtered or unexported fields }
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 NewInsertBuilder ¶
func NewInsertBuilder(b StatementBuilderType) *InsertBuilder
NewInsertBuilder creates new instance of InsertBuilder
func (*InsertBuilder) AppendToSql ¶
func (*InsertBuilder) Columns ¶
func (b *InsertBuilder) Columns(columns ...string) *InsertBuilder
Columns adds insert columns to the query.
func (*InsertBuilder) Exec ¶
func (b *InsertBuilder) Exec() (sql.Result, error)
Exec builds and Execs the query with the Runner set by RunWith.
func (*InsertBuilder) ExecContext ¶
ExecContext builds and Execs the query with the Runner set by RunWith using given context.
func (*InsertBuilder) Into ¶
func (b *InsertBuilder) Into(into string) *InsertBuilder
Into sets the INTO clause of the query.
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) Query ¶
func (b *InsertBuilder) Query() (RowsScanner, error)
Query builds and Querys the query with the Runner set by RunWith.
func (*InsertBuilder) QueryContext ¶
func (b *InsertBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
QueryContext builds and runs the query using given context and Query command.
func (*InsertBuilder) QueryRow ¶
func (b *InsertBuilder) QueryRow() RowScanner
QueryRow builds and QueryRows the query with the Runner set by RunWith.
func (*InsertBuilder) QueryRowContext ¶
func (b *InsertBuilder) QueryRowContext(ctx context.Context) RowScanner
QueryRowContext builds and runs the query using given context.
func (*InsertBuilder) Returning ¶
func (b *InsertBuilder) Returning(columns ...string) *InsertBuilder
Returning adds columns to RETURNING clause of the query
INSERT ... RETURNING is PostgreSQL specific extension
func (*InsertBuilder) ReturningSelect ¶
func (b *InsertBuilder) ReturningSelect(from *SelectBuilder, alias string) *InsertBuilder
ReturningSelect adds subquery to RETURNING clause of the query
INSERT ... RETURNING is PostgreSQL specific extension
func (*InsertBuilder) RunWith ¶
func (b *InsertBuilder) RunWith(runner BaseRunner) *InsertBuilder
RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
func (*InsertBuilder) Scan ¶
func (b *InsertBuilder) Scan(dest ...interface{}) error
Scan is a shortcut for QueryRow().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) ToSql ¶
func (b *InsertBuilder) ToSql() (sqlStr string, args []interface{}, err 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 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 Or ¶
type Or conj
Or is syntactic sugar that glues where/having parts with OR clause Ex:
.Where(Or{Expr("a > ?", 15), Expr("b < ?", 20), Expr("c is TRUE")})
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 Preparer ¶
type Preparer interface { Prepare(query string) (*sql.Stmt, error) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) }
Preparer is the interface that wraps the Prepare method.
Prepare executes the given query as implemented by database/sql.Prepare. Prepare executes the given query as implemented by database/sql.PrepareContext.
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 QueryRowerContext ¶
type QueryRowerContext interface {
QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner
}
QueryRowerContext is the interface that wraps the QueryRow method.
QueryRowContext executes the given query using given context as implemented by database/sql.QueryRowContext.
type Queryer ¶
type Queryer interface {
Query(query string, args ...interface{}) (RowsScanner, error)
}
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 {
QueryContext(ctx context.Context, query string, args ...interface{}) (RowsScanner, error)
}
QueryerContext is the interface that wraps the Query method.
QueryerContext executes the given query using given context 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 QueryRowWith ¶
func QueryRowWith(db QueryRower, s Sqlizer) RowScanner
QueryRowWith QueryRows the SQL returned by s with db.
func QueryRowWithContext ¶
func QueryRowWithContext(ctx context.Context, db QueryRowerContext, s Sqlizer) RowScanner
QueryRowWithContext QueryRows the SQL returned by s with db.
type RowsScanner ¶
type RowsScanner interface { Columns() ([]string, error) Next() bool Close() error Err() error RowScanner }
RowsScanner is the interface that wraps the sql row methods.
functions behave like database/sql.Rows methods
func QueryWith ¶
func QueryWith(db Queryer, s Sqlizer) (rows RowsScanner, err error)
QueryWith Querys the SQL returned by s with db.
func QueryWithContext ¶
func QueryWithContext(ctx context.Context, db QueryerContext, s Sqlizer) (rows RowsScanner, err error)
QueryWithContext Querys the SQL returned by s with db.
type Runner ¶
type Runner interface { Execer ExecerContext Queryer QueryerContext QueryRower QueryRowerContext }
Runner groups the Execer, Queryer, and QueryRower interfaces.
type SelectBuilder ¶
type SelectBuilder struct { StatementBuilderType // contains filtered or unexported fields }
SelectBuilder builds SQL SELECT statements.
func NewSelectBuilder ¶
func NewSelectBuilder(b StatementBuilderType) *SelectBuilder
NewSelectBuilder creates new instance of SelectBuilder
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 ("+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) Exec ¶
func (b *SelectBuilder) Exec() (sql.Result, error)
Exec builds and Execs the query with the Runner set by RunWith.
func (*SelectBuilder) ExecContext ¶
ExecContext builds and Execs the query with the Runner set by RunWith using given context.
func (*SelectBuilder) From ¶
func (b *SelectBuilder) From(tables ...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) 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) Options ¶
func (b *SelectBuilder) Options(options ...string) *SelectBuilder
Options adds select option to the query
func (*SelectBuilder) OrderBy ¶
func (b *SelectBuilder) OrderBy(orderBys ...Sqlizer) *SelectBuilder
OrderBy adds ORDER BY expressions 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) Query ¶
func (b *SelectBuilder) Query() (RowsScanner, error)
Query builds and Querys the query with the Runner set by RunWith.
func (*SelectBuilder) QueryContext ¶
func (b *SelectBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
QueryContext builds and Querys the query with the Runner set by RunWith in given context.
func (*SelectBuilder) QueryRow ¶
func (b *SelectBuilder) QueryRow() RowScanner
QueryRow builds and QueryRows the query with the Runner set by RunWith.
func (*SelectBuilder) QueryRowContext ¶
func (b *SelectBuilder) QueryRowContext(ctx context.Context) RowScanner
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.
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) ToSql ¶
func (b *SelectBuilder) ToSql() (sqlStr string, args []interface{}, err 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 ¶
type StatementBuilderType struct {
// contains filtered or unexported fields
}
StatementBuilderType is the type of StatementBuilder.
func (StatementBuilderType) Delete ¶
func (b StatementBuilderType) Delete(what ...string) *DeleteBuilder
Delete returns a DeleteBuilder for this StatementBuilder.
func (StatementBuilderType) Insert ¶
func (b StatementBuilderType) Insert(into string) *InsertBuilder
Insert returns a InsertBuilder for this StatementBuilder.
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 StatementBuilder.
func (StatementBuilderType) Update ¶
func (b StatementBuilderType) Update(table string) *UpdateBuilder
Update returns a UpdateBuilder for this StatementBuilder.
type UpdateBuilder ¶
type UpdateBuilder struct { StatementBuilderType // contains filtered or unexported fields }
UpdateBuilder builds SQL UPDATE statements.
func NewUpdateBuilder ¶
func NewUpdateBuilder(b StatementBuilderType) *UpdateBuilder
NewUpdateBuilder creates new instance of UpdateBuilder
func Update ¶
func Update(table string) *UpdateBuilder
Update returns a new UpdateBuilder with the given table name.
See UpdateBuilder.Table.
func (*UpdateBuilder) AppendToSql ¶
func (*UpdateBuilder) Exec ¶
func (b *UpdateBuilder) Exec() (sql.Result, error)
Exec builds and Execs the query with the Runner set by RunWith.
func (*UpdateBuilder) ExecContext ¶
ExecContext builds and Execs the query with the Runner set by RunWith using given context.
func (*UpdateBuilder) From ¶
func (b *UpdateBuilder) From(tables ...string) *UpdateBuilder
From adds tables to FROM clause of the query.
UPDATE ... FROM is an PostgreSQL specific extension
func (*UpdateBuilder) FromSelect ¶
func (b *UpdateBuilder) FromSelect(from *SelectBuilder, alias string) *UpdateBuilder
FromSelect adds subquery to FROM clause of the query.
UPDATE ... FROM is an PostgreSQL specific extension
func (*UpdateBuilder) Limit ¶
func (b *UpdateBuilder) Limit(limit uint64) *UpdateBuilder
Limit sets a LIMIT clause on the query.
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) Query ¶
func (b *UpdateBuilder) Query() (RowsScanner, error)
Query builds and Querys the query with the Runner set by RunWith.
func (*UpdateBuilder) QueryContext ¶
func (b *UpdateBuilder) QueryContext(ctx context.Context) (RowsScanner, error)
QueryContext builds and runs the query using given context and Query command.
func (*UpdateBuilder) QueryRow ¶
func (b *UpdateBuilder) QueryRow() RowScanner
QueryRow builds and QueryRows the query with the Runner set by RunWith.
func (*UpdateBuilder) QueryRowContext ¶
func (b *UpdateBuilder) QueryRowContext(ctx context.Context) RowScanner
QueryRowContext builds and runs the query using given context.
func (*UpdateBuilder) Returning ¶
func (b *UpdateBuilder) Returning(columns ...string) *UpdateBuilder
Returning adds columns to RETURNING clause of the query
UPDATE ... RETURNING is PostgreSQL specific extension
func (*UpdateBuilder) ReturningSelect ¶
func (b *UpdateBuilder) ReturningSelect(from *SelectBuilder, alias string) *UpdateBuilder
ReturningSelect adds subquery to RETURNING clause of the query
UPDATE ... RETURNING is PostgreSQL specific extension
func (*UpdateBuilder) RunWith ¶
func (b *UpdateBuilder) RunWith(runner BaseRunner) *UpdateBuilder
RunWith sets a Runner (like database/sql.DB) to be used with e.g. Exec.
func (*UpdateBuilder) Scan ¶
func (b *UpdateBuilder) Scan(dest ...interface{}) error
Scan is a shortcut for QueryRow().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) Table ¶
func (b *UpdateBuilder) Table(table string) *UpdateBuilder
Table sets the table to be updateb.
func (*UpdateBuilder) ToSql ¶
func (b *UpdateBuilder) ToSql() (sqlStr string, args []interface{}, err 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.