From 60bb0577839444a0944d39e4a4456155a4f5124a Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Mon, 31 Aug 2020 03:40:18 +0000 Subject: [PATCH] add a new mysql operator for force case sensitie query --- orm/db.go | 11 ++++++----- orm/db_mysql.go | 9 +++++---- orm/orm_test.go | 8 ++++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/orm/db.go b/orm/db.go index 9a1827e8..7536a422 100644 --- a/orm/db.go +++ b/orm/db.go @@ -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, @@ -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" } diff --git a/orm/db_mysql.go b/orm/db_mysql.go index ff6516b7..8dd1e755 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -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": "> ?", diff --git a/orm/orm_test.go b/orm/orm_test.go index f96fb941..18e48288 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -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))