(app App, selectQuery string)
| 318 | var castRegex = regexp.MustCompile(`(?is)^cast\s*\(.*\s+as\s+(\w+)\s*\)$`) |
| 319 | |
| 320 | func 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, |
no test coverage detected
searching dependent graphs…