builder: github.com/go-xorm/builder Index | Files

package builder

import "github.com/go-xorm/builder"

Package builder is a simple and powerful sql builder for Go.

Make sure you have installed Go 1.1+ and then:

go get github.com/go-xorm/builder

WARNNING: Currently, only query conditions are supported. Below is the supported conditions.

1. Eq is a redefine of a map, you can give one or more conditions to Eq

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Eq{"a":1})
// a=? [1]
sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
// b=? AND c=? ["c", 0]
sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
// b=? AND c=? ["c", 0]
sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
// b=? OR b=? ["c", "d"]
sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
// b IN (?,?) ["c", "d"]
sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
// b=? AND c IN (?,?) [1, 2, 3]

2. Neq is the same to Eq

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Neq{"a":1})
// a<>? [1]
sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
// b<>? AND c<>? ["c", 0]
sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
// b<>? AND c<>? ["c", 0]
sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
// b<>? OR b<>? ["c", "d"]
sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
// b NOT IN (?,?) ["c", "d"]
sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
// b<>? AND c NOT IN (?,?) [1, 2, 3]

3. Gt, Gte, Lt, Lte

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
// a>? AND b>=? [1, 2]
sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
// a<? OR b<=? [1, 2]

4. Like

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Like{"a", "c"})
// a LIKE ? [%c%]

5. Expr you can customerize your sql with Expr

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Expr("a = ? ", 1))
// a = ? [1]
sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
// a=(select id from table where c = ?) [1]

6. In and NotIn

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(In("a", 1, 2, 3))
// a IN (?,?,?) [1,2,3]
sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
// a IN (?,?,?) [1,2,3]
sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
// a IN (select id from b where c = ?) [1]

7. IsNull and NotNull

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(IsNull{"a"})
// a IS NULL []
sql, args, _ := ToSQL(NotNull{"b"})
 // b IS NOT NULL []

8. And(conds ...Cond), And can connect one or more condtions via AND

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
// a=? AND b LIKE ? AND d<>? [1, %c%, 2]

9. Or(conds ...Cond), Or can connect one or more conditions via Or

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
// a=? OR b LIKE ? OR d<>? [1, %c%, 2]
sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
// a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]

10. Between

import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Between("a", 1, 2))
// a BETWEEN 1 AND 2

11. define yourself conditions Since Cond is a interface, you can define yourself conditions and compare with them

Index

Package Files

builder.go builder_delete.go builder_insert.go builder_select.go builder_update.go cond.go cond_and.go cond_between.go cond_compare.go cond_eq.go cond_expr.go cond_in.go cond_like.go cond_neq.go cond_not.go cond_notin.go cond_null.go cond_or.go doc.go error.go

Variables

var (
    // ErrNotSupportType not supported SQL type error
    ErrNotSupportType = errors.New("not supported SQL type")
    // ErrNoNotInConditions no NOT IN params error
    ErrNoNotInConditions = errors.New("No NOT IN conditions")
    // ErrNoInConditions no IN params error
    ErrNoInConditions = errors.New("No IN conditions")
)

func ToSQL Uses

func ToSQL(cond interface{}) (string, []interface{}, error)

ToSQL convert a builder or condtions to SQL and args

func WriteMap Uses

func WriteMap(w Writer, data map[string]interface{}, op string) error

WriteMap writes conditions' SQL to Writer, op could be =, <>, >, <, <=, >= and etc.

type Between Uses

type Between struct {
    Col     string
    LessVal interface{}
    MoreVal interface{}
}

Between implmentes between condition

func (Between) And Uses

func (between Between) And(conds ...Cond) Cond

And implments And with other conditions

func (Between) IsValid Uses

func (between Between) IsValid() bool

IsValid tests if the condition is valid

func (Between) Or Uses

func (between Between) Or(conds ...Cond) Cond

Or implments Or with other conditions

func (Between) WriteTo Uses

func (between Between) WriteTo(w Writer) error

WriteTo write data to Writer

type Builder Uses

type Builder struct {
    // contains filtered or unexported fields
}

Builder describes a SQL statement

func Delete Uses

func Delete(conds ...Cond) *Builder

Delete creates a delete Builder

func Insert Uses

func Insert(eq Eq) *Builder

Insert creates an insert Builder

func Select Uses

func Select(cols ...string) *Builder

Select creates a select Builder

