From 21713a5246d76bdfa56da07cf41989abfa698b0e Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 10 Nov 2013 09:57:34 +0800 Subject: [PATCH] Update sql validator for Count() --- chain.go | 2 +- gorm_test.go | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/chain.go b/chain.go index 18c24584..b6b14ab2 100644 --- a/chain.go +++ b/chain.go @@ -273,7 +273,7 @@ func (s *Chain) Debug() *Chain { } func (s *Chain) validSql(str string) (result bool) { - result = regexp.MustCompile("^\\s*[\\w][\\w\\s,.]*[\\w]\\s*$").MatchString(str) + result = regexp.MustCompile("^\\s*[\\w\\s,.*()]*\\s*$").MatchString(str) if !result { s.err(errors.New(fmt.Sprintf("SQL is not valid, %s", str))) } diff --git a/gorm_test.go b/gorm_test.go index d855f598..b165db15 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -533,7 +533,11 @@ func TestOr(t *testing.T) { func TestCount(t *testing.T) { var count, count1, count2 int64 var users []User - db.Where("name = ?", "1").Or("name = ?", "3").Find(&users).Count(&count) + + if err := db.Where("name = ?", "1").Or("name = ?", "3").Find(&users).Count(&count).Error; err != nil { + t.Errorf("Count should have no error", err) + } + if count != int64(len(users)) { t.Errorf("Count() method should get same value of users count") }