builder_union_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2018 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. sql, args, 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. sql, args, 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. sql, args, 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. }