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)
| 301 | // currently we don't support non-string model ids |
| 302 | // (see https://github.com/pocketbase/pocketbase/issues/3110). |
| 303 | func 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. |
| 338 | func resaveViewsWithChangedFields(app App, excludeIds ...string) error { |
no test coverage detected
searching dependent graphs…