r11060: fix loop error on clisp. RESULT-SET should be T at start of loop to avoid...
[clsql.git] / db-postgresql-socket / postgresql-socket-api.lisp
index dabaad923b8bbd59886924ef7ceba182d97fffdf..e9d23f3fb33617a375ea7e0d7965ebc7a6a8a55a 100644 (file)
@@ -4,7 +4,7 @@
 ;;;;
 ;;;; Name:     postgresql-socket-api.lisp
 ;;;; Purpose:  Low-level PostgreSQL interface using sockets
-;;;; Authors:  Kevin M. Rosenberg based on original code by Pierre R. Mai 
+;;;; Authors:  Kevin M. Rosenberg based on original code by Pierre R. Mai
 ;;;; Created:  Feb 2002
 ;;;;
 ;;;; $Id$
@@ -107,10 +107,13 @@ socket interface"
 (defun send-socket-value-string (socket value)
   (declare (type stream socket)
           (type string value))
+  #-sb-unicode
   (loop for char across value
-       for code = (char-code char)
-       do (write-byte code socket)
-       finally (write-byte 0 socket))
+        for code = (char-code char)
+        do (write-byte code socket)
+        finally (write-byte 0 socket))
+  #+sb-unicode
+  (write-sequence (sb-ext:string-to-octets value :null-terminate t) socket)
   nil)
 
 (defun send-socket-value-limstring (socket value limit)
@@ -151,10 +154,20 @@ socket interface"
 
 (defun read-socket-value-string (socket)
   (declare (type stream socket))
+  #-sb-unicode
   (with-output-to-string (out)
     (loop for code = (read-byte socket)
-         until (zerop code)
-         do (write-char (code-char code) out))))
+          until (zerop code)
+          do (write-char (code-char code) out)))
+  #+sb-unicode
+  (let ((bytes (make-array 64
+                           :element-type '(unsigned-byte 8)
+                           :adjustable t
+                           :fill-pointer 0)))
+    (loop for code = (read-byte socket)
+          until (zerop code)
+          do (vector-push-extend code bytes))
+    (sb-ext:octets-to-string bytes)))
 
 
 (defmacro define-message-sender (name (&rest args) &rest clauses)
@@ -200,27 +213,32 @@ socket interface"
   (int32 key))
 
 
