RecordQuery returns a new Record select query from a collection model, id or name. In case a collection id or name is provided and that collection doesn't actually exists, the generated query will be created with a cancelled context and will fail once an executor (Row(), One(), All(), etc.) is call
(collectionModelOrIdentifier any)
| 24 | // actually exists, the generated query will be created with a cancelled context |
| 25 | // and will fail once an executor (Row(), One(), All(), etc.) is called. |
| 26 | func (app *BaseApp) RecordQuery(collectionModelOrIdentifier any) *dbx.SelectQuery { |
| 27 | var tableName string |
| 28 | |
| 29 | collection, collectionErr := getCollectionByModelOrIdentifier(app, collectionModelOrIdentifier) |
| 30 | if collection != nil { |
| 31 | tableName = collection.Name |
| 32 | } |
| 33 | if tableName == "" { |
| 34 | // update with some fake table name for easier debugging |
| 35 | tableName = "@@__invalidCollectionModelOrIdentifier" |
| 36 | } |
| 37 | |
| 38 | query := app.ConcurrentDB().Select(app.ConcurrentDB().QuoteSimpleColumnName(tableName) + ".*").From(tableName) |
| 39 | |
| 40 | // in case of an error attach a new context and cancel it immediately with the error |
| 41 | if collectionErr != nil { |
| 42 | ctx, cancelFunc := context.WithCancelCause(context.Background()) |
| 43 | query.WithContext(ctx) |
| 44 | cancelFunc(collectionErr) |
| 45 | } |
| 46 | |
| 47 | return query.WithBuildHook(func(q *dbx.Query) { |
| 48 | q.WithExecHook(execLockRetry(app.config.QueryTimeout, defaultMaxLockRetries)). |
| 49 | WithOneHook(func(q *dbx.Query, a any, op func(b any) error) error { |
| 50 | if a == nil { |
| 51 | return op(a) |
| 52 | } |
| 53 | |
| 54 | switch v := a.(type) { |
| 55 | case *Record: |
| 56 | record, err := resolveRecordOneHook(collection, op) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | *v = *record |
| 62 | |
| 63 | return nil |
| 64 | case RecordProxy: |
| 65 | record, err := resolveRecordOneHook(collection, op) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | v.SetProxyRecord(record) |
| 71 | return nil |
| 72 | default: |
| 73 | return op(a) |
| 74 | } |
| 75 | }). |
| 76 | WithAllHook(func(q *dbx.Query, sliceA any, op func(sliceB any) error) error { |
| 77 | if sliceA == nil { |
| 78 | return op(sliceA) |
| 79 | } |
| 80 | |
| 81 | switch v := sliceA.(type) { |
| 82 | case *[]*Record: |
| 83 | records, err := resolveRecordAllHook(collection, op) |
no test coverage detected