Lightweight and fast SQL builder for Go language
Go to file
2022-09-30 12:53:11 -04:00
testdata add sql fiddler & test data sql scripts 2018-08-21 16:50:59 +08:00
.drone.yml Update drone.yml to add compatible. (#90) 2022-08-10 13:30:07 +08:00
.gitignore convert nil to null string and add .gitignore (#68) 2019-10-18 13:58:48 +00:00
as.go add aliasing to joins 2022-09-29 13:28:04 -04:00
builder_b_test.go update From to allow for As support to avoid multiple ways of doing the same thing 2022-09-29 13:43:50 -04:00
builder_conflict_test.go all pending changes 2022-09-30 11:42:22 -04:00
builder_conflict.go all pending changes 2022-09-30 11:42:22 -04:00
builder_delete_test.go updates to syntactically correct deletes 2022-09-30 12:50:37 -04:00
builder_delete.go updates to syntactically correct deletes 2022-09-30 12:50:37 -04:00
builder_insert_test.go all pending changes 2022-09-30 11:42:22 -04:00
builder_insert.go all pending changes 2022-09-30 11:42:22 -04:00
builder_join_test.go updates to syntactically correct deletes 2022-09-30 12:50:37 -04:00
builder_join.go updates to syntactically correct deletes 2022-09-30 12:50:37 -04:00
builder_limit_test.go fix statements 2022-09-30 12:53:11 -04:00
builder_limit.go updates to syntactically correct deletes 2022-09-30 12:50:37 -04:00
builder_select_test.go update From to allow for As support to avoid multiple ways of doing the same thing 2022-09-29 13:43:50 -04:00
builder_select.go add aliasing to joins 2022-09-29 13:28:04 -04:00
builder_set_operations_test.go Support for INTERSECT and EXCEPT (#64) 2019-09-26 01:52:50 +00:00
builder_set_operations.go all pending changes 2022-09-30 11:42:22 -04:00
builder_test.go update From to allow for As support to avoid multiple ways of doing the same thing 2022-09-29 13:43:50 -04:00
builder_update_test.go Fix bug when update with no Where (#69) 2019-10-18 09:08:16 +00:00
builder_update.go Fix bug when update with no Where (#69) 2019-10-18 09:08:16 +00:00
builder.go all pending changes 2022-09-30 11:42:22 -04:00
cond_and.go Expose Express struct (#86) 2022-05-31 09:51:26 +08:00
cond_between.go Expose Express struct (#86) 2022-05-31 09:51:26 +08:00
cond_compare.go Expose Express struct (#86) 2022-05-31 09:51:26 +08:00
cond_eq.go all pending changes 2022-09-30 11:42:22 -04:00
cond_exists_test.go Support Exists and NotExists (#84) 2022-04-01 20:07:12 +08:00
cond_exists.go Support Exists and NotExists (#84) 2022-04-01 20:07:12 +08:00
cond_if_test.go fix bug on condif (#54) 2019-04-22 16:26:13 +08:00
cond_if.go fix bug on condif (#54) 2019-04-22 16:26:13 +08:00
cond_in.go Improve IN and NOT IN queries (#76) 2022-07-07 08:18:02 +08:00
cond_like.go add more tests 2018-04-26 22:25:18 +08:00
cond_neq.go Expose Express struct (#86) 2022-05-31 09:51:26 +08:00
cond_not_exists.go Support Exists and NotExists (#84) 2022-04-01 20:07:12 +08:00
cond_not.go add more tests 2018-04-26 22:25:18 +08:00
cond_notin.go Improve IN and NOT IN queries (#76) 2022-07-07 08:18:02 +08:00
cond_null.go golint fixed 2016-12-11 13:24:10 +08:00
cond_or.go Expose Express struct (#86) 2022-05-31 09:51:26 +08:00
cond_test.go Improve IN and NOT IN queries (#76) 2022-07-07 08:18:02 +08:00
cond.go Writer support all stringbuilder's methods (#61) 2019-09-22 07:47:15 +00:00
doc.go fix README 2019-06-17 22:28:53 +08:00
error.go all pending changes 2022-09-30 11:42:22 -04:00
expr.go Add methods for expr (#88) 2022-05-31 10:00:08 +08:00
go.mod Fix limitation missed after ToSQL and enable sqlfidder tests (#75) 2020-03-22 01:50:57 +00:00
go.sum Fix limitation missed after ToSQL and enable sqlfidder tests (#75) 2020-03-22 01:50:57 +00:00
LICENSE added LICENSE 2016-09-22 13:16:43 +08:00
README.md Support Exists and NotExists (#84) 2022-04-01 20:07:12 +08:00
sql_test.go Fix CI (#87) 2022-05-31 09:54:58 +08:00
sql.go Fix sql arg nil 2020-12-22 20:59:54 +08:00
sqlfiddle_test.go Fix CI (#87) 2022-05-31 09:54:58 +08:00
writer.go Writer support all stringbuilder's methods (#61) 2019-09-22 07:47:15 +00:00

SQL builder

Build Status

Package builder is a lightweight and fast SQL builder for Go and XORM.

Make sure you have installed Go 1.8+ and then:

go get xorm.io/builder

Insert

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

// INSERT INTO table1 SELECT * FROM table2
sql, err := builder.Insert().Into("table1").Select().From("table2").ToBoundSQL()

// INSERT INTO table1 (a, b) SELECT b, c FROM table2
sql, err = builder.Insert("a, b").Into("table1").Select("b, c").From("table2").ToBoundSQL()

Select

// Simple Query
sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
// With join
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()
// From sub query
sql, args, err := Select("sub.id").From(Select("c").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
// From union query
sql, args, err = Select("sub.id").From(
	Select("id").From("table1").Where(Eq{"a": 1}).Union("all", Select("id").From("table1").Where(Eq{"a": 2})),"sub").
	Where(Eq{"b": 1}).ToSQL()
// With order by
sql, args, err = Select("a", "b", "c").From("table1").Where(Eq{"f1": "v1", "f2": "v2"}).
		OrderBy("a ASC").ToSQL()
// With limit.
// Be careful! You should set up specific dialect for builder before performing a query with LIMIT
sql, args, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").OrderBy("a ASC").
		Limit(5, 10).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()

Union

sql, args, err := Select("*").From("a").Where(Eq{"status": "1"}).
		Union("all", Select("*").From("a").Where(Eq{"status": "2"})).
		Union("distinct", Select("*").From("a").Where(Eq{"status": "3"})).
		Union("", Select("*").From("a").Where(Eq{"status": "4"})).
		ToSQL()

Conditions

  • Eq is a redefine of a map, you can give one or more conditions to Eq
import . "xorm.io/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 . "xorm.io/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 . "xorm.io/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 . "xorm.io/builder"

sql, args, _ := ToSQL(Like{"a", "c"})
// a LIKE ? [%c%]
  • Expr you can customerize your sql with Expr
import . "xorm.io/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 . "xorm.io/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(NotIn("a", Expr("select id from b where c = ?", 1))))
// a NOT IN (select id from b where c = ?) [1]
  • Exists and NotExists
import . "xorm.io/builder"

sql, args, _ := ToSQL(Exists(Select("a").From("table")))
// EXISTS (SELECT a FROM table)
sql, args, _ := ToSQL(NotExists(Select("a").From("table")))
// NOT EXISTS (SELECT a FROM table)
  • IsNull and NotNull
import . "xorm.io/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 conditions via And
import . "xorm.io/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 . "xorm.io/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 . "xorm.io/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.