squirrel

package module
v0.0.0-...-2be5dd9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2018 License: MIT Imports: 9 Imported by: 0

README

Squirrel - fluent SQL generator for Go

or if you prefer using master (which may be arbitrarily ahead of or behind v1):

NOTE: as of Go 1.6, go get correctly clones the Github default branch (which is v1 in this repo).

import "github.com/Guazi-inc/squirrel"

GoDoc Build Status

_Note: This project has moved from github.com/lann/squirrel to github.com/Masterminds/squirrel. Lann remains the architect of the project, but we're helping him curate.

Squirrel is not an ORM. For an application of Squirrel, check out structable, a table-struct mapper

Squirrel helps you build SQL queries from composable parts:

import sq "github.com/Masterminds/squirrel"

users := sq.Select("*").From("users").Join("emails USING (email_id)")

active := users.Where(sq.Eq{"deleted_at": nil})

sql, args, err := active.ToSql()

sql == "SELECT * FROM users JOIN emails USING (email_id) WHERE deleted_at IS NULL"
sql, args, err := sq.
    Insert("users").Columns("name", "age").
    Values("moe", 13).Values("larry", sq.Expr("? + 5", 12)).
    ToSql()

sql == "INSERT INTO users (name,age) VALUES (?,?),(?,? + 5)"

Squirrel can also execute queries directly:

stooges := users.Where(sq.Eq{"username": []string{"moe", "larry", "curly", "shemp"}})
three_stooges := stooges.Limit(3)
rows, err := three_stooges.RunWith(db).Query()

// Behaves like:
rows, err := db.Query("SELECT * FROM users WHERE username IN (?,?,?,?) LIMIT 3",
                      "moe", "larry", "curly", "shemp")

Squirrel makes conditional query building a breeze:

if len(q) > 0 {
    users = users.Where("name LIKE ?", fmt.Sprint("%", q, "%"))
}

You can escape question mask by inserting two question marks:

