builder_select_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_Select(t *testing.T) {
  11. sql, args, err := Select("c, d").From("table1").ToSQL()
  12. assert.NoError(t, err)
  13. assert.EqualValues(t, "SELECT c, d FROM table1", sql)
  14. assert.EqualValues(t, []interface{}(nil), args)
  15. sql, args, err = Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL()
  16. assert.NoError(t, err)
  17. assert.EqualValues(t, "SELECT c, d FROM table1 WHERE a=?", sql)
  18. assert.EqualValues(t, []interface{}{1}, args)
  19. sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
  20. RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
  21. assert.NoError(t, err)
  22. assert.EqualValues(t, "SELECT c, d FROM table1 LEFT JOIN table2 ON table1.id=? AND table2.id<? RIGHT JOIN table3 ON table2.id = table3.tid WHERE a=?",
  23. sql)
  24. assert.EqualValues(t, []interface{}{1, 3, 1}, args)
  25. sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
  26. FullJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
  27. assert.NoError(t, err)
  28. assert.EqualValues(t, "SELECT c, d FROM table1 LEFT JOIN table2 ON table1.id=? AND table2.id<? FULL JOIN table3 ON table2.id = table3.tid WHERE a=?",
  29. sql)
  30. assert.EqualValues(t, []interface{}{1, 3, 1}, args)
  31. sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
  32. CrossJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
  33. assert.NoError(t, err)
  34. assert.EqualValues(t, "SELECT c, d FROM table1 LEFT JOIN table2 ON table1.id=? AND table2.id<? CROSS JOIN table3 ON table2.id = table3.tid WHERE a=?",
  35. sql)
  36. assert.EqualValues(t, []interface{}{1, 3, 1}, args)
  37. sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})).
  38. InnerJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL()
  39. assert.NoError(t, err)
  40. assert.EqualValues(t, "SELECT c, d FROM table1 LEFT JOIN table2 ON table1.id=? AND table2.id<? INNER JOIN table3 ON table2.id = table3.tid WHERE a=?",
  41. sql)
  42. assert.EqualValues(t, []interface{}{1, 3, 1}, args)
  43. _, _, err = Select("c, d").ToSQL()
  44. assert.Error(t, err)
  45. assert.EqualValues(t, ErrNoTableName, err)
  46. }
  47. func TestBuilderSelectGroupBy(t *testing.T) {
  48. sql, args, err := Select("c").From("table1").GroupBy("c").Having("count(c)=1").ToSQL()
  49. assert.NoError(t, err)
  50. assert.EqualValues(t, "SELECT c FROM table1 GROUP BY c HAVING count(c)=1", sql)
  51. assert.EqualValues(t, 0, len(args))
  52. fmt.Println(sql, args)
  53. }
  54. func TestBuilderSelectOrderBy(t *testing.T) {
  55. sql, args, err := Select("c").From("table1").OrderBy("c DESC").ToSQL()
  56. assert.NoError(t, err)
  57. assert.EqualValues(t, "SELECT c FROM table1 ORDER BY c DESC", sql)
  58. assert.EqualValues(t, 0, len(args))
  59. fmt.Println(sql, args)
  60. }
  61. func TestBuilder_From(t *testing.T) {
  62. // simple one
  63. sql, args, err := Select("c").From("table1").ToSQL()
  64. assert.NoError(t, err)
  65. assert.EqualValues(t, "SELECT c FROM table1", sql)
  66. assert.EqualValues(t, 0, len(args))
  67. // from sub with alias
  68. sql, args, err = Select("sub.id").From(Select("id").From("table1").Where(Eq{"a": 1}),
  69. "sub").Where(Eq{"b": 1}).ToSQL()
  70. assert.NoError(t, err)
  71. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?) sub WHERE b=?", sql)
  72. assert.EqualValues(t, []interface{}{1, 1}, args)
  73. // from sub without alias and with conditions
  74. sql, args, err = Select("sub.id").From(Select("id").From("table1").Where(Eq{"a": 1})).Where(Eq{"b": 1}).ToSQL()
  75. assert.Error(t, err)
  76. assert.EqualValues(t, ErrUnnamedDerivedTable, err)
  77. // from sub without alias and conditions
  78. sql, args, err = Select("sub.id").From(Select("id").From("table1").Where(Eq{"a": 1})).ToSQL()
  79. assert.NoError(t, err)
  80. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?)", sql)
  81. assert.EqualValues(t, []interface{}{1}, args)
  82. // from union with alias
  83. sql, args, err = Select("sub.id").From(
  84. Select("id").From("table1").Where(Eq{"a": 1}).Union(
  85. "all", Select("id").From("table1").Where(Eq{"a": 2})), "sub").Where(Eq{"b": 1}).ToSQL()
  86. assert.NoError(t, err)
  87. assert.EqualValues(t, "SELECT sub.id FROM ((SELECT id FROM table1 WHERE a=?) UNION ALL (SELECT id FROM table1 WHERE a=?)) sub WHERE b=?", sql)
  88. assert.EqualValues(t, []interface{}{1, 2, 1}, args)
  89. // from union without alias
  90. _, _, err = Select("sub.id").From(
  91. Select("id").From("table1").Where(Eq{"a": 1}).Union(
  92. "all", Select("id").From("table1").Where(Eq{"a": 2}))).Where(Eq{"b": 1}).ToSQL()
  93. assert.Error(t, err)
  94. assert.EqualValues(t, ErrUnnamedDerivedTable, err)
  95. // will raise error
  96. _, _, err = Select("c").From(Insert(Eq{"a": 1}).From("table1"), "table1").ToSQL()
  97. assert.Error(t, err)
  98. assert.EqualValues(t, ErrUnexpectedSubQuery, err)
  99. // will raise error
  100. _, _, err = Select("c").From(Delete(Eq{"a": 1}).From("table1"), "table1").ToSQL()
  101. assert.Error(t, err)
  102. assert.EqualValues(t, ErrUnexpectedSubQuery, err)
  103. // from a sub-query in different dialect
  104. _, _, err = MySQL().Select("sub.id").From(
  105. Oracle().Select("id").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
  106. assert.Error(t, err)
  107. assert.EqualValues(t, ErrInconsistentDialect, err)
  108. // from a sub-query (dialect set up)
  109. sql, args, err = MySQL().Select("sub.id").From(
  110. MySQL().Select("id").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
  111. assert.NoError(t, err)
  112. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?) sub WHERE b=?", sql)
  113. assert.EqualValues(t, []interface{}{1, 1}, args)
  114. // from a sub-query (dialect not set up)
  115. sql, args, err = MySQL().Select("sub.id").From(
  116. Select("id").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
  117. assert.NoError(t, err)
  118. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?) sub WHERE b=?", sql)
  119. assert.EqualValues(t, []interface{}{1, 1}, args)
  120. }