Files
gf/database/gdb/gdb_structure.go

145 lines
3.9 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 (
2019-06-19 09:06:52 +08:00
"fmt"
"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"
)
// 字段类型转换将数据库字段类型转换为golang变量类型
2019-07-22 15:10:40 +08:00
func (bs *dbBase) 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)
// 这里的字符串判断是为兼容不同的数据库类型,如: mssql
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)
case "datetime":
t, _ := gtime.StrToTime(string(fieldValue))
return t.String()
2019-06-19 09:06:52 +08:00
default:
// 自动识别类型, 以便默认支持更多数据库类型
switch {
case strings.Contains(t, "int"):
2019-07-22 15:10:40 +08:00
return gconv.Int(string(fieldValue))
2019-06-19 09:06:52 +08:00
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-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
}
}
}
// 将map的数据按照fields进行过滤只保留与表字段同名的数据
func (bs *dbBase) filterFields(table string, data map[string]interface{}) map[string]interface{} {
2019-09-16 16:54:52 +08:00
// Must use data copy avoiding change the origin data map.
newDataMap := make(map[string]interface{}, len(data))
if fields, err := bs.db.TableFields(table); 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
}
// 返回当前数据库所有的数据表名称
func (bs *dbBase) Tables() (tables []string, err error) {
result := (Result)(nil)
result, err = bs.GetAll(`SHOW TABLES`)
if err != nil {
return
}
for _, m := range result {
for _, v := range m {
tables = append(tables, v.String())
}
}
return
}
// 获得指定表表的数据结构构造成map哈希表返回其中键名为表字段名称键值为字段数据结构.
func (bs *dbBase) TableFields(table string) (fields map[string]*TableField, err error) {
2019-06-19 09:06:52 +08:00
// 缓存不存在时会查询数据表结构,缓存后不过期,直至程序重启(重新部署)
v := bs.cache.GetOrSetFunc("table_fields_"+table, func() interface{} {
result := (Result)(nil)
result, err = bs.GetAll(fmt.Sprintf(`SHOW COLUMNS FROM %s`, bs.db.quoteWord(table)))
2019-06-19 09:06:52 +08:00
if err != nil {
return nil
}
fields = make(map[string]*TableField)
for i, m := range result {
fields[m["Field"].String()] = &TableField{
Index: i,
Name: m["Field"].String(),
Type: m["Type"].String(),
Null: m["Null"].Bool(),
Key: m["Key"].String(),
Default: m["Default"].Val(),
Extra: m["Extra"].String(),
}
2019-06-19 09:06:52 +08:00
}
return fields
}, 0)
if err == nil {
fields = v.(map[string]*TableField)
2019-06-19 09:06:52 +08:00
}
return
}