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.

115 lines
4.3 KiB

2 years ago
  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. _, _, err = Select("c, d").ToSQL()
  20. assert.Error(t, err)
  21. assert.EqualValues(t, ErrNoTableName, err)
  22. }
  23. func TestBuilderSelectGroupBy(t *testing.T) {
  24. sql, args, err := Select("c").From("table1").GroupBy("c").Having("count(c)=1").ToSQL()
  25. assert.NoError(t, err)
  26. assert.EqualValues(t, "SELECT c FROM table1 GROUP BY c HAVING count(c)=1", sql)
  27. assert.EqualValues(t, 0, len(args))
  28. fmt.Println(sql, args)
  29. }
  30. func TestBuilderSelectOrderBy(t *testing.T) {
  31. sql, args, err := Select("c").From("table1").OrderBy("c DESC").ToSQL()
  32. assert.NoError(t, err)
  33. assert.EqualValues(t, "SELECT c FROM table1 ORDER BY c DESC", sql)
  34. assert.EqualValues(t, 0, len(args))
  35. fmt.Println(sql, args)
  36. }
  37. func TestBuilder_From(t *testing.T) {
  38. // simple one
  39. sql, args, err := Select("c").From("table1").ToSQL()
  40. assert.NoError(t, err)
  41. assert.EqualValues(t, "SELECT c FROM table1", sql)
  42. assert.EqualValues(t, 0, len(args))
  43. // from sub with alias
  44. sql, args, err = Select("sub.id").From(Select("id").From("table1").Where(Eq{"a": 1}),
  45. "sub").Where(Eq{"b": 1}).ToSQL()
  46. assert.NoError(t, err)
  47. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?) sub WHERE b=?", sql)
  48. assert.EqualValues(t, []interface{}{1, 1}, args)
  49. // from sub without alias and with conditions
  50. sql, args, err = Select("sub.id").From(Select("id").From("table1").Where(Eq{"a": 1})).Where(Eq{"b": 1}).ToSQL()
  51. assert.Error(t, err)
  52. assert.EqualValues(t, ErrUnnamedDerivedTable, err)
  53. // from sub without alias and conditions
  54. sql, args, err = Select("sub.id").From(Select("id").From("table1").Where(Eq{"a": 1})).ToSQL()
  55. assert.NoError(t, err)
  56. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?)", sql)
  57. assert.EqualValues(t, []interface{}{1}, args)
  58. // from union with alias
  59. sql, args, err = Select("sub.id").From(
  60. Select("id").From("table1").Where(Eq{"a": 1}).Union(
  61. "all", Select("id").From("table1").Where(Eq{"a": 2})), "sub").Where(Eq{"b": 1}).ToSQL()
  62. assert.NoError(t, err)
  63. 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)
  64. assert.EqualValues(t, []interface{}{1, 2, 1}, args)
  65. // from union without alias
  66. _, _, err = Select("sub.id").From(
  67. Select("id").From("table1").Where(Eq{"a": 1}).Union(
  68. "all", Select("id").From("table1").Where(Eq{"a": 2}))).Where(Eq{"b": 1}).ToSQL()
  69. assert.Error(t, err)
  70. assert.EqualValues(t, ErrUnnamedDerivedTable, err)
  71. // will raise error
  72. _, _, err = Select("c").From(Insert(Eq{"a": 1}).From("table1"), "table1").ToSQL()
  73. assert.Error(t, err)
  74. assert.EqualValues(t, ErrUnexpectedSubQuery, err)
  75. // will raise error
  76. _, _, err = Select("c").From(Delete(Eq{"a": 1}).From("table1"), "table1").ToSQL()
  77. assert.Error(t, err)
  78. assert.EqualValues(t, ErrUnexpectedSubQuery, err)
  79. // from a sub-query in different dialect
  80. _, _, err = MySQL().Select("sub.id").From(
  81. Oracle().Select("id").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
  82. assert.Error(t, err)
  83. assert.EqualValues(t, ErrInconsistentDialect, err)
  84. // from a sub-query (dialect set up)
  85. sql, args, err = MySQL().Select("sub.id").From(
  86. MySQL().Select("id").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
  87. assert.NoError(t, err)
  88. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?) sub WHERE b=?", sql)
  89. assert.EqualValues(t, []interface{}{1, 1}, args)
  90. // from a sub-query (dialect not set up)
  91. sql, args, err = MySQL().Select("sub.id").From(
  92. Select("id").From("table1").Where(Eq{"a": 1}), "sub").Where(Eq{"b": 1}).ToSQL()
  93. assert.NoError(t, err)
  94. assert.EqualValues(t, "SELECT sub.id FROM (SELECT id FROM table1 WHERE a=?) sub WHERE b=?", sql)
  95. assert.EqualValues(t, []interface{}{1, 1}, args)
  96. }