X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=db-postgresql-socket%2Fpostgresql-socket-api.lisp;h=3ca868a2645469e8b44457cf92121aad5dc1a243;hb=1b07d2fd927cf8f1943ac0a0b8c980d1dc707076;hp=66b2912a9f84291e8124e612850ac7e88d972804;hpb=36509b8d390e94b2b0f8e681c09d5e89452978d8;p=clsql.git diff --git a/db-postgresql-socket/postgresql-socket-api.lisp b/db-postgresql-socket/postgresql-socket-api.lisp index 66b2912..3ca868a 100644 --- a/db-postgresql-socket/postgresql-socket-api.lisp +++ b/db-postgresql-socket/postgresql-socket-api.lisp @@ -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$ @@ -213,8 +213,7 @@ socket interface" (int32 key)) -(defun read-socket-sequence (stream length) - "KMR -- Added to support reading from binary stream into a string" +(defun read-socket-sequence (stream length &optional (allow-wide t)) (declare (stream stream) (optimize (speed 3) (safety 0))) #-sb-unicode @@ -226,8 +225,9 @@ socket interface" (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))) - + (if allow-wide + (sb-ext:octets-to-string bytes) + (map 'string #'code-char bytes)))) ;;; Support for encrypted password transmission @@ -236,7 +236,7 @@ socket interface" (defvar *crypt-library-loaded* nil) (unless *crypt-library-loaded* - (uffi:load-foreign-library + (uffi:load-foreign-library (uffi:find-foreign-library "libcrypt" '(#+(or 64bit x86-64) "/usr/lib64/" "/usr/lib/" "/usr/local/lib/" "/lib/")) @@ -254,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))))) @@ -334,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 + 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)))) @@ -361,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) @@ -407,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)) @@ -451,7 +468,7 @@ troubles." 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 @@ -479,14 +496,14 @@ connection, if it is still open." (postgresql-connection-password connection)) (force-output socket)) (4 - (let ((salt (read-socket-sequence socket 2))) + (let ((salt (read-socket-sequence socket 2 nil))) (send-encrypted-password-message socket (crypt-password (postgresql-connection-password connection) salt))) (force-output socket)) (5 - (let ((salt (read-socket-sequence socket 4))) + (let ((salt (read-socket-sequence socket 4 nil))) (let* ((pwd2 (encrypt-md5 (postgresql-connection-password connection) (postgresql-connection-user connection))) (pwd (encrypt-md5 pwd2 salt))) @@ -678,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 (+ @@ -696,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) @@ -720,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 @@ -739,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)))