2020-02-19 07:53:46 +03:00
|
|
|
package callbacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/jinzhu/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SelectAndOmitColumns get select and omit columns, select -> true, omit -> false
|
2020-04-08 03:15:00 +03:00
|
|
|
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-02-19 07:53:46 +03:00
|
|
|
if field := stmt.Schema.LookUpField(column); field != nil {
|
|
|
|
results[field.DBName] = true
|
|
|
|
} else {
|
|
|
|
results[column] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// omit columns
|
|
|
|
for _, omit := range stmt.Omits {
|
|
|
|
if field := stmt.Schema.LookUpField(omit); field != nil {
|
|
|
|
results[field.DBName] = false
|
|
|
|
} else {
|
|
|
|
results[omit] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 03:15:00 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-08 03:15:00 +03:00
|
|
|
if requireCreate && !field.Creatable {
|
2020-04-19 18:11:56 +03:00
|
|
|
results[name] = false
|
2020-04-08 03:15:00 +03:00
|
|
|
} else if requireUpdate && !field.Updatable {
|
2020-04-19 18:11:56 +03:00
|
|
|
results[name] = false
|
2020-04-08 03:15:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 18:50:48 +03:00
|
|
|
return results, !notRestricted && len(stmt.Selects) > 0
|
2020-02-19 07:53:46 +03:00
|
|
|
}
|
|
|
|
|
2020-04-08 03:15:00 +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))
|
2020-04-08 03:15:00 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-08 03:15:00 +03:00
|
|
|
// 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{}{}
|
2020-04-08 03:15:00 +03:00
|
|
|
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
|
|
|
|
}
|