mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve datetime coverting for gdb
This commit is contained in:
@ -20,7 +20,7 @@ import (
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
// 字段类型转换,将数据库字段类型转换为golang变量类型
|
||||
// convertValue converts field value from database type to golang variable type.
|
||||
func (bs *dbBase) convertValue(fieldValue []byte, fieldType string) interface{} {
|
||||
t, _ := gregex.ReplaceString(`\(.+\)`, "", fieldType)
|
||||
t = strings.ToLower(t)
|
||||
@ -45,7 +45,7 @@ func (bs *dbBase) convertValue(fieldValue []byte, fieldType string) interface{}
|
||||
|
||||
case "bit":
|
||||
s := string(fieldValue)
|
||||
// 这里的字符串判断是为兼容不同的数据库类型,如: mssql
|
||||
// mssql is true|false string.
|
||||
if strings.EqualFold(s, "true") {
|
||||
return 1
|
||||
}
|
||||
@ -57,16 +57,17 @@ func (bs *dbBase) convertValue(fieldValue []byte, fieldType string) interface{}
|
||||
case "bool":
|
||||
return gconv.Bool(fieldValue)
|
||||
|
||||
case "datetime":
|
||||
case "date":
|
||||
t, _ := gtime.StrToTime(string(fieldValue))
|
||||
return t.Format("Y-m-d")
|
||||
|
||||
case "datetime", "timestamp":
|
||||
t, _ := gtime.StrToTime(string(fieldValue))
|
||||
return t.String()
|
||||
|
||||
default:
|
||||
// 自动识别类型, 以便默认支持更多数据库类型
|
||||
// Auto detect field type, using key match.
|
||||
switch {
|
||||
case strings.Contains(t, "int"):
|
||||
return gconv.Int(string(fieldValue))
|
||||
|
||||
case strings.Contains(t, "text") || strings.Contains(t, "char"):
|
||||
return string(fieldValue)
|
||||
|
||||
@ -79,6 +80,17 @@ func (bs *dbBase) convertValue(fieldValue []byte, fieldType string) interface{}
|
||||
case strings.Contains(t, "binary") || strings.Contains(t, "blob"):
|
||||
return fieldValue
|
||||
|
||||
case strings.Contains(t, "int"):
|
||||
return gconv.Int(string(fieldValue))
|
||||
|
||||
case strings.Contains(t, "time"):
|
||||
t, _ := gtime.StrToTime(string(fieldValue))
|
||||
return t.String()
|
||||
|
||||
case strings.Contains(t, "date"):
|
||||
t, _ := gtime.StrToTime(string(fieldValue))
|
||||
return t.Format("Y-m-d")
|
||||
|
||||
default:
|
||||
return string(fieldValue)
|
||||
}
|
||||
@ -87,7 +99,7 @@ func (bs *dbBase) convertValue(fieldValue []byte, fieldType string) interface{}
|
||||
|
||||
// 将map的数据按照fields进行过滤,只保留与表字段同名的数据
|
||||
func (bs *dbBase) filterFields(table string, data map[string]interface{}) map[string]interface{} {
|
||||
// Must use data copy avoiding change the origin data map.
|
||||
// It must use data copy here to avoid changing the origin data map.
|
||||
newDataMap := make(map[string]interface{}, len(data))
|
||||
if fields, err := bs.db.TableFields(table); err == nil {
|
||||
for k, v := range data {
|
||||
|
||||
@ -127,7 +127,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
gtest.Assert(one["passport"].String(), "user_3")
|
||||
gtest.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
||||
gtest.Assert(one["nickname"].String(), "name_3")
|
||||
gtest.Assert(one["create_time"].String(), timeStr)
|
||||
gtest.Assert(one["create_time"].GTime().String(), timeStr)
|
||||
|
||||
// *struct
|
||||
timeStr = gtime.Now().String()
|
||||
@ -148,7 +148,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
gtest.Assert(one["passport"].String(), "t4")
|
||||
gtest.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
||||
gtest.Assert(one["nickname"].String(), "name_4")
|
||||
gtest.Assert(one["create_time"].String(), timeStr)
|
||||
gtest.Assert(one["create_time"].GTime().String(), timeStr)
|
||||
|
||||
// batch with Insert
|
||||
timeStr = gtime.Now().String()
|
||||
@ -178,7 +178,7 @@ func Test_DB_Insert(t *testing.T) {
|
||||
gtest.Assert(one["passport"].String(), "t200")
|
||||
gtest.Assert(one["password"].String(), "25d55ad283aa400af464c76d71qw07ad")
|
||||
gtest.Assert(one["nickname"].String(), "T200")
|
||||
gtest.Assert(one["create_time"].String(), timeStr)
|
||||
gtest.Assert(one["create_time"].GTime().String(), timeStr)
|
||||
})
|
||||
}
|
||||
|
||||
@ -299,7 +299,7 @@ func Test_DB_Save(t *testing.T) {
|
||||
gtest.Assert(one["passport"].String(), "t1")
|
||||
gtest.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
||||
gtest.Assert(one["nickname"].String(), "T11")
|
||||
gtest.Assert(one["create_time"].String(), timeStr)
|
||||
gtest.Assert(one["create_time"].GTime().String(), timeStr)
|
||||
})
|
||||
}
|
||||
|
||||
@ -324,7 +324,7 @@ func Test_DB_Replace(t *testing.T) {
|
||||
gtest.Assert(one["passport"].String(), "t1")
|
||||
gtest.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
|
||||
gtest.Assert(one["nickname"].String(), "T11")
|
||||
gtest.Assert(one["create_time"].String(), timeStr)
|
||||
gtest.Assert(one["create_time"].GTime().String(), timeStr)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user