Files
gf/database/gdb/gdb_structure.go

121 lines
3.0 KiB
Go
Raw Normal View History

// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gdb
import (
"strings"
2019-09-03 00:06:24 +08:00
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/os/gtime"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gbinary"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/util/gconv"
)
2019-12-17 21:06:34 +08:00
// convertValue automatically checks and converts field value from database type
// to golang variable type.
2020-03-08 00:17:42 +08:00
func (c *Core) convertValue(fieldValue []byte, fieldType string) interface{} {
2019-06-19 09:06:52 +08:00
t, _ := gregex.ReplaceString(`\(.+\)`, "", fieldType)
t = strings.ToLower(t)
switch t {
case "binary", "varbinary", "blob", "tinyblob", "mediumblob", "longblob":
2019-07-22 15:10:40 +08:00
return fieldValue
2019-10-17 20:57:49 +08:00
case "int", "tinyint", "small_int", "smallint", "medium_int", "mediumint":
2019-09-03 00:06:24 +08:00
if gstr.ContainsI(fieldType, "unsigned") {
gconv.Uint(string(fieldValue))
}
2019-07-22 15:10:40 +08:00
return gconv.Int(string(fieldValue))
2019-10-17 20:57:49 +08:00
case "big_int", "bigint":
2019-09-03 00:06:24 +08:00
if gstr.ContainsI(fieldType, "unsigned") {
gconv.Uint64(string(fieldValue))
}
2019-07-22 15:10:40 +08:00
return gconv.Int64(string(fieldValue))
2019-06-19 09:06:52 +08:00
case "float", "double", "decimal":
2019-07-22 15:10:40 +08:00
return gconv.Float64(string(fieldValue))
case "bit":
2019-07-22 15:10:40 +08:00
s := string(fieldValue)
2019-10-28 16:42:30 +08:00
// mssql is true|false string.
if strings.EqualFold(s, "true") {
return 1
}
if strings.EqualFold(s, "false") {
return 0
}
2019-07-22 15:10:40 +08:00
return gbinary.BeDecodeToInt64(fieldValue)
case "bool":
2019-06-19 09:06:52 +08:00
return gconv.Bool(fieldValue)
2019-10-28 16:42:30 +08:00
case "date":
t, _ := gtime.StrToTime(string(fieldValue))
return t.Format("Y-m-d")
case "datetime", "timestamp":
t, _ := gtime.StrToTime(string(fieldValue))
return t.String()
2019-06-19 09:06:52 +08:00
default:
2019-10-28 16:42:30 +08:00
// Auto detect field type, using key match.
2019-06-19 09:06:52 +08:00
switch {
case strings.Contains(t, "text") || strings.Contains(t, "char"):
2019-07-22 15:10:40 +08:00
return string(fieldValue)
2019-06-19 09:06:52 +08:00
case strings.Contains(t, "float") || strings.Contains(t, "double"):
2019-07-22 15:10:40 +08:00
return gconv.Float64(string(fieldValue))
2019-06-19 09:06:52 +08:00
case strings.Contains(t, "bool"):
2019-07-22 15:10:40 +08:00
return gconv.Bool(string(fieldValue))
2019-06-19 09:06:52 +08:00
case strings.Contains(t, "binary") || strings.Contains(t, "blob"):
2019-07-22 15:10:40 +08:00
return fieldValue
2019-10-28 16:42:30 +08:00
case strings.Contains(t, "int"):
return gconv.Int(string(fieldValue))
case strings.Contains(t, "time"):
s := string(fieldValue)
t, err := gtime.StrToTime(s)
if err != nil {
return s
}
2019-10-28 16:42:30 +08:00
return t.String()
case strings.Contains(t, "date"):
s := string(fieldValue)
t, err := gtime.StrToTime(s)
if err != nil {
return s
}
2019-10-28 16:42:30 +08:00
return t.Format("Y-m-d")
2019-06-19 09:06:52 +08:00
default:
2019-07-22 15:10:40 +08:00
return string(fieldValue)
2019-06-19 09:06:52 +08:00
}
}
}
2019-12-17 21:06:34 +08:00
// filterFields removes all key-value pairs which are not the field of given table.
2020-03-08 00:17:42 +08:00
func (c *Core) filterFields(schema, table string, data map[string]interface{}) map[string]interface{} {
2019-12-17 21:06:34 +08:00
// It must use data copy here to avoid its changing the origin data map.
2019-09-16 16:54:52 +08:00
newDataMap := make(map[string]interface{}, len(data))
2020-03-08 00:17:42 +08:00
if fields, err := c.DB.TableFields(table, schema); err == nil {
2019-09-16 16:54:52 +08:00
for k, v := range data {
if _, ok := fields[k]; ok {
newDataMap[k] = v
2019-06-19 09:06:52 +08:00
}
}
}
2019-09-16 16:54:52 +08:00
return newDataMap
}