MCPcopy
hub / github.com/go-gorm/gorm / ConvertSliceOfMapToValuesForCreate

Function ConvertSliceOfMapToValuesForCreate

callbacks/helper.go:43–94  ·  view source on GitHub ↗

ConvertSliceOfMapToValuesForCreate convert slice of map to values

(stmt *gorm.Statement, mapValues []map[string]interface{})

Source from the content-addressed store, hash-verified

41
42// ConvertSliceOfMapToValuesForCreate convert slice of map to values
43func 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
96func hasReturning(tx *gorm.DB, supportReturning bool) (bool, gorm.ScanMode) {
97 if supportReturning {

Callers 2

ConvertToCreateValuesFunction · 0.85

Calls 3

SelectAndOmitColumnsMethod · 0.80
LookUpFieldMethod · 0.80
AddErrorMethod · 0.65

Tested by 1