FindRecordByViewFile returns the original Record of the provided view collection file.
(viewCollectionModelOrIdentifier any, fileFieldName string, filename string)
| 195 | |
| 196 | // FindRecordByViewFile returns the original Record of the provided view collection file. |
| 197 | func (app *BaseApp) FindRecordByViewFile(viewCollectionModelOrIdentifier any, fileFieldName string, filename string) (*Record, error) { |
| 198 | view, err := getCollectionByModelOrIdentifier(app, viewCollectionModelOrIdentifier) |
| 199 | if err != nil { |
| 200 | return nil, err |
| 201 | } |
| 202 | |
| 203 | if !view.IsView() { |
| 204 | return nil, errors.New("not a view collection") |
| 205 | } |
| 206 | |
| 207 | var findFirstNonViewQueryFileField func(int) (*queryField, error) |
| 208 | findFirstNonViewQueryFileField = func(level int) (*queryField, error) { |
| 209 | // check the level depth to prevent infinite circular recursion |
| 210 | // (the limit is arbitrary and may change in the future) |
| 211 | if level > 5 { |
| 212 | return nil, errors.New("reached the max recursion level of view collection file field queries") |
| 213 | } |
| 214 | |
| 215 | queryFields, err := parseQueryToFields(app, view.ViewQuery) |
| 216 | if err != nil { |
| 217 | return nil, err |
| 218 | } |
| 219 | |
| 220 | for _, item := range queryFields { |
| 221 | if item.collection == nil || |
| 222 | item.original == nil || |
| 223 | item.field.GetName() != fileFieldName { |
| 224 | continue |
| 225 | } |
| 226 | |
| 227 | if item.collection.IsView() { |
| 228 | view = item.collection |
| 229 | fileFieldName = item.original.GetName() |
| 230 | return findFirstNonViewQueryFileField(level + 1) |
| 231 | } |
| 232 | |
| 233 | return item, nil |
| 234 | } |
| 235 | |
| 236 | return nil, errors.New("no query file field found") |
| 237 | } |
| 238 | |
| 239 | qf, err := findFirstNonViewQueryFileField(1) |
| 240 | if err != nil { |
| 241 | return nil, err |
| 242 | } |
| 243 | |
| 244 | cleanFieldName := inflector.Columnify(qf.original.GetName()) |
| 245 | |
| 246 | record := &Record{} |
| 247 | |
| 248 | query := app.RecordQuery(qf.collection).Limit(1) |
| 249 | |
| 250 | if opt, ok := qf.original.(MultiValuer); !ok || !opt.IsMultiple() { |
| 251 | query.AndWhere(dbx.HashExp{cleanFieldName: filename}) |
| 252 | } else { |
| 253 | query.InnerJoin( |
| 254 | fmt.Sprintf(`%s as {{_je_file}}`, dbutils.JSONEach(cleanFieldName)), |
nothing calls this directly
no test coverage detected