From c219c5a705ac69e6c5352b9c09190824a957f2b7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 2 Oct 2019 13:23:35 +0800 Subject: [PATCH] fix bug when query map condtion with no quote --- session_get_test.go | 25 +++++++++++++++++++++++++ statement.go | 8 ++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/session_get_test.go b/session_get_test.go index f93068c8..fcef992e 100644 --- a/session_get_test.go +++ b/session_get_test.go @@ -618,3 +618,28 @@ func TestCustomTypes(t *testing.T) { assert.True(t, has) assert.EqualValues(t, 32, age) } + +func TestGetViaMapCond(t *testing.T) { + type GetViaMapCond struct { + Id int64 + Platform int + Index int + } + + assert.NoError(t, prepareEngine()) + assertSync(t, new(GetViaMapCond)) + + var ( + r GetViaMapCond + platformStr = colMapper.Obj2Table("Platform") + indexStr = colMapper.Obj2Table("Index") + query = map[string]interface{}{ + platformStr: 1, + indexStr: 1, + } + ) + + has, err := testEngine.Where(query).Get(&r) + assert.NoError(t, err) + assert.False(t, has) +} diff --git a/statement.go b/statement.go index ae396c4b..67e35213 100644 --- a/statement.go +++ b/statement.go @@ -149,8 +149,12 @@ func (statement *Statement) And(query interface{}, args ...interface{}) *Stateme cond := builder.Expr(query.(string), args...) statement.cond = statement.cond.And(cond) case map[string]interface{}: - cond := builder.Eq(query.(map[string]interface{})) - statement.cond = statement.cond.And(cond) + queryMap := query.(map[string]interface{}) + newMap := make(map[string]interface{}) + for k, v := range queryMap { + newMap[statement.Engine.Quote(k)] = v + } + statement.cond = statement.cond.And(builder.Eq(newMap)) case builder.Cond: cond := query.(builder.Cond) statement.cond = statement.cond.And(cond) -- 2.40.1