2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2019-01-14 22:55:43 +08:00
|
|
|
|
//
|
|
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2019-01-14 22:55:43 +08:00
|
|
|
|
|
|
|
|
|
|
package gdb
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
|
"fmt"
|
2019-07-11 15:41:06 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
|
2019-09-03 00:06:24 +08:00
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
|
|
|
|
2019-09-02 15:48:25 +08:00
|
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
|
"github.com/gogf/gf/encoding/gbinary"
|
2019-07-11 18:58:31 +08:00
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
|
"github.com/gogf/gf/text/gregex"
|
|
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2019-01-14 22:55:43 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 字段类型转换,将数据库字段类型转换为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-01-14 22:55:43 +08:00
|
|
|
|
|
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-01-14 22:55:43 +08:00
|
|
|
|
|
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-01-14 22:55:43 +08:00
|
|
|
|
|
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))
|
2019-01-14 22:55:43 +08:00
|
|
|
|
|
2019-07-11 18:58:31 +08:00
|
|
|
|
case "bit":
|
2019-07-22 15:10:40 +08:00
|
|
|
|
s := string(fieldValue)
|
2019-07-11 18:58:31 +08:00
|
|
|
|
// 这里的字符串判断是为兼容不同的数据库类型,如: 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)
|
2019-07-11 18:58:31 +08:00
|
|
|
|
|
|
|
|
|
|
case "bool":
|
2019-06-19 09:06:52 +08:00
|
|
|
|
return gconv.Bool(fieldValue)
|
2019-01-14 22:55:43 +08:00
|
|
|
|
|
2019-08-14 22:44:57 +08:00
|
|
|
|
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-01-14 22:55:43 +08:00
|
|
|
|
|
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-01-14 22:55:43 +08:00
|
|
|
|
|
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-01-14 22:55:43 +08:00
|
|
|
|
|
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-01-14 22:55:43 +08:00
|
|
|
|
|
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-01-14 22:55:43 +08:00
|
|
|
|
|
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-01-14 22:55:43 +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))
|
2019-09-02 15:48:25 +08:00
|
|
|
|
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
|
2019-01-14 22:55:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-09-02 15:48:25 +08:00
|
|
|
|
// 返回当前数据库所有的数据表名称
|
|
|
|
|
|
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)
|
2019-07-22 23:48:39 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2019-09-02 15:48:25 +08:00
|
|
|
|
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 {
|
2019-09-02 15:48:25 +08:00
|
|
|
|
fields = v.(map[string]*TableField)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
2019-01-14 22:55:43 +08:00
|
|
|
|
}
|