From 8736ffaf6ff87334153ed38591af30136fadd76c Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Sun, 30 Aug 2020 23:38:52 +0800 Subject: [PATCH 1/4] use 'BINARY' key word for exact operator for mysql db --- orm/db_mysql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm/db_mysql.go b/orm/db_mysql.go index 6e99058e..ff6516b7 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -22,7 +22,7 @@ import ( // mysql operators. var mysqlOperators = map[string]string{ - "exact": "= ?", + "exact": "= BINARY ?", "iexact": "LIKE ?", "contains": "LIKE BINARY ?", "icontains": "LIKE ?", 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 2/4] 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)) From ff53e12191a6ab4ec27ec5cbbb301c2e6046d6fd Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Mon, 31 Aug 2020 15:29:48 +0000 Subject: [PATCH 3/4] skip strictexact operator test for db drivers other than mysql --- orm/orm_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/orm/orm_test.go b/orm/orm_test.go index 18e48288..b18a7082 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -822,13 +822,16 @@ 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)) + if dORM.Driver().Name() == "mysql" { + // Now only mysql support `strictexact` + 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__strictexact", "slene").Count() + throwFail(t, err) + throwFail(t, AssertIs(num, 1)) + } num, err = qs.Filter("user_name__contains", "e").Count() throwFail(t, err) From 91410be72279e01e4acd82a369663cf714130f28 Mon Sep 17 00:00:00 2001 From: CadenGuo <411189077@qq.com> Date: Mon, 31 Aug 2020 15:47:37 +0000 Subject: [PATCH 4/4] orm_test:use predefined variable to check db driver --- orm/orm_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orm/orm_test.go b/orm/orm_test.go index b18a7082..93668235 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -822,7 +822,7 @@ func TestOperators(t *testing.T) { throwFail(t, err) throwFail(t, AssertIs(num, 1)) - if dORM.Driver().Name() == "mysql" { + if IsMysql { // Now only mysql support `strictexact` num, err = qs.Filter("user_name__strictexact", "Slene").Count() throwFail(t, err)