1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: postgresql-socket.cl
6 ;;;; Purpose: Low-level PostgreSQL interface using sockets
7 ;;;; Programmers: Kevin M. Rosenberg based on
8 ;;;; Original code by Pierre R. Mai
10 ;;;; Date Started: Feb 2002
12 ;;;; $Id: postgresql-socket-api.cl,v 1.13 2002/04/06 23:23:47 kevin Exp $
14 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
15 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
17 ;;;; CLSQL users are granted the rights to distribute and use this software
18 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
19 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
20 ;;;; *************************************************************************
23 ;;;; Changes by Kevin Rosenberg
24 ;;;; - Added socket open functions for Allegro and Lispworks
25 ;;;; - Changed CMUCL FFI to UFFI
26 ;;;; - Added necessary (force-output) for socket streams on
27 ;;;; Allegro and Lispworks
28 ;;;; - Added initialization variable
29 ;;;; - Added field type processing
32 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
33 (in-package :postgresql-socket)
35 (uffi:def-enum pgsql-ftype
43 (defmethod database-type-library-loaded ((database-type
44 (eql :postgresql-socket)))
45 "T if foreign library was able to be loaded successfully. Always true for
52 (defmacro define-message-constants (description &rest clauses)
53 (assert (evenp (length clauses)))
54 (loop with seen-characters = nil
55 for (name char) on clauses by #'cddr
56 for char-code = (char-code char)
57 for doc-string = (format nil "~A (~:C): ~A" description char name)
58 if (member char seen-characters)
59 do (error "Duplicate message type ~@C for group ~A" char description)
62 `(defconstant ,name ,char-code ,doc-string)
64 and do (push char seen-characters)
66 (return `(progn ,@result-clauses))))
68 (eval-when (:compile-toplevel :load-toplevel :execute)
69 (define-message-constants "Backend Message Constants"
70 +ascii-row-message+ #\D
71 +authentication-message+ #\R
72 +backend-key-message+ #\K
73 +binary-row-message+ #\B
74 +completed-response-message+ #\C
75 +copy-in-response-message+ #\G
76 +copy-out-response-message+ #\H
77 +cursor-response-message+ #\P
78 +empty-query-response-message+ #\I
79 +error-response-message+ #\E
80 +function-response-message+ #\V
81 +notice-response-message+ #\N
82 +notification-response-message+ #\A
83 +ready-for-query-message+ #\Z
84 +row-description-message+ #\T))
86 (defgeneric send-socket-value (type socket value))
88 (defmethod send-socket-value ((type (eql 'int32)) socket (value integer))
89 (write-byte (ldb (byte 8 24) value) socket)
90 (write-byte (ldb (byte 8 16) value) socket)
91 (write-byte (ldb (byte 8 8) value) socket)
92 (write-byte (ldb (byte 8 0) value) socket))
94 (defmethod send-socket-value ((type (eql 'int16)) socket (value integer))
95 (write-byte (ldb (byte 8 8) value) socket)
96 (write-byte (ldb (byte 8 0) value) socket))
98 (defmethod send-socket-value ((type (eql 'int8)) socket (value integer))
99 (write-byte (ldb (byte 8 0) value) socket))
101 (defmethod send-socket-value ((type (eql 'string)) socket (value string))
102 (loop for char across value
103 for code = (char-code char)
104 do (write-byte code socket)
105 finally (write-byte 0 socket)))
107 (defmethod send-socket-value ((type (eql 'limstring)) socket (value string))
108 (loop for char across value
109 for code = (char-code char)
110 do (write-byte code socket)))
112 (defmethod send-socket-value ((type (eql 'byte)) socket (value integer))
113 (write-byte value socket))
115 (defmethod send-socket-value ((type (eql 'byte)) socket (value character))
116 (write-byte (char-code value) socket))
118 (defmethod send-socket-value ((type (eql 'byte)) socket value)
119 (write-sequence value socket))
121 (defgeneric read-socket-value (type socket))
123 (defmethod read-socket-value ((type (eql 'int32)) socket)
125 (setf (ldb (byte 8 24) result) (read-byte socket))
126 (setf (ldb (byte 8 16) result) (read-byte socket))
127 (setf (ldb (byte 8 8) result) (read-byte socket))
128 (setf (ldb (byte 8 0) result) (read-byte socket))
131 (defmethod read-socket-value ((type (eql 'int16)) socket)
133 (setf (ldb (byte 8 8) result) (read-byte socket))
134 (setf (ldb (byte 8 0) result) (read-byte socket))
137 (defmethod read-socket-value ((type (eql 'int8)) socket)
140 (defmethod read-socket-value ((type (eql 'string)) socket)
141 (with-output-to-string (out)
142 (loop for code = (read-byte socket)
144 do (write-char (code-char code) out))))
146 (defgeneric skip-socket-value (type socket))
148 (defmethod skip-socket-value ((type (eql 'int32)) socket)
149 (dotimes (i 4) (read-byte socket)))
151 (defmethod skip-socket-value ((type (eql 'int16)) socket)
152 (dotimes (i 2) (read-byte socket)))
154 (defmethod skip-socket-value ((type (eql 'int8)) socket)
157 (defmethod skip-socket-value ((type (eql 'string)) socket)
158 (loop until (zerop (read-byte socket))))
160 (defmacro define-message-sender (name (&rest args) &rest clauses)
161 (loop with socket-var = (gensym)
162 for (type value) in clauses
164 `(send-socket-value ',type ,socket-var ,value)
168 `(defun ,name (,socket-var ,@args)
171 (defun pad-limstring (string limit)
172 (let ((result (make-string limit :initial-element #\NULL)))
173 (loop for char across string
174 for index from 0 below limit
175 do (setf (char result index) char))
178 (define-message-sender send-startup-message
179 (database user &optional (command-line "") (backend-tty ""))
181 (int32 #x00020000) ; Version 2.0
182 (limstring (pad-limstring database 64))
183 (limstring (pad-limstring user 32))
184 (limstring (pad-limstring command-line 64))
185 (limstring (pad-limstring "" 64)) ; Unused
186 (limstring (pad-limstring backend-tty 64)))
188 (define-message-sender send-terminate-message ()
191 (define-message-sender send-unencrypted-password-message (password)
192 (int32 (+ 5 (length password)))
195 (define-message-sender send-query-message (query)
199 (define-message-sender send-encrypted-password-message (crypted-password)
200 (int32 (+ 5 (length crypted-password)))
201 (string crypted-password))
203 (define-message-sender send-cancel-request (pid key)
205 (int32 80877102) ; Magic
210 (defun read-socket-sequence (string stream)
211 "KMR -- Added to support reading from binary stream into a string"
212 (declare (optimize (speed 3) (safety 0))
214 (dotimes (i (length string))
216 (setf (char string i) (code-char (read-byte stream))))
220 ;;; Support for encrypted password transmission
222 (defvar *crypt-library-loaded* nil)
224 (defun crypt-password (password salt)
225 "Encrypt a password for transmission to a PostgreSQL server."
226 (unless *crypt-library-loaded*
227 (uffi:load-foreign-library
228 (find-foreign-library "libcrypt"
229 '("/usr/lib/" "/usr/local/lib/" "/lib/"))
230 :supporting-libaries '("c"))
231 (eval '(uffi:def-function "crypt"
234 :returning :cstring))
235 (setq *crypt-library-loaded* t))
236 (uffi:with-cstring (password-cstring password)
237 (uffi:with-cstring (salt-cstring salt)
238 (uffi:convert-from-cstring (crypt password-cstring salt-cstring)))))
239 ;;; Condition hierarchy
241 (define-condition postgresql-condition (condition)
242 ((connection :initarg :connection :reader postgresql-condition-connection)
243 (message :initarg :message :reader postgresql-condition-message))
246 (format stream "~@<~A occurred on connection ~A. ~:@_Reason: ~A~:@>"
248 (postgresql-condition-connection c)
249 (postgresql-condition-message c)))))
251 (define-condition postgresql-error (error postgresql-condition)
254 (define-condition postgresql-fatal-error (postgresql-error)
257 (define-condition postgresql-login-error (postgresql-fatal-error)
260 (define-condition postgresql-warning (warning postgresql-condition)
263 (define-condition postgresql-notification (postgresql-condition)
267 (format stream "~@<Asynchronous notification on connection ~A: ~:@_~A~:@>"
268 (postgresql-condition-connection c)
269 (postgresql-condition-message c)))))
273 (defstruct postgresql-connection
285 (defstruct postgresql-cursor
292 (defconstant +postgresql-server-default-port+ 5432
293 "Default port of PostgreSQL server.")
295 (defvar *postgresql-server-socket-timeout* 60
296 "Timeout in seconds for reads from the PostgreSQL server.")
300 (defun open-postgresql-socket (host port)
303 ;; Directory to unix-domain socket
304 (ext:connect-to-unix-socket
306 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
309 (ext:connect-to-inet-socket host port))))
312 (defun open-postgresql-socket-stream (host port)
313 (system:make-fd-stream
314 (open-postgresql-socket host port)
315 :input t :output t :element-type '(unsigned-byte 8)
317 :timeout *postgresql-server-socket-timeout*))
320 (defun open-postgresql-socket-stream (host port)
323 (let ((path (namestring
324 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
326 (socket:make-socket :type :stream :address-family :file
328 :remote-filename path :local-filename path)))
330 (socket:with-pending-connect
331 (mp:with-timeout (*postgresql-server-socket-timeout* (error "connect failed"))
332 (socket:make-socket :type :stream :address-family :internet
333 :remote-port port :remote-host host
334 :connect :active :nodelay t))))
338 (defun open-postgresql-socket-stream (host port)
341 (error "File sockets not supported on Lispworks."))
343 (comm:open-tcp-stream host port :direction :io :element-type '(unsigned-byte 8)
344 :read-timeout *postgresql-server-socket-timeout*))
347 ;;; Interface Functions
349 (defun open-postgresql-connection (&key (host (cmucl-compat:required-argument))
350 (port +postgresql-server-default-port+)
351 (database (cmucl-compat:required-argument))
352 (user (cmucl-compat:required-argument))
353 options tty password)
354 "Open a connection to a PostgreSQL server with the given parameters.
355 Note that host, database and user arguments must be supplied.
357 If host is a pathname, it is assumed to name a directory containing
358 the local unix-domain sockets of the server, with port selecting which
359 of those sockets to open. If host is a string, it is assumed to be
360 the name of the host running the PostgreSQL server. In that case a
361 TCP connection to the given port on that host is opened in order to
362 communicate with the server. In either case the port argument
363 defaults to `+postgresql-server-default-port+'.
365 Password is the clear-text password to be passed in the authentication
366 phase to the server. Depending on the server set-up, it is either
367 passed in the clear, or encrypted via crypt and a server-supplied
368 salt. In that case the alien function specified by `*crypt-library*'
369 and `*crypt-function-name*' is used for encryption.
371 Note that all the arguments (including the clear-text password
372 argument) are stored in the `postgresql-connection' structure, in
373 order to facilitate automatic reconnection in case of communication
375 (reopen-postgresql-connection
376 (make-postgresql-connection :host host :port port
377 :options (or options "") :tty (or tty "")
378 :database database :user user
379 :password (or password ""))))
381 (defun reopen-postgresql-connection (connection)
382 "Reopen the given PostgreSQL connection. Closes any existing
383 connection, if it is still open."
384 (when (postgresql-connection-open-p connection)
385 (close-postgresql-connection connection))
386 (let ((socket (open-postgresql-socket-stream
387 (postgresql-connection-host connection)
388 (postgresql-connection-port connection))))
391 (setf (postgresql-connection-socket connection) socket)
392 (send-startup-message socket
393 (postgresql-connection-database connection)
394 (postgresql-connection-user connection)
395 (postgresql-connection-options connection)
396 (postgresql-connection-tty connection))
397 (force-output socket)
399 (case (read-socket-value 'int8 socket)
400 (#.+authentication-message+
401 (case (read-socket-value 'int32 socket)
404 (error 'postgresql-login-error
405 :connection connection
407 "Postmaster expects unsupported Kerberos authentication."))
409 (send-unencrypted-password-message
411 (postgresql-connection-password connection)))
413 (let ((salt (make-string 2)))
414 (read-socket-sequence salt socket)
415 (send-encrypted-password-message
418 (postgresql-connection-password connection) salt))))
420 (error 'postgresql-login-error
421 :connection connection
423 "Postmaster expects unknown authentication method."))))
424 (#.+error-response-message+
425 (let ((message (read-socket-value 'string socket)))
426 (error 'postgresql-login-error
427 :connection connection :message message)))
429 (error 'postgresql-login-error
430 :connection connection
432 "Received garbled message from Postmaster"))))
433 ;; Start backend communication
434 (force-output socket)
436 (case (read-socket-value 'int8 socket)
437 (#.+backend-key-message+
438 (setf (postgresql-connection-pid connection)
439 (read-socket-value 'int32 socket)
440 (postgresql-connection-key connection)
441 (read-socket-value 'int32 socket)))
442 (#.+ready-for-query-message+
445 (#.+error-response-message+
446 (let ((message (read-socket-value 'string socket)))
447 (error 'postgresql-login-error
448 :connection connection
450 (#.+notice-response-message+
451 (let ((message (read-socket-value 'string socket)))
452 (warn 'postgresql-warning :connection connection
455 (error 'postgresql-login-error
456 :connection connection
458 "Received garbled message from Postmaster")))))
462 (defun close-postgresql-connection (connection &optional abort)
465 (send-terminate-message (postgresql-connection-socket connection))))
466 (close (postgresql-connection-socket connection)))
468 (defun postgresql-connection-open-p (connection)
469 (let ((socket (postgresql-connection-socket connection)))
470 (and socket (streamp socket) (open-stream-p socket))))
472 (defun ensure-open-postgresql-connection (connection)
473 (unless (postgresql-connection-open-p connection)
474 (reopen-postgresql-connection connection)))
476 (defun process-async-messages (connection)
477 (assert (postgresql-connection-open-p connection))
478 ;; Process any asnychronous messages
479 (loop with socket = (postgresql-connection-socket connection)
480 while (listen socket)
482 (case (read-socket-value 'int8 socket)
483 (#.+notice-response-message+
484 (let ((message (read-socket-value 'string socket)))
485 (warn 'postgresql-warning :connection connection
487 (#.+notification-response-message+
488 (let ((pid (read-socket-value 'int32 socket))
489 (message (read-socket-value 'string socket)))
490 (when (= pid (postgresql-connection-pid connection))
491 (signal 'postgresql-notification :connection connection
494 (close-postgresql-connection connection)
495 (error 'postgresql-fatal-error :connection connection
496 :message "Received garbled message from backend")))))
498 (defun start-query-execution (connection query)
499 (ensure-open-postgresql-connection connection)
500 (process-async-messages connection)
501 (send-query-message (postgresql-connection-socket connection) query)
502 (force-output (postgresql-connection-socket connection)))
504 (defun wait-for-query-results (connection)
505 (assert (postgresql-connection-open-p connection))
506 (let ((socket (postgresql-connection-socket connection))
510 (case (read-socket-value 'int8 socket)
511 (#.+completed-response-message+
512 (return (values :completed (read-socket-value 'string socket))))
513 (#.+cursor-response-message+
514 (setq cursor-name (read-socket-value 'string socket)))
515 (#.+row-description-message+
516 (let* ((count (read-socket-value 'int16 socket))
521 (read-socket-value 'string socket)
522 (read-socket-value 'int32 socket)
523 (read-socket-value 'int16 socket)
524 (read-socket-value 'int32 socket)))))
527 (make-postgresql-cursor :connection connection
530 (#.+copy-in-response-message+
532 (#.+copy-out-response-message+
534 (#.+ready-for-query-message+
538 (#.+error-response-message+
539 (let ((message (read-socket-value 'string socket)))
541 (make-condition 'postgresql-error
542 :connection connection :message message))))
543 (#.+notice-response-message+
544 (let ((message (read-socket-value 'string socket)))
545 (warn 'postgresql-warning
546 :connection connection :message message)))
547 (#.+notification-response-message+
548 (let ((pid (read-socket-value 'int32 socket))
549 (message (read-socket-value 'string socket)))
550 (when (= pid (postgresql-connection-pid connection))
551 (signal 'postgresql-notification :connection connection
554 (close-postgresql-connection connection)
555 (error 'postgresql-fatal-error :connection connection
556 :message "Received garbled message from backend"))))))
558 (defun read-null-bit-vector (socket count)
559 (let ((result (make-array count :element-type 'bit)))
560 (dotimes (offset (ceiling count 8))
561 (loop with byte = (read-byte socket)
562 for index from (* offset 8) below (min count (* (1+ offset) 8))
563 for weight downfrom 7
564 do (setf (aref result index) (ldb (byte 1 weight) byte))))
568 (defun read-field (socket type)
569 (let ((length (- (read-socket-value 'int32 socket) 4)))
572 (read-integer-from-socket socket length))
574 (read-double-from-socket socket length))
576 (let ((result (make-string length)))
577 (read-socket-sequence result socket)
580 (uffi:def-constant +char-code-zero+ (char-code #\0))
581 (uffi:def-constant +char-code-minus+ (char-code #\-))
582 (uffi:def-constant +char-code-plus+ (char-code #\+))
583 (uffi:def-constant +char-code-period+ (char-code #\.))
584 (uffi:def-constant +char-code-lower-e+ (char-code #\e))
585 (uffi:def-constant +char-code-upper-e+ (char-code #\E))
587 (defun read-integer-from-socket (socket length)
588 (declare (fixnum length))
592 (first-char (read-byte socket))
594 (declare (fixnum first-char))
595 (decf length) ;; read first char
597 ((= first-char +char-code-minus+)
599 ((= first-char +char-code-plus+)
602 (setq val (- first-char +char-code-zero+))))
608 (- (read-byte socket) +char-code-zero+))))
613 (defmacro ascii-digit (int)
614 (let ((offset (gensym)))
615 `(let ((,offset (- ,int +char-code-zero+)))
616 (declare (fixnum ,int ,offset))
617 (if (and (>= ,offset 0)
622 (defun read-double-from-socket (socket length)
623 (declare (fixnum length))
624 (let ((before-decimal 0)
631 (char (read-byte socket)))
632 (declare (fixnum char exponent decimal-count))
633 (decf length) ;; already read first character
635 ((= char +char-code-minus+)
637 ((= char +char-code-plus+)
639 ((= char +char-code-period+)
642 (setq before-decimal (ascii-digit char))
643 (unless before-decimal
644 (error "Unexpected value"))))
648 (setq char (read-byte socket))
649 ;; (format t "~&len:~D, i:~D, char:~D, minusp:~A, decimalp:~A" length i char minusp decimalp)
650 (let ((weight (ascii-digit char)))
652 ((and weight (not decimalp)) ;; before decimal point
653 (setq before-decimal (+ weight (* 10 before-decimal))))
654 ((and weight decimalp) ;; after decimal point
655 (setq after-decimal (+ weight (* 10 after-decimal)))
656 (incf decimal-count))
657 ((and (= char +char-code-period+))
659 ((or (= char +char-code-lower-e+) ;; E is for exponent
660 (= char +char-code-upper-e+))
661 (setq exponent (read-integer-from-socket socket (- length i 1)))
662 (setq exponent (or exponent 0))
665 (break "Unexpected value"))
668 (setq result (* (+ (coerce before-decimal 'double-float)
670 (expt 10 (- decimal-count))))
678 (defun read-double-from-socket (socket length)
679 (let ((result (make-string length)))
680 (read-socket-sequence result socket)
681 (let ((*read-default-float-format* 'double-float))
682 (read-from-string result))))
684 (defun read-cursor-row (cursor types)
685 (let* ((connection (postgresql-cursor-connection cursor))
686 (socket (postgresql-connection-socket connection))
687 (fields (postgresql-cursor-fields cursor)))
688 (assert (postgresql-connection-open-p connection))
690 (let ((code (read-socket-value 'int8 socket)))
692 (#.+ascii-row-message+
694 (loop with count = (length fields)
695 with null-vector = (read-null-bit-vector socket count)
697 for null-bit across null-vector
699 for null-p = (zerop null-bit)
704 (read-field socket (nth i types)))))
705 (#.+binary-row-message+
707 (#.+completed-response-message+
708 (return (values nil (read-socket-value 'string socket))))
709 (#.+error-response-message+
710 (let ((message (read-socket-value 'string socket)))
711 (error 'postgresql-error
712 :connection connection :message message)))
713 (#.+notice-response-message+
714 (let ((message (read-socket-value 'string socket)))
715 (warn 'postgresql-warning
716 :connection connection :message message)))
717 (#.+notification-response-message+
718 (let ((pid (read-socket-value 'int32 socket))
719 (message (read-socket-value 'string socket)))
720 (when (= pid (postgresql-connection-pid connection))
721 (signal 'postgresql-notification :connection connection
724 (close-postgresql-connection connection)
725 (error 'postgresql-fatal-error :connection connection
726 :message "Received garbled message from backend")))))))
728 (defun map-into-indexed (result-seq func seq)
729 (dotimes (i (length seq))
731 (setf (elt result-seq i)
732 (funcall func (elt seq i) i)))
735 (defun copy-cursor-row (cursor sequence types)
736 (let* ((connection (postgresql-cursor-connection cursor))
737 (socket (postgresql-connection-socket connection))
738 (fields (postgresql-cursor-fields cursor)))
739 (assert (= (length fields) (length sequence)))
741 (let ((code (read-socket-value 'int8 socket)))
743 (#.+ascii-row-message+
746 (let* ((count (length sequence))
747 (null-vector (read-null-bit-vector socket count)))
750 (if (zerop (elt null-vector i))
751 (setf (elt sequence i) nil)
752 (let ((value (read-field socket (nth i types))))
753 (setf (elt sequence i) value)))))
756 #'(lambda (null-bit i)
759 (read-field socket (nth i types))))
760 (read-null-bit-vector socket (length sequence)))))
761 (#.+binary-row-message+
763 (#.+completed-response-message+
764 (return (values nil (read-socket-value 'string socket))))
765 (#.+error-response-message+
766 (let ((message (read-socket-value 'string socket)))
767 (error 'postgresql-error
768 :connection connection :message message)))
769 (#.+notice-response-message+
770 (let ((message (read-socket-value 'string socket)))
771 (warn 'postgresql-warning
772 :connection connection :message message)))
773 (#.+notification-response-message+
774 (let ((pid (read-socket-value 'int32 socket))
775 (message (read-socket-value 'string socket)))
776 (when (= pid (postgresql-connection-pid connection))
777 (signal 'postgresql-notification :connection connection
780 (close-postgresql-connection connection)
781 (error 'postgresql-fatal-error :connection connection
782 :message "Received garbled message from backend")))))))
784 (defun skip-cursor-row (cursor)
785 (let* ((connection (postgresql-cursor-connection cursor))
786 (socket (postgresql-connection-socket connection))
787 (fields (postgresql-cursor-fields cursor)))
789 (let ((code (read-socket-value 'int8 socket)))
791 (#.+ascii-row-message+
792 (loop for null-bit across
793 (read-null-bit-vector socket (length fields))
795 (unless (zerop null-bit)
796 (let* ((length (read-socket-value 'int32 socket)))
797 (loop repeat (- length 4) do (read-byte socket)))))
799 (#.+binary-row-message+
801 (#.+completed-response-message+
802 (return (values nil (read-socket-value 'string socket))))
803 (#.+error-response-message+
804 (let ((message (read-socket-value 'string socket)))
805 (error 'postgresql-error
806 :connection connection :message message)))
807 (#.+notice-response-message+
808 (let ((message (read-socket-value 'string socket)))
809 (warn 'postgresql-warning
810 :connection connection :message message)))
811 (#.+notification-response-message+
812 (let ((pid (read-socket-value 'int32 socket))
813 (message (read-socket-value 'string socket)))
814 (when (= pid (postgresql-connection-pid connection))
815 (signal 'postgresql-notification :connection connection
818 (close-postgresql-connection connection)
819 (error 'postgresql-fatal-error :connection connection
820 :message "Received garbled message from backend")))))))
822 (defun run-query (connection query &optional (types nil))
823 (start-query-execution connection query)
824 (multiple-value-bind (status cursor)
825 (wait-for-query-results connection)
826 (assert (eq status :cursor))
827 (loop for row = (read-cursor-row cursor types)
831 (wait-for-query-results connection))))