You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # SQL builder
  2. [![Build Status](https://drone.gitea.com/api/badges/xorm/builder/status.svg)](https://drone.gitea.com/xorm/builder) [![](http://gocover.io/_badge/xorm.io/builder)](http://gocover.io/xorm.io/builder)
  3. [![](https://goreportcard.com/badge/xorm.io/builder)](https://goreportcard.com/report/xorm.io/builder)
  4. Package builder is a lightweight and fast SQL builder for Go and XORM.
  5. Make sure you have installed Go 1.8+ and then:
  6. go get xorm.io/builder
  7. # Insert
  8. ```Go
  9. sql, args, err := builder.Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL()
  10. // INSERT INTO table1 SELECT * FROM table2
  11. sql, err := builder.Insert().Into("table1").Select().From("table2").ToBoundSQL()
  12. // INSERT INTO table1 (a, b) SELECT b, c FROM table2
  13. sql, err = builder.Insert("a, b").Into("table1").Select("b, c").From("table2").ToBoundSQL()
  14. ```
  15. # Select
  16. ```Go
  17. // Simple Query
  18. sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
  19. // With join
  20. sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
  21. RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
  22. // From sub query
  23. sql, args, err := Select("sub.id").From(Select("c").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
  24. // From union query
  25. sql, args, err = Select("sub.id").From(
  26. Select("id").From("table1").Where(Eq{"a": 1}).Union("all", Select("id").From("table1").Where(Eq{"a": 2})),"sub").
  27. Where(Eq{"b": 1}).ToSQL()
  28. // With order by
  29. sql, args, err = Select("a", "b", "c").From("table1").Where(Eq{"f1": "v1", "f2": "v2"}).
  30. OrderBy("a ASC").ToSQL()
  31. // With limit.
  32. // Be careful! You should set up specific dialect for builder before performing a query with LIMIT
  33. sql, args, err = Dialect(MYSQL).Select("a", "b", "c").From("table1").OrderBy("a ASC").
  34. Limit(5, 10).ToSQL()
  35. ```
  36. # Update
  37. ```Go
  38. sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL()
  39. ```
  40. # Delete
  41. ```Go
  42. sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL()
  43. ```
  44. # Union
  45. ```Go
  46. sql, args, err := Select("*").From("a").Where(Eq{"status": "1"}).
  47. Union("all", Select("*").From("a").Where(Eq{"status": "2"})).
  48. Union("distinct", Select("*").From("a").Where(Eq{"status": "3"})).
  49. Union("", Select("*").From("a").Where(Eq{"status": "4"})).
  50. ToSQL()
  51. ```
  52. # Conditions
  53. * `Eq` is a redefine of a map, you can give one or more conditions to `Eq`
  54. ```Go
  55. import . "xorm.io/builder"
  56. sql, args, _ := ToSQL(Eq{"a":1})
  57. // a=? [1]
  58. sql, args, _ := ToSQL(Eq{"b":"c"}.And(Eq{"c": 0}))
  59. // b=? AND c=? ["c", 0]
  60. sql, args, _ := ToSQL(Eq{"b":"c", "c":0})
  61. // b=? AND c=? ["c", 0]
  62. sql, args, _ := ToSQL(Eq{"b":"c"}.Or(Eq{"b":"d"}))
  63. // b=? OR b=? ["c", "d"]
  64. sql, args, _ := ToSQL(Eq{"b": []string{"c", "d"}})
  65. // b IN (?,?) ["c", "d"]
  66. sql, args, _ := ToSQL(Eq{"b": 1, "c":[]int{2, 3}})
  67. // b=? AND c IN (?,?) [1, 2, 3]
  68. ```
  69. * `Neq` is the same to `Eq`
  70. ```Go
  71. import . "xorm.io/builder"
  72. sql, args, _ := ToSQL(Neq{"a":1})
  73. // a<>? [1]
  74. sql, args, _ := ToSQL(Neq{"b":"c"}.And(Neq{"c": 0}))
  75. // b<>? AND c<>? ["c", 0]
  76. sql, args, _ := ToSQL(Neq{"b":"c", "c":0})
  77. // b<>? AND c<>? ["c", 0]
  78. sql, args, _ := ToSQL(Neq{"b":"c"}.Or(Neq{"b":"d"}))
  79. // b<>? OR b<>? ["c", "d"]
  80. sql, args, _ := ToSQL(Neq{"b": []string{"c", "d"}})
  81. // b NOT IN (?,?) ["c", "d"]
  82. sql, args, _ := ToSQL(Neq{"b": 1, "c":[]int{2, 3}})
  83. // b<>? AND c NOT IN (?,?) [1, 2, 3]
  84. ```
  85. * `Gt`, `Gte`, `Lt`, `Lte`
  86. ```Go
  87. import . "xorm.io/builder"
  88. sql, args, _ := ToSQL(Gt{"a", 1}.And(Gte{"b", 2}))
  89. // a>? AND b>=? [1, 2]
  90. sql, args, _ := ToSQL(Lt{"a", 1}.Or(Lte{"b", 2}))
  91. // a<? OR b<=? [1, 2]
  92. ```
  93. * `Like`
  94. ```Go
  95. import . "xorm.io/builder"
  96. sql, args, _ := ToSQL(Like{"a", "c"})
  97. // a LIKE ? [%c%]
  98. ```
  99. * `Expr` you can customerize your sql with `Expr`
  100. ```Go
  101. import . "xorm.io/builder"
  102. sql, args, _ := ToSQL(Expr("a = ? ", 1))
  103. // a = ? [1]
  104. sql, args, _ := ToSQL(Eq{"a": Expr("select id from table where c = ?", 1)})
  105. // a=(select id from table where c = ?) [1]
  106. ```
  107. * `In` and `NotIn`
  108. ```Go
  109. import . "xorm.io/builder"
  110. sql, args, _ := ToSQL(In("a", 1, 2, 3))
  111. // a IN (?,?,?) [1,2,3]
  112. sql, args, _ := ToSQL(In("a", []int{1, 2, 3}))
  113. // a IN (?,?,?) [1,2,3]
  114. sql, args, _ := ToSQL(In("a", Expr("select id from b where c = ?", 1))))
  115. // a IN (select id from b where c = ?) [1]
  116. ```
  117. * `IsNull` and `NotNull`
  118. ```Go
  119. import . "xorm.io/builder"
  120. sql, args, _ := ToSQL(IsNull{"a"})
  121. // a IS NULL []
  122. sql, args, _ := ToSQL(NotNull{"b"})
  123. // b IS NOT NULL []
  124. ```
  125. * `And(conds ...Cond)`, And can connect one or more condtions via And
  126. ```Go
  127. import . "xorm.io/builder"
  128. sql, args, _ := ToSQL(And(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
  129. // a=? AND b LIKE ? AND d<>? [1, %c%, 2]
  130. ```
  131. * `Or(conds ...Cond)`, Or can connect one or more conditions via Or
  132. ```Go
  133. import . "xorm.io/builder"
  134. sql, args, _ := ToSQL(Or(Eq{"a":1}, Like{"b", "c"}, Neq{"d", 2}))
  135. // a=? OR b LIKE ? OR d<>? [1, %c%, 2]
  136. sql, args, _ := ToSQL(Or(Eq{"a":1}, And(Like{"b", "c"}, Neq{"d", 2})))
  137. // a=? OR (b LIKE ? AND d<>?) [1, %c%, 2]
  138. ```
  139. * `Between`
  140. ```Go
  141. import . "xorm.io/builder"
  142. sql, args, _ := ToSQL(Between{"a", 1, 2})
  143. // a BETWEEN 1 AND 2
  144. ```
  145. * Define yourself conditions
  146. Since `Cond` is an interface.
  147. ```Go
  148. type Cond interface {
  149. WriteTo(Writer) error
  150. And(...Cond) Cond
  151. Or(...Cond) Cond
  152. IsValid() bool
  153. }
  154. ```
  155. You can define yourself conditions and compose with other `Cond`.