```sql
SELECT * FROM nodes WHERE meta->'format' ??| array[?,?]

will generate with the Dollar Placeholder:

SELECT * FROM nodes WHERE meta->'format' ?| array[$1,$2]

License

Squirrel is released under the MIT License.

Documentation

Overview

Package squirrel provides a fluent SQL generator.

See https://github.com/lann/squirrel for examples.

Index

Constants

This section is empty.

Variables

View Source
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{}
)
View Source
var StatementBuilder = StatementBuilderType(builder.EmptyBuilder).PlaceholderFormat(Question)

StatementBuilder is a parent builder for other builders, e.g. SelectBuilder.

Functions

func Alias

func Alias(expr Sqlizer, alias string) aliasExpr

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 DebugSqlizer

func DebugSqlizer(s Sqlizer) string

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 Expr

func Expr(sql string, args ...interface{}) expr

Expr builds value expressions for InsertBuilder and UpdateBuilder.

Ex:

.Values(Expr("FROM_UNIXTIME(?)", t))

func Placeholders

func Placeholders(count int) string

Placeholders returns a string with count ? placeholders joined with commas.

Types

type And

type And conj

func (And) ToSql

func (a And) ToSql() (string, []interface{})

type CaseBuilder

type CaseBuilder builder.Builder

CaseBuilder builds SQL CASE construct which could be used as parts of queries.

func (CaseBuilder) Else

func (b CaseBuilder) Else(expr interface{}) CaseBuilder

What sets optional "ELSE ..." part for CASE construct

func (CaseBuilder) ToSql

func (b CaseBuilder) ToSql() (string, []interface{})

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

type DeleteBuilder builder.Builder

DeleteBuilder builds SQL DELETE statements.

func (DeleteBuilder) Condition

func (b DeleteBuilder) Condition() WhereConditions

Condition

func (DeleteBuilder) Eq

func (b DeleteBuilder) Eq(column string, arg interface{}) WhereConditions

eq

func (DeleteBuilder) Expr

func (b DeleteBuilder) Expr(sql string, args ...interface{}) WhereConditions

expr

func (DeleteBuilder) From

func (b DeleteBuilder) From(from string) DeleteCondition

From sets the table to be deleted from.

func (DeleteBuilder) GroupBy

func (b DeleteBuilder) GroupBy(groupBys ...string) WhereConditions

OrderBy adds ORDER BY expressions to the query.

func (DeleteBuilder) Gt

func (b DeleteBuilder) Gt(column string, arg interface{}) WhereConditions

gt

func (DeleteBuilder) GtOrEq

func (b DeleteBuilder) GtOrEq(column string, arg interface{}) WhereConditions

gtOrEq

func (DeleteBuilder) Having

func (b DeleteBuilder) Having(pred interface{}, rest ...interface{}) WhereConditions

func (DeleteBuilder) Limit

func (b DeleteBuilder) Limit(limit int) WhereConditions

Limit sets a LIMIT clause on the query.

func (DeleteBuilder) Lt

func (b DeleteBuilder) Lt(column string, arg interface{}) WhereConditions

lt

func (DeleteBuilder) LtOrEq

func (b DeleteBuilder) LtOrEq(column string, arg interface{}) WhereConditions

ltOrEq

func (DeleteBuilder) NotEq

func (b DeleteBuilder) NotEq(column string, arg interface{}) WhereConditions

func (DeleteBuilder) Offset

func (b DeleteBuilder) Offset(offset int) WhereConditions

Offset sets a OFFSET clause on the query.

func (DeleteBuilder) OrderBy

func (b DeleteBuilder) OrderBy(orderBys ...string) WhereConditions

OrderBy adds ORDER BY expressions to the query.

func (DeleteBuilder) PlaceholderFormat

func (b DeleteBuilder) PlaceholderFormat(f PlaceholderFormat) WhereConditions

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (DeleteBuilder) Prefix

func (b DeleteBuilder) Prefix(sql string, args ...interface{}) DeleteCondition

Prefix adds an expression to the beginning of the query

func (DeleteBuilder) Suffix

func (b DeleteBuilder) Suffix(sql string, args ...interface{}) WhereConditions

Suffix adds an expression to the end of the query

func (DeleteBuilder) ToSql

func (b DeleteBuilder) ToSql() (string, []interface{})

ToSql builds the query into a SQL string and bound args.

func (DeleteBuilder) Where

func (b DeleteBuilder) Where(pred interface{}, args ...interface{}) WhereConditions

Where adds WHERE expressions to the query.

See SelectBuilder.Where for more information.

type DeleteCondition

type DeleteCondition interface {
	Prefix(string, ...interface{}) DeleteCondition
	From(string) DeleteCondition
	WhereConditions
}

func Delete

func Delete(from string) DeleteCondition

Delete returns a new DeleteBuilder with the given table name.

See DeleteBuilder.Table.

type Eq

type Eq map[string]interface{}

Eq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(Eq{"id": 1})

func (Eq) ToSql

func (eq Eq) ToSql() (sql string, args []interface{})

type Gt

type Gt Lt

Gt is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(Gt{"id": 1}) == "id > 1"

func (Gt) ToSql

func (gt Gt) ToSql() (sql string, args []interface{})

type GtOrEq

type GtOrEq Lt

GtOrEq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(GtOrEq{"id": 1}) == "id >= 1"

func (GtOrEq) ToSql

func (gtOrEq GtOrEq) ToSql() (sql string, args []interface{})

type InsertBuilder

type InsertBuilder builder.Builder

InsertBuilder builds SQL INSERT statements.

func (InsertBuilder) Columns

func (b InsertBuilder) Columns(columns ...string) InsertCondition

Columns adds insert columns to the query.

func (InsertBuilder) Into

func (b InsertBuilder) Into(from string) InsertCondition

Into sets the INTO clause of the query.

func (InsertBuilder) Options

func (b InsertBuilder) Options(options ...string) InsertCondition

Options adds keyword options before the INTO clause of the query.

func (InsertBuilder) PlaceholderFormat

func (b InsertBuilder) PlaceholderFormat(f PlaceholderFormat) InsertCondition

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (InsertBuilder) Prefix

func (b InsertBuilder) Prefix(sql string, args ...interface{}) InsertCondition

Prefix adds an expression to the beginning of the query

func (InsertBuilder) Select

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{}) InsertCondition

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{}) InsertCondition

Suffix adds an expression to the end of the query

func (InsertBuilder) ToSql

func (b InsertBuilder) ToSql() (string, []interface{})

ToSql builds the query into a SQL string and bound args.

func (InsertBuilder) Values

func (b InsertBuilder) Values(values ...interface{}) InsertCondition

Values adds a single row's values to the query.

type InsertCondition

type InsertCondition interface {
	ToSql() (string, []interface{})
	PlaceholderFormat(PlaceholderFormat) InsertCondition
	Prefix(string, ...interface{}) InsertCondition
	Options(...string) InsertCondition
	Into(string) InsertCondition
	Columns(...string) InsertCondition
	Values(...interface{}) InsertCondition
	Suffix(string, ...interface{}) InsertCondition
	SetMap(map[string]interface{}) InsertCondition
	Select(SelectCondition) InsertCondition
}

func Insert

func Insert(into string) InsertCondition

Insert returns a new InsertBuilder with the given table name.

See InsertBuilder.Into.

type JoinBuilder

type JoinBuilder builder.Builder

JoinBuilder builds SQL SELECT statements.

func (JoinBuilder) Condition

func (b JoinBuilder) Condition() WhereConditions

Condition

func (JoinBuilder) Eq

func (b JoinBuilder) Eq(column string, arg interface{}) WhereConditions

eq

func (JoinBuilder) Expr

func (b JoinBuilder) Expr(sql string, args ...interface{}) WhereConditions

expr

func (JoinBuilder) GroupBy

func (b JoinBuilder) GroupBy(groupBys ...string) WhereConditions

GroupBy adds GROUP BY expressions to the query.

func (JoinBuilder) Gt

func (b JoinBuilder) Gt(column string, arg interface{}) WhereConditions

gt

func (JoinBuilder) GtOrEq

func (b JoinBuilder) GtOrEq(column string, arg interface{}) WhereConditions

gtOrEq

func (JoinBuilder) Having

func (b JoinBuilder) Having(pred interface{}, rest ...interface{}) WhereConditions

Having adds an expression to the HAVING clause of the query.

See Where.

func (JoinBuilder) Join

func (b JoinBuilder) Join(join string, rest ...interface{}) JoinCondition

Join adds a JOIN clause to the query.

func (JoinBuilder) JoinClause

func (b JoinBuilder) JoinClause(pred interface{}, args ...interface{}) JoinCondition

JoinClause adds a join clause to the query.

func (JoinBuilder) LeftJoin

func (b JoinBuilder) LeftJoin(join string, rest ...interface{}) JoinCondition

LeftJoin adds a LEFT JOIN clause to the query.

func (JoinBuilder) Limit

func (b JoinBuilder) Limit(limit int) WhereConditions

Limit sets a LIMIT clause on the query.

func (JoinBuilder) Lt

func (b JoinBuilder) Lt(column string, arg interface{}) WhereConditions

lt

func (JoinBuilder) LtOrEq

func (b JoinBuilder) LtOrEq(column string, arg interface{}) WhereConditions

ltOrEq

func (JoinBuilder) NotEq

func (b JoinBuilder) NotEq(column string, arg interface{}) WhereConditions

func (JoinBuilder) Offset

func (b JoinBuilder) Offset(offset int) WhereConditions

Offset sets a OFFSET clause on the query.

func (JoinBuilder) Or

func (b JoinBuilder) Or(pred ...interface{}) WhereConditions

or

func (JoinBuilder) OrderBy

func (b JoinBuilder) OrderBy(orderBys ...string) WhereConditions

OrderBy adds ORDER BY expressions to the query.

func (JoinBuilder) PlaceholderFormat

func (b JoinBuilder) PlaceholderFormat(f PlaceholderFormat) WhereConditions

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (JoinBuilder) RightJoin

func (b JoinBuilder) RightJoin(join string, rest ...interface{}) JoinCondition

RightJoin adds a RIGHT JOIN clause to the query.

func (JoinBuilder) Suffix

func (b JoinBuilder) Suffix(sql string, args ...interface{}) WhereConditions

Suffix adds an expression to the end of the query

func (JoinBuilder) ToSql

func (b JoinBuilder) ToSql() (string, []interface{})

ToSql builds the query into a SQL string and bound args.

func (JoinBuilder) Where

func (b JoinBuilder) Where(pred interface{}, args ...interface{}) WhereConditions

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 JoinCondition

type JoinCondition interface {
	JoinClause(interface{}, ...interface{}) JoinCondition
	Join(string, ...interface{}) JoinCondition
	LeftJoin(string, ...interface{}) JoinCondition
	RightJoin(string, ...interface{}) JoinCondition
	WhereConditions
}

func Join

func Join(join string, rest ...interface{}) JoinCondition

新增的join方法

func JoinClause

func JoinClause(pred interface{}, args ...interface{}) JoinCondition

func LeftJoin

func LeftJoin(join string, rest ...interface{}) JoinCondition

func RightJoin

func RightJoin(join string, rest ...interface{}) JoinCondition

type Lt

type Lt map[string]interface{}

Lt is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(Lt{"id": 1})

func (Lt) ToSql

func (lt Lt) ToSql() (sql string, args []interface{})

type LtOrEq

type LtOrEq Lt

LtOrEq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(LtOrEq{"id": 1}) == "id <= 1"

func (LtOrEq) ToSql

func (ltOrEq LtOrEq) ToSql() (sql string, args []interface{})

type NotEq

type NotEq Eq

NotEq is syntactic sugar for use with Where/Having/Set methods. Ex:

.Where(NotEq{"id": 1}) == "id <> 1"

func (NotEq) ToSql

func (neq NotEq) ToSql() (sql string, args []interface{})

type Or

type Or conj

func (Or) ToSql

func (o Or) ToSql() (string, []interface{})

type PlaceholderFormat

type PlaceholderFormat interface {
	ReplacePlaceholders(sql string) (string, error)
}

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

type SelectBuilder builder.Builder

SelectBuilder builds SQL SELECT statements.

func (SelectBuilder) Column

func (b SelectBuilder) Column(column interface{}, args ...interface{}) SelectCondition

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) SelectCondition

Columns adds result columns to the query.

func (SelectBuilder) Condition

func (b SelectBuilder) Condition() WhereConditions

Condition

func (SelectBuilder) Distinct

func (b SelectBuilder) Distinct() SelectCondition

Distinct adds a DISTINCT clause to the query.

func (SelectBuilder) Eq

func (b SelectBuilder) Eq(column string, arg interface{}) WhereConditions

eq

func (SelectBuilder) Expr

func (b SelectBuilder) Expr(sql string, args ...interface{}) WhereConditions

expr

func (SelectBuilder) From

func (b SelectBuilder) From(from string) SelectCondition

From sets the FROM clause of the query.

func (SelectBuilder) FromSelect

func (b SelectBuilder) FromSelect(from SelectCondition, alias string) SelectCondition

FromSelect sets a subquery into the FROM clause of the query.

func (SelectBuilder) GroupBy

func (b SelectBuilder) GroupBy(groupBys ...string) WhereConditions

GroupBy adds GROUP BY expressions to the query.

func (SelectBuilder) Gt

func (b SelectBuilder) Gt(column string, arg interface{}) WhereConditions

gt

func (SelectBuilder) GtOrEq

func (b SelectBuilder) GtOrEq(column string, arg interface{}) WhereConditions

gtOrEq

func (SelectBuilder) Having

func (b SelectBuilder) Having(pred interface{}, rest ...interface{}) WhereConditions

Having adds an expression to the HAVING clause of the query.

See Where.

func (SelectBuilder) Join

func (b SelectBuilder) Join(join string, rest ...interface{}) JoinCondition

Join adds a JOIN clause to the query.

func (SelectBuilder) JoinClause

func (b SelectBuilder) JoinClause(pred interface{}, args ...interface{}) JoinCondition

JoinClause adds a join clause to the query.

func (SelectBuilder) LeftJoin

func (b SelectBuilder) LeftJoin(join string, rest ...interface{}) JoinCondition

LeftJoin adds a LEFT JOIN clause to the query.

func (SelectBuilder) Limit

func (b SelectBuilder) Limit(limit int) WhereConditions

Limit sets a LIMIT clause on the query.

func (SelectBuilder) Lt

func (b SelectBuilder) Lt(column string, arg interface{}) WhereConditions

lt

func (SelectBuilder) LtOrEq

func (b SelectBuilder) LtOrEq(column string, arg interface{}) WhereConditions

ltOrEq

func (SelectBuilder) NotEq

func (b SelectBuilder) NotEq(column string, arg interface{}) WhereConditions

func (SelectBuilder) Offset

func (b SelectBuilder) Offset(offset int) WhereConditions

Offset sets a OFFSET clause on the query.

func (SelectBuilder) Options

func (b SelectBuilder) Options(options ...string) SelectCondition

Options adds select option to the query

func (SelectBuilder) OrderBy

func (b SelectBuilder) OrderBy(orderBys ...string) WhereConditions

OrderBy adds ORDER BY expressions to the query.

func (SelectBuilder) PlaceholderFormat

func (b SelectBuilder) PlaceholderFormat(f PlaceholderFormat) WhereConditions

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (SelectBuilder) Prefix

func (b SelectBuilder) Prefix(sql string, args ...interface{}) SelectCondition

Prefix adds an expression to the beginning of the query

func (SelectBuilder) RightJoin

func (b SelectBuilder) RightJoin(join string, rest ...interface{}) JoinCondition

RightJoin adds a RIGHT JOIN clause to the query.

func (SelectBuilder) Suffix

func (b SelectBuilder) Suffix(sql string, args ...interface{}) WhereConditions

Suffix adds an expression to the end of the query

func (SelectBuilder) ToSql

func (b SelectBuilder) ToSql() (string, []interface{})

ToSql builds the query into a SQL string and bound args.

func (SelectBuilder) Where

func (b SelectBuilder) Where(pred interface{}, args ...interface{}) WhereConditions

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 SelectCondition

type SelectCondition interface {
	Prefix(string, ...interface{}) SelectCondition
	Distinct() SelectCondition
	Options(...string) SelectCondition
	Columns(...string) SelectCondition
	Column(interface{}, ...interface{}) SelectCondition
	From(string) SelectCondition
	FromSelect(SelectCondition, string) SelectCondition
	JoinCondition
}

func Select

func Select(columns ...string) SelectCondition

Select returns a new SelectBuilder, optionally setting some result columns.

See SelectBuilder.Columns.

type Sqlizer

type Sqlizer interface {
	ToSql() (string, []interface{})
}

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 builder.Builder

StatementBuilderType is the type of StatementBuilder.

func (StatementBuilderType) Condition

func (b StatementBuilderType) Condition() WhereConditions

func (StatementBuilderType) Count

func (b StatementBuilderType) Count(columns string) SelectCondition

func (StatementBuilderType) Delete

Delete returns a DeleteBuilder for this StatementBuilderType.

func (StatementBuilderType) Insert

Insert returns a InsertBuilder for this StatementBuilderType.

func (StatementBuilderType) Join

func (b StatementBuilderType) Join(join string, rest ...interface{}) JoinCondition

func (StatementBuilderType) JoinClause

func (b StatementBuilderType) JoinClause(pred interface{}, args ...interface{}) JoinCondition

func (StatementBuilderType) LeftJoin

func (b StatementBuilderType) LeftJoin(join string, rest ...interface{}) JoinCondition

func (StatementBuilderType) PlaceholderFormat

PlaceholderFormat sets the PlaceholderFormat field for any child builders.

func (StatementBuilderType) RightJoin

func (b StatementBuilderType) RightJoin(join string, rest ...interface{}) JoinCondition

func (StatementBuilderType) Select

func (b StatementBuilderType) Select(columns ...string) SelectCondition

Select returns a SelectBuilder for this StatementBuilderType.

func (StatementBuilderType) Update

Update returns a UpdateBuilder for this StatementBuilderType.

func (StatementBuilderType) Where

func (b StatementBuilderType) Where(pred interface{}, args ...interface{}) WhereConditions

type UpdateBuilder

type UpdateBuilder builder.Builder

UpdateBuilder builds SQL UPDATE statements.

func (UpdateBuilder) Condition

func (b UpdateBuilder) Condition() WhereConditions

Condition

func (UpdateBuilder) DecrBy

func (b UpdateBuilder) DecrBy(column string, num int) UpdateCondition

func (UpdateBuilder) Eq

func (b UpdateBuilder) Eq(column string, arg interface{}) WhereConditions

eq

func (UpdateBuilder) Expr

func (b UpdateBuilder) Expr(sql string, args ...interface{}) WhereConditions

expr

func (UpdateBuilder) GroupBy

func (b UpdateBuilder) GroupBy(groupBys ...string) WhereConditions

GroupBy adds GROUP BY expressions to the query.

func (UpdateBuilder) Gt

func (b UpdateBuilder) Gt(column string, arg interface{}) WhereConditions

gt

func (UpdateBuilder) GtOrEq

func (b UpdateBuilder) GtOrEq(column string, arg interface{}) WhereConditions

gtOrEq

func (UpdateBuilder) Having

func (b UpdateBuilder) Having(pred interface{}, rest ...interface{}) WhereConditions

Having adds an expression to the HAVING clause of the query.

See Where.

func (UpdateBuilder) IncrBy

func (b UpdateBuilder) IncrBy(column string, num int) UpdateCondition

func (UpdateBuilder) Limit

func (b UpdateBuilder) Limit(limit int) WhereConditions

Limit sets a LIMIT clause on the update.

func (UpdateBuilder) Lt

func (b UpdateBuilder) Lt(column string, arg interface{}) WhereConditions

lt

func (UpdateBuilder) LtOrEq

func (b UpdateBuilder) LtOrEq(column string, arg interface{}) WhereConditions

ltOrEq

func (UpdateBuilder) NotEq

func (b UpdateBuilder) NotEq(column string, arg interface{}) WhereConditions

func (UpdateBuilder) Offset

func (b UpdateBuilder) Offset(offset int) WhereConditions

Offset sets a OFFSET clause on the query.

func (UpdateBuilder) OrderBy

func (b UpdateBuilder) OrderBy(orderBys ...string) WhereConditions

OrderBy adds ORDER BY expressions to the query.

func (UpdateBuilder) PlaceholderFormat

func (b UpdateBuilder) PlaceholderFormat(f PlaceholderFormat) WhereConditions

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (UpdateBuilder) Prefix

func (b UpdateBuilder) Prefix(sql string, args ...interface{}) UpdateCondition

Prefix adds an expression to the beginning of the query

func (UpdateBuilder) Set

func (b UpdateBuilder) Set(column string, value interface{}) UpdateCondition

Set adds SET clauses to the query.

func (UpdateBuilder) SetMap

func (b UpdateBuilder) SetMap(clauses map[string]interface{}) UpdateCondition

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{}) WhereConditions

Suffix adds an expression to the end of the query

func (UpdateBuilder) Table

func (b UpdateBuilder) Table(table string) UpdateCondition

Table sets the table to be updated.

func (UpdateBuilder) ToSql

func (b UpdateBuilder) ToSql() (string, []interface{})

ToSql builds the query into a SQL string and bound args.

func (UpdateBuilder) Where

func (b UpdateBuilder) Where(pred interface{}, args ...interface{}) WhereConditions

Where adds WHERE expressions to the query.

See SelectBuilder.Where for more information.

type UpdateCondition

type UpdateCondition interface {
	Prefix(string, ...interface{}) UpdateCondition
	Table(string) UpdateCondition
	Set(string, interface{}) UpdateCondition
	IncrBy(string, int) UpdateCondition
	DecrBy(string, int) UpdateCondition
	SetMap(map[string]interface{}) UpdateCondition
	WhereConditions
}

func Update

func Update(table string) UpdateCondition

Update returns a new UpdateBuilder with the given table name.

See UpdateBuilder.Table.

type WhereBuilder

type WhereBuilder builder.Builder

WhereBuilder builds SQL SELECT statements.

func (WhereBuilder) Condition

func (b WhereBuilder) Condition() WhereConditions

Condition

func (WhereBuilder) Eq

func (b WhereBuilder) Eq(column string, arg interface{}) WhereConditions

eq

func (WhereBuilder) Expr

func (b WhereBuilder) Expr(sql string, args ...interface{}) WhereConditions

expr

func (WhereBuilder) GroupBy

func (b WhereBuilder) GroupBy(groupBys ...string) WhereConditions

GroupBy adds GROUP BY expressions to the query.

func (WhereBuilder) Gt

func (b WhereBuilder) Gt(column string, arg interface{}) WhereConditions

gt

func (WhereBuilder) GtOrEq

func (b WhereBuilder) GtOrEq(column string, arg interface{}) WhereConditions

gtOrEq

func (WhereBuilder) Having

func (b WhereBuilder) Having(pred interface{}, rest ...interface{}) WhereConditions

Having adds an expression to the HAVING clause of the query.

See Where.

func (WhereBuilder) Limit

func (b WhereBuilder) Limit(limit int) WhereConditions

Limit sets a LIMIT clause on the query.

func (WhereBuilder) Lt

func (b WhereBuilder) Lt(column string, arg interface{}) WhereConditions

lt

func (WhereBuilder) LtOrEq

func (b WhereBuilder) LtOrEq(column string, arg interface{}) WhereConditions

ltOrEq

func (WhereBuilder) NotEq

func (b WhereBuilder) NotEq(column string, arg interface{}) WhereConditions

func (WhereBuilder) Offset

func (b WhereBuilder) Offset(offset int) WhereConditions

Offset sets a OFFSET clause on the query.

func (WhereBuilder) Or

func (b WhereBuilder) Or(pred ...interface{}) WhereBuilder

or

func (WhereBuilder) OrderBy

func (b WhereBuilder) OrderBy(orderBys ...string) WhereConditions

OrderBy adds ORDER BY expressions to the query.

func (WhereBuilder) PlaceholderFormat

func (b WhereBuilder) PlaceholderFormat(f PlaceholderFormat) WhereConditions

PlaceholderFormat sets PlaceholderFormat (e.g. Question or Dollar) for the query.

func (WhereBuilder) Suffix

func (b WhereBuilder) Suffix(sql string, args ...interface{}) WhereConditions

Suffix adds an expression to the end of the query

func (WhereBuilder) ToSql

func (b WhereBuilder) ToSql() (string, []interface{})

ToSql builds the query into a SQL string and bound args.

func (WhereBuilder) Where

func (b WhereBuilder) Where(pred interface{}, args ...interface{}) WhereConditions

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 WhereConditions

type WhereConditions interface {
	ToSql() (string, []interface{})
	PlaceholderFormat(PlaceholderFormat) WhereConditions
	Where(interface{}, ...interface{}) WhereConditions
	Condition() WhereConditions
	Expr(string, ...interface{}) WhereConditions
	Eq(string, interface{}) WhereConditions
	NotEq(string, interface{}) WhereConditions
	Gt(string, interface{}) WhereConditions
	GtOrEq(string, interface{}) WhereConditions
	Lt(string, interface{}) WhereConditions
	LtOrEq(string, interface{}) WhereConditions
	OrderBy(...string) WhereConditions
	GroupBy(...string) WhereConditions
	Having(interface{}, ...interface{}) WhereConditions
	Limit(int) WhereConditions
	Offset(int) WhereConditions
	Suffix(string, ...interface{}) WhereConditions
}

func Condition

func Condition() WhereConditions

func Where

func Where(pred interface{}, args ...interface{}) WhereConditions

新增的where方法

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL