mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
删除mssql中的convertValue方法。
修改测试案例中的datetime类型的字段在查到数据后做转换
This commit is contained in:
parent
5d7d8abbde
commit
8f7363df41
@ -14,10 +14,7 @@ package gdb
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/encoding/gbinary"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/text/gregex"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -176,64 +173,3 @@ func (db *dbMssql) getTableFields(table string) (fields map[string]string, err e
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 字段类型转换,将数据库字段类型转换为golang变量类型
|
||||
//mssql的datetime类型返回的格式为:"2018-02-09T20:46:17Z",所以这里对datetime类型转换为:"2018-02-09 20:46:17"
|
||||
//@TODO 下面这些类型的处理是从MYSQL拷过来的,需要针对mssql完善
|
||||
// 字段类型转换,将数据库字段类型转换为golang变量类型
|
||||
func (db *dbMssql) convertValue(fieldValue []byte, fieldType string) interface{} {
|
||||
t, _ := gregex.ReplaceString(`\(.+\)`, "", fieldType)
|
||||
t = strings.ToLower(t)
|
||||
switch t {
|
||||
case "binary", "varbinary", "blob", "tinyblob", "mediumblob", "longblob":
|
||||
return fieldValue
|
||||
|
||||
case "int", "tinyint", "small_int", "medium_int":
|
||||
return gconv.Int(string(fieldValue))
|
||||
|
||||
case "big_int":
|
||||
return gconv.Int64(string(fieldValue))
|
||||
|
||||
case "float", "double", "decimal":
|
||||
return gconv.Float64(string(fieldValue))
|
||||
|
||||
case "bit":
|
||||
s := string(fieldValue)
|
||||
// 这里的字符串判断是为兼容不同的数据库类型,如: mssql
|
||||
if strings.EqualFold(s, "true") {
|
||||
return 1
|
||||
}
|
||||
if strings.EqualFold(s, "false") {
|
||||
return 0
|
||||
}
|
||||
return gbinary.BeDecodeToInt64(fieldValue)
|
||||
|
||||
case "bool":
|
||||
return gconv.Bool(fieldValue)
|
||||
|
||||
default:
|
||||
// 自动识别类型, 以便默认支持更多数据库类型
|
||||
switch {
|
||||
case strings.Contains(t, "datetime"):
|
||||
t, _ := gtime.StrToTime(gconv.String(fieldValue))
|
||||
return t.UTC().String()
|
||||
case strings.Contains(t, "int"):
|
||||
return gconv.Int(string(fieldValue))
|
||||
|
||||
case strings.Contains(t, "text") || strings.Contains(t, "char"):
|
||||
return string(fieldValue)
|
||||
|
||||
case strings.Contains(t, "float") || strings.Contains(t, "double"):
|
||||
return gconv.Float64(string(fieldValue))
|
||||
|
||||
case strings.Contains(t, "bool"):
|
||||
return gconv.Bool(string(fieldValue))
|
||||
|
||||
case strings.Contains(t, "binary") || strings.Contains(t, "blob"):
|
||||
return fieldValue
|
||||
|
||||
default:
|
||||
return string(fieldValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ package gdb_test
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/frame/g"
|
||||
@ -406,6 +407,14 @@ func Test_Model_Struct_Mssql(t *testing.T) {
|
||||
|
||||
n, _ := res.RowsAffected()
|
||||
gtest.Assert(n, 1)
|
||||
|
||||
res, err = msdb.Table(table).Data("create_time", "2018-10-10 00:01:10").Where("id = ?", 2).Update()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
n, _ = res.RowsAffected()
|
||||
gtest.Assert(n, 1)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
@ -432,6 +441,7 @@ func Test_Model_Struct_Mssql(t *testing.T) {
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
fmt.Println("id=1 ", user.CreateTime.String())
|
||||
gtest.Assert(user.NickName, "T111")
|
||||
gtest.Assert(user.CreateTime.String(), "2018-10-10 00:01:10")
|
||||
})
|
||||
@ -444,11 +454,12 @@ func Test_Model_Struct_Mssql(t *testing.T) {
|
||||
CreateTime *gtime.Time
|
||||
}
|
||||
user := new(User)
|
||||
err := msdb.Table(table).Where("id=1").Struct(user)
|
||||
err := msdb.Table(table).Where("id=2").Struct(user)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
gtest.Assert(user.NickName, "T111")
|
||||
fmt.Println("id=2 ", user.CreateTime.String())
|
||||
gtest.Assert(user.NickName, "T2")
|
||||
gtest.Assert(user.CreateTime.String(), "2018-10-10 00:01:10")
|
||||
})
|
||||
|
||||
@ -875,11 +886,11 @@ func Test_Model_Limit_Mssql(t *testing.T) {
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(result[0]["CREATE_TIME"].String(), result[0]["CREATE_TIME"].GTime().String(), result[0]["CREATE_TIME"].Time().String())
|
||||
gtest.Assert(len(result), 3)
|
||||
gtest.Assert(result[0]["ID"].Int(), 1)
|
||||
gtest.Assert(result[0]["NICKNAME"].String(), "T1")
|
||||
gtest.Assert(result[0]["CREATE_TIME"].String(), "2018-10-10 00:01:10")
|
||||
gtest.Assert(result[0]["CREATE_TIME"].GTime().String(), "2018-10-10 00:01:10")
|
||||
//gtest.Assert(result[0]["CREATE_TIME"].GTime("Y-m-d H:i:s").String(), "2018-10-10 00:01:10")
|
||||
|
||||
gtest.Assert(result[1]["ID"].Int(), 2)
|
||||
|
@ -71,7 +71,7 @@ func Test_Model_Inherit_MapToStruct_Mssql(t *testing.T) {
|
||||
}
|
||||
type Base struct {
|
||||
Ids
|
||||
CreateTime string `json:"create_time"`
|
||||
CreateTime *gtime.Time `json:"create_time"`
|
||||
}
|
||||
type User struct {
|
||||
Base
|
||||
|
@ -15,7 +15,11 @@ import (
|
||||
)
|
||||
|
||||
func Test_TX_Query_Mssql(t *testing.T) {
|
||||
tx, err := db.Begin()
|
||||
if msdb == nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := msdb.Begin()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
@ -43,7 +47,11 @@ func Test_TX_Query_Mssql(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_TX_Exec_Mssql(t *testing.T) {
|
||||
tx, err := db.Begin()
|
||||
if msdb == nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx, err := msdb.Begin()
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
@ -65,6 +73,10 @@ func Test_TX_Exec_Mssql(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_TX_Commit_Mssql(t *testing.T) {
|
||||
if msdb == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if msdb == nil {
|
||||
return
|
||||
}
|
||||
@ -347,7 +359,7 @@ func Test_TX_Update_Mssql(t *testing.T) {
|
||||
if value, err := msdb.Table(table).Fields("create_time").Where("id", 3).Value(); err != nil {
|
||||
gtest.Fatal(err)
|
||||
} else {
|
||||
gtest.Assert(value.String(), "2010-10-10 00:00:01")
|
||||
gtest.Assert(value.GTime().String(), "2010-10-10 00:00:01")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user