func Update Uses

func Update(updates ...Eq) *Builder

Update creates an update Builder

func (*Builder) And Uses

func (b *Builder) And(cond Cond) *Builder

And sets AND condition

func (*Builder) CrossJoin Uses

func (b *Builder) CrossJoin(joinTable string, joinCond interface{}) *Builder

CrossJoin sets cross join SQL

func (*Builder) Delete Uses

func (b *Builder) Delete(conds ...Cond) *Builder

Delete sets delete SQL

func (*Builder) From Uses

func (b *Builder) From(tableName string) *Builder

From sets the table name

func (*Builder) FullJoin Uses

func (b *Builder) FullJoin(joinTable string, joinCond interface{}) *Builder

FullJoin sets full join SQL

func (*Builder) InnerJoin Uses

func (b *Builder) InnerJoin(joinTable string, joinCond interface{}) *Builder

InnerJoin sets inner join

func (*Builder) Insert Uses

func (b *Builder) Insert(eq Eq) *Builder

Insert sets insert SQL

func (*Builder) Into Uses

func (b *Builder) Into(tableName string) *Builder

Into sets insert table name

func (*Builder) Join Uses

func (b *Builder) Join(joinType, joinTable string, joinCond interface{}) *Builder

Join sets join table and contions

func (*Builder) LeftJoin Uses

func (b *Builder) LeftJoin(joinTable string, joinCond interface{}) *Builder

LeftJoin sets left join SQL

func (*Builder) Or Uses

func (b *Builder) Or(cond Cond) *Builder

Or sets OR condition

func (*Builder) RightJoin Uses

func (b *Builder) RightJoin(joinTable string, joinCond interface{}) *Builder

RightJoin sets right join SQL

func (*Builder) Select Uses

func (b *Builder) Select(cols ...string) *Builder

Select sets select SQL

func (*Builder) ToSQL Uses

func (b *Builder) ToSQL() (string, []interface{}, error)

ToSQL convert a builder to SQL and args

func (*Builder) Update Uses

func (b *Builder) Update(updates ...Eq) *Builder

Update sets update SQL

func (*Builder) Where Uses

func (b *Builder) Where(cond Cond) *Builder

Where sets where SQL

func (*Builder) WriteTo Uses

func (b *Builder) WriteTo(w Writer) error

WriteTo implements Writer interface

type BytesWriter Uses

type BytesWriter struct {
    // contains filtered or unexported fields
}

BytesWriter implments Writer and save SQL in bytes.Buffer

func NewWriter Uses

func NewWriter() *BytesWriter

NewWriter creates a new string writer

func (*BytesWriter) Append Uses

func (s *BytesWriter) Append(args ...interface{})

Append appends args to Writer

func (*BytesWriter) Write Uses

func (s *BytesWriter) Write(buf []byte) (int, error)

Write writes data to Writer

type Cond Uses

type Cond interface {
    WriteTo(Writer) error
    And(...Cond) Cond
    Or(...Cond) Cond
    IsValid() bool
}

Cond defines an interface

func And Uses

func And(conds ...Cond) Cond

And generates AND conditions

func Expr Uses

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

Expr generate customerize SQL

func In Uses

func In(col string, values ...interface{}) Cond

In generates IN condition

func NewCond Uses

func NewCond() Cond

NewCond creates an empty condition

func NotIn Uses

func NotIn(col string, values ...interface{}) Cond

NotIn generate NOT IN condition

func Or Uses

func Or(conds ...Cond) Cond

Or sets OR conditions

type Decr Uses

type Decr int

Decr implements a type used by Eq

type Eq Uses

type Eq map[string]interface{}

Eq defines equals conditions

func (Eq) And Uses

func (eq Eq) And(conds ...Cond) Cond

And implements And with other conditions

func (Eq) IsValid Uses

func (eq Eq) IsValid() bool

IsValid tests if this Eq is valid

func (Eq) Or Uses

func (eq Eq) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Eq) WriteTo Uses

func (eq Eq) WriteTo(w Writer) error

WriteTo writes SQL to Writer

type Gt Uses

type Gt map[string]interface{}

Gt defines > condition

func (Gt) And Uses

func (gt Gt) And(conds ...Cond) Cond

And implements And with other conditions

func (Gt) IsValid Uses

func (gt Gt) IsValid() bool

IsValid tests if this Eq is valid

