From 6e5ce98d23c65fd7283c84382abfde62a29f9930 Mon Sep 17 00:00:00 2001 From: CyJaySong <29367599+cyjaysong@users.noreply.github.com> Date: Wed, 14 Aug 2024 21:16:17 +0800 Subject: [PATCH] fix(database/gdb): fix #3649 when constructing query param, `gdb.Row` value not directly write to Buffer (#3718) --- .../drivers/mysql/mysql_z_unit_issue_test.go | 17 +++++++++++++++++ database/gdb/gdb_func.go | 6 +----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/contrib/drivers/mysql/mysql_z_unit_issue_test.go b/contrib/drivers/mysql/mysql_z_unit_issue_test.go index 4cbaa619105..e30bc1733c5 100644 --- a/contrib/drivers/mysql/mysql_z_unit_issue_test.go +++ b/contrib/drivers/mysql/mysql_z_unit_issue_test.go @@ -1199,3 +1199,20 @@ func Test_Issue3238(t *testing.T) { } }) } + +// https://github.com/gogf/gf/issues/3649 +func Test_Issue3649(t *testing.T) { + table := createInitTable() + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + sql, err := gdb.CatchSQL(context.Background(), func(ctx context.Context) (err error) { + user := db.Model(table).Ctx(ctx) + _, err = user.Where("create_time = ?", gdb.Raw("now()")).WhereLT("create_time", gdb.Raw("now()")).Count() + return + }) + t.AssertNil(err) + sqlStr := fmt.Sprintf("SELECT COUNT(1) FROM `%s` WHERE (create_time = now()) AND (`create_time` < now())", table) + t.Assert(sql[0], sqlStr) + }) +} diff --git a/database/gdb/gdb_func.go b/database/gdb/gdb_func.go index 38e0d5b4c9f..9064f4a2ed9 100644 --- a/database/gdb/gdb_func.go +++ b/database/gdb/gdb_func.go @@ -777,11 +777,7 @@ func formatWhereKeyValue(in formatWhereKeyValueInput) (newArgs []interface{}) { } else { in.Buffer.WriteString(quotedKey) } - if s, ok := in.Value.(Raw); ok { - in.Buffer.WriteString(gconv.String(s)) - } else { - in.Args = append(in.Args, in.Value) - } + in.Args = append(in.Args, in.Value) } } return in.Args