1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: postgresql-socket-api.lisp
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
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 clsql-base-sys:database-type-library-loaded ((database-type
44 (eql :postgresql-socket)))
45 "T if foreign library was able to be loaded successfully. Always true for
49 (defmethod clsql-base-sys:database-type-load-foreign ((database-type (eql :postgresql-socket)))
55 (defmacro define-message-constants (description &rest clauses)
56 (assert (evenp (length clauses)))
57 (loop with seen-characters = nil
58 for (name char) on clauses by #'cddr
59 for char-code = (char-code char)
60 for doc-string = (format nil "~A (~:C): ~A" description char name)
61 if (member char seen-characters)
62 do (error "Duplicate message type ~@C for group ~A" char description)
65 `(defconstant ,name ,char-code ,doc-string)
67 and do (push char seen-characters)
69 (return `(progn ,@result-clauses))))
71 (eval-when (:compile-toplevel :load-toplevel :execute)
72 (define-message-constants "Backend Message Constants"
73 +ascii-row-message+ #\D
74 +authentication-message+ #\R
75 +backend-key-message+ #\K
76 +binary-row-message+ #\B
77 +completed-response-message+ #\C
78 +copy-in-response-message+ #\G
79 +copy-out-response-message+ #\H
80 +cursor-response-message+ #\P
81 +empty-query-response-message+ #\I
82 +error-response-message+ #\E
83 +function-response-message+ #\V
84 +notice-response-message+ #\N
85 +notification-response-message+ #\A
86 +ready-for-query-message+ #\Z
87 +row-description-message+ #\T))
90 (declaim (inline read-byte write-byte))
92 (defun send-socket-value-int32 (socket value)
93 (declare (type stream socket)
94 (type (unsigned-byte 32) value))
95 (write-byte (ldb (byte 8 24) value) socket)
96 (write-byte (ldb (byte 8 16) value) socket)
97 (write-byte (ldb (byte 8 8) value) socket)
98 (write-byte (ldb (byte 8 0) value) socket)
101 (defun send-socket-value-int16 (socket value)
102 (declare (type stream socket)
103 (type (unsigned-byte 16) value))
104 (write-byte (ldb (byte 8 8) value) socket)
105 (write-byte (ldb (byte 8 0) value) socket)
108 (defun send-socket-value-int8 (socket value)
109 (declare (type stream socket)
110 (type (unsigned-byte 8) value))
111 (write-byte (ldb (byte 8 0) value) socket)
114 (defun send-socket-value-char-code (socket value)
115 (declare (type stream socket)
116 (type character value))
117 (write-byte (ldb (byte 8 0) (char-code value)) socket)
120 (defun send-socket-value-string (socket value)
121 (declare (type stream socket)
123 (loop for char across value
124 for code = (char-code char)
125 do (write-byte code socket)
126 finally (write-byte 0 socket))
129 (defun send-socket-value-limstring (socket value limit)
130 (declare (type stream socket)
133 (let ((length (length value)))
134 (dotimes (i (min length limit))
135 (let ((code (char-code (char value i))))
136 (write-byte code socket)))
137 (dotimes (i (- limit length))
138 (write-byte 0 socket)))
142 (defun read-socket-value-int32 (socket)
143 (declare (type stream socket))
144 (declare (optimize (speed 3)))
146 (declare (type (unsigned-byte 32) result))
147 (setf (ldb (byte 8 24) result) (read-byte socket))
148 (setf (ldb (byte 8 16) result) (read-byte socket))
149 (setf (ldb (byte 8 8) result) (read-byte socket))
150 (setf (ldb (byte 8 0) result) (read-byte socket))
153 (defun read-socket-value-int16 (socket)
154 (declare (type stream socket))
156 (declare (type (unsigned-byte 16) result))
157 (setf (ldb (byte 8 8) result) (read-byte socket))
158 (setf (ldb (byte 8 0) result) (read-byte socket))
161 (defun read-socket-value-int8 (socket)
162 (declare (type stream socket))
165 (defun read-socket-value-string (socket)
166 (declare (type stream socket))
167 (with-output-to-string (out)
168 (loop for code = (read-byte socket)
170 do (write-char (code-char code) out))))
173 (defmacro define-message-sender (name (&rest args) &rest clauses)
174 (let ((socket-var (gensym))
176 (dolist (clause clauses)
177 (let* ((type (first clause))
178 (fn (intern (concatenate 'string (symbol-name '#:send-socket-value-)
179 (symbol-name type)))))
180 (push `(,fn ,socket-var ,@(rest clause)) body)))
181 `(defun ,name (,socket-var ,@args)
184 (define-message-sender send-startup-message
185 (database user &optional (command-line "") (backend-tty ""))
187 (int32 #x00020000) ; Version 2.0
188 (limstring database 64)
190 (limstring command-line 64)
191 (limstring "" 64) ; Unused
192 (limstring backend-tty 64))
194 (define-message-sender send-terminate-message ()
197 (define-message-sender send-unencrypted-password-message (password)
198 (int32 (+ 5 (length password)))
201 (define-message-sender send-query-message (query)
205 (define-message-sender send-encrypted-password-message (crypted-password)
206 (int32 (+ 5 (length crypted-password)))
207 (string crypted-password))
209 (define-message-sender send-cancel-request (pid key)
211 (int32 80877102) ; Magic
216 (defun read-socket-sequence (string stream)
217 "KMR -- Added to support reading from binary stream into a string"
218 (declare (string string)
220 (optimize (speed 3) (safety 0)))
221 (dotimes (i (length string))
223 (setf (char string i) (code-char (read-byte stream))))
227 ;;; Support for encrypted password transmission
230 (eval-when (compile eval load)
231 (defvar *crypt-library-loaded* nil)
233 (unless *crypt-library-loaded*
234 (uffi:load-foreign-library
235 (uffi:find-foreign-library "libcrypt"
236 '("/usr/lib/" "/usr/local/lib/" "/lib/"))
237 :supporting-libraries '("c"))
238 (setq *crypt-library-loaded* t)))
240 (in-package :postgresql-socket)
242 (uffi:def-function "crypt"
247 (defun crypt-password (password salt)
248 "Encrypt a password for transmission to a PostgreSQL server."
249 (uffi:with-cstring (password-cstring password)
250 (uffi:with-cstring (salt-cstring salt)
251 (uffi:convert-from-cstring
252 (crypt password-cstring salt-cstring)))))
255 ;;;; Condition hierarchy
257 (define-condition postgresql-condition (condition)
258 ((connection :initarg :connection :reader postgresql-condition-connection)
259 (message :initarg :message :reader postgresql-condition-message))
262 (format stream "~@<~A occurred on connection ~A. ~:@_Reason: ~A~:@>"
264 (postgresql-condition-connection c)
265 (postgresql-condition-message c)))))
267 (define-condition postgresql-error (error postgresql-condition)
270 (define-condition postgresql-fatal-error (postgresql-error)
273 (define-condition postgresql-login-error (postgresql-fatal-error)
276 (define-condition postgresql-warning (warning postgresql-condition)
279 (define-condition postgresql-notification (postgresql-condition)
283 (format stream "~@<Asynchronous notification on connection ~A: ~:@_~A~:@>"
284 (postgresql-condition-connection c)
285 (postgresql-condition-message c)))))
289 (defstruct postgresql-connection
301 (defstruct postgresql-cursor
308 (defconstant +postgresql-server-default-port+ 5432
309 "Default port of PostgreSQL server.")
311 (defvar *postgresql-server-socket-timeout* 60
312 "Timeout in seconds for reads from the PostgreSQL server.")
316 (defun open-postgresql-socket (host port)
319 ;; Directory to unix-domain socket
320 (ext:connect-to-unix-socket
322 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
325 (ext:connect-to-inet-socket host port))))
328 (defun open-postgresql-socket-stream (host port)
329 (system:make-fd-stream
330 (open-postgresql-socket host port)
331 :input t :output t :element-type '(unsigned-byte 8)
333 :timeout *postgresql-server-socket-timeout*))
336 (defun open-postgresql-socket-stream (host port)
339 (let ((path (namestring
340 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
342 (socket:make-socket :type :stream :address-family :file
344 :remote-filename path :local-filename path)))
346 (socket:with-pending-connect
347 (mp:with-timeout (*postgresql-server-socket-timeout* (error "connect failed"))
348 (socket:make-socket :type :stream :address-family :internet
349 :remote-port port :remote-host host
350 :connect :active :nodelay t))))
354 (defun open-postgresql-socket-stream (host port)
357 (error "File sockets not supported on Lispworks."))
359 (comm:open-tcp-stream host port :direction :io :element-type '(unsigned-byte 8)
360 :read-timeout *postgresql-server-socket-timeout*))
363 ;;; Interface Functions
365 (defun open-postgresql-connection (&key (host (cmucl-compat:required-argument))
366 (port +postgresql-server-default-port+)
367 (database (cmucl-compat:required-argument))
368 (user (cmucl-compat:required-argument))
369 options tty password)
370 "Open a connection to a PostgreSQL server with the given parameters.
371 Note that host, database and user arguments must be supplied.
373 If host is a pathname, it is assumed to name a directory containing
374 the local unix-domain sockets of the server, with port selecting which
375 of those sockets to open. If host is a string, it is assumed to be
376 the name of the host running the PostgreSQL server. In that case a
377 TCP connection to the given port on that host is opened in order to
378 communicate with the server. In either case the port argument
379 defaults to `+postgresql-server-default-port+'.
381 Password is the clear-text password to be passed in the authentication
382 phase to the server. Depending on the server set-up, it is either
383 passed in the clear, or encrypted via crypt and a server-supplied
384 salt. In that case the alien function specified by `*crypt-library*'
385 and `*crypt-function-name*' is used for encryption.
387 Note that all the arguments (including the clear-text password
388 argument) are stored in the `postgresql-connection' structure, in
389 order to facilitate automatic reconnection in case of communication
391 (reopen-postgresql-connection
392 (make-postgresql-connection :host host :port port
393 :options (or options "") :tty (or tty "")
394 :database database :user user
395 :password (or password ""))))
397 (defun encrypt-md5 (plaintext salt)
399 (format nil "~{~2,'0X~}"
400 (coerce (md5:md5sum-sequence (concatenate 'string plaintext salt)) 'list))))
402 (defun reopen-postgresql-connection (connection)
403 "Reopen the given PostgreSQL connection. Closes any existing
404 connection, if it is still open."
405 (when (postgresql-connection-open-p connection)
406 (close-postgresql-connection connection))
407 (let ((socket (open-postgresql-socket-stream
408 (postgresql-connection-host connection)
409 (postgresql-connection-port connection))))
412 (setf (postgresql-connection-socket connection) socket)
413 (send-startup-message socket
414 (postgresql-connection-database connection)
415 (postgresql-connection-user connection)
416 (postgresql-connection-options connection)
417 (postgresql-connection-tty connection))
418 (force-output socket)
420 (case (read-socket-value-int8 socket)
421 (#.+authentication-message+
422 (case (read-socket-value-int32 socket)
425 (error 'postgresql-login-error
426 :connection connection
428 "Postmaster expects unsupported Kerberos authentication."))
430 (send-unencrypted-password-message
432 (postgresql-connection-password connection)))
434 (let ((salt (make-string 2)))
435 (read-socket-sequence salt socket)
436 (send-encrypted-password-message
439 (postgresql-connection-password connection) salt))))
441 (let ((salt (make-string 4)))
442 (read-socket-sequence salt socket)
443 (let* ((pwd2 (encrypt-md5 (postgresql-connection-password connection)
444 (postgresql-connection-user connection)))
445 (pwd (encrypt-md5 pwd2 salt)))
446 (send-encrypted-password-message
448 (concatenate 'string "md5" pwd)))))
450 (error 'postgresql-login-error
451 :connection connection
453 "Postmaster expects unknown authentication method."))))
454 (#.+error-response-message+
455 (let ((message (read-socket-value-string socket)))
456 (error 'postgresql-login-error
457 :connection connection :message message)))
459 (error 'postgresql-login-error
460 :connection connection
462 "Received garbled message from Postmaster"))))
463 ;; Start backend communication
464 (force-output socket)
466 (case (read-socket-value-int8 socket)
467 (#.+backend-key-message+
468 (setf (postgresql-connection-pid connection)
469 (read-socket-value-int32 socket)
470 (postgresql-connection-key connection)
471 (read-socket-value-int32 socket)))
472 (#.+ready-for-query-message+
475 (#.+error-response-message+
476 (let ((message (read-socket-value-string socket)))
477 (error 'postgresql-login-error
478 :connection connection
480 (#.+notice-response-message+
481 (let ((message (read-socket-value-string socket)))
482 (warn 'postgresql-warning :connection connection
485 (error 'postgresql-login-error
486 :connection connection
488 "Received garbled message from Postmaster")))))
492 (defun close-postgresql-connection (connection &optional abort)
495 (send-terminate-message (postgresql-connection-socket connection))))
496 (close (postgresql-connection-socket connection)))
498 (defun postgresql-connection-open-p (connection)
499 (let ((socket (postgresql-connection-socket connection)))
500 (and socket (streamp socket) (open-stream-p socket))))
502 (defun ensure-open-postgresql-connection (connection)
503 (unless (postgresql-connection-open-p connection)
504 (reopen-postgresql-connection connection)))
506 (defun process-async-messages (connection)
507 (assert (postgresql-connection-open-p connection))
508 ;; Process any asnychronous messages
509 (loop with socket = (postgresql-connection-socket connection)
510 while (listen socket)
512 (case (read-socket-value-int8 socket)
513 (#.+notice-response-message+
514 (let ((message (read-socket-value-string socket)))
515 (warn 'postgresql-warning :connection connection
517 (#.+notification-response-message+
518 (let ((pid (read-socket-value-int32 socket))
519 (message (read-socket-value-string socket)))
520 (when (= pid (postgresql-connection-pid connection))
521 (signal 'postgresql-notification :connection connection
524 (close-postgresql-connection connection)
525 (error 'postgresql-fatal-error :connection connection
526 :message "Received garbled message from backend")))))
528 (defun start-query-execution (connection query)
529 (ensure-open-postgresql-connection connection)
530 (process-async-messages connection)
531 (send-query-message (postgresql-connection-socket connection) query)
532 (force-output (postgresql-connection-socket connection)))
534 (defun wait-for-query-results (connection)
535 (assert (postgresql-connection-open-p connection))
536 (let ((socket (postgresql-connection-socket connection))
540 (case (read-socket-value-int8 socket)
541 (#.+completed-response-message+
542 (return (values :completed (read-socket-value-string socket))))
543 (#.+cursor-response-message+
544 (setq cursor-name (read-socket-value-string socket)))
545 (#.+row-description-message+
546 (let* ((count (read-socket-value-int16 socket))
551 (read-socket-value-string socket)
552 (read-socket-value-int32 socket)
553 (read-socket-value-int16 socket)
554 (read-socket-value-int32 socket)))))
557 (make-postgresql-cursor :connection connection
560 (#.+copy-in-response-message+
562 (#.+copy-out-response-message+
564 (#.+ready-for-query-message+
568 (#.+error-response-message+
569 (let ((message (read-socket-value-string socket)))
571 (make-condition 'postgresql-error
572 :connection connection :message message))))
573 (#.+notice-response-message+
574 (let ((message (read-socket-value-string socket)))
575 (warn 'postgresql-warning
576 :connection connection :message message)))
577 (#.+notification-response-message+
578 (let ((pid (read-socket-value-int32 socket))
579 (message (read-socket-value-string socket)))
580 (when (= pid (postgresql-connection-pid connection))
581 (signal 'postgresql-notification :connection connection
584 (close-postgresql-connection connection)
585 (error 'postgresql-fatal-error :connection connection
586 :message "Received garbled message from backend"))))))
588 (defun read-null-bit-vector (socket count)
589 (let ((result (make-array count :element-type 'bit)))
590 (dotimes (offset (ceiling count 8))
591 (loop with byte = (read-byte socket)
592 for index from (* offset 8) below (min count (* (1+ offset) 8))
593 for weight downfrom 7
594 do (setf (aref result index) (ldb (byte 1 weight) byte))))
598 (defun read-field (socket type)
599 (let ((length (- (read-socket-value-int32 socket) 4)))
602 (read-integer-from-socket socket length))
604 (read-double-from-socket socket length))
606 (let ((result (make-string length)))
607 (read-socket-sequence result socket)
610 (uffi:def-constant +char-code-zero+ (char-code #\0))
611 (uffi:def-constant +char-code-minus+ (char-code #\-))
612 (uffi:def-constant +char-code-plus+ (char-code #\+))
613 (uffi:def-constant +char-code-period+ (char-code #\.))
614 (uffi:def-constant +char-code-lower-e+ (char-code #\e))
615 (uffi:def-constant +char-code-upper-e+ (char-code #\E))
617 (defun read-integer-from-socket (socket length)
618 (declare (fixnum length))
622 (first-char (read-byte socket))
624 (declare (fixnum first-char))
625 (decf length) ;; read first char
627 ((= first-char +char-code-minus+)
629 ((= first-char +char-code-plus+)
632 (setq val (- first-char +char-code-zero+))))
638 (- (read-byte socket) +char-code-zero+))))
643 (defmacro ascii-digit (int)
644 (let ((offset (gensym)))
645 `(let ((,offset (- ,int +char-code-zero+)))
646 (declare (fixnum ,int ,offset))
647 (if (and (>= ,offset 0)
652 (defun read-double-from-socket (socket length)
653 (declare (fixnum length))
654 (let ((before-decimal 0)
661 (char (read-byte socket)))
662 (declare (fixnum char exponent decimal-count))
663 (decf length) ;; already read first character
665 ((= char +char-code-minus+)
667 ((= char +char-code-plus+)
669 ((= char +char-code-period+)
672 (setq before-decimal (ascii-digit char))
673 (unless before-decimal
674 (error "Unexpected value"))))
678 (setq char (read-byte socket))
679 ;; (format t "~&len:~D, i:~D, char:~D, minusp:~A, decimalp:~A" length i char minusp decimalp)
680 (let ((weight (ascii-digit char)))
682 ((and weight (not decimalp)) ;; before decimal point
683 (setq before-decimal (+ weight (* 10 before-decimal))))
684 ((and weight decimalp) ;; after decimal point
685 (setq after-decimal (+ weight (* 10 after-decimal)))
686 (incf decimal-count))
687 ((and (= char +char-code-period+))
689 ((or (= char +char-code-lower-e+) ;; E is for exponent
690 (= char +char-code-upper-e+))
691 (setq exponent (read-integer-from-socket socket (- length i 1)))
692 (setq exponent (or exponent 0))
695 (break "Unexpected value"))
698 (setq result (* (+ (coerce before-decimal 'double-float)
700 (expt 10 (- decimal-count))))
708 (defun read-double-from-socket (socket length)
709 (let ((result (make-string length)))
710 (read-socket-sequence result socket)
711 (let ((*read-default-float-format* 'double-float))
712 (read-from-string result))))
714 (defun read-cursor-row (cursor types)
715 (let* ((connection (postgresql-cursor-connection cursor))
716 (socket (postgresql-connection-socket connection))
717 (fields (postgresql-cursor-fields cursor)))
718 (assert (postgresql-connection-open-p connection))
720 (let ((code (read-socket-value-int8 socket)))
722 (#.+ascii-row-message+
724 (loop with count = (length fields)
725 with null-vector = (read-null-bit-vector socket count)
727 for null-bit across null-vector
729 for null-p = (zerop null-bit)
734 (read-field socket (nth i types)))))
735 (#.+binary-row-message+
737 (#.+completed-response-message+
738 (return (values nil (read-socket-value-string socket))))
739 (#.+error-response-message+
740 (let ((message (read-socket-value-string socket)))
741 (error 'postgresql-error
742 :connection connection :message message)))
743 (#.+notice-response-message+
744 (let ((message (read-socket-value-string socket)))
745 (warn 'postgresql-warning
746 :connection connection :message message)))
747 (#.+notification-response-message+
748 (let ((pid (read-socket-value-int32 socket))
749 (message (read-socket-value-string socket)))
750 (when (= pid (postgresql-connection-pid connection))
751 (signal 'postgresql-notification :connection connection
754 (close-postgresql-connection connection)
755 (error 'postgresql-fatal-error :connection connection
756 :message "Received garbled message from backend")))))))
758 (defun map-into-indexed (result-seq func seq)
759 (dotimes (i (length seq))
761 (setf (elt result-seq i)
762 (funcall func (elt seq i) i)))
765 (defun copy-cursor-row (cursor sequence types)
766 (let* ((connection (postgresql-cursor-connection cursor))
767 (socket (postgresql-connection-socket connection))
768 (fields (postgresql-cursor-fields cursor)))
769 (assert (= (length fields) (length sequence)))
771 (let ((code (read-socket-value-int8 socket)))
773 (#.+ascii-row-message+
776 (let* ((count (length sequence))
777 (null-vector (read-null-bit-vector socket count)))
780 (if (zerop (elt null-vector i))
781 (setf (elt sequence i) nil)
782 (let ((value (read-field socket (nth i types))))
783 (setf (elt sequence i) value)))))
786 #'(lambda (null-bit i)
789 (read-field socket (nth i types))))
790 (read-null-bit-vector socket (length sequence)))))
791 (#.+binary-row-message+
793 (#.+completed-response-message+
794 (return (values nil (read-socket-value-string socket))))
795 (#.+error-response-message+
796 (let ((message (read-socket-value-string socket)))
797 (error 'postgresql-error
798 :connection connection :message message)))
799 (#.+notice-response-message+
800 (let ((message (read-socket-value-string socket)))
801 (warn 'postgresql-warning
802 :connection connection :message message)))
803 (#.+notification-response-message+
804 (let ((pid (read-socket-value-int32 socket))
805 (message (read-socket-value-string socket)))
806 (when (= pid (postgresql-connection-pid connection))
807 (signal 'postgresql-notification :connection connection
810 (close-postgresql-connection connection)
811 (error 'postgresql-fatal-error :connection connection
812 :message "Received garbled message from backend")))))))
814 (defun skip-cursor-row (cursor)
815 (let* ((connection (postgresql-cursor-connection cursor))
816 (socket (postgresql-connection-socket connection))
817 (fields (postgresql-cursor-fields cursor)))
819 (let ((code (read-socket-value-int8 socket)))
821 (#.+ascii-row-message+
822 (loop for null-bit across
823 (read-null-bit-vector socket (length fields))
825 (unless (zerop null-bit)
826 (let* ((length (read-socket-value-int32 socket)))
827 (loop repeat (- length 4) do (read-byte socket)))))
829 (#.+binary-row-message+
831 (#.+completed-response-message+
832 (return (values nil (read-socket-value-string socket))))
833 (#.+error-response-message+
834 (let ((message (read-socket-value-string socket)))
835 (error 'postgresql-error
836 :connection connection :message message)))
837 (#.+notice-response-message+
838 (let ((message (read-socket-value-string socket)))
839 (warn 'postgresql-warning
840 :connection connection :message message)))
841 (#.+notification-response-message+
842 (let ((pid (read-socket-value-int32 socket))
843 (message (read-socket-value-string socket)))
844 (when (= pid (postgresql-connection-pid connection))
845 (signal 'postgresql-notification :connection connection
848 (close-postgresql-connection connection)
849 (error 'postgresql-fatal-error :connection connection
850 :message "Received garbled message from backend")))))))
852 (defun run-query (connection query &optional (types nil))
853 (start-query-execution connection query)
854 (multiple-value-bind (status cursor)
855 (wait-for-query-results connection)
856 (assert (eq status :cursor))
857 (loop for row = (read-cursor-row cursor types)
861 (wait-for-query-results connection))))
864 (declaim (ext:maybe-inline read-byte write-byte))