LOOP-FOR-AS-TUPLES — Iterate over all the tuples of a query via a loop clause
Loop Clause
var [type-spec] being {each | the} {record | records | tuple | tuples} {in | of} query [from database]
A d-var-spec, as defined in the grammar for loop-clauses in the ANSI Standard for Common Lisp. This allows for the usual loop-style destructuring.
An optional type-spec either simple or destructured, as defined in the grammar for loop-clauses in the ANSI Standard for Common Lisp.
An sql expression that represents an SQL query which is expected to return a (possibly empty) result set, where each tuple has as many attributes as function takes arguments.
An optional database object. This will default to the value of *default-database*.
This clause is an iteration driver for loop, that binds the given variable (possibly destructured) to the consecutive tuples (which are represented as lists of attribute values) in the result set returned by executing the SQL query expression on the database specified.
(defvar *my-db* (connect '("dent" "newesim" "dent" "dent")) "My database" => *MY-DB* (loop with time-graph = (make-hash-table :test #'equal) with event-graph = (make-hash-table :test #'equal) for (time event) being the tuples of "select time,event from log" from *my-db* do (incf (gethash time time-graph 0)) (incf (gethash event event-graph 0)) finally (flet ((show-graph (k v) (format t "~40A => ~5D~%" k v))) (format t "~&Time-Graph:~%===========~%") (maphash #'show-graph time-graph) (format t "~&~%Event-Graph:~%============~%") (maphash #'show-graph event-graph)) (return (values time-graph event-graph))) >> Time-Graph: >> =========== >> D => 53000 >> X => 3 >> test-me => 3000 >> >> Event-Graph: >> ============ >> CLOS Benchmark entry. => 9000 >> Demo Text... => 3 >> doit-text => 3000 >> C Benchmark entry. => 12000 >> CLOS Benchmark entry => 32000 => #<EQUAL hash table, 3 entries {48350A1D}> => #<EQUAL hash table, 5 entries {48350FCD}>