MCPcopy
hub / github.com/pocketbase/pocketbase / parseQueryToFields

Function parseQueryToFields

core/view.go:320–485  ·  view source on GitHub ↗
(app App, selectQuery string)

Source from the content-addressed store, hash-verified

318var castRegex = regexp.MustCompile(`(?is)^cast\s*\(.*\s+as\s+(\w+)\s*\)$`)
319
320func parseQueryToFields(app App, selectQuery string) (map[string]*queryField, error) {
321 p := new(identifiersParser)
322 if err := p.parse(selectQuery); err != nil {
323 return nil, err
324 }
325
326 collections, err := findCollectionsByIdentifiers(app, p.tables)
327 if err != nil {
328 return nil, err
329 }
330
331 result := make(map[string]*queryField, len(p.columns))
332
333 var mainTable identifier
334
335 if len(p.tables) > 0 {
336 mainTable = p.tables[0]
337 }
338
339 for _, col := range p.columns {
340 colLower := strings.ToLower(col.original)
341
342 // pk (always assume text field for now)
343 if col.alias == FieldNameId {
344 result[col.alias] = &queryField{
345 field: defaultViewIdField(),
346 }
347 continue
348 }
349
350 // numeric aggregations
351 if strings.HasPrefix(colLower, "count(") {
352 result[col.alias] = &queryField{
353 field: &NumberField{
354 Name: col.alias,
355 OnlyInt: true,
356 },
357 }
358 continue
359 }
360 if strings.HasPrefix(colLower, "total(") {
361 result[col.alias] = &queryField{
362 field: &NumberField{
363 Name: col.alias,
364 },
365 }
366 continue
367 }
368
369 castMatch := castRegex.FindStringSubmatch(colLower)
370
371 // casts
372 if len(castMatch) == 2 {
373 switch castMatch[1] {
374 case "real", "decimal", "numeric":
375 result[col.alias] = &queryField{
376 field: &NumberField{
377 Name: col.alias,

Callers 2

CreateViewFieldsMethod · 0.85
FindRecordByViewFileMethod · 0.85

Calls 11

GetNameMethod · 0.95
GetIdMethod · 0.95
PseudorandomStringFunction · 0.92
defaultViewIdFieldFunction · 0.85
defaultViewFieldFunction · 0.85
GetByIdMethod · 0.80
parseMethod · 0.65
SetIdMethod · 0.65
SetNameMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…