chickadee » postgresql » copy-query*-fold

copy-query-fold KONS KNIL CONNECTION QUERY #!rest PARAMSprocedure
(copy-query*-fold KONS KNIL CONNECTION QUERY [PARAMS] [format: FORMAT] [raw: RAW?])procedure

This is the fundamental COPY TO STDOUT iterator. It calls (kons data seed) for every row of COPY data returned by QUERY, where data is either a string or a blob depending on whether the COPY query asked for binary or text data and seed is the accumulated result from previous calls (initially knil), ie its pattern looks like (KONS DATAN ... (KONS DATA2 (KONS DATA1 KNIL))). It returns the final accumulated result.

The starred and nonstarred version are analogous to query and query*.

(use postgresql)

(let ((conn (connect '((dbname . test)))))
  (copy-query-fold
    (lambda (data sum)
      ;; Note the string-drop-right is necessary because the default
      ;; COPY format uses newlines to indicate row endings
      (+ (string->number (string-drop-right data 1)) sum))
    0 conn "COPY (SELECT 1 UNION SELECT 2) TO STDOUT"))
 => 3