r9199: fold clsql-base and clsql-base-sys into clsql-base
[clsql.git] / db-postgresql-socket / postgresql-socket-api.lisp
index 4dffc99241aea6007e3f12af83c43a4e63f3ba30..5630f04eceb3c2254a2a3c2253586583201a7fed 100644 (file)
@@ -2,16 +2,14 @@
 ;;;; *************************************************************************
 ;;;; FILE IDENTIFICATION
 ;;;;
-;;;; Name:          postgresql-socket-api.lisp
-;;;; Purpose:       Low-level PostgreSQL interface using sockets
-;;;; Programmers:   Kevin M. Rosenberg based on
-;;;;                Original code by Pierre R. Mai 
-;;;;                
-;;;; Date Started:  Feb 2002
+;;;; Name:     postgresql-socket-api.lisp
+;;;; Purpose:  Low-level PostgreSQL interface using sockets
+;;;; Authors:  Kevin M. Rosenberg based on original code by Pierre R. Mai 
+;;;; Created:  Feb 2002
 ;;;;
-;;;; $Id: postgresql-socket-api.lisp,v 1.2 2002/10/21 07:45:50 kevin Exp $
+;;;; $Id$
 ;;;;
-;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
+;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
 ;;;;
 ;;;; CLSQL users are granted the rights to distribute and use this software
 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
 ;;;; *************************************************************************
 
-
-;;;; Changes by Kevin Rosenberg
-;;;;  - Added socket open functions for Allegro and Lispworks
-;;;;  - Changed CMUCL FFI to UFFI
-;;;;  - Added necessary (force-output) for socket streams on 
-;;;;     Allegro and Lispworks
-;;;;  - Added initialization variable
-;;;;  - Added field type processing
-
-(declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
-(in-package :postgresql-socket)
+(in-package #:postgresql-socket)
 
 (uffi:def-enum pgsql-ftype
     ((:bytea 17)
      (:float4 700)
      (:float8 701)))
 
-(defmethod database-type-library-loaded ((database-type
+(defmethod clsql-base:database-type-library-loaded ((database-type
                                          (eql :postgresql-socket)))
   "T if foreign library was able to be loaded successfully. Always true for
 socket interface"
   t)
-                                     
+
+(defmethod clsql-base:database-type-load-foreign ((database-type (eql :postgresql-socket)))
+  t)
+
 
 ;;; Message I/O stuff
 
@@ -308,7 +298,6 @@ socket interface"
 (defvar *postgresql-server-socket-timeout* 60
   "Timeout in seconds for reads from the PostgreSQL server.")
 
-
 #+(or cmu scl)
 (defun open-postgresql-socket (host port)
   (etypecase host
@@ -321,6 +310,26 @@ socket interface"
     (string
      (ext:connect-to-inet-socket host port))))
 
+#+sbcl
+(defun open-postgresql-socket (host port)
+  (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))))
+    (string
+     (let ((sock (make-instance 'sb-bsd-sockets:inet-socket
+                               :type :stream
+                               :protocol :tcp)))
+       (sb-bsd-sockets:socket-connect 
+       sock 
+       (sb-bsd-sockets:host-ent-address
+        (sb-bsd-sockets:get-host-by-name host)) 
+       port)
+       sock))))
+
 #+(or cmu scl)
 (defun open-postgresql-socket-stream (host port)
   (system:make-fd-stream
@@ -329,6 +338,14 @@ socket interface"
    :buffering :none
    :timeout *postgresql-server-socket-timeout*))
 
+
+#+sbcl
+(defun open-postgresql-socket-stream (host port)
+  (sb-bsd-sockets:socket-make-stream
+   (open-postgresql-socket host port) :input t :output t 
+   :element-type '(unsigned-byte 8)))
+  
+
 #+allegro
 (defun open-postgresql-socket-stream (host port)
   (etypecase host
@@ -344,8 +361,22 @@ socket interface"
         (mp:with-timeout (*postgresql-server-socket-timeout* (error "connect failed"))
           (socket:make-socket :type :stream :address-family :internet
                               :remote-port port :remote-host host
-                              :connect :active :nodelay t))))
-    ))
+                              :connect :active :nodelay t))))))
+
+#+openmcl
+(defun open-postgresql-socket-stream (host port)
+  (etypecase host
+    (pathname
+     (let ((path (namestring
+                 (make-pathname :name ".s.PGSQL" :type (princ-to-string port)
+                                :defaults host))))
+       (ccl:make-socket :type :stream :address-family :file
+                       :connect :active
+                       :remote-filename path :local-filename path)))
+    (string
+     (ccl:make-socket :type :stream :address-family :internet
+                     :remote-port port :remote-host host
+                     :connect :active :nodelay t))))
 
 #+lispworks
 (defun open-postgresql-socket-stream (host port)
@@ -391,6 +422,11 @@ troubles."
                               :database database :user user
                               :password (or password ""))))
 
+(defun encrypt-md5 (plaintext salt)
+  (string-downcase
+   (format nil "~{~2,'0X~}"
+          (coerce (md5: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."
@@ -429,6 +465,15 @@ connection, if it is still open."
                        socket
                        (crypt-password
                         (postgresql-connection-password connection) salt))))
+                   (5
+                    (let ((salt (make-string 4)))
+                      (read-socket-sequence salt socket)
+                      (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)))))
                    (t
                     (error 'postgresql-login-error
                            :connection connection
@@ -832,12 +877,12 @@ connection, if it is still open."
             (error 'postgresql-fatal-error :connection connection
                    :message "Received garbled message from backend")))))))
 
-(defun run-query (connection query &optional (types nil))
+(defun run-query (connection query &optional (result-types nil))
   (start-query-execution connection query)
   (multiple-value-bind (status cursor)
       (wait-for-query-results connection)
     (assert (eq status :cursor))
-    (loop for row = (read-cursor-row cursor types)
+    (loop for row = (read-cursor-row cursor result-types)
          while row
          collect row
          finally