PasteRack.org
Paste # 63431
2018-11-23 11:58:31

Fork as a new paste.

Paste viewed 1233 times.


Embed:

  1. #lang racket
  2. (define (intersect-tags . k)
  3.   (define-values (exclude-tags include-tags) (split-list (λ (w) (regexp-match? "^-" w)) k))
  4.  
  5.   (ltime "intersect-tags"
  6.     (map row->struct
  7.          (apply query-rows (sqldb-param)
  8.                 (format (string-append "SELECT Files.filename, Files.file_id, Files.size, Files.num_tags FROM Files "
  9.                                        "  INNER JOIN "
  10.                                        "    (SELECT file_id FROM FileTags WHERE tag_id IN (~a) GROUP BY file_id HAVING COUNT(*)=~a"
  11.                                        "       EXCEPT"
  12.                                        "     SELECT file_id FROM FileTags WHERE tag_id IN (~a))"
  13.                                        "    AS sub "
  14.                                        "  ON Files.file_id=sub.file_id ")
  15.                         (string-join (map (λ (x) "?") include-tags) ",")
  16.                         (length include-tags)
  17.                         (string-join (map (λ (x) "?") exclude-tags) ","))
  18.                 (append (map (λ (t) (tag-id-lookup t)) include-tags)
  19.                         (map (λ (t) (tag-id-lookup (substring t 1))) exclude-tags))))))

=>