Count returns the number of deduplicated names matching the query (ignoring limit/offset), so the total matches what Search can display.
(ctx context.Context, q SearchQuery)
| 235 | // Count returns the number of deduplicated names matching the query (ignoring |
| 236 | // limit/offset), so the total matches what Search can display. |
| 237 | func (s *Store) Count(ctx context.Context, q SearchQuery) (int, error) { |
| 238 | if err := s.Init(ctx); err != nil { |
| 239 | return 0, err |
| 240 | } |
| 241 | db, err := s.open() |
| 242 | if err != nil { |
| 243 | return 0, err |
| 244 | } |
| 245 | defer db.Close() |
| 246 | |
| 247 | pattern := "%" + q.Query + "%" |
| 248 | args := []any{pattern, pattern, pattern, pattern, pattern} |
| 249 | if q.Kind != "" { |
| 250 | args = append([]any{q.Kind}, args...) |
| 251 | } |
| 252 | |
| 253 | var count int |
| 254 | err = db.QueryRowContext(ctx, ` |
| 255 | SELECT COUNT(*) FROM ( |
| 256 | SELECT DISTINCT lower(name) FROM metadata_items WHERE `+matchPredicate(q.Kind)+` |
| 257 | )`, |
| 258 | args...).Scan(&count) |
| 259 | if err != nil { |
| 260 | return 0, fmt.Errorf("metadata: count: %w", err) |
| 261 | } |
| 262 | return count, nil |
| 263 | } |
| 264 | |
| 265 | // GetItem returns a single item by kind and install_key. |
| 266 | func (s *Store) GetItem(ctx context.Context, kind, installKey string) (Item, error) { |