| 1 |
package callbacks |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"sort" |
| 6 |
|
| 7 |
"gorm.io/gorm" |
| 8 |
"gorm.io/gorm/clause" |
| 9 |
) |
| 10 |
|
| 11 |
// ConvertMapToValuesForCreate convert map to values |
| 12 |
func ConvertMapToValuesForCreate(stmt *gorm.Statement, mapValue map[string]interface{}) (values clause.Values) { |
| 13 |
values.Columns = make([]clause.Column, 0, len(mapValue)) |
| 14 |
selectColumns, restricted := stmt.SelectAndOmitColumns(true, false) |
| 15 |
|
| 16 |
keys := make([]string, 0, len(mapValue)) |
| 17 |
for k := range mapValue { |
| 18 |
keys = append(keys, k) |
| 19 |
} |
| 20 |
sort.Strings(keys) |
| 21 |
|
| 22 |
for _, k := range keys { |
| 23 |
value := mapValue[k] |
| 24 |
if stmt.Schema != nil { |
| 25 |
if field := stmt.Schema.LookUpField(k); field != nil { |
| 26 |
k = field.DBName |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
if v, ok := selectColumns[k]; (ok && v) || (!ok && !restricted) { |
| 31 |
values.Columns = append(values.Columns, clause.Column{Name: k}) |
| 32 |
if len(values.Values) == 0 { |
| 33 |
values.Values = [][]interface{}{{}} |
| 34 |
} |
| 35 |
|
| 36 |
values.Values[0] = append(values.Values[0], value) |
| 37 |
} |
| 38 |
} |
| 39 |
return |
| 40 |
} |
| 41 |
|
| 42 |
// ConvertSliceOfMapToValuesForCreate convert slice of map to values |
| 43 |
func ConvertSliceOfMapToValuesForCreate(stmt *gorm.Statement, mapValues []map[string]interface{}) (values clause.Values) { |
| 44 |
columns := make([]string, 0, len(mapValues)) |
| 45 |
|
| 46 |
// when the length of mapValues is zero,return directly here |
| 47 |
// no need to call stmt.SelectAndOmitColumns method |
| 48 |
if len(mapValues) == 0 { |
| 49 |
stmt.AddError(gorm.ErrEmptySlice) |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
var ( |
| 54 |
result = make(map[string][]interface{}, len(mapValues)) |
| 55 |
selectColumns, restricted = stmt.SelectAndOmitColumns(true, false) |
| 56 |
) |
| 57 |
|
| 58 |
for idx, mapValue := range mapValues { |
| 59 |
for k, v := range mapValue { |
| 60 |
if stmt.Schema != nil { |
| 61 |
if field := stmt.Schema.LookUpField(k); field != nil { |
| 62 |
k = field.DBName |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
if _, ok := result[k]; !ok { |
| 67 |
if v, ok := selectColumns[k]; (ok && v) || (!ok && !restricted) { |
| 68 |
result[k] = make([]interface{}, len(mapValues)) |
| 69 |
columns = append(columns, k) |
| 70 |
} else { |
| 71 |
continue |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
result[k][idx] = v |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
sort.Strings(columns) |
| 80 |
values.Values = make([][]interface{}, len(mapValues)) |
| 81 |
values.Columns = make([]clause.Column, len(columns)) |
| 82 |
for idx, column := range columns { |
| 83 |
values.Columns[idx] = clause.Column{Name: column} |
| 84 |
|
| 85 |
for i, v := range result[column] { |
| 86 |
if len(values.Values[i]) == 0 { |
| 87 |
values.Values[i] = make([]interface{}, len(columns)) |
| 88 |
} |
| 89 |
|
| 90 |
values.Values[i][idx] = v |
| 91 |
} |
| 92 |
} |
| 93 |
return |
| 94 |
} |
| 95 |
|
| 96 |
func hasReturning(tx *gorm.DB, supportReturning bool) (bool, gorm.ScanMode) { |
| 97 |
if supportReturning { |
| 98 |
if c, ok := tx.Statement.Clauses["RETURNING"]; ok { |
| 99 |
returning, _ := c.Expression.(clause.Returning) |
| 100 |
if len(returning.Columns) == 0 || (len(returning.Columns) == 1 && returning.Columns[0].Name == "*") { |
| 101 |
return true, 0 |
| 102 |
} |
| 103 |
return true, gorm.ScanUpdate |
| 104 |
} |
| 105 |
} |
| 106 |
return false, 0 |
| 107 |
} |
| 108 |
|
| 109 |
func checkMissingWhereConditions(db *gorm.DB) { |
| 110 |
if !db.AllowGlobalUpdate && db.Error == nil { |
| 111 |
where, withCondition := db.Statement.Clauses["WHERE"] |
| 112 |
if withCondition { |
| 113 |
if _, withSoftDelete := db.Statement.Clauses["soft_delete_enabled"]; withSoftDelete { |
| 114 |
whereClause, _ := where.Expression.(clause.Where) |
| 115 |
withCondition = len(whereClause.Exprs) > 1 |
| 116 |
} |
| 117 |
} |
| 118 |
if !withCondition { |
| 119 |
db.AddError(gorm.ErrMissingWhereClause) |
| 120 |
} |
| 121 |
return |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
type visitMap = map[reflect.Value]bool |
| 126 |
|
| 127 |
// Check if circular values, return true if loaded |
| 128 |
func loadOrStoreVisitMap(visitMap *visitMap, v reflect.Value) (loaded bool) { |
| 129 |
if v.Kind() == reflect.Ptr { |
| 130 |
v = v.Elem() |
| 131 |
} |
| 132 |
|
| 133 |
switch v.Kind() { |
| 134 |
case reflect.Slice, reflect.Array: |
| 135 |
loaded = true |
| 136 |
for i := 0; i < v.Len(); i++ { |
| 137 |
if !loadOrStoreVisitMap(visitMap, v.Index(i)) { |
| 138 |
loaded = false |
| 139 |
} |
| 140 |
} |
| 141 |
case reflect.Struct, reflect.Interface: |
| 142 |
if v.CanAddr() { |
| 143 |
p := v.Addr() |
| 144 |
if _, ok := (*visitMap)[p]; ok { |
| 145 |
return true |
| 146 |
} |
| 147 |
(*visitMap)[p] = true |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return |
| 152 |
} |