Documentation ¶
Overview ¶
Package sphinxql is a flexible and powerful tool to build SQL string and associated args.
Index ¶
- Variables
- func Escape(ident string) string
- func EscapeAll(ident ...string) []string
- func Flatten(slices interface{}) (flattened []interface{})
- func List(arg interface{}) interface{}
- func Named(name string, arg interface{}) interface{}
- func Raw(expr string) interface{}
- func SnakeCaseMapper(field string) string
- type Args
- type Builder
- type Cond
- func (c *Cond) And(andExpr ...string) string
- func (c *Cond) Between(field string, lower, upper interface{}) string
- func (c *Cond) E(field string, value interface{}) string
- func (c *Cond) Equal(field string, value interface{}) string
- func (c *Cond) G(field string, value interface{}) string
- func (c *Cond) GE(field string, value interface{}) string
- func (c *Cond) GreaterEqualThan(field string, value interface{}) string
- func (c *Cond) GreaterThan(field string, value interface{}) string
- func (c *Cond) In(field string, value ...interface{}) string
- func (c *Cond) IsNotNull(field string) string
- func (c *Cond) IsNull(field string) string
- func (c *Cond) L(field string, value interface{}) string
- func (c *Cond) LE(field string, value interface{}) string
- func (c *Cond) LessEqualThan(field string, value interface{}) string
- func (c *Cond) LessThan(field string, value interface{}) string
- func (c *Cond) Like(field string, value interface{}) string
- func (c *Cond) Match(value string) string
- func (c *Cond) NE(field string, value interface{}) string
- func (c *Cond) NotBetween(field string, lower, upper interface{}) string
- func (c *Cond) NotEqual(field string, value interface{}) string
- func (c *Cond) NotIn(field string, value ...interface{}) string
- func (c *Cond) NotLike(field string, value interface{}) string
- func (c *Cond) Or(orExpr ...string) string
- func (c *Cond) Var(value interface{}) string
- type DeleteBuilder
- func (db *DeleteBuilder) Build() (sql string, args []interface{})
- func (db *DeleteBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
- func (db *DeleteBuilder) DeleteFrom(table string) *DeleteBuilder
- func (db *DeleteBuilder) SQL(sql string) *DeleteBuilder
- func (db *DeleteBuilder) SetFlavor(flavor Flavor) (old Flavor)
- func (db *DeleteBuilder) String() string
- func (db *DeleteBuilder) Where(andExpr ...string) *DeleteBuilder
- type FieldMapperFunc
- type Flavor
- func (f Flavor) Interpolate(sql string, args []interface{}) (string, error)
- func (f Flavor) NewDeleteBuilder() *DeleteBuilder
- func (f Flavor) NewInsertBuilder() *InsertBuilder
- func (f Flavor) NewSelectBuilder() *SelectBuilder
- func (f Flavor) NewUpdateBuilder() *UpdateBuilder
- func (f Flavor) Quote(name string) string
- func (f Flavor) String() string
- type InsertBuilder
- func (ib *InsertBuilder) Build() (sql string, args []interface{})
- func (ib *InsertBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
- func (ib *InsertBuilder) Cols(col ...string) *InsertBuilder
- func (ib *InsertBuilder) InsertIgnoreInto(table string) *InsertBuilder
- func (ib *InsertBuilder) InsertInto(table string) *InsertBuilder
- func (ib *InsertBuilder) ReplaceInto(table string) *InsertBuilder
- func (ib *InsertBuilder) SQL(sql string) *InsertBuilder
- func (ib *InsertBuilder) SetFlavor(flavor Flavor) (old Flavor)
- func (ib *InsertBuilder) String() string
- func (ib *InsertBuilder) Values(value ...interface{}) *InsertBuilder
- func (ib *InsertBuilder) Var(arg interface{}) string
- type NamedIntegerList
- type Opt
- type OrdBy
- type RankerOptionValue
- type SelectBuilder
- func (sb *SelectBuilder) As(name, alias string) string
- func (sb *SelectBuilder) Build() (sql string, args []interface{})
- func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
- func (sb *SelectBuilder) BuilderAs(builder Builder, alias string) string
- func (sb *SelectBuilder) From(table ...string) *SelectBuilder
- func (sb *SelectBuilder) GroupBy(col ...string) *SelectBuilder
- func (sb *SelectBuilder) Having(andExpr ...string) *SelectBuilder
- func (sb *SelectBuilder) Limit(limit int) *SelectBuilder
- func (sb *SelectBuilder) Offset(offset int) *SelectBuilder
- func (sb *SelectBuilder) Option(optionExpr ...string) *SelectBuilder
- func (sb *SelectBuilder) OrderBy(orderByExpr ...string) *SelectBuilder
- func (sb *SelectBuilder) SQL(sql string) *SelectBuilder
- func (sb *SelectBuilder) Select(col ...string) *SelectBuilder
- func (sb *SelectBuilder) SetFlavor(flavor Flavor) (old Flavor)
- func (sb *SelectBuilder) String() string
- func (sb *SelectBuilder) Where(andExpr ...string) *SelectBuilder
- func (sb *SelectBuilder) WithinGroupOrderBy(withinGroupOrderByExpr ...string) *SelectBuilder
- type Struct
- func (s *Struct) Addr(value interface{}) []interface{}
- func (s *Struct) AddrForTag(tag string, value interface{}) []interface{}
- func (s *Struct) AddrWithCols(cols []string, value interface{}) []interface{}
- func (s *Struct) DeleteFrom(table string) *DeleteBuilder
- func (s *Struct) For(flavor Flavor) *Struct
- func (s *Struct) InsertIgnoreInto(table string, value ...interface{}) *InsertBuilder
- func (s *Struct) InsertIgnoreIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder
- func (s *Struct) InsertInto(table string, value ...interface{}) *InsertBuilder
- func (s *Struct) InsertIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder
- func (s *Struct) ReplaceInto(table string, value ...interface{}) *InsertBuilder
- func (s *Struct) ReplaceIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder
- func (s *Struct) SelectFrom(table string) *SelectBuilder
- func (s *Struct) SelectFromForTag(table string, tag string) *SelectBuilder
- func (s *Struct) Update(table string, value interface{}) *UpdateBuilder
- func (s *Struct) UpdateForTag(table string, tag string, value interface{}) *UpdateBuilder
- func (s *Struct) WithFieldMapper(mapper FieldMapperFunc) *Struct
- type UnquotedString
- type UpdateBuilder
- func (ub *UpdateBuilder) Add(field string, value interface{}) string
- func (ub *UpdateBuilder) Assign(field string, value interface{}) string
- func (ub *UpdateBuilder) Build() (sql string, args []interface{})
- func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
- func (ub *UpdateBuilder) Decr(field string) string
- func (ub *UpdateBuilder) Div(field string, value interface{}) string
- func (ub *UpdateBuilder) Incr(field string) string
- func (ub *UpdateBuilder) Mul(field string, value interface{}) string
- func (ub *UpdateBuilder) Option(optionExpr ...string) *UpdateBuilder
- func (ub *UpdateBuilder) SQL(sql string) *UpdateBuilder
- func (ub *UpdateBuilder) Set(assignment ...string) *UpdateBuilder
- func (ub *UpdateBuilder) SetFlavor(flavor Flavor) (old Flavor)
- func (ub *UpdateBuilder) SetMore(assignment ...string) *UpdateBuilder
- func (ub *UpdateBuilder) String() string
- func (ub *UpdateBuilder) Sub(field string, value interface{}) string
- func (ub *UpdateBuilder) Update(table string) *UpdateBuilder
- func (ub *UpdateBuilder) Where(andExpr ...string) *UpdateBuilder
Examples ¶
- Build
- BuildNamed
- Buildf
- DeleteBuilder
- DeleteBuilder.SQL
- DeleteFrom
- FieldMapperFunc
- Flavor.Interpolate (SphinxSearch)
- InsertBuilder
- InsertBuilder (InsertIgnore)
- InsertBuilder (ReplaceInto)
- InsertBuilder.SQL
- InsertIgnoreInto
- InsertInto
- ReplaceInto
- Select
- SelectBuilder
- SelectBuilder (AdvancedUsage)
- SelectBuilder (Limit_offset)
- SelectBuilder (VarInCols)
- SelectBuilder.SQL
- Struct (BuildDELETE)
- Struct (BuildINSERT)
- Struct (BuildUPDATE)
- Struct (UseStructAsORM)
- Struct (UseTag)
- Update
- UpdateBuilder
- UpdateBuilder.SQL
- UpdateBuilder.SetMore
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInterpolateNotImplemented means the method or feature is not implemented right now. ErrInterpolateNotImplemented = errors.New("go-sphinxql: interpolation for this flavor is not implemented") // ErrInterpolateMissingArgs means there are some args missing in query, so it's not possible to // prepare a query with such args. ErrInterpolateMissingArgs = errors.New("go-sphinxql: not enough args when interpolating") // ErrInterpolateUnsupportedArgs means that some types of the args are not supported. ErrInterpolateUnsupportedArgs = errors.New("go-sphinxql: unsupported args when interpolating") )
var ( // DBTag is the struct tag to describe the name for a field in struct. DBTag = "db" // FieldTag is the struct tag to describe the tag name for a field in struct. // Use "," to separate different tags. FieldTag = "fieldtag" // FieldOpt is the options for a struct field. // As db column can contain "," in theory, field options should be provided in a separated tag. FieldOpt = "fieldopt" )
var ( // DefaultFlavor is the default flavor for all builders. DefaultFlavor = SphinxSearch )
Functions ¶
func Flatten ¶
func Flatten(slices interface{}) (flattened []interface{})
Flatten recursively extracts values in slices and returns a flattened []interface{} with all values. If slices is not a slice, return `[]interface{}{slices}`.
func List ¶
func List(arg interface{}) interface{}
List marks arg as a list of data. If arg is `[]int{1, 2, 3}`, it will be compiled to `?, ?, ?` with args `[1 2 3]`.
func Named ¶
func Named(name string, arg interface{}) interface{}
Named creates a named argument. Unlike `sql.Named`, this named argument works only with `Build` or `BuildNamed` for convenience and will be replaced to a `?` after `Compile`.
func Raw ¶
func Raw(expr string) interface{}
Raw marks the expr as a raw value which will not be added to args.
func SnakeCaseMapper ¶
SnakeCaseMapper is a field mapper which can convert field name from CamelCase to snake_case.
For instance, it will convert "MyField" to "my_field".
SnakeCaseMapper uses package "xstrings" to do the conversion. See https://pkg.go.dev/github.com/huandu/xstrings#ToSnakeCase for conversion rules.
Types ¶
type Args ¶
type Args struct { // The default flavor used by `Args#Compile` Flavor Flavor // contains filtered or unexported fields }
Args stores arguments associated with a SQL.
func (*Args) Compile ¶
func (args *Args) Compile(format string, initialValue ...interface{}) (query string, values []interface{})
Compile compiles builder's format to standard sql and returns associated args.
The format string uses a special syntax to represent arguments.
$? refers successive arguments passed in the call. It works similar as `%v` in `fmt.Sprintf`. $0 $1 ... $n refers nth-argument passed in the call. Next $? will use arguments n+1. ${name} refers a named argument created by `Named` with `name`. $$ is a "$" string.
func (*Args) CompileWithFlavor ¶
func (args *Args) CompileWithFlavor(format string, flavor Flavor, initialValue ...interface{}) (query string, values []interface{})
CompileWithFlavor compiles builder's format to standard sql with flavor and returns associated args.
See doc for `Compile` to learn details.
type Builder ¶
type Builder interface { Build() (sql string, args []interface{}) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{}) }
Builder is a general SQL builder. It's used by Args to create nested SQL like the `IN` expression in `SELECT * FROM t1 WHERE id IN (SELECT id FROM t2)`.
func Build ¶
Build creates a Builder from a format string. The format string uses special syntax to represent arguments. See doc in `Args#Compile` for syntax details.
Example ¶
sb := NewSelectBuilder() sb.Select("id").From("user").Where(sb.In("status", 1, 2)) b := Build("EXPLAIN $? LEFT JOIN SELECT * FROM $? WHERE created_at > $? AND state IN (${states}) AND modified_at BETWEEN $2 AND $?", sb, Raw("banned"), 1514458225, 1514544625, Named("states", List([]int{3, 4, 5}))) s, args := b.Build() fmt.Println(s) fmt.Println(args)
Output: EXPLAIN SELECT id FROM user WHERE status IN (?, ?) LEFT JOIN SELECT * FROM banned WHERE created_at > ? AND state IN (?, ?, ?) AND modified_at BETWEEN ? AND ? [1 2 1514458225 3 4 5 1514458225 1514544625]
func BuildNamed ¶
BuildNamed creates a Builder from a format string. The format string uses `${key}` to refer the value of named by key.
Example ¶
b := BuildNamed("SELECT * FROM ${table} WHERE status IN (${status}) AND name LIKE ${name} AND created_at > ${time} AND modified_at < ${time} + 86400", map[string]interface{}{ "time": sql.Named("start", 1234567890), "status": List([]int{1, 2, 5}), "name": "Huan%", "table": Raw("user"), }) s, args := b.Build() fmt.Println(s) fmt.Println(args)
Output: SELECT * FROM user WHERE status IN (?, ?, ?) AND name LIKE ? AND created_at > @start AND modified_at < @start + 86400 [1 2 5 Huan% {{} start 1234567890}]
func Buildf ¶
Buildf creates a Builder from a format string using `fmt.Sprintf`-like syntax. As all arguments will be converted to a string internally, e.g. "$0", only `%v` and `%s` are valid.
Example ¶
sb := NewSelectBuilder() sb.Select("id").From("user") explain := Buildf("EXPLAIN %v LEFT JOIN SELECT * FROM banned WHERE state IN (%v, %v)", sb, 1, 2) s, args := explain.Build() fmt.Println(s) fmt.Println(args)
Output: EXPLAIN SELECT id FROM user LEFT JOIN SELECT * FROM banned WHERE state IN (?, ?) [1 2]
func WithFlavor ¶
WithFlavor creates a new Builder based on builder with a default flavor.
type Cond ¶
type Cond struct {
Args *Args
}
Cond provides several helper methods to build conditions.
func (*Cond) GreaterEqualThan ¶
GreaterEqualThan represents "field >= value".
func (*Cond) GreaterThan ¶
GreaterThan represents "field > value".
func (*Cond) LessEqualThan ¶
LessEqualThan represents "field <= value".
func (*Cond) NotBetween ¶
NotBetween represents "field NOT BETWEEN lower AND upper".
type DeleteBuilder ¶
type DeleteBuilder struct { Cond // contains filtered or unexported fields }
DeleteBuilder is a builder to build DELETE.
Example ¶
db := NewDeleteBuilder() db.DeleteFrom("demo.user") db.Where( db.GreaterThan("id", 1234), db.Like("name", "%Du"), db.Or( db.IsNull("id_card"), db.In("status", 1, 2, 5), ), "modified_at > created_at + "+db.Var(86400), // It's allowed to write arbitrary SQL. ) sql, args := db.Build() fmt.Println(sql) fmt.Println(args)
Output: DELETE FROM demo.user WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND modified_at > created_at + ? [1234 %Du 1 2 5 86400]
func DeleteFrom ¶
func DeleteFrom(table string) *DeleteBuilder
DeleteFrom sets table name in DELETE.
Example ¶
sql := DeleteFrom("demo.user"). Where( "status = 1", ). String() fmt.Println(sql)
Output: DELETE FROM demo.user WHERE status = 1
func NewDeleteBuilder ¶
func NewDeleteBuilder() *DeleteBuilder
NewDeleteBuilder creates a new DELETE builder.
func (*DeleteBuilder) Build ¶
func (db *DeleteBuilder) Build() (sql string, args []interface{})
Build returns compiled DELETE string and args. They can be used in `DB#Query` of package `database/sql` directly.
func (*DeleteBuilder) BuildWithFlavor ¶
func (db *DeleteBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
BuildWithFlavor returns compiled DELETE string and args with flavor and initial args. They can be used in `DB#Query` of package `database/sql` directly.
func (*DeleteBuilder) DeleteFrom ¶
func (db *DeleteBuilder) DeleteFrom(table string) *DeleteBuilder
DeleteFrom sets table name in DELETE.
func (*DeleteBuilder) SQL ¶
func (db *DeleteBuilder) SQL(sql string) *DeleteBuilder
SQL adds an arbitrary sql to current position.
Example ¶
db := NewDeleteBuilder() db.SQL(`/* before */`) db.DeleteFrom("demo.user") db.SQL("PARTITION (p0)") db.Where( db.GreaterThan("id", 1234), ) db.SQL("/* after where */") sql, args := db.Build() fmt.Println(sql) fmt.Println(args)
Output: /* before */ DELETE FROM demo.user PARTITION (p0) WHERE id > ? /* after where */ [1234]
func (*DeleteBuilder) SetFlavor ¶
func (db *DeleteBuilder) SetFlavor(flavor Flavor) (old Flavor)
SetFlavor sets the flavor of compiled sql.
func (*DeleteBuilder) String ¶
func (db *DeleteBuilder) String() string
String returns the compiled DELETE string.
func (*DeleteBuilder) Where ¶
func (db *DeleteBuilder) Where(andExpr ...string) *DeleteBuilder
Where sets expressions of WHERE in DELETE.
type FieldMapperFunc ¶
FieldMapperFunc is a func to map struct field names to column names, which will be used in query as columns.
Example ¶
type Orders struct { ID int64 UserID int64 ProductName string Status int UserAddrLine1 string UserAddrLine2 string CreatedAt time.Time } // Create a Struct for Orders. orders := NewStruct(new(Orders)) // Set the default field mapper to snake_case mapper globally. DefaultFieldMapper = SnakeCaseMapper // Field names are converted to snake_case words. sql1, _ := orders.SelectFrom("orders").Limit(10).Build() fmt.Println(sql1) // Changing the default field mapper will *NOT* affect field names in orders. // Once field name conversion is done, they will not be changed again. DefaultFieldMapper = SomeOtherMapper sql2, _ := orders.SelectFrom("orders").Limit(10).Build() fmt.Println(sql1 == sql2)
Output: SELECT orders.id, orders.user_id, orders.product_name, orders.status, orders.user_addr_line1, orders.user_addr_line2, orders.created_at FROM orders LIMIT 10 true
var ( // DefaultFieldMapper is the default field name to table column name mapper func. // It's nil by default which means field name will be kept as it is. // // If a Struct has its own mapper func, the DefaultFieldMapper is ignored in this Struct. // Field tag has precedence over all kinds of field mapper functions. // // Field mapper is called only once on a Struct when the Struct is used to create builder for the first time. DefaultFieldMapper FieldMapperFunc )
type Flavor ¶
type Flavor int
Flavor is the flag to control the format of compiled sql.
const (
SphinxSearch Flavor
)
Supported flavors.
func (Flavor) Interpolate ¶
Interpolate parses sql returned by `Args#Compile` or `Builder`, and interpolate args to replace placeholders in the sql.
If there are some args missing in sql, e.g. the number of placeholders are larger than len(args), returns ErrMissingArgs error.
Example (SphinxSearch) ¶
sb := SphinxSearch.NewSelectBuilder() sb.Select("name").From("user").Where( sb.NE("id", 1234), sb.E("name", "Charmy Liu"), sb.Like("desc", "%mother's day%"), ) sql, args := sb.Build() query, err := SphinxSearch.Interpolate(sql, args) fmt.Println(query) fmt.Println(err)
Output: SELECT name FROM user WHERE id <> 1234 AND name = 'Charmy Liu' AND desc LIKE '%mother\'s day%' <nil>
func (Flavor) NewDeleteBuilder ¶
func (f Flavor) NewDeleteBuilder() *DeleteBuilder
NewDeleteBuilder creates a new DELETE builder with flavor.
func (Flavor) NewInsertBuilder ¶
func (f Flavor) NewInsertBuilder() *InsertBuilder
NewInsertBuilder creates a new INSERT builder with flavor.
func (Flavor) NewSelectBuilder ¶
func (f Flavor) NewSelectBuilder() *SelectBuilder
NewSelectBuilder creates a new SELECT builder with flavor.
func (Flavor) NewUpdateBuilder ¶
func (f Flavor) NewUpdateBuilder() *UpdateBuilder
NewUpdateBuilder creates a new UPDATE builder with flavor.
type InsertBuilder ¶
type InsertBuilder struct {
// contains filtered or unexported fields
}
InsertBuilder is a builder to build INSERT.
Example ¶
ib := NewInsertBuilder() ib.InsertInto("demo.user") ib.Cols("id", "name", "status", "created_at") ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())")) ib.Values(2, "Charmy Liu", 1, 1234567890) sql, args := ib.Build() fmt.Println(sql) fmt.Println(args)
Output: INSERT INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?) [1 Huan Du 1 2 Charmy Liu 1 1234567890]
Example (InsertIgnore) ¶
ib := NewInsertBuilder() ib.InsertIgnoreInto("demo.user") ib.Cols("id", "name", "status", "created_at") ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())")) ib.Values(2, "Charmy Liu", 1, 1234567890) sql, args := ib.Build() fmt.Println(sql) fmt.Println(args)
Output: INSERT IGNORE INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?) [1 Huan Du 1 2 Charmy Liu 1 1234567890]
Example (ReplaceInto) ¶
ib := NewInsertBuilder() ib.ReplaceInto("demo.user") ib.Cols("id", "name", "status", "created_at") ib.Values(1, "Huan Du", 1, Raw("UNIX_TIMESTAMP(NOW())")) ib.Values(2, "Charmy Liu", 1, 1234567890) sql, args := ib.Build() fmt.Println(sql) fmt.Println(args)
Output: REPLACE INTO demo.user (id, name, status, created_at) VALUES (?, ?, ?, UNIX_TIMESTAMP(NOW())), (?, ?, ?, ?) [1 Huan Du 1 2 Charmy Liu 1 1234567890]
func InsertIgnoreInto ¶
func InsertIgnoreInto(table string) *InsertBuilder
InsertIgnoreInto sets table name in INSERT IGNORE.
Example ¶
sql, args := InsertIgnoreInto("demo.user"). Cols("id", "name", "status"). Values(4, "Sample", 2). Build() fmt.Println(sql) fmt.Println(args)
Output: INSERT IGNORE INTO demo.user (id, name, status) VALUES (?, ?, ?) [4 Sample 2]
func InsertInto ¶
func InsertInto(table string) *InsertBuilder
InsertInto sets table name in INSERT.
Example ¶
sql, args := InsertInto("demo.user"). Cols("id", "name", "status"). Values(4, "Sample", 2). Build() fmt.Println(sql) fmt.Println(args)
Output: INSERT INTO demo.user (id, name, status) VALUES (?, ?, ?) [4 Sample 2]
func NewInsertBuilder ¶
func NewInsertBuilder() *InsertBuilder
NewInsertBuilder creates a new INSERT builder.
func ReplaceInto ¶
func ReplaceInto(table string) *InsertBuilder
ReplaceInto sets table name and changes the verb of ib to REPLACE. REPLACE INTO is a SphinxSearch extension to the SQL standard.
Example ¶
sql, args := ReplaceInto("demo.user"). Cols("id", "name", "status"). Values(4, "Sample", 2). Build() fmt.Println(sql) fmt.Println(args)
Output: REPLACE INTO demo.user (id, name, status) VALUES (?, ?, ?) [4 Sample 2]
func (*InsertBuilder) Build ¶
func (ib *InsertBuilder) Build() (sql string, args []interface{})
Build returns compiled INSERT string and args. They can be used in `DB#Query` of package `database/sql` directly.
func (*InsertBuilder) BuildWithFlavor ¶
func (ib *InsertBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
BuildWithFlavor returns compiled INSERT string and args with flavor and initial args. They can be used in `DB#Query` of package `database/sql` directly.
func (*InsertBuilder) Cols ¶
func (ib *InsertBuilder) Cols(col ...string) *InsertBuilder
Cols sets columns in INSERT.
func (*InsertBuilder) InsertIgnoreInto ¶
func (ib *InsertBuilder) InsertIgnoreInto(table string) *InsertBuilder
InsertIgnoreInto sets table name in INSERT IGNORE.
func (*InsertBuilder) InsertInto ¶
func (ib *InsertBuilder) InsertInto(table string) *InsertBuilder
InsertInto sets table name in INSERT.
func (*InsertBuilder) ReplaceInto ¶
func (ib *InsertBuilder) ReplaceInto(table string) *InsertBuilder
ReplaceInto sets table name and changes the verb of ib to REPLACE. REPLACE INTO is a SphinxSearch extension to the SQL standard.
func (*InsertBuilder) SQL ¶
func (ib *InsertBuilder) SQL(sql string) *InsertBuilder
SQL adds an arbitrary sql to current position.
Example ¶
ib := NewInsertBuilder() ib.SQL("/* before */") ib.InsertInto("demo.user") ib.SQL("PARTITION (p0)") ib.Cols("id", "name", "status", "created_at") ib.SQL("/* after cols */") ib.Values(3, "Shawn Du", 1, 1234567890) ib.SQL(ib.Var(Build("ON DUPLICATE KEY UPDATE status = $?", 1))) sql, args := ib.Build() fmt.Println(sql) fmt.Println(args)
Output: /* before */ INSERT INTO demo.user PARTITION (p0) (id, name, status, created_at) /* after cols */ VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE status = ? [3 Shawn Du 1 1234567890 1]
func (*InsertBuilder) SetFlavor ¶
func (ib *InsertBuilder) SetFlavor(flavor Flavor) (old Flavor)
SetFlavor sets the flavor of compiled sql.
func (*InsertBuilder) String ¶
func (ib *InsertBuilder) String() string
String returns the compiled INSERT string.
func (*InsertBuilder) Values ¶
func (ib *InsertBuilder) Values(value ...interface{}) *InsertBuilder
Values adds a list of values for a row in INSERT.
func (*InsertBuilder) Var ¶
func (ib *InsertBuilder) Var(arg interface{}) string
Var returns a placeholder for value.
type NamedIntegerList ¶
NamedIntegerList represents named integer list for options.
type Opt ¶
type Opt struct {
Args *Args
}
Opt provides several helper methods to build options.
func (*Opt) ExportRanker ¶
ExportRanker builds a ranker = export(expr) OPTION.
func (*Opt) ExprRanker ¶
ExprRanker builds a ranker = expr(expr) OPTION.
func (*Opt) FieldWeights ¶
func (o *Opt) FieldWeights(values NamedIntegerList) string
FieldWeights builds a field_weights OPTION.
func (*Opt) MaxMatches ¶
MaxMatches builds a max_matches OPTION.
func (*Opt) Ranker ¶
func (o *Opt) Ranker(value RankerOptionValue) string
Ranker builds a ranker OPTION.
type RankerOptionValue ¶
type RankerOptionValue = UnquotedString
RankerOptionValue is an alias of UnquotedString.
const ( RankerProximityBM25 RankerOptionValue = "proximity_bm25" RankerBM25 RankerOptionValue = "bm25" RankerNone RankerOptionValue = "none" RankerWordCount RankerOptionValue = "wordcount" RankerProximity RankerOptionValue = "proximity" RankerMatchAny RankerOptionValue = "matchany" RankerFieldMask RankerOptionValue = "fieldmask" RankerSPH04 RankerOptionValue = "sph04" RankerExpr RankerOptionValue = "expr" RankerExport RankerOptionValue = "export" )
RankerOptionValue enum
type SelectBuilder ¶
SelectBuilder is a builder to build SELECT.
Example ¶
sb := NewSelectBuilder() sb.Select("id", "name", sb.As("COUNT(*)", "t")) sb.From("demo.user") sb.Where( sb.Match("@(name) test"), sb.GreaterThan("id", 1234), sb.Like("name", "%Du"), sb.Or( sb.IsNull("id_card"), sb.In("status", 1, 2, 5), ), sb.NotIn( "id", NewSelectBuilder().Select("id").From("banned"), ), // Nested SELECT. "modified_at > created_at + "+sb.Var(86400), // It's allowed to write arbitrary SQL. ) sb.GroupBy("status").Having(sb.NotIn("status", 4, 5)) sb.WithinGroupOrderBy(sb.Desc("status")) sb.OrderBy( sb.Asc("modified_at"), ) sb.Limit(10).Offset(5) sb.Option( sb.Comment("kekw"), sb.Ranker(RankerWordCount), ) s, args := sb.Build() fmt.Println(s) fmt.Println(args)
Output: SELECT id, name, COUNT(*) AS t FROM demo.user WHERE MATCH(?) AND id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND id NOT IN (SELECT id FROM banned) AND modified_at > created_at + ? GROUP BY status HAVING status NOT IN (?, ?) WITHIN GROUP ORDER BY status DESC ORDER BY modified_at ASC LIMIT 5,10 OPTION comment = ?, ranker = ? [@(name) test 1234 %Du 1 2 5 86400 4 5 kekw wordcount]
Example (AdvancedUsage) ¶
sb := NewSelectBuilder() innerSb := NewSelectBuilder() sb.Select("id", "name") sb.From( sb.BuilderAs(innerSb, "user"), ) sb.Where( sb.In("status", Flatten([]int{1, 2, 3})...), sb.Between("created_at", sql.Named("start", 1234567890), sql.Named("end", 1234599999)), ) sb.OrderBy( sb.Desc("modified_at"), ) innerSb.Select("*") innerSb.From("banned") innerSb.Where( innerSb.NotIn("name", Flatten([]string{"Huan Du", "Charmy Liu"})...), ) s, args := sb.Build() fmt.Println(s) fmt.Println(args)
Output: SELECT id, name FROM (SELECT * FROM banned WHERE name NOT IN (?, ?)) AS user WHERE status IN (?, ?, ?) AND created_at BETWEEN @start AND @end ORDER BY modified_at DESC [Huan Du Charmy Liu 1 2 3 {{} start 1234567890} {{} end 1234599999}]
Example (Limit_offset) ¶
flavors := []Flavor{SphinxSearch} results := make([][]string, len(flavors)) sb := NewSelectBuilder() saveResults := func() { for i, f := range flavors { s, _ := sb.BuildWithFlavor(f) results[i] = append(results[i], s) } } sb.Select("*") sb.From("user") // Case #1: limit < 0 and offset < 0 // // All: No limit or offset in query. sb.Limit(-1) sb.Offset(-1) saveResults() // Case #2: limit < 0 and offset >= 0 // // SphinxSearch: Ignore offset if the limit is not set. sb.Limit(-1) sb.Offset(0) saveResults() // Case #3: limit >= 0 and offset >= 0 // // All: Set both limit and offset. sb.Limit(1) sb.Offset(0) saveResults() // Case #4: limit >= 0 and offset < 0 // // All: Set limit in query. sb.Limit(1) sb.Offset(-1) saveResults() for i, result := range results { fmt.Println() fmt.Println(flavors[i]) for n, s := range result { fmt.Printf("#%d: %s\n", n+1, s) } }
Output: SphinxSearch #1: SELECT * FROM user #2: SELECT * FROM user #3: SELECT * FROM user LIMIT 0,1 #4: SELECT * FROM user LIMIT 1
Example (VarInCols) ¶
// Column name may contain some characters, e.g. the $ sign, which have special meanings in builders. // It's recommended to call Escape() or EscapeAll() to escape the name. sb := NewSelectBuilder() v := sb.Var("foo") sb.Select(Escape("colHasA$Sign"), v) sb.From("table") s, args := sb.Build() fmt.Println(s) fmt.Println(args)
Output: SELECT colHasA$Sign, ? FROM table [foo]
func NewSelectBuilder ¶
func NewSelectBuilder() *SelectBuilder
NewSelectBuilder creates a new SELECT builder.
func Select ¶
func Select(col ...string) *SelectBuilder
Select sets columns in SELECT.
Example ¶
// Build a SQL to create a HIVE table. s := Select("columns[0] id", "columns[1] name", "columns[2] year"). From("`all-users.csv`"). Limit(100). String() fmt.Println(s)
Output: SELECT columns[0] id, columns[1] name, columns[2] year FROM `all-users.csv` LIMIT 100
func (*SelectBuilder) As ¶
func (sb *SelectBuilder) As(name, alias string) string
As returns an AS expression.
func (*SelectBuilder) Build ¶
func (sb *SelectBuilder) Build() (sql string, args []interface{})
Build returns compiled SELECT string and args. They can be used in `DB#Query` of package `database/sql` directly.
func (*SelectBuilder) BuildWithFlavor ¶
func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
BuildWithFlavor returns compiled SELECT string and args with flavor and initial args. They can be used in `DB#Query` of package `database/sql` directly.
func (*SelectBuilder) BuilderAs ¶
func (sb *SelectBuilder) BuilderAs(builder Builder, alias string) string
BuilderAs returns an AS expression wrapping a complex SQL. According to SQL syntax, SQL built by builder is surrounded by parens.
func (*SelectBuilder) From ¶
func (sb *SelectBuilder) From(table ...string) *SelectBuilder
From sets table names in SELECT.
func (*SelectBuilder) GroupBy ¶
func (sb *SelectBuilder) GroupBy(col ...string) *SelectBuilder
GroupBy sets columns of GROUP BY in SELECT.
func (*SelectBuilder) Having ¶
func (sb *SelectBuilder) Having(andExpr ...string) *SelectBuilder
Having sets expressions of HAVING in SELECT.
func (*SelectBuilder) Limit ¶
func (sb *SelectBuilder) Limit(limit int) *SelectBuilder
Limit sets the LIMIT in SELECT.
func (*SelectBuilder) Offset ¶
func (sb *SelectBuilder) Offset(offset int) *SelectBuilder
Offset sets the LIMIT offset in SELECT.
func (*SelectBuilder) Option ¶
func (sb *SelectBuilder) Option(optionExpr ...string) *SelectBuilder
Option sets expressions of OPTION in SELECT.
func (*SelectBuilder) OrderBy ¶
func (sb *SelectBuilder) OrderBy(orderByExpr ...string) *SelectBuilder
OrderBy sets expressions of ORDER BY in SELECT.
func (*SelectBuilder) SQL ¶
func (sb *SelectBuilder) SQL(sql string) *SelectBuilder
SQL adds an arbitrary sql to current position.
Example ¶
sb := NewSelectBuilder() sb.SQL("/* before */") sb.Select("u.id", "u.name", "c.type", "p.nickname") sb.SQL("/* after select */") sb.From("user u") sb.SQL("/* after from */") sb.Where( "u.modified_at > u.created_at", ) sb.SQL("/* after where */") sb.OrderBy("id") sb.SQL("/* after order by */") sb.Limit(10) sb.SQL("/* after limit */") s := sb.String() fmt.Println(s)
Output: /* before */ SELECT u.id, u.name, c.type, p.nickname /* after select */ FROM user u /* after from */ WHERE u.modified_at > u.created_at /* after where */ ORDER BY id /* after order by */ LIMIT 10 /* after limit */
func (*SelectBuilder) Select ¶
func (sb *SelectBuilder) Select(col ...string) *SelectBuilder
Select sets columns in SELECT.
func (*SelectBuilder) SetFlavor ¶
func (sb *SelectBuilder) SetFlavor(flavor Flavor) (old Flavor)
SetFlavor sets the flavor of compiled sql.
func (*SelectBuilder) String ¶
func (sb *SelectBuilder) String() string
String returns the compiled SELECT string.
func (*SelectBuilder) Where ¶
func (sb *SelectBuilder) Where(andExpr ...string) *SelectBuilder
Where sets expressions of WHERE in SELECT.
func (*SelectBuilder) WithinGroupOrderBy ¶
func (sb *SelectBuilder) WithinGroupOrderBy(withinGroupOrderByExpr ...string) *SelectBuilder
WithinGroupOrderBy sets expressions of WITHIN GROUP ORDER BY in SELECT.
type Struct ¶
type Struct struct { Flavor Flavor // contains filtered or unexported fields }
Struct represents a struct type.
All methods in Struct are thread-safe. We can define a global variable to hold a Struct and use it in any goroutine.
Example (BuildDELETE) ¶
// Suppose we defined following type and global variable. // // type User struct { // ID int64 `db:"id"` // Name string `db:"name"` // Status int `db:"status"` // } // // var userStruct = NewStruct(new(User)) // Prepare DELETE query. user := &User{ ID: 1234, Name: "Huan Du", Status: 1, } b := userStruct.DeleteFrom("user") b.Where(b.E("id", user.ID)) // Execute the query. sql, args := b.Build() db.Exec(sql, args...) fmt.Println(sql) fmt.Println(args)
Output: DELETE FROM user WHERE id = ? [1234]
Example (BuildINSERT) ¶
// Suppose we defined following type and global variable. // // type User struct { // ID int64 `db:"id"` // Name string `db:"name"` // Status int `db:"status"` // } // // var userStruct = NewStruct(new(User)) // Prepare INSERT query. user := &User{ ID: 1234, Name: "Huan Du", Status: 1, } ib := userStruct.InsertInto("user", user) // Execute the query. sql, args := ib.Build() db.Exec(sql, args...) fmt.Println(sql) fmt.Println(args)
Output: INSERT INTO user (id, name, status) VALUES (?, ?, ?) [1234 Huan Du 1]
Example (BuildUPDATE) ¶
// Suppose we defined following type and global variable. // // type User struct { // ID int64 `db:"id"` // Name string `db:"name"` // Status int `db:"status"` // } // // var userStruct = NewStruct(new(User)) // Prepare UPDATE query. user := &User{ ID: 1234, Name: "Huan Du", Status: 1, } ub := userStruct.Update("user", user) ub.Where(ub.E("id", user.ID)) // Execute the query. sql, args := ub.Build() db.Exec(sql, args...) fmt.Println(sql) fmt.Println(args)
Output: UPDATE user SET id = ?, name = ?, status = ? WHERE id = ? [1234 Huan Du 1 1234]
Example (UseStructAsORM) ¶
// Suppose we defined following type and global variable. // // type User struct { // ID int64 `db:"id"` // Name string `db:"name"` // Status int `db:"status"` // } // // var userStruct = NewStruct(new(User)) // Prepare SELECT query. sb := userStruct.SelectFrom("user") sb.Where(sb.E("id", 1234)) // Execute the query. sql, args := sb.Build() rows, _ := db.Query(sql, args...) defer func(rows testRows) { _ = rows.Close() }(rows) // Scan row data to user. var user User _ = rows.Scan(userStruct.Addr(&user)...) fmt.Println(sql) fmt.Println(args) fmt.Printf("%#v", user)
Output: SELECT user.id, user.name, user.status FROM user WHERE id = ? [1234] sphinxql.User{ID:1234, Name:"huandu", Status:1}
Example (UseTag) ¶
// Suppose we defined following type and global variable. // // type Order struct { // ID int64 `db:"id" fieldtag:"update,paid"` // State int `db:"state" fieldtag:"paid"` // SkuID int64 `db:"sku_id"` // UserID int64 `db:"user_id"` // Price int64 `db:"price" fieldtag:"update"` // Discount int64 `db:"discount" fieldtag:"update"` // Desc string `db:"desc" fieldtag:"update" fieldopt:"withquote"` // `desc` is a keyword. // CreatedAt int64 `db:"created_at"` // ModifiedAt int64 `db:"modified_at" fieldtag:"update,paid"` // } // // var orderStruct = NewStruct(new(Order)) createOrder := func(table string) { now := time.Now().Unix() order := &Order{ ID: 1234, State: OrderStateCreated, SkuID: 5678, UserID: 7527, Price: 1000, Discount: 0, Desc: "Best goods", CreatedAt: now, ModifiedAt: now, } b := orderStruct.InsertInto(table, &order) sql, args := b.Build() db.Exec(sql, args) fmt.Println(sql) } updatePrice := func(table string) { tag := "update" // Read order from database. var order Order sql, args := orderStruct.SelectFromForTag(table, tag).Where("id = 1234").Build() rows, _ := db.Query(sql, args...) defer func(rows testRows) { _ = rows.Close() }(rows) _ = rows.Scan(orderStruct.AddrForTag(tag, &order)...) // Discount for this user. // Use tag "update" to update necessary columns only. order.Discount += 100 order.ModifiedAt = time.Now().Unix() // Save the order. b := orderStruct.UpdateForTag(table, tag, &order) b.Where(b.E("id", order.ID)) sql, args = b.Build() db.Exec(sql, args...) fmt.Println(sql) } updateState := func(table string) { tag := "paid" // Read order from database. var order Order sql, args := orderStruct.SelectFromForTag(table, tag).Where("id = 1234").Build() rows, _ := db.Query(sql, args...) defer func(rows testRows) { _ = rows.Close() }(rows) _ = rows.Scan(orderStruct.AddrForTag(tag, &order)...) // Update state to paid when user has paid for the order. // Use tag "paid" to update necessary columns only. if order.State != OrderStateCreated { // Report state error here. return } // Update order state. order.State = OrderStatePaid order.ModifiedAt = time.Now().Unix() // Save the order. b := orderStruct.UpdateForTag(table, tag, &order) b.Where(b.E("id", order.ID)) sql, args = b.Build() db.Exec(sql, args...) fmt.Println(sql) } table := "order" createOrder(table) updatePrice(table) updateState(table) fmt.Println("done")
Output: INSERT INTO order (id, state, sku_id, user_id, price, discount, `desc`, created_at, modified_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) UPDATE order SET price = ?, discount = ?, `desc` = ?, modified_at = ? WHERE id = ? done
func NewStruct ¶
func NewStruct(structValue interface{}) *Struct
NewStruct analyzes type information in structValue and creates a new Struct with all structValue fields. If structValue is not a struct, NewStruct returns a dummy Struct.
func (*Struct) Addr ¶
func (s *Struct) Addr(value interface{}) []interface{}
Addr takes address of all exported fields of the s from the value. The returned result can be used in `Row#Scan` directly.
func (*Struct) AddrForTag ¶
AddrForTag takes address of all fields of the s tagged with tag from the value. The returned result can be used in `Row#Scan` directly.
If tag is not defined in s in advance,
func (*Struct) AddrWithCols ¶
AddrWithCols takes address of all columns defined in cols from the value. The returned result can be used in `Row#Scan` directly.
func (*Struct) DeleteFrom ¶
func (s *Struct) DeleteFrom(table string) *DeleteBuilder
DeleteFrom creates a new `DeleteBuilder` with table name.
Caller is responsible to set WHERE condition to match right record.
func (*Struct) For ¶
For sets the default flavor of s and returns a shadow copy of s. The original s.Flavor is not changed.
func (*Struct) InsertIgnoreInto ¶
func (s *Struct) InsertIgnoreInto(table string, value ...interface{}) *InsertBuilder
InsertIgnoreInto creates a new `InsertBuilder` with table name using verb INSERT IGNORE INTO. By default, all exported fields of s are set as columns by calling `InsertBuilder#Cols`, and value is added as a list of values by calling `InsertBuilder#Values`.
InsertIgnoreInto never returns any error. If the type of any item in value is not expected, it will be ignored. If value is an empty slice, `InsertBuilder#Values` will not be called.
func (*Struct) InsertIgnoreIntoForTag ¶
func (s *Struct) InsertIgnoreIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder
InsertIgnoreIntoForTag creates a new `InsertBuilder` with table name using verb INSERT IGNORE INTO. By default, exported fields tagged with tag are set as columns by calling `InsertBuilder#Cols`, and value is added as a list of values by calling `InsertBuilder#Values`.
InsertIgnoreIntoForTag never returns any error. If the type of any item in value is not expected, it will be ignored. If value is an empty slice, `InsertBuilder#Values` will not be called.
func (*Struct) InsertInto ¶
func (s *Struct) InsertInto(table string, value ...interface{}) *InsertBuilder
InsertInto creates a new `InsertBuilder` with table name using verb INSERT INTO. By default, all exported fields of s are set as columns by calling `InsertBuilder#Cols`, and value is added as a list of values by calling `InsertBuilder#Values`.
InsertInto never returns any error. If the type of any item in value is not expected, it will be ignored. If value is an empty slice, `InsertBuilder#Values` will not be called.
func (*Struct) InsertIntoForTag ¶
func (s *Struct) InsertIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder
InsertIntoForTag creates a new `InsertBuilder` with table name using verb INSERT INTO. By default, exported fields tagged with tag are set as columns by calling `InsertBuilder#Cols`, and value is added as a list of values by calling `InsertBuilder#Values`.
InsertIntoForTag never returns any error. If the type of any item in value is not expected, it will be ignored. If value is an empty slice, `InsertBuilder#Values` will not be called.
func (*Struct) ReplaceInto ¶
func (s *Struct) ReplaceInto(table string, value ...interface{}) *InsertBuilder
ReplaceInto creates a new `InsertBuilder` with table name using verb REPLACE INTO. By default, all exported fields of s are set as columns by calling `InsertBuilder#Cols`, and value is added as a list of values by calling `InsertBuilder#Values`.
ReplaceInto never returns any error. If the type of any item in value is not expected, it will be ignored. If value is an empty slice, `InsertBuilder#Values` will not be called.
func (*Struct) ReplaceIntoForTag ¶
func (s *Struct) ReplaceIntoForTag(table string, tag string, value ...interface{}) *InsertBuilder
ReplaceIntoForTag creates a new `InsertBuilder` with table name using verb REPLACE INTO. By default, exported fields tagged with tag are set as columns by calling `InsertBuilder#Cols`, and value is added as a list of values by calling `InsertBuilder#Values`.
ReplaceIntoForTag never returns any error. If the type of any item in value is not expected, it will be ignored. If value is an empty slice, `InsertBuilder#Values` will not be called.
func (*Struct) SelectFrom ¶
func (s *Struct) SelectFrom(table string) *SelectBuilder
SelectFrom creates a new `SelectBuilder` with table name. By default, all exported fields of the s are listed as columns in SELECT.
Caller is responsible to set WHERE condition to find right record.
func (*Struct) SelectFromForTag ¶
func (s *Struct) SelectFromForTag(table string, tag string) *SelectBuilder
SelectFromForTag creates a new `SelectBuilder` with table name for a specified tag. By default, all fields of the s tagged with tag are listed as columns in SELECT.
Caller is responsible to set WHERE condition to find right record.
func (*Struct) Update ¶
func (s *Struct) Update(table string, value interface{}) *UpdateBuilder
Update creates a new `UpdateBuilder` with table name. By default, all exported fields of the s is assigned in UPDATE with the field values from value. If value's type is not the same as that of s, Update returns a dummy `UpdateBuilder` with table name.
Caller is responsible to set WHERE condition to match right record.
func (*Struct) UpdateForTag ¶
func (s *Struct) UpdateForTag(table string, tag string, value interface{}) *UpdateBuilder
UpdateForTag creates a new `UpdateBuilder` with table name. By default, all fields of the s tagged with tag is assigned in UPDATE with the field values from value. If value's type is not the same as that of s, UpdateForTag returns a dummy `UpdateBuilder` with table name.
Caller is responsible to set WHERE condition to match right record.
func (*Struct) WithFieldMapper ¶
func (s *Struct) WithFieldMapper(mapper FieldMapperFunc) *Struct
WithFieldMapper returns a new Struct based on s with custom field mapper. The original s is not changed.
type UnquotedString ¶
type UnquotedString string
UnquotedString is a string which will be passed as-is during interpolation.
type UpdateBuilder ¶
UpdateBuilder is a builder to build UPDATE.
Example ¶
ub := NewUpdateBuilder() ub.Update("demo.user") ub.Set( ub.Assign("type", "sys"), ub.Incr("credit"), "modified_at = UNIX_TIMESTAMP(NOW())", // It's allowed to write arbitrary SQL. ) ub.Where( ub.GreaterThan("id", 1234), ub.Like("name", "%Du"), ub.Or( ub.IsNull("id_card"), ub.In("status", 1, 2, 5), ), "modified_at > created_at + "+ub.Var(86400), // It's allowed to write arbitrary SQL. ) ub.Option( ub.Comment("kekw"), ) sql, args := ub.Build() fmt.Println(sql) fmt.Println(args)
Output: UPDATE demo.user SET type = ?, credit = credit + 1, modified_at = UNIX_TIMESTAMP(NOW()) WHERE id > ? AND name LIKE ? AND (id_card IS NULL OR status IN (?, ?, ?)) AND modified_at > created_at + ? OPTION comment = ? [sys 1234 %Du 1 2 5 86400 kekw]
func NewUpdateBuilder ¶
func NewUpdateBuilder() *UpdateBuilder
NewUpdateBuilder creates a new UPDATE builder.
func Update ¶
func Update(table string) *UpdateBuilder
Update sets table name in UPDATE.
Example ¶
sql := Update("demo.user"). Set( "visited = visited + 1", ). Where( "id = 1234", ). String() fmt.Println(sql)
Output: UPDATE demo.user SET visited = visited + 1 WHERE id = 1234
func (*UpdateBuilder) Add ¶
func (ub *UpdateBuilder) Add(field string, value interface{}) string
Add represents SET "field = field + value" in UPDATE.
func (*UpdateBuilder) Assign ¶
func (ub *UpdateBuilder) Assign(field string, value interface{}) string
Assign represents SET "field = value" in UPDATE.
func (*UpdateBuilder) Build ¶
func (ub *UpdateBuilder) Build() (sql string, args []interface{})
Build returns compiled UPDATE string and args. They can be used in `DB#Query` of package `database/sql` directly.
func (*UpdateBuilder) BuildWithFlavor ¶
func (ub *UpdateBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}) (sql string, args []interface{})
BuildWithFlavor returns compiled UPDATE string and args with flavor and initial args. They can be used in `DB#Query` of package `database/sql` directly.
func (*UpdateBuilder) Decr ¶
func (ub *UpdateBuilder) Decr(field string) string
Decr represents SET "field = field - 1" in UPDATE.
func (*UpdateBuilder) Div ¶
func (ub *UpdateBuilder) Div(field string, value interface{}) string
Div represents SET "field = field / value" in UPDATE.
func (*UpdateBuilder) Incr ¶
func (ub *UpdateBuilder) Incr(field string) string
Incr represents SET "field = field + 1" in UPDATE.
func (*UpdateBuilder) Mul ¶
func (ub *UpdateBuilder) Mul(field string, value interface{}) string
Mul represents SET "field = field * value" in UPDATE.
func (*UpdateBuilder) Option ¶
func (ub *UpdateBuilder) Option(optionExpr ...string) *UpdateBuilder
Option sets expressions of OPTION in UPDATE.
func (*UpdateBuilder) SQL ¶
func (ub *UpdateBuilder) SQL(sql string) *UpdateBuilder
SQL adds an arbitrary sql to current position.
Example ¶
ub := NewUpdateBuilder() ub.SQL("/* before */") ub.Update("demo.user") ub.SQL("/* after update */") ub.Set( ub.Assign("type", "sys"), ) ub.SQL("/* after set */") sql := ub.String() fmt.Println(sql)
Output: /* before */ UPDATE demo.user /* after update */ SET type = ? /* after set */
func (*UpdateBuilder) Set ¶
func (ub *UpdateBuilder) Set(assignment ...string) *UpdateBuilder
Set sets the assignments in SET.
func (*UpdateBuilder) SetFlavor ¶
func (ub *UpdateBuilder) SetFlavor(flavor Flavor) (old Flavor)
SetFlavor sets the flavor of compiled sql.
func (*UpdateBuilder) SetMore ¶
func (ub *UpdateBuilder) SetMore(assignment ...string) *UpdateBuilder
SetMore appends the assignments in SET.
Example ¶
ub := NewUpdateBuilder() ub.Update("demo.user") ub.Set( ub.Assign("type", "sys"), ub.Incr("credit"), ) ub.SetMore( "modified_at = UNIX_TIMESTAMP(NOW())", // It's allowed to write arbitrary SQL. ) sql, args := ub.Build() fmt.Println(sql) fmt.Println(args)
Output: UPDATE demo.user SET type = ?, credit = credit + 1, modified_at = UNIX_TIMESTAMP(NOW()) [sys]
func (*UpdateBuilder) String ¶
func (ub *UpdateBuilder) String() string
String returns the compiled UPDATE string.
func (*UpdateBuilder) Sub ¶
func (ub *UpdateBuilder) Sub(field string, value interface{}) string
Sub represents SET "field = field - value" in UPDATE.
func (*UpdateBuilder) Update ¶
func (ub *UpdateBuilder) Update(table string) *UpdateBuilder
Update sets table name in UPDATE.
func (*UpdateBuilder) Where ¶
func (ub *UpdateBuilder) Where(andExpr ...string) *UpdateBuilder
Where sets expressions of WHERE in UPDATE.