-(defun read-socket-sequence (string stream)
+(defun read-socket-sequence (stream length)
   "KMR -- Added to support reading from binary stream into a string"
-  (declare (string string)
-          (stream stream)
+  (declare (stream stream)
           (optimize (speed 3) (safety 0)))
-  (dotimes (i (length string))
-    (declare (fixnum i))
-    (setf (char string i) (code-char (read-byte stream))))
-  string)
+  #-sb-unicode
+  (let ((result (make-string length)))
+    (dotimes (i length result)
+      (declare (fixnum i))
+      (setf (char result i) (code-char (read-byte stream)))))
+  #+sb-unicode
+  (let ((bytes (make-array length :element-type '(unsigned-byte 8))))
+    (declare (type (simple-array (unsigned-byte 8) (*)) bytes))
+    (read-sequence bytes stream)
+    (sb-ext:octets-to-string bytes)))
 
 
 ;;; Support for encrypted password transmission
 
 #-scl
-(eval-when (compile eval load)
+(eval-when (:compile-toplevel :load-toplevel :execute)
   (defvar *crypt-library-loaded* nil)
 
   (unless *crypt-library-loaded*
-    (uffi:load-foreign-library 
+    (uffi:load-foreign-library
      (uffi:find-foreign-library "libcrypt"
-                          '(#+64bit "/usr/lib64/"
+                          '(#+(or 64bit x86-64) "/usr/lib64/"
                             "/usr/lib/" "/usr/local/lib/" "/lib/"))
      :supporting-libraries '("c"))
     (setq *crypt-library-loaded* t)))
@@ -236,7 +254,7 @@ socket interface"
   "Encrypt a password for transmission to a PostgreSQL server."
   (uffi:with-cstring (password-cstring password)
     (uffi:with-cstring (salt-cstring salt)
-      (uffi:convert-from-cstring 
+      (uffi:convert-from-cstring
        (crypt password-cstring salt-cstring)))))
 
 \f
@@ -316,18 +334,22 @@ socket interface"
   (etypecase host
     (pathname
      ;; Directory to unix-domain socket
-     (sb-bsd-sockets:socket-connect
-      (namestring
-       (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
-                     :defaults host))))
+     (let ((sock (make-instance 'sb-bsd-sockets:local-socket
+                               :type :stream)))
+       (sb-bsd-sockets:socket-connect
+        sock
+        (namestring
+         (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
+                        :defaults host)))
+       sock))
     (string
      (let ((sock (make-instance 'sb-bsd-sockets:inet-socket
                                :type :stream
                                :protocol :tcp)))
-       (sb-bsd-sockets:socket-connect 
-       sock 
+       (sb-bsd-sockets:socket-connect
+       sock
        (sb-bsd-sockets:host-ent-address
-        (sb-bsd-sockets:get-host-by-name host)) 
+        (sb-bsd-sockets:get-host-by-name host))
        port)
        sock))))
 
@@ -343,9 +365,9 @@ socket interface"
 #+sbcl
 (defun open-postgresql-socket-stream (host port)
   (sb-bsd-sockets:socket-make-stream
-   (open-postgresql-socket host port) :input t :output t 
+   (open-postgresql-socket host port) :input t :output t
    :element-type '(unsigned-byte 8)))
-  
+
 
 #+allegro
 (defun open-postgresql-socket-stream (host port)
@@ -389,6 +411,19 @@ socket interface"
                           :read-timeout *postgresql-server-socket-timeout*))
     ))
 
+
+#+clisp
+(defun open-postgresql-socket-stream (host port)
+  (etypecase host
+    (pathname
+     (error "Not supported"))
+    (string
+     (socket:socket-connect
+      port host
+      :element-type '(unsigned-byte 8)
+      :timeout *postgresql-server-socket-timeout*))))
+
+
 ;;; Interface Functions
 
 (defun open-postgresql-connection (&key (host (cmucl-compat:required-argument))
@@ -426,14 +461,14 @@ troubles."
 (defun encrypt-md5 (plaintext salt)
   (string-downcase
    (format nil "~{~2,'0X~}"
-          (coerce (md5:md5sum-sequence (concatenate 'string plaintext salt)) 'list))))
+          (coerce (md5sum-sequence (concatenate 'string plaintext salt)) 'list))))
 
 (defun reopen-postgresql-connection (connection)
   "Reopen the given PostgreSQL connection.  Closes any existing
 connection, if it is still open."
   (when (postgresql-connection-open-p connection)
     (close-postgresql-connection connection))
-  (let ((socket (open-postgresql-socket-stream 
+  (let ((socket (open-postgresql-socket-stream
                  (postgresql-connection-host connection)
                  (postgresql-connection-port connection))))
     (unwind-protect
@@ -458,23 +493,24 @@ connection, if it is still open."
                    (3
                     (send-unencrypted-password-message
                      socket
-                     (postgresql-connection-password connection)))
+                     (postgresql-connection-password connection))
+                      (force-output socket))
                    (4
-                    (let ((salt (make-string 2)))
-                      (read-socket-sequence salt socket)
+                    (let ((salt (read-socket-sequence socket 2)))
                       (send-encrypted-password-message
                        socket
                        (crypt-password
-                        (postgresql-connection-password connection) salt))))
+                        (postgresql-connection-password connection) salt)))
+                     (force-output socket))
                    (5
-                    (let ((salt (make-string 4)))
-                      (read-socket-sequence salt socket)
+                    (let ((salt (read-socket-sequence socket 4)))
                       (let* ((pwd2 (encrypt-md5 (postgresql-connection-password connection)
                                                 (postgresql-connection-user connection)))
                              (pwd (encrypt-md5 pwd2 salt)))
                         (send-encrypted-password-message
                          socket
-                         (concatenate 'string "md5" pwd)))))
+                         (concatenate 'string "md5" pwd))))
+                     (force-output socket))
                    (t
                     (error 'postgresql-login-error
                            :connection connection
@@ -539,6 +575,7 @@ connection, if it is still open."
        while (listen socket)
        do
        (case (read-socket-value-int8 socket)
+         (#.+ready-for-query-message+)
          (#.+notice-response-message+
           (let ((message (read-socket-value-string socket)))
             (warn 'postgresql-warning :connection connection
@@ -633,9 +670,7 @@ connection, if it is still open."
       (:double
        (read-double-from-socket socket length))
       (t
-       (let ((result (make-string length)))
-        (read-socket-sequence result socket)
-        result)))))
+       (read-socket-sequence socket length)))))
 
 (uffi:def-constant +char-code-zero+ (char-code #\0))
 (uffi:def-constant +char-code-minus+ (char-code #\-))
@@ -660,7 +695,7 @@ connection, if it is still open."
        )               ;; nothing to do
        (t
        (setq val (- first-char +char-code-zero+))))
-      
+
       (dotimes (i length)
        (declare (fixnum i))
        (setq val (+
@@ -678,7 +713,7 @@ connection, if it is still open."
               (< ,offset 10))
          ,offset
          nil))))
-      
+
 (defun read-double-from-socket (socket length)
   (declare (fixnum length))
   (let ((before-decimal 0)
@@ -702,13 +737,13 @@ connection, if it is still open."
        (setq before-decimal (ascii-digit char))
        (unless before-decimal
         (error "Unexpected value"))))
-    
+
     (block loop
       (dotimes (i length)
        (setq char (read-byte socket))
        ;;      (format t "~&len:~D, i:~D, char:~D, minusp:~A, decimalp:~A" length i char minusp decimalp)
        (let ((weight (ascii-digit char)))
-         (cond 
+         (cond
           ((and weight (not decimalp)) ;; before decimal point
            (setq before-decimal (+ weight (* 10 before-decimal))))
           ((and weight decimalp) ;; after decimal point
@@ -721,19 +756,19 @@ connection, if it is still open."
            (setq exponent (read-integer-from-socket socket (- length i 1)))
            (setq exponent (or exponent 0))
            (return-from loop))
-         (t 
+         (t
           (break "Unexpected value"))
          )
        )))
     (setq result (* (+ (coerce before-decimal 'double-float)
-                      (* after-decimal 
+                      (* after-decimal
                          (expt 10 (- decimal-count))))
                    (expt 10 exponent)))
     (if minusp
        (- result)
        result)))
-       
-      
+
+
 #+ignore
 (defun read-double-from-socket (socket length)
   (let ((result (make-string length)))