Files
gf/database/gdb/gdb_structure.go

136 lines
3.7 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"
"github.com/gogf/gf/os/gtime"
"strings"
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"
)
//// 同步数据库表结构到内存中
//func (bs *dbBase) syncTableStructure() {
// bs.tables = make(map[string]map[string]string)
// for _, table := range bs.db.getTables() {
// bs.tables[table], _ = bs.db.getTableFields(table)
// }
//}
// 字段类型转换将数据库字段类型转换为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
case "int", "tinyint", "small_int", "medium_int":
2019-07-22 15:10:40 +08:00
return gconv.Int(string(fieldValue))
2019-06-19 09:06:52 +08:00
case "big_int":
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-06-19 09:06:52 +08:00
if fields, err := bs.db.getTableFields(table); err == nil {
for k, _ := range data {
if _, ok := fields[k]; !ok {
delete(data, k)
}
}
}
return data
}
// 获得指定表表的数据结构构造成map哈希表返回其中键名为表字段名称键值暂无用途(默认为字段数据类型).
func (bs *dbBase) getTableFields(table string) (fields map[string]string, 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]string)
for _, m := range result {
fields[m["Field"].String()] = m["Type"].String()
}
return fields
}, 0)
if err == nil {
fields = v.(map[string]string)
}
return
}
/*
// 获取当前数据库所有的表结构
func (bs *dbBase) getTables() []string {
if result, _ := bs.GetAll(`SHOW TABLES`); result != nil {
array := make([]string, len(result))
for i, m := range result {
for _, v := range m {
array[i] = v.String()
break
}
}
return array
}
return nil
}
2019-06-19 09:06:52 +08:00
*/