gorm/callbacks/helper.go

123 lines
2.9 KiB
Go
Raw Normal View History

2020-02-19 07:53:46 +03:00
package callbacks
import (
"sort"
2020-06-02 04:16:07 +03:00
"gorm.io/gorm"
"gorm.io/gorm/clause"
2020-02-19 07:53:46 +03:00
)
// SelectAndOmitColumns get select and omit columns, select -> true, omit -> false
func SelectAndOmitColumns(stmt *gorm.Statement, requireCreate, requireUpdate bool) (map[string]bool, bool) {
2020-02-19 07:53:46 +03:00
results := map[string]bool{}
2020-05-23 18:50:48 +03:00
notRestricted := false
2020-02-19 07:53:46 +03:00
// select columns
for _, column := range stmt.Selects {
2020-03-08 08:24:08 +03:00
if column == "*" {
2020-05-23 18:50:48 +03:00
notRestricted = true
2020-03-08 08:24:08 +03:00
for _, dbName := range stmt.Schema.DBNames {
results[dbName] = true
}
2020-03-23 17:40:12 +03:00
break
2020-03-08 08:24:08 +03:00
}
2020-05-24 12:24:23 +03:00
if field := stmt.Schema.LookUpField(column); field != nil && field.DBName != "" {
2020-02-19 07:53:46 +03:00
results[field.DBName] = true
} else {
results[column] = true
}
}
// omit columns
for _, omit := range stmt.Omits {
2020-05-31 08:34:53 +03:00
if field := stmt.Schema.LookUpField(omit); field != nil && field.DBName != "" {
2020-02-19 07:53:46 +03:00
results[field.DBName] = false
} else {
results[omit] = false
}
}
if stmt.Schema != nil {
2020-04-19 18:11:56 +03:00
for _, field := range stmt.Schema.Fields {
name := field.DBName
if name == "" {
name = field.Name
}
if requireCreate && !field.Creatable {
2020-04-19 18:11:56 +03:00
results[name] = false
} else if requireUpdate && !field.Updatable {
2020-04-19 18:11:56 +03:00
results[name] = false
}
}
}
2020-05-23 18:50:48 +03:00
return results, !notRestricted && len(stmt.Selects) > 0
2020-02-19 07:53:46 +03:00
}
// ConvertMapToValuesForCreate convert map to values
func ConvertMapToValuesForCreate(stmt *gorm.Statement, mapValue map[string]interface{}) (values clause.Values) {
2020-02-19 07:53:46 +03:00
columns := make([]string, 0, len(mapValue))
selectColumns, restricted := SelectAndOmitColumns(stmt, true, false)
2020-02-19 07:53:46 +03:00
var keys []string
for k, _ := range mapValue {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
2020-03-07 08:43:20 +03:00
value := mapValue[k]
2020-02-19 07:53:46 +03:00
if field := stmt.Schema.LookUpField(k); field != nil {
k = field.DBName
}
if v, ok := selectColumns[k]; (ok && v) || (!ok && !restricted) {
columns = append(columns, k)
2020-03-07 08:43:20 +03:00
values.Values[0] = append(values.Values[0], value)
2020-02-19 07:53:46 +03:00
}
}
return
}
// ConvertSliceOfMapToValuesForCreate convert slice of map to values
func ConvertSliceOfMapToValuesForCreate(stmt *gorm.Statement, mapValues []map[string]interface{}) (values clause.Values) {
2020-02-19 07:53:46 +03:00
var (
columns = []string{}
result = map[string][]interface{}{}
selectColumns, restricted = SelectAndOmitColumns(stmt, true, false)
2020-02-19 07:53:46 +03:00
)
for idx, mapValue := range mapValues {
for k, v := range mapValue {
if field := stmt.Schema.LookUpField(k); field != nil {
k = field.DBName
}
if _, ok := result[k]; !ok {
if v, ok := selectColumns[k]; (ok && v) || (!ok && !restricted) {
result[k] = make([]interface{}, len(mapValues))
columns = append(columns, k)
} else {
continue
}
}
result[k][idx] = v
}
}
sort.Strings(columns)
values.Values = make([][]interface{}, len(mapValues))
for idx, column := range columns {
for i, v := range result[column] {
if i == 0 {
values.Values[i] = make([]interface{}, len(columns))
}
values.Values[i][idx] = v
}
}
return
}