dialect/sql: append UNION before ORDER BY (#1761)

* fix: builder to append UNION before ORDER_BY

* gofmt builder_test.go
This commit is contained in:
Khadija Sidhpuri
2021-07-23 02:03:34 +05:30
committed by GitHub
parent 444b5f57b9
commit 364ab46333
2 changed files with 15 additions and 3 deletions

View File

@@ -2320,6 +2320,9 @@ func (s *Selector) Query() (string, []interface{}) {
b.WriteString(" HAVING ")
b.Join(s.having)
}
if len(s.union) > 0 {
s.joinUnion(&b)
}
if len(s.order) > 0 {
s.joinOrder(&b)
}
@@ -2331,9 +2334,6 @@ func (s *Selector) Query() (string, []interface{}) {
b.WriteString(" OFFSET ")
b.WriteString(strconv.Itoa(*s.offset))
}
if len(s.union) > 0 {
s.joinUnion(&b)
}
s.joinLock(&b)
s.total = b.total
s.AddError(b.Err())

View File

@@ -1703,3 +1703,15 @@ func TestSelectWithLock(t *testing.T) {
require.Equal(t, "SELECT * FROM `users` WHERE `id` = ? LOCK IN SHARE MODE", query)
require.Equal(t, 20, args[0])
}
func TestSelector_UnionOrderBy(t *testing.T) {
table := Table("users")
query, _ := Dialect(dialect.Postgres).
Select("*").
From(table).
Where(EQ("active", true)).
Union(Select("*").From(Table("old_users1"))).
OrderBy(table.C("whatever")).
Query()
require.Equal(t, `SELECT * FROM "users" WHERE "active" = $1 UNION SELECT * FROM "old_users1" ORDER BY "users"."whatever"`, query)
}