builder_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // Copyright 2016 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. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. type MyInt int
  10. func TestBuilderCond(t *testing.T) {
  11. var cases = []struct {
  12. cond Cond
  13. sql string
  14. args []interface{}
  15. }{
  16. {
  17. Eq{"a": 1}.And(Like{"b", "c"}).Or(Eq{"a": 2}.And(Like{"b", "g"})),
  18. "(a=? AND b LIKE ?) OR (a=? AND b LIKE ?)",
  19. []interface{}{1, "%c%", 2, "%g%"},
  20. },
  21. {
  22. Eq{"a": 1}.Or(Like{"b", "c"}).And(Eq{"a": 2}.Or(Like{"b", "g"})),
  23. "(a=? OR b LIKE ?) AND (a=? OR b LIKE ?)",
  24. []interface{}{1, "%c%", 2, "%g%"},
  25. },
  26. {
  27. Eq{"d": []string{"e", "f"}},
  28. "d IN (?,?)",
  29. []interface{}{"e", "f"},
  30. },
  31. {
  32. Eq{"e": Select("id").From("f").Where(Eq{"g": 1})},
  33. "e=(SELECT id FROM f WHERE g=?)",
  34. []interface{}{1},
  35. },
  36. {
  37. Eq{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
  38. "e=(SELECT id FROM f WHERE g=?)",
  39. []interface{}{1},
  40. },
  41. {
  42. Like{"a", "%1"}.And(Like{"b", "%2"}),
  43. "a LIKE ? AND b LIKE ?",
  44. []interface{}{"%1", "%2"},
  45. },
  46. {
  47. Like{"a", "%1"}.Or(Like{"b", "%2"}),
  48. "a LIKE ? OR b LIKE ?",
  49. []interface{}{"%1", "%2"},
  50. },
  51. {
  52. Neq{"d": "e"}.Or(Neq{"f": "g"}),
  53. "d<>? OR f<>?",
  54. []interface{}{"e", "g"},
  55. },
  56. {
  57. Neq{"d": []string{"e", "f"}},
  58. "d NOT IN (?,?)",
  59. []interface{}{"e", "f"},
  60. },
  61. {
  62. Neq{"e": Select("id").From("f").Where(Eq{"g": 1})},
  63. "e<>(SELECT id FROM f WHERE g=?)",
  64. []interface{}{1},
  65. },
  66. {
  67. Neq{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
  68. "e<>(SELECT id FROM f WHERE g=?)",
  69. []interface{}{1},
  70. },
  71. {
  72. Lt{"d": 3},
  73. "d<?",
  74. []interface{}{3},
  75. },
  76. {
  77. Lt{"d": 3}.And(Lt{"e": 4}),
  78. "d<? AND e<?",
  79. []interface{}{3, 4},
  80. },
  81. {
  82. Lt{"d": 3}.Or(Lt{"e": 4}),
  83. "d<? OR e<?",
  84. []interface{}{3, 4},
  85. },
  86. {
  87. Lt{"e": Select("id").From("f").Where(Eq{"g": 1})},
  88. "e<(SELECT id FROM f WHERE g=?)",
  89. []interface{}{1},
  90. },
  91. {
  92. Lt{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
  93. "e<(SELECT id FROM f WHERE g=?)",
  94. []interface{}{1},
  95. },
  96. {
  97. Lte{"d": 3},
  98. "d<=?",
  99. []interface{}{3},
  100. },
  101. {
  102. Lte{"d": 3}.And(Lte{"e": 4}),
  103. "d<=? AND e<=?",
  104. []interface{}{3, 4},
  105. },
  106. {
  107. Lte{"d": 3}.Or(Lte{"e": 4}),
  108. "d<=? OR e<=?",
  109. []interface{}{3, 4},
  110. },
  111. {
  112. Lte{"e": Select("id").From("f").Where(Eq{"g": 1})},
  113. "e<=(SELECT id FROM f WHERE g=?)",
  114. []interface{}{1},
  115. },
  116. {
  117. Lte{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
  118. "e<=(SELECT id FROM f WHERE g=?)",
  119. []interface{}{1},
  120. },
  121. {
  122. Gt{"d": 3},
  123. "d>?",
  124. []interface{}{3},
  125. },
  126. {
  127. Gt{"d": 3}.And(Gt{"e": 4}),
  128. "d>? AND e>?",
  129. []interface{}{3, 4},
  130. },
  131. {
  132. Gt{"d": 3}.Or(Gt{"e": 4}),
  133. "d>? OR e>?",
  134. []interface{}{3, 4},
  135. },
  136. {
  137. Gt{"e": Select("id").From("f").Where(Eq{"g": 1})},
  138. "e>(SELECT id FROM f WHERE g=?)",
  139. []interface{}{1},
  140. },
  141. {
  142. Gt{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
  143. "e>(SELECT id FROM f WHERE g=?)",
  144. []interface{}{1},
  145. },
  146. {
  147. Gte{"d": 3},
  148. "d>=?",
  149. []interface{}{3},
  150. },
  151. {
  152. Gte{"d": 3}.And(Gte{"e": 4}),
  153. "d>=? AND e>=?",
  154. []interface{}{3, 4},
  155. },
  156. {
  157. Gte{"d": 3}.Or(Gte{"e": 4}),
  158. "d>=? OR e>=?",
  159. []interface{}{3, 4},
  160. },
  161. {
  162. Gte{"e": Select("id").From("f").Where(Eq{"g": 1})},
  163. "e>=(SELECT id FROM f WHERE g=?)",
  164. []interface{}{1},
  165. },
  166. {
  167. Gte{"e": Expr("SELECT id FROM f WHERE g=?", 1)},
  168. "e>=(SELECT id FROM f WHERE g=?)",
  169. []interface{}{1},
  170. },
  171. {
  172. Between{"d", 0, 2},
  173. "d BETWEEN ? AND ?",
  174. []interface{}{0, 2},
  175. },
  176. {
  177. Between{"d", 0, Expr("CAST('2003-01-01' AS DATE)")},
  178. "d BETWEEN ? AND CAST('2003-01-01' AS DATE)",
  179. []interface{}{0},
  180. },
  181. {
  182. Between{"d", Expr("CAST('2003-01-01' AS DATE)"), 2},
  183. "d BETWEEN CAST('2003-01-01' AS DATE) AND ?",
  184. []interface{}{2},
  185. },
  186. {
  187. Between{"d", Expr("CAST('2003-01-01' AS DATE)"), Expr("CAST('2003-01-01' AS DATE)")},
  188. "d BETWEEN CAST('2003-01-01' AS DATE) AND CAST('2003-01-01' AS DATE)",
  189. []interface{}{},
  190. },
  191. {
  192. Between{"d", 0, 2}.And(Between{"e", 3, 4}),
  193. "d BETWEEN ? AND ? AND e BETWEEN ? AND ?",
  194. []interface{}{0, 2, 3, 4},
  195. },
  196. {
  197. Between{"d", 0, 2}.Or(Between{"e", 3, 4}),
  198. "d BETWEEN ? AND ? OR e BETWEEN ? AND ?",
  199. []interface{}{0, 2, 3, 4},
  200. },
  201. {
  202. Expr("a < ?", 1),
  203. "a < ?",
  204. []interface{}{1},
  205. },
  206. {
  207. Expr("a < ?", 1).And(Eq{"b": 2}),
  208. "(a < ?) AND b=?",
  209. []interface{}{1, 2},
  210. },
  211. {
  212. Expr("a < ?", 1).Or(Neq{"b": 2}),
  213. "(a < ?) OR b<>?",
  214. []interface{}{1, 2},
  215. },
  216. {
  217. IsNull{"d"},
  218. "d IS NULL",
  219. []interface{}{},
  220. },
  221. {
  222. IsNull{"d"}.And(IsNull{"e"}),
  223. "d IS NULL AND e IS NULL",
  224. []interface{}{},
  225. },
  226. {
  227. IsNull{"d"}.Or(IsNull{"e"}),
  228. "d IS NULL OR e IS NULL",
  229. []interface{}{},
  230. },
  231. {
  232. NotNull{"d"},
  233. "d IS NOT NULL",
  234. []interface{}{},
  235. },
  236. {
  237. NotNull{"d"}.And(NotNull{"e"}),
  238. "d IS NOT NULL AND e IS NOT NULL",
  239. []interface{}{},
  240. },
  241. {
  242. NotNull{"d"}.Or(NotNull{"e"}),
  243. "d IS NOT NULL OR e IS NOT NULL",
  244. []interface{}{},
  245. },
  246. {
  247. NotIn("a", 1, 2).And(NotIn("b", "c", "d")),
  248. "a NOT IN (?,?) AND b NOT IN (?,?)",
  249. []interface{}{1, 2, "c", "d"},
  250. },
  251. {
  252. In("a", 1, 2).Or(In("b", "c", "d")),
  253. "a IN (?,?) OR b IN (?,?)",
  254. []interface{}{1, 2, "c", "d"},
  255. },
  256. {
  257. In("a", []int{1, 2}).Or(In("b", []string{"c", "d"})),
  258. "a IN (?,?) OR b IN (?,?)",
  259. []interface{}{1, 2, "c", "d"},
  260. },
  261. {
  262. In("a", Expr("select id from x where name > ?", "b")),
  263. "a IN (select id from x where name > ?)",
  264. []interface{}{"b"},
  265. },
  266. {
  267. In("a", []MyInt{1, 2}).Or(In("b", []string{"c", "d"})),
  268. "a IN (?,?) OR b IN (?,?)",
  269. []interface{}{MyInt(1), MyInt(2), "c", "d"},
  270. },
  271. {
  272. In("a", []int{}),
  273. "0=1",
  274. []interface{}{},
  275. },
  276. {
  277. In("a", []int{1}),
  278. "a IN (?)",
  279. []interface{}{1},
  280. },
  281. {
  282. In("a", []int8{}),
  283. "0=1",
  284. []interface{}{},
  285. },
  286. {
  287. In("a", []int8{1}),
  288. "a IN (?)",
  289. []interface{}{1},
  290. },
  291. {
  292. In("a", []int16{}),
  293. "0=1",
  294. []interface{}{},
  295. },
  296. {
  297. In("a", []int16{1}),
  298. "a IN (?)",
  299. []interface{}{1},
  300. },
  301. {
  302. In("a", []int32{}),
  303. "0=1",
  304. []interface{}{},
  305. },
  306. {
  307. In("a", []int32{1}),
  308. "a IN (?)",
  309. []interface{}{1},
  310. },
  311. {
  312. In("a", []int64{}),
  313. "0=1",
  314. []interface{}{},
  315. },
  316. {
  317. In("a", []int64{1}),
  318. "a IN (?)",
  319. []interface{}{1},
  320. },
  321. {
  322. In("a", []uint{}),
  323. "0=1",
  324. []interface{}{},
  325. },
  326. {
  327. In("a", []uint{1}),
  328. "a IN (?)",
  329. []interface{}{1},
  330. },
  331. {
  332. In("a", []uint8{}),
  333. "0=1",
  334. []interface{}{},
  335. },
  336. {
  337. In("a", []uint8{1}),
  338. "a IN (?)",
  339. []interface{}{1},
  340. },
  341. {
  342. In("a", []uint16{}),
  343. "0=1",
  344. []interface{}{},
  345. },
  346. {
  347. In("a", []uint16{1}),
  348. "a IN (?)",
  349. []interface{}{1},
  350. },
  351. {
  352. In("a", []uint32{}),
  353. "0=1",
  354. []interface{}{},
  355. },
  356. {
  357. In("a", []uint32{1}),
  358. "a IN (?)",
  359. []interface{}{1},
  360. },
  361. {
  362. In("a", []uint64{}),
  363. "0=1",
  364. []interface{}{},
  365. },
  366. {
  367. In("a", []uint64{1}),
  368. "a IN (?)",
  369. []interface{}{1},
  370. },
  371. {
  372. In("a", []string{}),
  373. "0=1",
  374. []interface{}{},
  375. },
  376. {
  377. In("a", []interface{}{}),
  378. "0=1",
  379. []interface{}{},
  380. },
  381. {
  382. In("a", []MyInt{}),
  383. "0=1",
  384. []interface{}{},
  385. },
  386. {
  387. In("a", []interface{}{1, 2, 3}).And(Eq{"b": "c"}),
  388. "a IN (?,?,?) AND b=?",
  389. []interface{}{1, 2, 3, "c"},
  390. },
  391. {
  392. In("a", Select("id").From("b").Where(Eq{"c": 1})),
  393. "a IN (SELECT id FROM b WHERE c=?)",
  394. []interface{}{1},
  395. },
  396. {
  397. NotIn("a", Expr("select id from x where name > ?", "b")),
  398. "a NOT IN (select id from x where name > ?)",
  399. []interface{}{"b"},
  400. },
  401. {
  402. NotIn("a", []int{}),
  403. "0=0",
  404. []interface{}{},
  405. },
  406. {
  407. NotIn("a", []int{1}),
  408. "a NOT IN (?)",
  409. []interface{}{1},
  410. },
  411. {
  412. NotIn("a", []int8{}),
  413. "0=0",
  414. []interface{}{},
  415. },
  416. {
  417. NotIn("a", []int8{1}),
  418. "a NOT IN (?)",
  419. []interface{}{1},
  420. },
  421. {
  422. NotIn("a", []int16{}),
  423. "0=0",
  424. []interface{}{},
  425. },
  426. {
  427. NotIn("a", []int16{1}),
  428. "a NOT IN (?)",
  429. []interface{}{1},
  430. },
  431. {
  432. NotIn("a", []int32{}),
  433. "0=0",
  434. []interface{}{},
  435. },
  436. {
  437. NotIn("a", []int32{1}),
  438. "a NOT IN (?)",
  439. []interface{}{1},
  440. },
  441. {
  442. NotIn("a", []int64{}),
  443. "0=0",
  444. []interface{}{},
  445. },
  446. {
  447. NotIn("a", []int64{1}),
  448. "a NOT IN (?)",
  449. []interface{}{1},
  450. },
  451. {
  452. NotIn("a", []uint{}),
  453. "0=0",
  454. []interface{}{},
  455. },
  456. {
  457. NotIn("a", []uint{1}),
  458. "a NOT IN (?)",
  459. []interface{}{1},
  460. },
  461. {
  462. NotIn("a", []uint8{}),
  463. "0=0",
  464. []interface{}{},
  465. },
  466. {
  467. NotIn("a", []uint8{1}),
  468. "a NOT IN (?)",
  469. []interface{}{1},
  470. },
  471. {
  472. NotIn("a", []uint16{}),
  473. "0=0",
  474. []interface{}{},
  475. },
  476. {
  477. NotIn("a", []uint16{1}),
  478. "a NOT IN (?)",
  479. []interface{}{1},
  480. },
  481. {
  482. NotIn("a", []uint32{}),
  483. "0=0",
  484. []interface{}{},
  485. },
  486. {
  487. NotIn("a", []uint32{1}),
  488. "a NOT IN (?)",
  489. []interface{}{1},
  490. },
  491. {
  492. NotIn("a", []uint64{}),
  493. "0=0",
  494. []interface{}{},
  495. },
  496. {
  497. NotIn("a", []uint64{1}),
  498. "a NOT IN (?)",
  499. []interface{}{1},
  500. },
  501. {
  502. NotIn("a", []interface{}{}),
  503. "0=0",
  504. []interface{}{},
  505. },
  506. {
  507. NotIn("a", []string{}),
  508. "0=0",
  509. []interface{}{},
  510. },
  511. {
  512. NotIn("a", []MyInt{}),
  513. "0=0",
  514. []interface{}{},
  515. },
  516. {
  517. NotIn("a", []MyInt{1, 2}),
  518. "a NOT IN (?,?)",
  519. []interface{}{1, 2},
  520. },
  521. {
  522. NotIn("a", []interface{}{1, 2, 3}).And(Eq{"b": "c"}),
  523. "a NOT IN (?,?,?) AND b=?",
  524. []interface{}{1, 2, 3, "c"},
  525. },
  526. {
  527. NotIn("a", []interface{}{1, 2, 3}).Or(Eq{"b": "c"}),
  528. "a NOT IN (?,?,?) OR b=?",
  529. []interface{}{1, 2, 3, "c"},
  530. },
  531. {
  532. NotIn("a", Select("id").From("b").Where(Eq{"c": 1})),
  533. "a NOT IN (SELECT id FROM b WHERE c=?)",
  534. []interface{}{1},
  535. },
  536. {
  537. Or(Eq{"a": 1, "b": 2}, Eq{"c": 3, "d": 4}),
  538. "(a=? AND b=?) OR (c=? AND d=?)",
  539. []interface{}{1, 2, 3, 4},
  540. },
  541. {
  542. Not{Eq{"a": 1, "b": 2}},
  543. "NOT (a=? AND b=?)",
  544. []interface{}{1, 2},
  545. },
  546. {
  547. Not{Neq{"a": 1, "b": 2}},
  548. "NOT (a<>? AND b<>?)",
  549. []interface{}{1, 2},
  550. },
  551. {
  552. Not{Eq{"a": 1}.And(Eq{"b": 2})},
  553. "NOT (a=? AND b=?)",
  554. []interface{}{1, 2},
  555. },
  556. {
  557. Not{Neq{"a": 1}.And(Neq{"b": 2})},
  558. "NOT (a<>? AND b<>?)",
  559. []interface{}{1, 2},
  560. },
  561. {
  562. Not{Eq{"a": 1}}.And(Neq{"b": 2}),
  563. "NOT a=? AND b<>?",
  564. []interface{}{1, 2},
  565. },
  566. {
  567. Not{Eq{"a": 1}}.Or(Neq{"b": 2}),
  568. "NOT a=? OR b<>?",
  569. []interface{}{1, 2},
  570. },
  571. }
  572. for _, k := range cases {
  573. sql, args, err := ToSQL(k.cond)
  574. assert.NoError(t, err)
  575. assert.EqualValues(t, k.sql, sql)
  576. for i := 0; i < 10; i++ {
  577. sql2, _, err := ToSQL(k.cond)
  578. assert.NoError(t, err)
  579. assert.EqualValues(t, sql, sql2)
  580. }
  581. assert.EqualValues(t, len(args), len(k.args))
  582. if len(args) > 0 {
  583. for i := 0; i < len(args); i++ {
  584. assert.EqualValues(t, k.args[i], args[i])
  585. }
  586. }
  587. }
  588. }
  589. func TestSubquery(t *testing.T) {
  590. subb := Select("id").From("table_b").Where(Eq{"b": "a"})
  591. b := Select("a, b").From("table_a").Where(
  592. Eq{
  593. "b_id": subb,
  594. "id": 23,
  595. },
  596. )
  597. sql, args, err := b.ToSQL()
  598. assert.NoError(t, err)
  599. assert.EqualValues(t, "SELECT a, b FROM table_a WHERE b_id=(SELECT id FROM table_b WHERE b=?) AND id=?", sql)
  600. assert.EqualValues(t, []interface{}{"a", 23}, args)
  601. }
  602. // https://github.com/go-xorm/xorm/issues/820
  603. func TestExprCond(t *testing.T) {
  604. b := Select("id").From("table1").Where(expr{sql: "a=? OR b=?", args: []interface{}{1, 2}}).Where(Or(Eq{"c": 3}, Eq{"d": 4}))
  605. sql, args, err := b.ToSQL()
  606. assert.NoError(t, err)
  607. assert.EqualValues(t, "table1", b.TableName())
  608. assert.EqualValues(t, "SELECT id FROM table1 WHERE (a=? OR b=?) AND (c=? OR d=?)", sql)
  609. assert.EqualValues(t, []interface{}{1, 2, 3, 4}, args)
  610. }
  611. func TestBuilder_ToBoundSQL(t *testing.T) {
  612. newSQL, err := Select("id").From("table").Where(In("a", 1, 2)).ToBoundSQL()
  613. assert.NoError(t, err)
  614. assert.EqualValues(t, "SELECT id FROM table WHERE a IN (1,2)", newSQL)
  615. }
  616. func TestBuilder_From2(t *testing.T) {
  617. b := Select("id").From("table_b", "tb").Where(Eq{"b": "a"})
  618. sql, args, err := b.ToSQL()
  619. assert.NoError(t, err)
  620. assert.EqualValues(t, "SELECT id FROM table_b tb WHERE b=?", sql)
  621. assert.EqualValues(t, []interface{}{"a"}, args)
  622. b = Select().From("table_b", "tb").Where(Eq{"b": "a"})
  623. sql, args, err = b.ToSQL()
  624. assert.NoError(t, err)
  625. assert.EqualValues(t, "SELECT * FROM table_b tb WHERE b=?", sql)
  626. assert.EqualValues(t, []interface{}{"a"}, args)
  627. }
  628. func TestBuilder_And(t *testing.T) {
  629. b := Select("id").From("table_b", "tb").Where(Eq{"b": "a"}).And(Neq{"c": "d"})
  630. sql, args, err := b.ToSQL()
  631. assert.NoError(t, err)
  632. assert.EqualValues(t, "SELECT id FROM table_b tb WHERE b=? AND c<>?", sql)
  633. assert.EqualValues(t, []interface{}{"a", "d"}, args)
  634. }
  635. func TestBuilder_Or(t *testing.T) {
  636. b := Select("id").From("table_b", "tb").Where(Eq{"b": "a"}).Or(Neq{"c": "d"})
  637. sql, args, err := b.ToSQL()
  638. assert.NoError(t, err)
  639. assert.EqualValues(t, "SELECT id FROM table_b tb WHERE b=? OR c<>?", sql)
  640. assert.EqualValues(t, []interface{}{"a", "d"}, args)
  641. }