| 1 |
package schema |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"reflect" |
| 7 |
"regexp" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"gorm.io/gorm/clause" |
| 11 |
"gorm.io/gorm/utils" |
| 12 |
) |
| 13 |
|
| 14 |
var embeddedCacheKey = "embedded_cache_store" |
| 15 |
|
| 16 |
func ParseTagSetting(str string, sep string) map[string]string { |
| 17 |
settings := map[string]string{} |
| 18 |
names := strings.Split(str, sep) |
| 19 |
|
| 20 |
var parsedNames []string |
| 21 |
for i := 0; i < len(names); i++ { |
| 22 |
s := names[i] |
| 23 |
for strings.HasSuffix(s, "\\") && i+1 < len(names) { |
| 24 |
i++ |
| 25 |
s = s[:len(s)-1] + sep + names[i] |
| 26 |
} |
| 27 |
parsedNames = append(parsedNames, s) |
| 28 |
} |
| 29 |
|
| 30 |
for _, tag := range parsedNames { |
| 31 |
values := strings.Split(tag, ":") |
| 32 |
k := strings.TrimSpace(strings.ToUpper(values[0])) |
| 33 |
if len(values) >= 2 { |
| 34 |
val := strings.Join(values[1:], ":") |
| 35 |
val = strings.ReplaceAll(val, `\"`, `"`) |
| 36 |
settings[k] = val |
| 37 |
} else if k != "" { |
| 38 |
settings[k] = k |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return settings |
| 43 |
} |
| 44 |
|
| 45 |
func toColumns(val string) (results []string) { |
| 46 |
if val != "" { |
| 47 |
for _, v := range strings.Split(val, ",") { |
| 48 |
results = append(results, strings.TrimSpace(v)) |
| 49 |
} |
| 50 |
} |
| 51 |
return |
| 52 |
} |
| 53 |
|
| 54 |
func removeSettingFromTag(tag reflect.StructTag, names ...string) reflect.StructTag { |
| 55 |
for _, name := range names { |
| 56 |
tag = reflect.StructTag(regexp.MustCompile(`(?i)(gorm:.*?)(`+name+`(:.*?)?)(;|("))`).ReplaceAllString(string(tag), "${1}${5}")) |
| 57 |
} |
| 58 |
return tag |
| 59 |
} |
| 60 |
|
| 61 |
func appendSettingFromTag(tag reflect.StructTag, value string) reflect.StructTag { |
| 62 |
t := tag.Get("gorm") |
| 63 |
if strings.Contains(t, value) { |
| 64 |
return tag |
| 65 |
} |
| 66 |
return reflect.StructTag(fmt.Sprintf(`gorm:"%s;%s"`, value, t)) |
| 67 |
} |
| 68 |
|
| 69 |
// GetRelationsValues get relations's values from a reflect value |
| 70 |
func GetRelationsValues(ctx context.Context, reflectValue reflect.Value, rels []*Relationship) (reflectResults reflect.Value) { |
| 71 |
for _, rel := range rels { |
| 72 |
reflectResults = reflect.MakeSlice(reflect.SliceOf(reflect.PointerTo(rel.FieldSchema.ModelType)), 0, 1) |
| 73 |
|
| 74 |
appendToResults := func(value reflect.Value) { |
| 75 |
if _, isZero := rel.Field.ValueOf(ctx, value); !isZero { |
| 76 |
result := reflect.Indirect(rel.Field.ReflectValueOf(ctx, value)) |
| 77 |
switch result.Kind() { |
| 78 |
case reflect.Struct: |
| 79 |
reflectResults = reflect.Append(reflectResults, result.Addr()) |
| 80 |
case reflect.Slice, reflect.Array: |
| 81 |
for i := 0; i < result.Len(); i++ { |
| 82 |
if elem := result.Index(i); elem.Kind() == reflect.Ptr { |
| 83 |
reflectResults = reflect.Append(reflectResults, elem) |
| 84 |
} else { |
| 85 |
reflectResults = reflect.Append(reflectResults, elem.Addr()) |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
switch reflectValue.Kind() { |
| 93 |
case reflect.Struct: |
| 94 |
appendToResults(reflectValue) |
| 95 |
case reflect.Slice: |
| 96 |
for i := 0; i < reflectValue.Len(); i++ { |
| 97 |
appendToResults(reflectValue.Index(i)) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
reflectValue = reflectResults |
| 102 |
} |
| 103 |
|
| 104 |
return |
| 105 |
} |
| 106 |
|
| 107 |
// GetIdentityFieldValuesMap get identity map from fields |
| 108 |
func GetIdentityFieldValuesMap(ctx context.Context, reflectValue reflect.Value, fields []*Field) (map[string][]reflect.Value, [][]interface{}) { |
| 109 |
var ( |
| 110 |
results = [][]interface{}{} |
| 111 |
dataResults = map[string][]reflect.Value{} |
| 112 |
loaded = map[interface{}]bool{} |
| 113 |
notZero, zero bool |
| 114 |
) |
| 115 |
|
| 116 |
if reflectValue.Kind() == reflect.Ptr || |
| 117 |
reflectValue.Kind() == reflect.Interface { |
| 118 |
reflectValue = reflectValue.Elem() |
| 119 |
} |
| 120 |
|
| 121 |
switch reflectValue.Kind() { |
| 122 |
case reflect.Map: |
| 123 |
results = [][]interface{}{make([]interface{}, len(fields))} |
| 124 |
for idx, field := range fields { |
| 125 |
mapValue := reflectValue.MapIndex(reflect.ValueOf(field.DBName)) |
| 126 |
if mapValue.IsZero() { |
| 127 |
mapValue = reflectValue.MapIndex(reflect.ValueOf(field.Name)) |
| 128 |
} |
| 129 |
results[0][idx] = mapValue.Interface() |
| 130 |
} |
| 131 |
|
| 132 |
dataResults[utils.ToStringKey(results[0]...)] = []reflect.Value{reflectValue} |
| 133 |
case reflect.Struct: |
| 134 |
results = [][]interface{}{make([]interface{}, len(fields))} |
| 135 |
|
| 136 |
for idx, field := range fields { |
| 137 |
results[0][idx], zero = field.ValueOf(ctx, reflectValue) |
| 138 |
notZero = notZero || !zero |
| 139 |
} |
| 140 |
|
| 141 |
if !notZero { |
| 142 |
return nil, nil |
| 143 |
} |
| 144 |
|
| 145 |
dataResults[utils.ToStringKey(results[0]...)] = []reflect.Value{reflectValue} |
| 146 |
case reflect.Slice, reflect.Array: |
| 147 |
for i := 0; i < reflectValue.Len(); i++ { |
| 148 |
elem := reflectValue.Index(i) |
| 149 |
elemKey := elem.Interface() |
| 150 |
if elem.Kind() != reflect.Ptr && elem.CanAddr() { |
| 151 |
elemKey = elem.Addr().Interface() |
| 152 |
} |
| 153 |
|
| 154 |
if _, ok := loaded[elemKey]; ok { |
| 155 |
continue |
| 156 |
} |
| 157 |
loaded[elemKey] = true |
| 158 |
|
| 159 |
fieldValues := make([]interface{}, len(fields)) |
| 160 |
notZero = false |
| 161 |
for idx, field := range fields { |
| 162 |
fieldValues[idx], zero = field.ValueOf(ctx, elem) |
| 163 |
notZero = notZero || !zero |
| 164 |
} |
| 165 |
|
| 166 |
if notZero { |
| 167 |
dataKey := utils.ToStringKey(fieldValues...) |
| 168 |
if _, ok := dataResults[dataKey]; !ok { |
| 169 |
results = append(results, fieldValues) |
| 170 |
dataResults[dataKey] = []reflect.Value{elem} |
| 171 |
} else { |
| 172 |
dataResults[dataKey] = append(dataResults[dataKey], elem) |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return dataResults, results |
| 179 |
} |
| 180 |
|
| 181 |
// GetIdentityFieldValuesMapFromValues get identity map from fields |
| 182 |
func GetIdentityFieldValuesMapFromValues(ctx context.Context, values []interface{}, fields []*Field) (map[string][]reflect.Value, [][]interface{}) { |
| 183 |
resultsMap := map[string][]reflect.Value{} |
| 184 |
results := [][]interface{}{} |
| 185 |
|
| 186 |
for _, v := range values { |
| 187 |
rm, rs := GetIdentityFieldValuesMap(ctx, reflect.Indirect(reflect.ValueOf(v)), fields) |
| 188 |
for k, v := range rm { |
| 189 |
resultsMap[k] = append(resultsMap[k], v...) |
| 190 |
} |
| 191 |
results = append(results, rs...) |
| 192 |
} |
| 193 |
return resultsMap, results |
| 194 |
} |
| 195 |
|
| 196 |
// ToQueryValues to query values |
| 197 |
func ToQueryValues(table string, foreignKeys []string, foreignValues [][]interface{}) (interface{}, []interface{}) { |
| 198 |
queryValues := make([]interface{}, len(foreignValues)) |
| 199 |
if len(foreignKeys) == 1 { |
| 200 |
for idx, r := range foreignValues { |
| 201 |
queryValues[idx] = r[0] |
| 202 |
} |
| 203 |
|
| 204 |
return clause.Column{Table: table, Name: foreignKeys[0]}, queryValues |
| 205 |
} |
| 206 |
|
| 207 |
columns := make([]clause.Column, len(foreignKeys)) |
| 208 |
for idx, key := range foreignKeys { |
| 209 |
columns[idx] = clause.Column{Table: table, Name: key} |
| 210 |
} |
| 211 |
|
| 212 |
for idx, r := range foreignValues { |
| 213 |
queryValues[idx] = r |
| 214 |
} |
| 215 |
|
| 216 |
return columns, queryValues |
| 217 |
} |
| 218 |
|
| 219 |
type embeddedNamer struct { |
| 220 |
Table string |
| 221 |
Namer |
| 222 |
} |