mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve package gdb supporting gtime.Time parameter for Where condition
This commit is contained in:
@ -1,10 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
// apiString is the type assert api for String.
|
||||
type apiString interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
func main() {
|
||||
a := []interface{}{1}
|
||||
fmt.Println(a[1])
|
||||
for i := 0; i < 10; i++ {
|
||||
switch 1 {
|
||||
case 1:
|
||||
continue
|
||||
}
|
||||
fmt.Println(i)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -206,8 +206,13 @@ func GetPrimaryKey(pointer interface{}) string {
|
||||
|
||||
// GetPrimaryKeyCondition returns a new where condition by primary field name.
|
||||
// The optional parameter <where> is like follows:
|
||||
// 123, []int{1, 2, 3}, "john", []string{"john", "smith"}
|
||||
// g.Map{"id": g.Slice{1,2,3}}, g.Map{"id": 1, "name": "john"}, etc.
|
||||
// 123
|
||||
// []int{1, 2, 3}
|
||||
// "john"
|
||||
// []string{"john", "smith"}
|
||||
// g.Map{"id": g.Slice{1,2,3}}
|
||||
// g.Map{"id": 1, "name": "john"}
|
||||
// etc.
|
||||
//
|
||||
// Note that it returns the given <where> parameter directly if there's the <primary> is empty.
|
||||
func GetPrimaryKeyCondition(primary string, where ...interface{}) (newWhereCondition []interface{}) {
|
||||
@ -448,11 +453,23 @@ func handleArguments(sql string, args []interface{}) (newSql string, newArgs []i
|
||||
newArgs = append(newArgs, arg)
|
||||
continue
|
||||
}
|
||||
// It converts the struct to string in default
|
||||
// if it implements the String interface.
|
||||
if v, ok := arg.(apiString); ok {
|
||||
switch v := arg.(type) {
|
||||
case time.Time, *time.Time:
|
||||
newArgs = append(newArgs, arg)
|
||||
continue
|
||||
|
||||
// Special handling for gtime.Time.
|
||||
case gtime.Time:
|
||||
newArgs = append(newArgs, v.String())
|
||||
continue
|
||||
|
||||
default:
|
||||
// It converts the struct to string in default
|
||||
// if it implements the String interface.
|
||||
if v, ok := arg.(apiString); ok {
|
||||
newArgs = append(newArgs, v.String())
|
||||
continue
|
||||
}
|
||||
}
|
||||
newArgs = append(newArgs, arg)
|
||||
|
||||
|
||||
@ -1274,6 +1274,11 @@ func Test_Model_Where_GTime(t *testing.T) {
|
||||
t.Assert(err, nil)
|
||||
t.Assert(len(result), 10)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Table(table).Where("create_time>?", *gtime.NewFromStr("2010-09-01")).All()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(len(result), 10)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_WherePri(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user