func (Gt) Or Uses

func (gt Gt) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Gt) WriteTo Uses

func (gt Gt) WriteTo(w Writer) error

WriteTo write SQL to Writer

type Gte Uses

type Gte map[string]interface{}

Gte defines >= condition

func (Gte) And Uses

func (gte Gte) And(conds ...Cond) Cond

And implements And with other conditions

func (Gte) IsValid Uses

func (gte Gte) IsValid() bool

IsValid tests if this Eq is valid

func (Gte) Or Uses

func (gte Gte) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Gte) WriteTo Uses

func (gte Gte) WriteTo(w Writer) error

WriteTo write SQL to Writer

type Incr Uses

type Incr int

Incr implements a type used by Eq

type IsNull Uses

type IsNull [1]string

IsNull defines IS NULL condition

func (IsNull) And Uses

func (isNull IsNull) And(conds ...Cond) Cond

And implements And with other conditions

func (IsNull) IsValid Uses

func (isNull IsNull) IsValid() bool

IsValid tests if this condition is valid

func (IsNull) Or Uses

func (isNull IsNull) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (IsNull) WriteTo Uses

func (isNull IsNull) WriteTo(w Writer) error

WriteTo write SQL to Writer

type Like Uses

type Like [2]string

Like defines like condition

func (Like) And Uses

func (like Like) And(conds ...Cond) Cond

And implements And with other conditions

func (Like) IsValid Uses

func (like Like) IsValid() bool

IsValid tests if this condition is valid

func (Like) Or Uses

func (like Like) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Like) WriteTo Uses

func (like Like) WriteTo(w Writer) error

WriteTo write SQL to Writer

type Lt Uses

type Lt map[string]interface{}

Lt defines < condition

func (Lt) And Uses

func (lt Lt) And(conds ...Cond) Cond

And implements And with other conditions

func (Lt) IsValid Uses

func (lt Lt) IsValid() bool

IsValid tests if this Eq is valid

func (Lt) Or Uses

func (lt Lt) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Lt) WriteTo Uses

func (lt Lt) WriteTo(w Writer) error

WriteTo write SQL to Writer

type Lte Uses

type Lte map[string]interface{}

Lte defines <= condition

func (Lte) And Uses

func (lte Lte) And(conds ...Cond) Cond

And implements And with other conditions

func (Lte) IsValid Uses

func (lte Lte) IsValid() bool

IsValid tests if this Eq is valid

func (Lte) Or Uses

func (lte Lte) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Lte) WriteTo Uses

func (lte Lte) WriteTo(w Writer) error

WriteTo write SQL to Writer

type Neq Uses

type Neq map[string]interface{}

Neq defines not equal conditions

func (Neq) And Uses

func (neq Neq) And(conds ...Cond) Cond

And implements And with other conditions

func (Neq) IsValid Uses

func (neq Neq) IsValid() bool

IsValid tests if this condition is valid

func (Neq) Or Uses

func (neq Neq) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Neq) WriteTo Uses

func (neq Neq) WriteTo(w Writer) error

WriteTo writes SQL to Writer

type Not Uses

type Not [1]Cond

Not defines NOT condition

func (Not) And Uses

func (not Not) And(conds ...Cond) Cond

And implements And with other conditions

func (Not) IsValid Uses

func (not Not) IsValid() bool

IsValid tests if this condition is valid

func (Not) Or Uses

func (not Not) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (Not) WriteTo Uses

func (not Not) WriteTo(w Writer) error

WriteTo writes SQL to Writer

type NotNull Uses

type NotNull [1]string

NotNull defines NOT NULL condition

func (NotNull) And Uses

func (notNull NotNull) And(conds ...Cond) Cond

And implements And with other conditions

func (NotNull) IsValid Uses

func (notNull NotNull) IsValid() bool

IsValid tests if this condition is valid

func (NotNull) Or Uses

func (notNull NotNull) Or(conds ...Cond) Cond

Or implements Or with other conditions

func (NotNull) WriteTo Uses

func (notNull NotNull) WriteTo(w Writer) error

WriteTo write SQL to Writer

type Writer Uses

type Writer interface {
    io.Writer
    Append(...interface{})
}

Writer defines the interface

Package builder imports 6 packages (graph) and is imported by 16 packages. Updated 2017-10-30. Refresh now. Tools for package owners.