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

278 lines
12 KiB

2 years ago
  1. // Copyright 2019 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "fmt"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestBuilder_Union(t *testing.T) {
  11. sql, args, err := Select("*").From("t1").Where(Eq{"status": "1"}).
  12. Union("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  13. Union("distinct", Select("*").From("t2").Where(Eq{"status": "3"})).
  14. Union("", Select("*").From("t2").Where(Eq{"status": "3"})).
  15. ToSQL()
  16. assert.NoError(t, err)
  17. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) UNION ALL (SELECT * FROM t2 WHERE status=?) UNION DISTINCT (SELECT * FROM t2 WHERE status=?) UNION (SELECT * FROM t2 WHERE status=?)", sql)
  18. assert.EqualValues(t, []interface{}{"1", "2", "3", "3"}, args)
  19. // sub-query will inherit dialect from the main one
  20. sql, args, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  21. Union("all", Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  22. Union("", Select("*").From("t2").Where(Eq{"status": "3"})).
  23. ToSQL()
  24. assert.NoError(t, err)
  25. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) UNION ALL (SELECT * FROM t2 WHERE status=? LIMIT 10) UNION (SELECT * FROM t2 WHERE status=?)", sql)
  26. assert.EqualValues(t, []interface{}{"1", "2", "3"}, args)
  27. // will raise error
  28. _, _, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  29. Union("all", Oracle().Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  30. ToSQL()
  31. assert.Error(t, err)
  32. assert.EqualValues(t, ErrInconsistentDialect, err)
  33. // will raise error
  34. _, _, err = Select("*").From("table1").Where(Eq{"a": "1"}).
  35. Union("all", Select("*").From("table2").Where(Eq{"a": "2"})).
  36. Where(Eq{"a": 2}).Limit(5, 10).
  37. ToSQL()
  38. assert.Error(t, err)
  39. assert.EqualValues(t, ErrNotUnexpectedUnionConditions, err)
  40. // will raise error
  41. _, _, err = Delete(Eq{"a": 1}).From("t1").
  42. Union("all", Select("*").From("t2").Where(Eq{"status": "2"})).ToSQL()
  43. assert.Error(t, err)
  44. assert.EqualValues(t, ErrUnsupportedUnionMembers, err)
  45. // will be overwrote by SELECT op
  46. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  47. Union("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  48. Select("*").From("t2").ToSQL()
  49. assert.NoError(t, err)
  50. fmt.Println(sql, args)
  51. // will be overwrote by DELETE op
  52. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  53. Union("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  54. Delete(Eq{"status": "1"}).From("t2").ToSQL()
  55. assert.NoError(t, err)
  56. fmt.Println(sql, args)
  57. // will be overwrote by INSERT op
  58. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  59. Union("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  60. Insert(Eq{"status": "1"}).Into("t2").ToSQL()
  61. assert.NoError(t, err)
  62. fmt.Println(sql, args)
  63. }
  64. func TestBuilder_Intersect(t *testing.T) {
  65. sql, args, err := Select("*").From("t1").Where(Eq{"status": "1"}).
  66. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  67. Intersect("distinct", Select("*").From("t2").Where(Eq{"status": "3"})).
  68. Intersect("", Select("*").From("t2").Where(Eq{"status": "3"})).
  69. ToSQL()
  70. assert.NoError(t, err)
  71. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) INTERSECT ALL (SELECT * FROM t2 WHERE status=?) INTERSECT DISTINCT (SELECT * FROM t2 WHERE status=?) INTERSECT (SELECT * FROM t2 WHERE status=?)", sql)
  72. assert.EqualValues(t, []interface{}{"1", "2", "3", "3"}, args)
  73. // sub-query will inherit dialect from the main one
  74. sql, args, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  75. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  76. Intersect("", Select("*").From("t2").Where(Eq{"status": "3"})).
  77. ToSQL()
  78. assert.NoError(t, err)
  79. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) INTERSECT ALL (SELECT * FROM t2 WHERE status=? LIMIT 10) INTERSECT (SELECT * FROM t2 WHERE status=?)", sql)
  80. assert.EqualValues(t, []interface{}{"1", "2", "3"}, args)
  81. // will raise error
  82. _, _, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  83. Intersect("all", Oracle().Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  84. ToSQL()
  85. assert.Error(t, err)
  86. assert.EqualValues(t, ErrInconsistentDialect, err)
  87. // will raise error
  88. _, _, err = Select("*").From("table1").Where(Eq{"a": "1"}).
  89. Intersect("all", Select("*").From("table2").Where(Eq{"a": "2"})).
  90. Where(Eq{"a": 2}).Limit(5, 10).
  91. ToSQL()
  92. assert.Error(t, err)
  93. assert.EqualValues(t, ErrNotUnexpectedUnionConditions, err)
  94. // will raise error
  95. _, _, err = Delete(Eq{"a": 1}).From("t1").
  96. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).ToSQL()
  97. assert.Error(t, err)
  98. assert.EqualValues(t, ErrUnsupportedUnionMembers, err)
  99. // will be overwrote by SELECT op
  100. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  101. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  102. Select("*").From("t2").ToSQL()
  103. assert.NoError(t, err)
  104. fmt.Println(sql, args)
  105. // will be overwrote by DELETE op
  106. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  107. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  108. Delete(Eq{"status": "1"}).From("t2").ToSQL()
  109. assert.NoError(t, err)
  110. fmt.Println(sql, args)
  111. // will be overwrote by INSERT op
  112. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  113. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  114. Insert(Eq{"status": "1"}).Into("t2").ToSQL()
  115. assert.NoError(t, err)
  116. fmt.Println(sql, args)
  117. }
  118. func TestBuilder_Except(t *testing.T) {
  119. sql, args, err := Select("*").From("t1").Where(Eq{"status": "1"}).
  120. Except("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  121. Except("distinct", Select("*").From("t2").Where(Eq{"status": "3"})).
  122. Except("", Select("*").From("t2").Where(Eq{"status": "3"})).
  123. ToSQL()
  124. assert.NoError(t, err)
  125. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) EXCEPT ALL (SELECT * FROM t2 WHERE status=?) EXCEPT DISTINCT (SELECT * FROM t2 WHERE status=?) EXCEPT (SELECT * FROM t2 WHERE status=?)", sql)
  126. assert.EqualValues(t, []interface{}{"1", "2", "3", "3"}, args)
  127. // sub-query will inherit dialect from the main one
  128. sql, args, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  129. Except("all", Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  130. Except("", Select("*").From("t2").Where(Eq{"status": "3"})).
  131. ToSQL()
  132. assert.NoError(t, err)
  133. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) EXCEPT ALL (SELECT * FROM t2 WHERE status=? LIMIT 10) EXCEPT (SELECT * FROM t2 WHERE status=?)", sql)
  134. assert.EqualValues(t, []interface{}{"1", "2", "3"}, args)
  135. // will raise error
  136. _, _, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  137. Except("all", Oracle().Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  138. ToSQL()
  139. assert.Error(t, err)
  140. assert.EqualValues(t, ErrInconsistentDialect, err)
  141. // will raise error
  142. _, _, err = Select("*").From("table1").Where(Eq{"a": "1"}).
  143. Except("all", Select("*").From("table2").Where(Eq{"a": "2"})).
  144. Where(Eq{"a": 2}).Limit(5, 10).
  145. ToSQL()
  146. assert.Error(t, err)
  147. assert.EqualValues(t, ErrNotUnexpectedUnionConditions, err)
  148. // will raise error
  149. _, _, err = Delete(Eq{"a": 1}).From("t1").
  150. Except("all", Select("*").From("t2").Where(Eq{"status": "2"})).ToSQL()
  151. assert.Error(t, err)
  152. assert.EqualValues(t, ErrUnsupportedUnionMembers, err)
  153. // will be overwrote by SELECT op
  154. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  155. Except("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  156. Select("*").From("t2").ToSQL()
  157. assert.NoError(t, err)
  158. fmt.Println(sql, args)
  159. // will be overwrote by DELETE op
  160. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  161. Except("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  162. Delete(Eq{"status": "1"}).From("t2").ToSQL()
  163. assert.NoError(t, err)
  164. fmt.Println(sql, args)
  165. // will be overwrote by INSERT op
  166. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  167. Except("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  168. Insert(Eq{"status": "1"}).Into("t2").ToSQL()
  169. assert.NoError(t, err)
  170. fmt.Println(sql, args)
  171. }
  172. func TestBuilder_SetOperations(t *testing.T) {
  173. sql, args, err := Select("*").From("t1").Where(Eq{"status": "1"}).
  174. Union("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  175. Intersect("distinct", Select("*").From("t2").Where(Eq{"status": "3"})).
  176. Except("", Select("*").From("t2").Where(Eq{"status": "3"})).
  177. ToSQL()
  178. assert.NoError(t, err)
  179. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) UNION ALL (SELECT * FROM t2 WHERE status=?) INTERSECT DISTINCT (SELECT * FROM t2 WHERE status=?) EXCEPT (SELECT * FROM t2 WHERE status=?)", sql)
  180. assert.EqualValues(t, []interface{}{"1", "2", "3", "3"}, args)
  181. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  182. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  183. Union("distinct", Select("*").From("t2").Where(Eq{"status": "3"})).
  184. Except("", Select("*").From("t2").Where(Eq{"status": "3"})).
  185. ToSQL()
  186. assert.NoError(t, err)
  187. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) INTERSECT ALL (SELECT * FROM t2 WHERE status=?) UNION DISTINCT (SELECT * FROM t2 WHERE status=?) EXCEPT (SELECT * FROM t2 WHERE status=?)", sql)
  188. assert.EqualValues(t, []interface{}{"1", "2", "3", "3"}, args)
  189. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  190. Except("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  191. Intersect("distinct", Select("*").From("t2").Where(Eq{"status": "3"})).
  192. Union("", Select("*").From("t2").Where(Eq{"status": "3"})).
  193. ToSQL()
  194. assert.NoError(t, err)
  195. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) EXCEPT ALL (SELECT * FROM t2 WHERE status=?) INTERSECT DISTINCT (SELECT * FROM t2 WHERE status=?) UNION (SELECT * FROM t2 WHERE status=?)", sql)
  196. assert.EqualValues(t, []interface{}{"1", "2", "3", "3"}, args)
  197. // sub-query will inherit dialect from the main one
  198. sql, args, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  199. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  200. Intersect("", Select("*").From("t2").Where(Eq{"status": "3"})).
  201. ToSQL()
  202. assert.NoError(t, err)
  203. assert.EqualValues(t, "(SELECT * FROM t1 WHERE status=?) INTERSECT ALL (SELECT * FROM t2 WHERE status=? LIMIT 10) INTERSECT (SELECT * FROM t2 WHERE status=?)", sql)
  204. assert.EqualValues(t, []interface{}{"1", "2", "3"}, args)
  205. // will raise error
  206. _, _, err = MySQL().Select("*").From("t1").Where(Eq{"status": "1"}).
  207. Intersect("all", Oracle().Select("*").From("t2").Where(Eq{"status": "2"}).Limit(10)).
  208. ToSQL()
  209. assert.Error(t, err)
  210. assert.EqualValues(t, ErrInconsistentDialect, err)
  211. // will raise error
  212. _, _, err = Select("*").From("table1").Where(Eq{"a": "1"}).
  213. Intersect("all", Select("*").From("table2").Where(Eq{"a": "2"})).
  214. Where(Eq{"a": 2}).Limit(5, 10).
  215. ToSQL()
  216. assert.Error(t, err)
  217. assert.EqualValues(t, ErrNotUnexpectedUnionConditions, err)
  218. // will raise error
  219. _, _, err = Delete(Eq{"a": 1}).From("t1").
  220. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).ToSQL()
  221. assert.Error(t, err)
  222. assert.EqualValues(t, ErrUnsupportedUnionMembers, err)
  223. // will be overwrote by SELECT op
  224. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  225. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  226. Select("*").From("t2").ToSQL()
  227. assert.NoError(t, err)
  228. fmt.Println(sql, args)
  229. // will be overwrote by DELETE op
  230. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  231. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  232. Delete(Eq{"status": "1"}).From("t2").ToSQL()
  233. assert.NoError(t, err)
  234. fmt.Println(sql, args)
  235. // will be overwrote by INSERT op
  236. sql, args, err = Select("*").From("t1").Where(Eq{"status": "1"}).
  237. Intersect("all", Select("*").From("t2").Where(Eq{"status": "2"})).
  238. Insert(Eq{"status": "1"}).Into("t2").ToSQL()
  239. assert.NoError(t, err)
  240. fmt.Println(sql, args)
  241. }