Skip to content

Commit

Permalink
add a new mysql operator for force case sensitie query
Browse files Browse the repository at this point in the history
  • Loading branch information
CadenGuo committed Aug 31, 2020
1 parent 8736ffa commit 60bb057
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 6 additions & 5 deletions orm/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ var (

var (
operators = map[string]bool{
"exact": true,
"iexact": true,
"contains": true,
"icontains": true,
"exact": true,
"iexact": true,
"strictexact": true,
"contains": true,
"icontains": true,
// "regex": true,
// "iregex": true,
"gt": true,
Expand Down Expand Up @@ -1202,7 +1203,7 @@ func (d *dbBase) GenerateOperatorSQL(mi *modelInfo, fi *fieldInfo, operator stri
}
sql = d.ins.OperatorSQL(operator)
switch operator {
case "exact":
case "exact", "strictexact":
if arg == nil {
params[0] = "IS NULL"
}
Expand Down
9 changes: 5 additions & 4 deletions orm/db_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import (

// mysql operators.
var mysqlOperators = map[string]string{
"exact": "= BINARY ?",
"iexact": "LIKE ?",
"contains": "LIKE BINARY ?",
"icontains": "LIKE ?",
"exact": "= ?",
"iexact": "LIKE ?",
"strictexact": "= BINARY ?",
"contains": "LIKE BINARY ?",
"icontains": "LIKE ?",
// "regex": "REGEXP BINARY ?",
// "iregex": "REGEXP ?",
"gt": "> ?",
Expand Down
8 changes: 8 additions & 0 deletions orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,14 @@ func TestOperators(t *testing.T) {
throwFail(t, err)
throwFail(t, AssertIs(num, 1))

num, err = qs.Filter("user_name__strictexact", "Slene").Count()
throwFail(t, err)
throwFail(t, AssertIs(num, 0))

num, err = qs.Filter("user_name__strictexact", "slene").Count()
throwFail(t, err)
throwFail(t, AssertIs(num, 1))

num, err = qs.Filter("user_name__contains", "e").Count()
throwFail(t, err)
throwFail(t, AssertIs(num, 2))
Expand Down

0 comments on commit 60bb057

Please sign in to comment.