From 28b8efe00c9935a9a9d32d00353989be8d712448 Mon Sep 17 00:00:00 2001 From: Gin Date: Wed, 18 Jan 2023 10:17:16 +0800 Subject: [PATCH] fix issue 2403 (#2404) --- .../mysql/mysql_feature_model_builder_test.go | 65 +++++++++++++++++++ database/gdb/gdb_func.go | 3 +- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/contrib/drivers/mysql/mysql_feature_model_builder_test.go b/contrib/drivers/mysql/mysql_feature_model_builder_test.go index 46b986e69..97490523e 100644 --- a/contrib/drivers/mysql/mysql_feature_model_builder_test.go +++ b/contrib/drivers/mysql/mysql_feature_model_builder_test.go @@ -9,8 +9,11 @@ package mysql_test import ( "testing" + "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" + "github.com/gogf/gf/v2/util/gmeta" ) func Test_Model_Builder(t *testing.T) { @@ -59,4 +62,66 @@ func Test_Model_Builder(t *testing.T) { t.AssertNil(err) t.Assert(len(all), 6) }) + + // Where with struct which has a field type of *gtime.Time + gtest.C(t, func(t *gtest.T) { + m := db.Model(table) + b := m.Builder() + + type Query struct { + Id interface{} + Nickname *gtime.Time + } + + where, args := b.Where(&Query{Id: 1}).Build() + t.Assert(where, "`id`=? AND `nickname` IS NULL") + t.Assert(args, []interface{}{1}) + }) + + // Where with struct which has a field type of *gjson.Json + gtest.C(t, func(t *gtest.T) { + m := db.Model(table) + b := m.Builder() + + type Query struct { + Id interface{} + Nickname *gjson.Json + } + + where, args := b.Where(&Query{Id: 1}).Build() + t.Assert(where, "`id`=? AND `nickname` IS NULL") + t.Assert(args, []interface{}{1}) + }) + + // Where with do struct which has a field type of *gtime.Time and generated by gf cli + gtest.C(t, func(t *gtest.T) { + m := db.Model(table) + b := m.Builder() + + type Query struct { + gmeta.Meta `orm:"do:true"` + Id interface{} + Nickname *gtime.Time + } + + where, args := b.Where(&Query{Id: 1}).Build() + t.Assert(where, "`id`=?") + t.Assert(args, []interface{}{1}) + }) + + // Where with do struct which has a field type of *gjson.Json and generated by gf cli + gtest.C(t, func(t *gtest.T) { + m := db.Model(table) + b := m.Builder() + + type Query struct { + gmeta.Meta `orm:"do:true"` + Id interface{} + Nickname *gjson.Json + } + + where, args := b.Where(&Query{Id: 1}).Build() + t.Assert(where, "`id`=?") + t.Assert(args, []interface{}{1}) + }) } diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index fa174cf26..449dc04b7 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -16,6 +16,7 @@ import ( "time" "github.com/gogf/gf/v2/container/garray" + "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/internal/empty" "github.com/gogf/gf/v2/internal/reflection" "github.com/gogf/gf/v2/internal/utils" @@ -210,7 +211,7 @@ func DataToMapDeep(value interface{}) map[string]interface{} { m := gconv.Map(value, structTagPriority...) for k, v := range m { switch v.(type) { - case time.Time, *time.Time, gtime.Time, *gtime.Time: + case time.Time, *time.Time, gtime.Time, *gtime.Time, gjson.Json, *gjson.Json: m[k] = v default: