MCPcopy
hub / github.com/pocketbase/pocketbase / normalizeViewQueryId

Function normalizeViewQueryId

core/collection_query.go:303–335  ·  view source on GitHub ↗

normalizeViewQueryId wraps (if necessary) the provided view query with a subselect to ensure that the id column is a text since currently we don't support non-string model ids (see https://github.com/pocketbase/pocketbase/issues/3110).

(app App, query string)

Source from the content-addressed store, hash-verified

301// currently we don't support non-string model ids
302// (see https://github.com/pocketbase/pocketbase/issues/3110).
303func normalizeViewQueryId(app App, query string) (string, error) {
304 query = strings.Trim(strings.TrimSpace(query), ";")
305
306 info, err := getQueryTableInfo(app, query)
307 if err != nil {
308 return "", err
309 }
310
311 for _, row := range info {
312 if strings.EqualFold(row.Name, FieldNameId) && strings.EqualFold(row.Type, "TEXT") {
313 return query, nil // no wrapping needed
314 }
315 }
316
317 // raw parse to preserve the columns order
318 rawParsed := new(identifiersParser)
319 if err := rawParsed.parse(query); err != nil {
320 return "", err
321 }
322
323 columns := make([]string, 0, len(rawParsed.columns))
324 for _, col := range rawParsed.columns {
325 if col.alias == FieldNameId {
326 columns = append(columns, fmt.Sprintf("CAST([[%s]] as TEXT) [[%s]]", col.alias, col.alias))
327 } else {
328 columns = append(columns, "[["+col.alias+"]]")
329 }
330 }
331
332 query = fmt.Sprintf("SELECT %s FROM (%s)", strings.Join(columns, ","), query)
333
334 return query, nil
335}
336
337// resaveViewsWithChangedFields updates all view collections with changed fields.
338func resaveViewsWithChangedFields(app App, excludeIds ...string) error {

Callers 2

saveViewCollectionFunction · 0.85
onCollectionSaveExecuteFunction · 0.85

Calls 2

getQueryTableInfoFunction · 0.85
parseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…