From 8ff21d6936c893c05e56168d4bf6295cc35c5d9b Mon Sep 17 00:00:00 2001 From: John Date: Tue, 9 Jul 2019 08:40:28 +0800 Subject: [PATCH] adding support of slice parameters for gdb.Model.Where --- g/database/gdb/gdb_unit_model_test.go | 9 +++++++++ g/database/gredis/gredis_unit_test.go | 5 +++-- geg/database/gdb/mysql/gdb_args_slice.go | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 geg/database/gdb/mysql/gdb_args_slice.go diff --git a/g/database/gdb/gdb_unit_model_test.go b/g/database/gdb/gdb_unit_model_test.go index 9f97fe586..8b5431fd5 100644 --- a/g/database/gdb/gdb_unit_model_test.go +++ b/g/database/gdb/gdb_unit_model_test.go @@ -625,6 +625,15 @@ func TestModel_Where(t *testing.T) { gtest.AssertGT(len(result), 0) gtest.Assert(result["id"].Int(), 3) }) + // slice parameter + gtest.Case(t, func() { + result, err := db.Table("user").Where("id=? and nickname=?", g.Slice{3, "T3"}).One() + if err != nil { + gtest.Fatal(err) + } + gtest.AssertGT(len(result), 0) + gtest.Assert(result["id"].Int(), 3) + }) gtest.Case(t, func() { result, err := db.Table("user").Where("id", 3).One() if err != nil { diff --git a/g/database/gredis/gredis_unit_test.go b/g/database/gredis/gredis_unit_test.go index 99e9427cf..c58bed430 100644 --- a/g/database/gredis/gredis_unit_test.go +++ b/g/database/gredis/gredis_unit_test.go @@ -7,11 +7,12 @@ package gredis_test import ( + "testing" + "time" + "github.com/gogf/gf/g/database/gredis" "github.com/gogf/gf/g/test/gtest" redis2 "github.com/gogf/gf/third/github.com/gomodule/redigo/redis" - "testing" - "time" ) var ( diff --git a/geg/database/gdb/mysql/gdb_args_slice.go b/geg/database/gdb/mysql/gdb_args_slice.go new file mode 100644 index 000000000..28e3193c8 --- /dev/null +++ b/geg/database/gdb/mysql/gdb_args_slice.go @@ -0,0 +1,17 @@ +package main + +import ( + "github.com/gogf/gf/g" +) + +func main() { + db := g.DB() + conditions := g.Map{ + "nickname like ?": "%T%", + "id between ? and ?": g.Slice{1, 3}, + "id >= ?": 1, + "create_time > ?": 0, + "id in(?)": g.Slice{1, 2, 3}, + } + db.Table("user").Where(conditions).OrderBy("id asc").All() +}