Merge pull request #339 from sabracrolleton/master
[postmodern.git] / cl-postgres / protocol.lisp
blob1dd5c92e1fb16f63b8172af987090384627b53cd
1 ;;;; -*- Mode: LISP; Syntax: Ansi-Common-Lisp; Base: 10; Package: CL-POSTGRES; -*-
2 (in-package :cl-postgres)
4 ;; For more information about the PostgreSQL scocket protocol, see
5 ;; http://www.postgresql.org/docs/current/interactive/protocol.html
7 (define-condition protocol-error (error)
8 ((message :initarg :message))
9 (:report (lambda (err stream)
10 (format stream "PostgreSQL protocol error: ~A"
11 (slot-value err 'message))))
12 (:documentation "This is raised if something really unexpected
13 happens in the communcation with the server. Should only happen in
14 case of a bug or a connection to something that is not a \(supported)
15 PostgreSQL server at all."))
17 (defparameter *connection-params* nil
18 "Bound to the current connection's parameter table when executing
19 a query.")
21 (defparameter *ssl-certificate-file* nil
22 "When set to a filename, this file will be used as client
23 certificate for SSL connections.")
24 (defparameter *ssl-key-file* nil
25 "When set to a filename, this file will be used as client key for
26 SSL connections.")
27 (defparameter *ssl-root-ca-file* nil
28 "Should be a path to a root certificate file, typically a pem file.
29 It is used for SSL connections when you want to verify the host using a
30 root ca.")
32 (defmacro message-case (socket &body clauses)
33 "Helper macro for reading messages from the server. A list of cases
34 \(characters that identify the message) can be given, each with a body
35 that handles the message, or the keyword :skip to skip the message.
36 Cases for error and warning messages are always added.
38 The body may contain an initial parameter of the form :LENGTH-SYM SYMBOL
39 where SYMBOL is a symbol to which the remaining length of the packet is
40 bound. This value indicates the number of bytes that have to be read
41 from the socket."
42 (let ((socket-name (gensym))
43 (size-name (gensym))
44 (char-name (gensym))
45 (iter-name (gensym))
46 (t-found nil)
47 (size-sym (and (eq (car clauses) :length-sym)
48 (progn (pop clauses)
49 (pop clauses)))))
51 (flet ((expand-characters (chars)
52 (cond ((eq chars t) (setf t-found t) t)
53 ((consp chars) (mapcar #'char-code chars))
54 (t (char-code chars)))))
55 `(let* ((,socket-name ,socket))
56 (declare (type stream ,socket-name))
57 (labels ((,iter-name ()
58 (let ((,char-name (read-uint1 ,socket-name))
59 (,size-name (read-uint4 ,socket-name)))
60 (declare (type (unsigned-byte 8) ,char-name)
61 (type (unsigned-byte 32) ,size-name)
62 (ignorable ,size-name))
63 (case ,char-name
64 (#.(char-code #\A)
65 (get-notification ,socket-name)
66 (,iter-name))
67 (#.(char-code #\E) (get-error ,socket-name))
68 (#.(char-code #\S) ;; ParameterStatus: read and continue
69 (update-parameter ,socket-name)
70 (,iter-name))
71 (#.(char-code #\K) ;; Backendkey : read and continue
72 (update-backend-key-data ,socket-name)
73 (,iter-name))
74 (#.(char-code #\N) ;; A warning
75 (get-warning ,socket-name)
76 (,iter-name))
77 ,@(mapcar
78 (lambda (clause)
79 `(,(expand-characters (first clause))
80 ,(if (eq (second clause) :skip)
81 `(skip-bytes ,socket-name (- ,size-name 4))
82 (if size-sym
83 `(let ((,size-sym (- ,size-name 4)))
84 ,@(cdr clause))
85 `(progn ,@(cdr clause))))))
86 clauses)
87 ,@(unless t-found
88 `((t (ensure-socket-is-closed ,socket-name)
89 (error 'protocol-error
90 :message
91 (format nil
92 "Unexpected message received: ~A ~a"
93 (code-char ,char-name) ,char-name)))))))))
94 (,iter-name))))))
97 (defun update-parameter (socket)
98 (let ((name (read-str socket))
99 (value (read-str socket)))
100 (setf (gethash name *connection-params*) value)))
102 (defun update-backend-key-data (socket)
103 (let ((pid (read-uint4 socket))
104 (secret-key (read-uint4 socket)))
105 (setf (gethash "pid" *connection-params*) pid)
106 (setf (gethash "secret-key" *connection-params*) secret-key)))
108 (defun read-byte-delimited (socket)
109 "Read the fields of a null-terminated list of byte + string values
110 and put them in an alist."
111 (loop :for type = (read-uint1 socket)
112 :until (zerop type)
113 :collect (cons (code-char type) (read-str socket))))
115 (define-condition postgresql-notification (simple-warning)
116 ((pid :initarg :pid :accessor postgresql-notification-pid)
117 (channel :initarg :channel :accessor postgresql-notification-channel)
118 (payload :initarg :payload :accessor postgresql-notification-payload))
119 (:documentation "The condition that is signalled when a notification message
120 is received from the PostgreSQL server. This is a WARNING condition which is
121 caught by the WAIT-FOR-NOTIFICATION function that implements synchronous
122 waiting for notifications."))
124 (defun get-notification (socket)
125 "Read an asynchronous notification message from the socket and
126 signal a condition for it."
127 (let ((pid (read-int4 socket))
128 (channel (read-str socket))
129 (payload (read-str socket)))
130 (warn 'postgresql-notification
131 :pid pid
132 :channel channel
133 :payload payload
134 :format-control "Asynchronous notification ~S~@[ (payload: ~S)~] ~
135 received from server process with PID ~D."
136 :format-arguments (list channel payload pid))))
138 (defun get-error (socket)
139 "Read an error message from the socket and raise the corresponding
140 database-error condition."
141 (let ((data (read-byte-delimited socket)))
142 (flet ((get-field (char)
143 (cdr (assoc char data))))
144 (let ((code (get-field #\C)))
145 ;; These are the errors "ADMIN SHUTDOWN" and "CRASH SHUTDOWN",
146 ;; in which case the server will close the connection right
147 ;; away.
148 (when (or (string= code "57P01") (string= code "57P02"))
149 (ensure-socket-is-closed socket))
150 (error (cl-postgres-error::get-error-type code)
151 :code code
152 :message (get-field #\M)
153 :detail (get-field #\D)
154 :hint (get-field #\H)
155 :context (get-field #\W)
156 :position (let ((position (get-field #\p)))
157 (when position (parse-integer position))))))))
159 (define-condition postgresql-warning (simple-warning)
162 (defun get-warning (socket)
163 "Read a warning from the socket and emit it."
164 (let ((data (read-byte-delimited socket)))
165 (flet ((get-field (char)
166 (cdr (assoc char data))))
167 (warn 'postgresql-warning
168 :format-control "PostgreSQL warning: ~A~@[~%~A~]"
169 :format-arguments (list (get-field #\M) (or (get-field #\D)
170 (get-field #\H)))))))
172 ;; The let is used to remember that we have found the
173 ;; cl+ssl:make-ssl-client-stream function before.
175 (let ((make-ssl-stream nil)
176 (verify-root-ca nil))
177 (defun initiate-ssl (socket required verify hostname)
178 "Initiate SSL handshake with the PostgreSQL server, and wrap the socket in
179 an SSL stream. When require is true, an error will be raised when the server
180 does not support SSL. When hostname is supplied, the server's certificate will
181 be matched against it."
182 (unless make-ssl-stream
183 (unless (find-package :cl+ssl)
184 (error 'database-error
185 :message "CL+SSL is not loaded. Load it to enable SSL."))
186 (setf make-ssl-stream (intern
187 (string '#:make-ssl-client-stream) :cl+ssl))
188 (setf verify-root-ca (intern
189 (string '#:ssl-load-global-verify-locations) :cl+ssl))
190 (when (and verify *ssl-root-ca-file*)
191 (funcall verify-root-ca *ssl-root-ca-file*)))
192 (ssl-request-message socket)
193 (force-output socket)
194 (ecase (read-byte socket)
195 (#.(char-code #\S)
196 (setf socket (funcall make-ssl-stream socket
197 :key *ssl-key-file*
198 :certificate *ssl-certificate-file*
199 :verify (if verify
200 :required
201 nil)
202 :hostname hostname)))
203 (#.(char-code #\N)
204 (when required
205 (error 'database-error
206 :message "Server does not support SSL encryption."))))))
208 (defun authenticate (socket conn)
209 "Try to initiate a connection. Caller should close the socket if this raises
210 a condition."
211 (let ((gss-context nil)
212 (gss-init-function nil)
213 (user (connection-user conn))
214 (password (connection-password conn))
215 (database (connection-db conn))
216 (hostname (connection-host conn))
217 (use-ssl (connection-use-ssl conn))
218 (application-name (connection-application-name conn))
219 (client-nonce nil)
220 (client-initial-response nil)
221 (expected-server-signature nil))
223 (unless (eq use-ssl :no)
224 (if (eq use-ssl :try)
225 (let ((old-socket socket)
226 (new-socket (initiate-ssl socket nil nil nil)))
227 (if new-socket (setf socket new-socket)
228 (setf socket old-socket)))
229 (setf socket (initiate-ssl socket (member use-ssl '(:require :yes :full))
230 (member use-ssl '(:yes :full))
231 (if (eq use-ssl :full) hostname))))
232 (when (listen socket) ; checks for attempted man-in-the-middle attack
233 (ecase *on-evidence-of-man-in-the-middle-attack*
234 (:error (error 'database-error
235 :message "Postmodern received an unexpectedly large packet in response to the request to use ssl. This may be evidence of an attempt to run a man-in-the-middle type attack. If you want to retry the connect and not throw an error, please set cl-postgres:*on-evidence-of-man-in-the-middle-attack* to :warn or :ignore"))
236 (:warn (warn "Postmodern received an unexpectedly large packet in response to the request to use ssl. This may be evidence of an attempt to run a man-in-the-middle type attack. If you want to some response other than a warning, please set cl-postgres:*on-evidence-of-man-in-the-middle-attack* to :error or :ignore"))
237 (:ignore t))))
238 (when (equal application-name "") (setf application-name "postmodern-default"))
239 (startup-message socket user database application-name)
240 (force-output socket)
241 (labels ((init-gss-msg (in-buffer)
242 (when (null gss-init-function)
243 (when (null (find-package "CL-GSS"))
244 (error 'database-error
245 :message "To use GSS authentication, make sure the CL-GSS package is loaded."))
246 (setq gss-init-function (find-symbol "INIT-SEC" "CL-GSS"))
247 (unless gss-init-function
248 (error 'database-error
249 :message "INIT-SEC not found in CL-GSS package")))
250 (multiple-value-bind (continue-needed context buffer flags)
251 (funcall gss-init-function
252 (format nil "~a@~a" (connection-service conn)
253 (connection-host conn))
254 :flags '(:mutual)
255 :context gss-context
256 :input-token in-buffer)
257 (declare (ignore flags))
258 (setq gss-context context)
259 (when buffer
260 (gss-auth-buffer-message socket buffer))
261 (force-output socket)
262 continue-needed))
263 (scram-msg-init (in-buffer)
264 (let ((server-message
265 (cl-postgres-trivial-utf-8:utf-8-bytes-to-string
266 in-buffer)))
267 (when (not (equal "SCRAM-SHA-256"
268 (subseq server-message 0 13)))
269 (cerror "Mixed messages on authentication methods"
270 server-message))
271 (setf client-nonce (gen-client-nonce))
272 (setf client-initial-response (gen-client-initial-response
273 user client-nonce))
274 (scram-type-message socket client-initial-response)
275 (force-output socket)))
276 (scram-msg-cont (in-buffer)
277 (multiple-value-bind (cont-message calculated-server-signature)
278 (aggregated-gen-final-client-message
279 user client-nonce (clp-utf8:utf-8-bytes-to-string in-buffer)
280 password
281 :salt-type :base64-string
282 :response-type :utf8-string)
283 (setf expected-server-signature calculated-server-signature)
284 (scram-cont-message socket cont-message)
285 (force-output socket)))
286 (scram-msg-fin (in-buffer)
287 (when (not (equal (cdar (split-server-response in-buffer))
288 expected-server-signature))
289 (cerror "Server signature not validated. Something is wrong"
290 (cdar (split-server-response in-buffer))))))
291 (loop
292 (message-case socket :length-sym size ;; Authentication message
293 (#\R (let ((type (read-uint4 socket)))
295 (ecase type
296 (0 (return))
297 (2 (error 'database-error
298 :message "Unsupported Kerberos authentication requested."))
299 (3 (unless password
300 (error "Server requested plain-password authentication, but no password was given."))
301 (plain-password-message socket password)
302 (force-output socket))
303 (4 (error 'database-error
304 :message "Unsupported crypt authentication requested."))
305 (5 (unless password
306 (error "Server requested md5-password authentication, but no password was given."))
307 (md5-password-message socket password user
308 (read-bytes socket 4))
309 (force-output socket))
310 (6 (error 'database-error
311 :message "Unsupported SCM authentication requested."))
312 (7 (when gss-context
313 (error 'database-error
314 :message "Got GSS init message when a context was already established"))
315 (init-gss-msg nil))
316 (8 (unless gss-context
317 (error 'database-error
318 :message "Got GSS continuation message without a context"))
319 (init-gss-msg (read-bytes socket (- size 4))))
320 (9 ) ; auth_required_sspi or auth_req_sspi sspi
321 ;negotiate without wrap() see postgresql
322 ; source code src/libpq/pqcomm.h
323 (10 (progn
324 (scram-msg-init
325 (read-bytes socket (- size 4))))) ;(read-simple-str socket)
326 (11 (progn
327 (scram-msg-cont
328 (read-bytes socket (- size 4))))) ;auth_sasl_continue or auth_req_sasl_cont
329 (12 (progn
330 (scram-msg-fin
331 (read-bytes socket (- size 4)))))))))))
332 (loop
333 (message-case socket
334 ;; ReadyForQuery
335 (#\Z (read-uint1 socket)
336 (return)))))
337 socket)
339 (defgeneric field-name (field)
340 (:documentation "This can be used to get information about the fields read
341 by a row reader. Given a field description, it returns the name the database
342 associated with this column."))
344 (defgeneric field-type (field)
345 (:documentation "This extracts the PostgreSQL OID associated with this column.
346 You can, if you really want to, query the pg_types table to find out more about
347 the types denoted by OIDs."))
349 (defclass field-description ()
350 ((name :initarg :name :accessor field-name)
351 (type-id :initarg :type-id :accessor field-type)
352 (interpreter :initarg :interpreter :accessor field-interpreter)
353 (receive-binary-p :initarg :receive-binary-p :reader field-binary-p))
354 (:documentation "Description of a field in a query result."))
356 (defun read-field-descriptions (socket)
357 "Read the field descriptions for a query result and put them into an
358 array of field-description objects."
359 (declare (type stream socket)
360 #.*optimize*)
361 (let* ((number (read-uint2 socket))
362 (descriptions (make-array number)))
363 (declare (type fixnum number)
364 (type (simple-array field-description) descriptions))
365 (dotimes (i number)
366 (let* ((name (read-str socket))
367 (table-oid (read-uint4 socket))
368 (column (read-uint2 socket))
369 (type-id (read-uint4 socket))
370 (size (read-uint2 socket))
371 (type-modifier (read-uint4 socket))
372 (format (read-uint2 socket))
373 (interpreter (get-type-interpreter type-id)))
374 (declare (ignore table-oid column size type-modifier format)
375 (type string name)
376 (type (unsigned-byte 32) type-id))
377 (setf (elt descriptions i)
378 (if (interpreter-binary-p interpreter)
379 (make-instance 'field-description
380 :name name :type-id type-id
381 :interpreter (type-interpreter-binary-reader
382 interpreter)
383 :receive-binary-p t)
384 (make-instance 'field-description
385 :name name :type-id type-id
386 :interpreter (type-interpreter-text-reader
387 interpreter)
388 :receive-binary-p nil)))))
389 descriptions))
391 (defun terminate-connection (socket)
392 "Close a connection, notifying the server."
393 (terminate-message socket)
394 (close socket))
396 ;; This is a hacky way to communicate the amount of effected rows up
397 ;; from look-for-row to the send-execute or send-query that (directly
398 ;; or indirectly) called it.
399 (defparameter *effected-rows* nil)
401 (defun look-for-row (socket)
402 "Read server messages until either a new row can be read, or there
403 are no more results. Return a boolean indicating whether any more
404 results are available, and, if available, stores the amount of
405 effected rows in *effected-rows*. Also handle getting out of
406 copy-in/copy-out states \(which are not supported)."
407 (declare (type stream socket)
408 #.*optimize*)
409 (loop
410 (message-case socket
411 ;; CommandComplete
412 (#\C (let* ((command-tag (read-str socket))
413 (space (position #\Space command-tag
414 :from-end t)))
415 (when space
416 (setf *effected-rows*
417 (parse-integer command-tag :junk-allowed t
418 :start (1+ space))))
419 (return-from look-for-row nil)))
420 ;; CopyInResponse
421 (#\G (read-uint1 socket)
422 (skip-bytes socket (* 2 (read-uint2 socket))) ; The field formats
423 (copy-done-message socket)
424 (error 'database-error
425 :message "Copy-in not supported."))
426 ;; CopyOutResponse
427 (#\H (read-uint1 socket)
428 (skip-bytes socket (* 2 (read-uint2 socket))) ; The field formats
429 (error 'database-error
430 :message "Copy-out not supported."))
431 ;; DataRow
432 (#\D (skip-bytes socket 2)
433 (return-from look-for-row t))
434 ;; EmptyQueryResponse
435 (#\I (warn "Empty query sent.")
436 (return-from look-for-row nil)))))
438 (defun try-to-sync (socket sync-sent)
439 "Try to re-synchronize a connection by sending a sync message if it
440 hasn't already been sent, and then looking for a ReadyForQuery
441 message."
442 (when (open-stream-p socket)
443 (let ((ok nil))
444 (unwind-protect
445 (progn
446 (unless sync-sent
447 (sync-message socket)
448 (force-output socket))
449 ;; TODO initiate timeout on the socket read, signal timeout error
450 (loop :while (and (not ok) (open-stream-p socket))
451 :do (message-case socket
452 (#\Z (read-uint1 socket)
453 (setf ok t))
454 (t :skip))))
455 (unless ok
456 ;; if we can't sync, make sure the socket is shot
457 ;; (e.g. a timeout, or aborting execution with a restart from sldb)
458 (ensure-socket-is-closed socket :abort t))))))
460 (defmacro with-syncing (&body body)
461 "Macro to wrap a block in a handler that will try to re-sync the
462 connection if something in the block raises a condition. Not hygienic
463 at all, only used right below here."
464 `(let ((sync-sent nil)
465 (ok nil))
466 (handler-case
467 (unwind-protect
468 (multiple-value-prog1
469 (progn ,@body)
470 (setf ok t))
471 (unless ok
472 (try-to-sync socket sync-sent)))
473 (end-of-file (c)
474 (ensure-socket-is-closed socket :abort t)
475 (error c)))))
477 (defmacro returning-effected-rows (value &body body)
478 "Computes a value, then runs a body, then returns, as multiple
479 values, that value and the amount of effected rows, if any (see
480 *effected rows*)."
481 (let ((value-name (gensym)))
482 `(let* ((*effected-rows* nil)
483 (,value-name ,value))
484 ,@body
485 (if *effected-rows*
486 (values ,value-name *effected-rows*)
487 ,value-name))))
489 (defun send-query (socket query row-reader)
490 "Send a query to the server, and apply the given row-reader to the
491 results."
492 (declare (type stream socket)
493 (type string query)
494 #.*optimize*)
495 (with-syncing
496 (with-query (query)
497 (let ((row-description nil))
498 (simple-parse-message socket query)
499 (simple-describe-message socket)
500 (flush-message socket)
501 (force-output socket)
502 (message-case socket
503 ;; ParseComplete
504 (#\1))
505 (message-case socket
506 ;; ParameterDescription
507 (#\t :skip))
508 (message-case socket
509 ;; RowDescription
510 (#\T (setf row-description (read-field-descriptions
511 socket)))
512 ;; NoData
513 (#\n))
514 (simple-bind-message socket (map 'vector
515 'field-binary-p row-description))
516 (simple-execute-message socket)
517 (sync-message socket)
518 (setf sync-sent t)
519 (force-output socket)
520 (message-case socket
521 ;; BindComplete
522 (#\2))
523 (returning-effected-rows
524 (if row-description
525 (funcall row-reader socket row-description)
526 (look-for-row socket))
527 (message-case socket
528 ;; ReadyForQuery, skipping transaction status
529 (#\Z (read-uint1 socket))))))))
531 (defun send-parse (socket name query parameters binary-parameters-p)
532 "Send a parse command to the server, giving it a name."
533 (declare (type stream socket)
534 (type string name query)
535 #.*optimize*)
536 (with-syncing
537 (with-query (query)
538 (if binary-parameters-p
539 (parse-message-binary-parameters socket name query parameters)
540 (parse-message socket name query))
541 (flush-message socket)
542 (force-output socket)
543 (message-case socket
544 ;; ParseComplete
545 (#\1)))))
548 (defun send-close (socket name)
549 "Send a close command to the server, giving it a name."
550 (declare (type stream socket)
551 (type string name)
552 #.*optimize*)
553 (with-syncing
554 (close-prepared-message socket name)
555 (flush-message socket)
556 (force-output socket)
557 (message-case socket
558 ;; CloseComplete
559 (#\3))))
561 (defun send-execute (socket name parameters row-reader binary-parameters-p)
562 "Used by cl-postgres:exec-prepared to Execute a previously parsed query,
563 and apply the given row-reader to the result."
564 (declare (type stream socket)
565 (type string name)
566 (type list parameters)
567 #.*optimize*)
568 (with-syncing
569 (let ((row-description nil)
570 (n-parameters 0))
571 (declare (type (unsigned-byte 16) n-parameters))
572 (describe-prepared-message socket name)
573 (flush-message socket)
574 (force-output socket)
575 (message-case socket
576 ;; ParameterDescription
577 (#\t (setf n-parameters (read-uint2 socket))
578 (skip-bytes socket (* 4 n-parameters))))
579 (message-case socket
580 ;; RowDescription
581 (#\T (setf row-description
582 (read-field-descriptions socket)))
583 ;; NoData
584 (#\n))
585 (unless (= (length parameters) n-parameters)
586 (error 'database-error
587 :message (format nil "Incorrect number of parameters given for prepared statement ~A. ~A parameters expected. ~A parameters received."
588 name n-parameters (length parameters))))
589 (bind-message socket name (map 'vector 'field-binary-p row-description)
590 parameters binary-parameters-p)
591 (simple-execute-message socket)
592 (sync-message socket)
593 (setf sync-sent t)
594 (force-output socket)
595 (message-case socket
596 ;; BindComplete
597 (#\2))
598 (returning-effected-rows
599 (if row-description
600 (funcall row-reader socket row-description)
601 (look-for-row socket))
602 (message-case socket
603 ;; CommandComplete
604 (#\C (read-str socket)
605 (message-case socket
606 (#\Z (read-uint1 socket))))
607 ;; ReadyForQuery, skipping transaction status
608 (#\Z (read-uint1 socket)))))))
610 (defun build-row-reader (function-form fields body)
611 "Helper for the following two macros."
612 (let ((socket (gensym)))
613 `(,@function-form (,socket ,fields)
614 (declare (type stream ,socket)
615 (type (simple-array field-description) ,fields))
616 (flet ((next-row ()
617 (look-for-row ,socket))
618 (next-field (field)
619 (declare (type field-description field))
620 (let ((size (read-int4 ,socket)))
621 (declare (type (signed-byte 32) size))
622 (if #-abcl (eq size -1)
623 #+abcl (eql size -1)
624 :null
625 (funcall (field-interpreter field)
626 ,socket size)))))
627 ,@body))))
629 (defmacro row-reader ((fields) &body body)
630 "Creates a row-reader, using the given name for the variable. Inside the body
631 this variable refers to a vector of field descriptions. On top of that, two
632 local functions are bound, next-row and next-field. The first will start
633 reading the next row in the result, and returns a boolean indicating whether
634 there is another row. The second will read and return one field, and should
635 be passed the corresponding field description from the fields argument as a
636 parameter.
638 A row reader should take care to iterate over all the rows in a result, and
639 within each row iterate over all the fields. This means it should contain
640 an outer loop that calls next-row, and every time next-row returns T it
641 should iterate over the fields vector and call next-field for every field.
643 The definition of list-row-reader should give you an idea what a row reader
644 looks like:
646 (row-reader (fields)
647 (loop :while (next-row)
648 :collect (loop :for field :across fields
649 :collect (next-field field))))
651 Obviously, row readers should not do things with the database connection
652 like, say, close it or start a new query, since it still reading out the
653 results from the current query. A row reader
654 is a function that is used to do something with the results of a query. It has
655 two local functions: next-row and next-field, the first should be called
656 once per row and will return a boolean indicating whether there are
657 any more rows, the second should be called once for every element in
658 the fields vector, with that field as argument, to read a single value
659 in a row. See list-row-reader in public.lisp for an example."
660 (build-row-reader '(lambda) fields body))
662 (defmacro def-row-reader (name (fields) &body body)
663 "The defun-like variant of row-reader: creates a row reader and gives it a
664 top-level function name."
665 (build-row-reader `(defun ,name) fields body))