mirror of https://github.com/go-gorm/gorm.git
Polish The Not Method
This commit is contained in:
parent
8a030c99eb
commit
c387c7d9ba
|
@ -78,6 +78,10 @@ db.Not([]int64{}).First(&user)
|
|||
//// user -> select * from users;
|
||||
db.Not("name", "jinzhu").First(&user)
|
||||
//// user -> select * from users where name <> "jinzhu"
|
||||
db.Not("name = ?", "jinzhu").First(&user)
|
||||
//// user -> select * from users where NOT(name = "jinzhu")
|
||||
db.Not("name <> ?", "jinzhu").First(&user)
|
||||
//// user -> select * from users where NOT(name <> "jinzhu")
|
||||
db.Not("name", []string{"jinzhu", "jinzhu 2"}).First(&user)
|
||||
//// user -> select * from users where name NOT IN ("jinzhu", "jinzhu 2")
|
||||
db.Not(User{Name: "jinzhu"}).First(&user)
|
||||
|
|
3
do.go
3
do.go
|
@ -434,6 +434,9 @@ func (s *Do) buildNotCondition(clause map[string]interface{}) (str string) {
|
|||
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
|
||||
id, _ := strconv.Atoi(value)
|
||||
return fmt.Sprintf("(%v <> %v)", s.model.primaryKeyDb(), id)
|
||||
} else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS) ").MatchString(value) {
|
||||
str = fmt.Sprintf(" NOT (%v) ", value)
|
||||
not_equal_sql = fmt.Sprintf(" NOT (%v) ", value)
|
||||
} else {
|
||||
str = fmt.Sprintf(" (%v NOT IN (?)) ", value)
|
||||
not_equal_sql = fmt.Sprintf(" (%v <> ?) ", value)
|
||||
|
|
12
gorm_test.go
12
gorm_test.go
|
@ -958,6 +958,18 @@ func TestNot(t *testing.T) {
|
|||
t.Errorf("Should find all users's name not equal 3")
|
||||
}
|
||||
|
||||
users4 = []User{}
|
||||
db.Not("name = ?", "3").Find(&users4)
|
||||
if len(users1)-len(users4) != int(name_3_count) {
|
||||
t.Errorf("Should find all users's name not equal 3")
|
||||
}
|
||||
|
||||
users4 = []User{}
|
||||
db.Not("name <> ?", "3").Find(&users4)
|
||||
if len(users4) != int(name_3_count) {
|
||||
t.Errorf("Should find all users's name not equal 3")
|
||||
}
|
||||
|
||||
db.Not(User{Name: "3"}).Find(&users5)
|
||||
if len(users1)-len(users5) != int(name_3_count) {
|
||||
t.Errorf("Should find all users's name not equal 3")
|
||||
|
|
Loading…
Reference in New Issue