2021-01-03 23:44:44 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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 (
|
2021-12-23 22:00:28 +08:00
|
|
|
"context"
|
2022-05-18 11:05:05 +08:00
|
|
|
"database/sql/driver"
|
2021-12-23 22:00:28 +08:00
|
|
|
"reflect"
|
2019-07-11 15:41:06 +08:00
|
|
|
"strings"
|
2020-08-21 23:41:12 +08:00
|
|
|
"time"
|
2019-07-11 15:41:06 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/encoding/gbinary"
|
2022-05-18 11:55:09 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2023-09-04 21:23:54 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/intlog"
|
2021-12-23 22:00:28 +08:00
|
|
|
"github.com/gogf/gf/v2/internal/json"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2022-08-11 21:47:35 +08:00
|
|
|
"github.com/gogf/gf/v2/text/gregex"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gutil"
|
2019-01-14 22:55:43 +08:00
|
|
|
)
|
|
|
|
|
2023-09-04 21:23:54 +08:00
|
|
|
// GetFieldTypeStr retrieves and returns the field type string for certain field by name.
|
|
|
|
func (c *Core) GetFieldTypeStr(ctx context.Context, fieldName, table, schema string) string {
|
|
|
|
field := c.GetFieldType(ctx, fieldName, table, schema)
|
|
|
|
if field != nil {
|
|
|
|
return field.Type
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFieldType retrieves and returns the field type object for certain field by name.
|
|
|
|
func (c *Core) GetFieldType(ctx context.Context, fieldName, table, schema string) *TableField {
|
|
|
|
fieldsMap, err := c.db.TableFields(ctx, table, schema)
|
|
|
|
if err != nil {
|
|
|
|
intlog.Errorf(
|
|
|
|
ctx,
|
|
|
|
`TableFields failed for table "%s", schema "%s": %+v`,
|
|
|
|
table, schema, err,
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for tableFieldName, tableField := range fieldsMap {
|
|
|
|
if tableFieldName == fieldName {
|
|
|
|
return tableField
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-23 22:00:28 +08:00
|
|
|
// ConvertDataForRecord is a very important function, which does converting for any data that
|
|
|
|
// will be inserted into table/collection as a record.
|
|
|
|
//
|
|
|
|
// The parameter `value` should be type of *map/map/*struct/struct.
|
|
|
|
// It supports embedded struct definition for struct.
|
2023-09-04 21:23:54 +08:00
|
|
|
func (c *Core) ConvertDataForRecord(ctx context.Context, value interface{}, table string) (map[string]interface{}, error) {
|
2022-05-18 11:55:09 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
data = DataToMapDeep(value)
|
|
|
|
)
|
2023-09-04 21:23:54 +08:00
|
|
|
for fieldName, fieldValue := range data {
|
|
|
|
data[fieldName], err = c.db.ConvertValueForField(
|
|
|
|
ctx,
|
|
|
|
c.GetFieldTypeStr(ctx, fieldName, table, c.GetSchema()),
|
|
|
|
fieldValue,
|
|
|
|
)
|
2022-05-18 11:55:09 +08:00
|
|
|
if err != nil {
|
2023-09-04 21:23:54 +08:00
|
|
|
return nil, gerror.Wrapf(err, `ConvertDataForRecord failed for value: %#v`, fieldValue)
|
2022-05-18 11:55:09 +08:00
|
|
|
}
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
2022-05-18 11:55:09 +08:00
|
|
|
return data, nil
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
|
|
|
|
2023-09-04 21:23:54 +08:00
|
|
|
// ConvertValueForField converts value to the type of the record field.
|
|
|
|
// The parameter `fieldType` is the target record field.
|
|
|
|
// The parameter `fieldValue` is the value that to be committed to record field.
|
|
|
|
func (c *Core) ConvertValueForField(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
|
2022-05-18 11:05:05 +08:00
|
|
|
var (
|
|
|
|
err error
|
2023-09-04 21:23:54 +08:00
|
|
|
convertedValue = fieldValue
|
2022-05-18 11:05:05 +08:00
|
|
|
)
|
|
|
|
// If `value` implements interface `driver.Valuer`, it then uses the interface for value converting.
|
2023-09-04 21:23:54 +08:00
|
|
|
if valuer, ok := fieldValue.(driver.Valuer); ok {
|
2022-05-18 11:05:05 +08:00
|
|
|
if convertedValue, err = valuer.Value(); err != nil {
|
2022-05-18 11:16:25 +08:00
|
|
|
if err != nil {
|
2022-05-18 11:55:09 +08:00
|
|
|
return nil, err
|
2022-05-18 11:16:25 +08:00
|
|
|
}
|
2022-05-18 11:05:05 +08:00
|
|
|
}
|
2022-05-18 11:55:09 +08:00
|
|
|
return convertedValue, nil
|
2022-05-18 11:05:05 +08:00
|
|
|
}
|
|
|
|
// Default value converting.
|
2021-12-23 22:00:28 +08:00
|
|
|
var (
|
2023-09-04 21:23:54 +08:00
|
|
|
rvValue = reflect.ValueOf(fieldValue)
|
2021-12-23 22:00:28 +08:00
|
|
|
rvKind = rvValue.Kind()
|
|
|
|
)
|
|
|
|
for rvKind == reflect.Ptr {
|
|
|
|
rvValue = rvValue.Elem()
|
|
|
|
rvKind = rvValue.Kind()
|
|
|
|
}
|
|
|
|
switch rvKind {
|
|
|
|
case reflect.Slice, reflect.Array, reflect.Map:
|
|
|
|
// It should ignore the bytes type.
|
2023-09-04 21:23:54 +08:00
|
|
|
if _, ok := fieldValue.([]byte); !ok {
|
2021-12-23 22:00:28 +08:00
|
|
|
// Convert the value to JSON.
|
2023-09-04 21:23:54 +08:00
|
|
|
convertedValue, err = json.Marshal(fieldValue)
|
2022-05-18 11:55:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case reflect.Struct:
|
2023-09-04 21:23:54 +08:00
|
|
|
switch r := fieldValue.(type) {
|
2021-12-23 22:00:28 +08:00
|
|
|
// If the time is zero, it then updates it to nil,
|
|
|
|
// which will insert/update the value to database as "null".
|
|
|
|
case time.Time:
|
|
|
|
if r.IsZero() {
|
2022-05-18 11:05:05 +08:00
|
|
|
convertedValue = nil
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case gtime.Time:
|
|
|
|
if r.IsZero() {
|
2022-05-18 11:05:05 +08:00
|
|
|
convertedValue = nil
|
2022-05-19 21:44:53 +08:00
|
|
|
} else {
|
|
|
|
convertedValue = r.Time
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case *gtime.Time:
|
|
|
|
if r.IsZero() {
|
2022-05-18 11:05:05 +08:00
|
|
|
convertedValue = nil
|
2022-05-19 21:44:53 +08:00
|
|
|
} else {
|
|
|
|
convertedValue = r.Time
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case *time.Time:
|
|
|
|
// Nothing to do.
|
|
|
|
|
|
|
|
case Counter, *Counter:
|
|
|
|
// Nothing to do.
|
|
|
|
|
|
|
|
default:
|
2023-06-28 22:06:51 +08:00
|
|
|
// If `value` implements interface iNil,
|
|
|
|
// check its IsNil() function, if got ture,
|
|
|
|
// which will insert/update the value to database as "null".
|
2023-09-04 21:23:54 +08:00
|
|
|
if v, ok := fieldValue.(iNil); ok && v.IsNil() {
|
2023-06-28 22:06:51 +08:00
|
|
|
convertedValue = nil
|
2023-09-04 21:23:54 +08:00
|
|
|
} else if s, ok := fieldValue.(iString); ok {
|
2023-06-28 22:06:51 +08:00
|
|
|
// Use string conversion in default.
|
2022-05-18 11:05:05 +08:00
|
|
|
convertedValue = s.String()
|
2021-12-23 22:00:28 +08:00
|
|
|
} else {
|
|
|
|
// Convert the value to JSON.
|
2023-09-04 21:23:54 +08:00
|
|
|
convertedValue, err = json.Marshal(fieldValue)
|
2022-05-18 11:55:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-18 11:55:09 +08:00
|
|
|
return convertedValue, nil
|
2021-12-23 22:00:28 +08:00
|
|
|
}
|
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
// CheckLocalTypeForField checks and returns corresponding type for given db type.
|
2023-09-04 21:23:54 +08:00
|
|
|
func (c *Core) CheckLocalTypeForField(ctx context.Context, fieldType string, fieldValue interface{}) (LocalType, error) {
|
2022-08-11 21:47:35 +08:00
|
|
|
var (
|
|
|
|
typeName string
|
|
|
|
typePattern string
|
|
|
|
)
|
|
|
|
match, _ := gregex.MatchString(`(.+?)\((.+)\)`, fieldType)
|
|
|
|
if len(match) == 3 {
|
|
|
|
typeName = gstr.Trim(match[1])
|
|
|
|
typePattern = gstr.Trim(match[2])
|
|
|
|
} else {
|
2022-08-26 14:30:33 +08:00
|
|
|
typeName = gstr.Split(fieldType, " ")[0]
|
2022-08-11 21:47:35 +08:00
|
|
|
}
|
2023-06-28 10:06:33 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
typeName = strings.ToLower(typeName)
|
2023-06-28 10:06:33 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
switch typeName {
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeBinary,
|
|
|
|
fieldTypeVarbinary,
|
|
|
|
fieldTypeBlob,
|
|
|
|
fieldTypeTinyblob,
|
|
|
|
fieldTypeMediumblob,
|
|
|
|
fieldTypeLongblob:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeBytes, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeInt,
|
|
|
|
fieldTypeTinyint,
|
|
|
|
fieldTypeSmallInt,
|
|
|
|
fieldTypeSmallint,
|
|
|
|
fieldTypeMediumInt,
|
|
|
|
fieldTypeMediumint,
|
|
|
|
fieldTypeSerial:
|
2022-08-11 21:47:35 +08:00
|
|
|
if gstr.ContainsI(fieldType, "unsigned") {
|
|
|
|
return LocalTypeUint, nil
|
|
|
|
}
|
|
|
|
return LocalTypeInt, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeBigInt,
|
|
|
|
fieldTypeBigint,
|
|
|
|
fieldTypeBigserial:
|
2022-08-11 21:47:35 +08:00
|
|
|
if gstr.ContainsI(fieldType, "unsigned") {
|
|
|
|
return LocalTypeUint64, nil
|
|
|
|
}
|
|
|
|
return LocalTypeInt64, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeReal:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeFloat32, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeDecimal,
|
|
|
|
fieldTypeMoney,
|
|
|
|
fieldTypeNumeric,
|
|
|
|
fieldTypeSmallmoney:
|
|
|
|
return LocalTypeString, nil
|
|
|
|
case
|
|
|
|
fieldTypeFloat,
|
|
|
|
fieldTypeDouble:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeFloat64, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeBit:
|
2022-08-11 21:47:35 +08:00
|
|
|
// It is suggested using bit(1) as boolean.
|
|
|
|
if typePattern == "1" {
|
|
|
|
return LocalTypeBool, nil
|
|
|
|
}
|
|
|
|
s := gconv.String(fieldValue)
|
|
|
|
// mssql is true|false string.
|
|
|
|
if strings.EqualFold(s, "true") || strings.EqualFold(s, "false") {
|
|
|
|
return LocalTypeBool, nil
|
|
|
|
}
|
|
|
|
if gstr.ContainsI(fieldType, "unsigned") {
|
|
|
|
return LocalTypeUint64Bytes, nil
|
|
|
|
}
|
|
|
|
return LocalTypeInt64Bytes, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeBool:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeBool, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeDate:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeDate, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeDatetime,
|
|
|
|
fieldTypeTimestamp,
|
|
|
|
fieldTypeTimestampz:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeDatetime, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeJson:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeJson, nil
|
|
|
|
|
|
|
|
case
|
2023-06-28 10:06:33 +08:00
|
|
|
fieldTypeJsonb:
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeJsonb, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Auto-detect field type, using key match.
|
|
|
|
switch {
|
|
|
|
case strings.Contains(typeName, "text") || strings.Contains(typeName, "char") || strings.Contains(typeName, "character"):
|
|
|
|
return LocalTypeString, nil
|
|
|
|
|
|
|
|
case strings.Contains(typeName, "float") || strings.Contains(typeName, "double") || strings.Contains(typeName, "numeric"):
|
|
|
|
return LocalTypeFloat64, nil
|
|
|
|
|
|
|
|
case strings.Contains(typeName, "bool"):
|
|
|
|
return LocalTypeBool, nil
|
|
|
|
|
|
|
|
case strings.Contains(typeName, "binary") || strings.Contains(typeName, "blob"):
|
|
|
|
return LocalTypeBytes, nil
|
|
|
|
|
|
|
|
case strings.Contains(typeName, "int"):
|
2022-08-26 14:30:33 +08:00
|
|
|
if gstr.ContainsI(fieldType, "unsigned") {
|
|
|
|
return LocalTypeUint, nil
|
|
|
|
}
|
2022-08-11 21:47:35 +08:00
|
|
|
return LocalTypeInt, nil
|
|
|
|
|
|
|
|
case strings.Contains(typeName, "time"):
|
|
|
|
return LocalTypeDatetime, nil
|
|
|
|
|
|
|
|
case strings.Contains(typeName, "date"):
|
|
|
|
return LocalTypeDatetime, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return LocalTypeString, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 21:16:26 +08:00
|
|
|
// ConvertValueForLocal converts value to local Golang type of value according field type name from database.
|
|
|
|
// The parameter `fieldType` is in lower case, like:
|
|
|
|
// `float(5,2)`, `unsigned double(5,2)`, `decimal(10,2)`, `char(45)`, `varchar(100)`, etc.
|
|
|
|
func (c *Core) ConvertValueForLocal(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
|
2021-02-08 17:57:21 +08:00
|
|
|
// If there's no type retrieved, it returns the `fieldValue` directly
|
|
|
|
// to use its original data type, as `fieldValue` is type of interface{}.
|
2020-07-23 21:01:16 +08:00
|
|
|
if fieldType == "" {
|
2022-07-07 21:16:26 +08:00
|
|
|
return fieldValue, nil
|
2020-07-23 21:01:16 +08:00
|
|
|
}
|
2022-08-11 21:47:35 +08:00
|
|
|
typeName, err := c.db.CheckLocalTypeForField(ctx, fieldType, fieldValue)
|
2022-07-22 16:44:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-21 22:59:14 +08:00
|
|
|
switch typeName {
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeBytes:
|
2023-09-04 21:23:54 +08:00
|
|
|
var typeNameStr = string(typeName)
|
|
|
|
if strings.Contains(typeNameStr, "binary") || strings.Contains(typeNameStr, "blob") {
|
2022-07-22 16:44:24 +08:00
|
|
|
return fieldValue, nil
|
|
|
|
}
|
2022-07-07 21:16:26 +08:00
|
|
|
return gconv.Bytes(fieldValue), nil
|
2019-01-14 22:55:43 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeInt:
|
2022-07-07 21:16:26 +08:00
|
|
|
return gconv.Int(gconv.String(fieldValue)), nil
|
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeUint:
|
2022-07-22 16:44:24 +08:00
|
|
|
return gconv.Uint(gconv.String(fieldValue)), nil
|
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeInt64:
|
2022-07-07 21:16:26 +08:00
|
|
|
return gconv.Int64(gconv.String(fieldValue)), nil
|
2019-01-14 22:55:43 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeUint64:
|
2022-07-22 16:44:24 +08:00
|
|
|
return gconv.Uint64(gconv.String(fieldValue)), nil
|
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeInt64Bytes:
|
2022-07-22 16:44:24 +08:00
|
|
|
return gbinary.BeDecodeToInt64(gconv.Bytes(fieldValue)), nil
|
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeUint64Bytes:
|
2022-07-22 16:44:24 +08:00
|
|
|
return gbinary.BeDecodeToUint64(gconv.Bytes(fieldValue)), nil
|
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeFloat32:
|
2022-07-07 21:16:26 +08:00
|
|
|
return gconv.Float32(gconv.String(fieldValue)), nil
|
2020-05-14 21:15:44 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeFloat64:
|
2022-07-07 21:16:26 +08:00
|
|
|
return gconv.Float64(gconv.String(fieldValue)), nil
|
2019-01-14 22:55:43 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeBool:
|
2020-07-28 20:31:50 +08:00
|
|
|
s := gconv.String(fieldValue)
|
2019-10-28 16:42:30 +08:00
|
|
|
// mssql is true|false string.
|
2019-07-11 18:58:31 +08:00
|
|
|
if strings.EqualFold(s, "true") {
|
2022-07-07 21:16:26 +08:00
|
|
|
return 1, nil
|
2019-07-11 18:58:31 +08:00
|
|
|
}
|
|
|
|
if strings.EqualFold(s, "false") {
|
2022-07-07 21:16:26 +08:00
|
|
|
return 0, nil
|
2019-07-11 18:58:31 +08:00
|
|
|
}
|
2022-07-07 21:16:26 +08:00
|
|
|
return gconv.Bool(fieldValue), nil
|
2019-01-14 22:55:43 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeDate:
|
2022-02-17 23:04:50 +08:00
|
|
|
// Date without time.
|
2020-08-21 23:41:12 +08:00
|
|
|
if t, ok := fieldValue.(time.Time); ok {
|
2022-07-07 21:16:26 +08:00
|
|
|
return gtime.NewFromTime(t).Format("Y-m-d"), nil
|
2020-08-21 23:41:12 +08:00
|
|
|
}
|
2020-07-28 20:31:50 +08:00
|
|
|
t, _ := gtime.StrToTime(gconv.String(fieldValue))
|
2022-07-07 21:16:26 +08:00
|
|
|
return t.Format("Y-m-d"), nil
|
2019-10-28 16:42:30 +08:00
|
|
|
|
2022-08-11 21:47:35 +08:00
|
|
|
case LocalTypeDatetime:
|
2020-08-21 23:41:12 +08:00
|
|
|
if t, ok := fieldValue.(time.Time); ok {
|
2022-07-07 21:16:26 +08:00
|
|
|
return gtime.NewFromTime(t), nil
|
2020-08-21 23:41:12 +08:00
|
|
|
}
|
2020-07-28 20:31:50 +08:00
|
|
|
t, _ := gtime.StrToTime(gconv.String(fieldValue))
|
2022-07-07 21:16:26 +08:00
|
|
|
return t, nil
|
2019-08-14 22:44:57 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
default:
|
2022-07-22 16:44:24 +08:00
|
|
|
return gconv.String(fieldValue), nil
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-01-14 22:55:43 +08:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:47:57 +08:00
|
|
|
// mappingAndFilterData automatically mappings the map key to table field and removes
|
|
|
|
// all key-value pairs that are not the field of given table.
|
2022-05-10 16:31:56 +08:00
|
|
|
func (c *Core) mappingAndFilterData(ctx context.Context, schema, table string, data map[string]interface{}, filter bool) (map[string]interface{}, error) {
|
|
|
|
fieldsMap, err := c.db.TableFields(ctx, c.guessPrimaryTableName(table), schema)
|
2021-11-27 12:59:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fieldsKeyMap := make(map[string]interface{}, len(fieldsMap))
|
|
|
|
for k := range fieldsMap {
|
|
|
|
fieldsKeyMap[k] = nil
|
|
|
|
}
|
|
|
|
// Automatic data key to table field name mapping.
|
|
|
|
var foundKey string
|
|
|
|
for dataKey, dataValue := range data {
|
|
|
|
if _, ok := fieldsKeyMap[dataKey]; !ok {
|
|
|
|
foundKey, _ = gutil.MapPossibleItemByKey(fieldsKeyMap, dataKey)
|
|
|
|
if foundKey != "" {
|
2021-12-22 17:42:26 +08:00
|
|
|
if _, ok = data[foundKey]; !ok {
|
|
|
|
data[foundKey] = dataValue
|
|
|
|
}
|
2021-11-27 12:59:41 +08:00
|
|
|
delete(data, dataKey)
|
2020-10-17 18:16:13 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-27 12:59:41 +08:00
|
|
|
}
|
|
|
|
// Data filtering.
|
|
|
|
// It deletes all key-value pairs that has incorrect field name.
|
|
|
|
if filter {
|
|
|
|
for dataKey := range data {
|
|
|
|
if _, ok := fieldsMap[dataKey]; !ok {
|
|
|
|
delete(data, dataKey)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-17 18:16:13 +08:00
|
|
|
return data, nil
|
2019-01-14 22:55:43 +08:00
|
|
|
}
|