builder

package
v0.0.0-...-2bc18d8 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2017 License: BSD-2-Clause, BSD-3-Clause Imports: 5 Imported by: 0

README

SQL 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

Insert

sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()

Select

sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()

sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
		RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()

Update

sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()

Delete

sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()

Conditions

  • 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]
  • 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]
  • 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]
  • Like
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Like{"a", "c"})
// a LIKE ? [%c%]
  • 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]
  • 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]
  • 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 []
  • 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]
  • 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]
  • Between
import . "github.com/go-xorm/builder"

sql, args, _ := ToSQL(Between{"a", 1, 2})
// a BETWEEN 1 AND 2
  • Define yourself conditions

Since Cond is an interface.

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

You can define yourself conditions and compose with other Cond.

Documentation

Overview

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

Make sure you have installed Go 1.1+ and then:

go get yougam/libraries/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/insionng/yougam/libraries/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/insionng/yougam/libraries/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/insionng/yougam/libraries/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/insionng/yougam/libraries/go-xorm/builder"

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

5. Expr you can customerize your sql with Expr

import . "github.com/insionng/yougam/libraries/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/insionng/yougam/libraries/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/insionng/yougam/libraries/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/insionng/yougam/libraries/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/insionng/yougam/libraries/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/insionng/yougam/libraries/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

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSupportType    = errors.New("not supported SQL type")
	ErrNoNotInConditions = errors.New("No NOT IN conditions")
	ErrNoInConditions    = errors.New("No IN conditions")
)

Functions

func NewWriter

func NewWriter() *stringWriter

func ToSQL

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

ToSQL convert a builder or condtions to SQL and args

func WriteMap

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

WriteMap

Types

type Between

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

Between

func (Between) And

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

func (Between) IsValid

func (between Between) IsValid() bool

func (Between) Or

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

func (Between) WriteTo

func (between Between) WriteTo(w Writer) error

type Builder

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

func Delete

func Delete(conds ...Cond) *Builder

func Insert

func Insert(eq Eq) *Builder

func Select

func Select(cols ...string) *Builder

func Update

func Update(updates ...Eq) *Builder

func (*Builder) And

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

func (*Builder) CrossJoin

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

func (*Builder) Delete

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

func (*Builder) From

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

func (*Builder) FullJoin

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

func (*Builder) InnerJoin

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

func (*Builder) Insert

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

func (*Builder) Into

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

func (*Builder) Join

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

func (*Builder) LeftJoin

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

func (*Builder) Or

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

func (*Builder) RightJoin

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

func (*Builder) Select

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

func (*Builder) ToSQL

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

ToSQL convert a builder to SQL and args

func (*Builder) Update

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

func (*Builder) Where

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

func (*Builder) WriteTo

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

type Cond

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

func And

func And(conds ...Cond) Cond

func Expr

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

func In

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

func NewCond

func NewCond() Cond

func NotIn

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

func Or

func Or(conds ...Cond) Cond

type Decr

type Decr int

type Eq

type Eq map[string]interface{}

func (Eq) And

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

func (Eq) IsValid

func (eq Eq) IsValid() bool

func (Eq) Or

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

func (Eq) WriteTo

func (eq Eq) WriteTo(w Writer) error

type Gt

type Gt map[string]interface{}

Gt

func (Gt) And

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

func (Gt) IsValid

func (gt Gt) IsValid() bool

func (Gt) Or

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

func (Gt) WriteTo

func (gt Gt) WriteTo(w Writer) error

type Gte

type Gte map[string]interface{}

Gte

func (Gte) And

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

func (Gte) IsValid

func (gte Gte) IsValid() bool

func (Gte) Or

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

func (Gte) WriteTo

func (gte Gte) WriteTo(w Writer) error

type Incr

type Incr int

type IsNull

type IsNull [1]string

IsNull

func (IsNull) And

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

func (IsNull) IsValid

func (isNull IsNull) IsValid() bool

func (IsNull) Or

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

func (IsNull) WriteTo

func (isNull IsNull) WriteTo(w Writer) error

type Like

type Like [2]string

func (Like) And

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

func (Like) IsValid

func (like Like) IsValid() bool

func (Like) Or

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

func (Like) WriteTo

func (like Like) WriteTo(w Writer) error

type Lt

type Lt map[string]interface{}

Lt

func (Lt) And

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

func (Lt) IsValid

func (lt Lt) IsValid() bool

func (Lt) Or

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

func (Lt) WriteTo

func (lt Lt) WriteTo(w Writer) error

type Lte

type Lte map[string]interface{}

Lte

func (Lte) And

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

func (Lte) IsValid

func (lte Lte) IsValid() bool

func (Lte) Or

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

func (Lte) WriteTo

func (lte Lte) WriteTo(w Writer) error

type Neq

type Neq map[string]interface{}

func (Neq) And

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

func (Neq) IsValid

func (neq Neq) IsValid() bool

func (Neq) Or

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

func (Neq) WriteTo

func (neq Neq) WriteTo(w Writer) error

type Not

type Not [1]Cond

func (Not) And

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

func (Not) IsValid

func (not Not) IsValid() bool

func (Not) Or

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

func (Not) WriteTo

func (not Not) WriteTo(w Writer) error

type NotNull

type NotNull [1]string

NotNull

func (NotNull) And

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

func (NotNull) IsValid

func (notNull NotNull) IsValid() bool

func (NotNull) Or

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

func (NotNull) WriteTo

func (notNull NotNull) WriteTo(w Writer) error

type Writer

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

Jump to

Keyboard shortcuts

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