From 3d36ff7e965b5fe6241089ff4d61e8255a0ff889 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Thu, 22 Sep 2022 20:18:17 +0300 Subject: [PATCH] unset ORDER BY for search count queries --- tools/search/provider.go | 2 +- tools/search/provider_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tools/search/provider.go b/tools/search/provider.go index 3eb2a03b..6b1986a6 100644 --- a/tools/search/provider.go +++ b/tools/search/provider.go @@ -199,7 +199,7 @@ func (s *Provider) Exec(items any) (*Result, error) { // count var totalCount int64 countQuery := modelsQuery - countQuery.Distinct(false).Select("COUNT(*)") + countQuery.Distinct(false).Select("COUNT(*)").OrderBy([]string{}...) // unset ORDER BY statements if s.countColumn != "" { countQuery.Select("COUNT(DISTINCT(" + s.countColumn + "))") } diff --git a/tools/search/provider_test.go b/tools/search/provider_test.go index b2021176..d03a4166 100644 --- a/tools/search/provider_test.go +++ b/tools/search/provider_test.go @@ -238,7 +238,7 @@ func TestProviderExecNonEmptyQuery(t *testing.T) { false, `{"page":1,"perPage":10,"totalItems":2,"totalPages":1,"items":[{"test1":1,"test2":"test2.1","test3":""},{"test1":2,"test2":"test2.2","test3":""}]}`, []string{ - "SELECT COUNT(*) FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC", + "SELECT COUNT(*) FROM `test` WHERE NOT (`test1` IS NULL)", "SELECT * FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC LIMIT 10", }, }, @@ -252,7 +252,7 @@ func TestProviderExecNonEmptyQuery(t *testing.T) { false, `{"page":1,"perPage":30,"totalItems":2,"totalPages":1,"items":[{"test1":1,"test2":"test2.1","test3":""},{"test1":2,"test2":"test2.2","test3":""}]}`, []string{ - "SELECT COUNT(*) FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC", + "SELECT COUNT(*) FROM `test` WHERE NOT (`test1` IS NULL)", "SELECT * FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC LIMIT 30", }, }, @@ -288,7 +288,7 @@ func TestProviderExecNonEmptyQuery(t *testing.T) { false, `{"page":1,"perPage":` + fmt.Sprint(MaxPerPage) + `,"totalItems":1,"totalPages":1,"items":[{"test1":2,"test2":"test2.2","test3":""}]}`, []string{ - "SELECT COUNT(*) FROM `test` WHERE ((NOT (`test1` IS NULL)) AND (COALESCE(test2, '') != COALESCE(null, ''))) AND (test1 >= 2) ORDER BY `test1` ASC, `test2` DESC", + "SELECT COUNT(*) FROM `test` WHERE ((NOT (`test1` IS NULL)) AND (COALESCE(test2, '') != COALESCE(null, ''))) AND (test1 >= 2)", "SELECT * FROM `test` WHERE ((NOT (`test1` IS NULL)) AND (COALESCE(test2, '') != COALESCE(null, ''))) AND (test1 >= 2) ORDER BY `test1` ASC, `test2` DESC LIMIT 400", }, }, @@ -302,7 +302,7 @@ func TestProviderExecNonEmptyQuery(t *testing.T) { false, `{"page":1,"perPage":10,"totalItems":0,"totalPages":0,"items":[]}`, []string{ - "SELECT COUNT(*) FROM `test` WHERE (NOT (`test1` IS NULL)) AND (COALESCE(test3, '') != COALESCE('', '')) ORDER BY `test1` ASC, `test3` ASC", + "SELECT COUNT(*) FROM `test` WHERE (NOT (`test1` IS NULL)) AND (COALESCE(test3, '') != COALESCE('', ''))", "SELECT * FROM `test` WHERE (NOT (`test1` IS NULL)) AND (COALESCE(test3, '') != COALESCE('', '')) ORDER BY `test1` ASC, `test3` ASC LIMIT 10", }, }, @@ -316,7 +316,7 @@ func TestProviderExecNonEmptyQuery(t *testing.T) { false, `{"page":2,"perPage":1,"totalItems":2,"totalPages":2,"items":[{"test1":2,"test2":"test2.2","test3":""}]}`, []string{ - "SELECT COUNT(*) FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC", + "SELECT COUNT(*) FROM `test` WHERE NOT (`test1` IS NULL)", "SELECT * FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC LIMIT 1 OFFSET 1", }, }, @@ -330,7 +330,7 @@ func TestProviderExecNonEmptyQuery(t *testing.T) { false, `{"page":2,"perPage":1,"totalItems":2,"totalPages":2,"items":[{"test1":2,"test2":"test2.2","test3":""}]}`, []string{ - "SELECT COUNT(DISTINCT(test.test1)) FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC", + "SELECT COUNT(DISTINCT(test.test1)) FROM `test` WHERE NOT (`test1` IS NULL)", "SELECT * FROM `test` WHERE NOT (`test1` IS NULL) ORDER BY `test1` ASC LIMIT 1 OFFSET 1", }, },