r2261: *** empty log message ***
[clsql.git] / interfaces / postgresql / postgresql-sql.cl
index db4128bf96c4eab0469eda865be9e716a44d029d..cb02f60bf3489d9781423e3f55a916ee7e419fd7 100644 (file)
@@ -8,7 +8,7 @@
 ;;;;                Original code by Pierre R. Mai 
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id: postgresql-sql.cl,v 1.8 2002/03/25 14:13:41 kevin Exp $
+;;;; $Id: postgresql-sql.cl,v 1.18 2002/05/27 17:19:30 kevin Exp $
 ;;;;
 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
@@ -22,7 +22,7 @@
 (in-package :cl-user)
 
 (defpackage :clsql-postgresql
-    (:use :common-lisp :clsql-sys :postgresql)
+    (:use :common-lisp :clsql-base-sys :postgresql :clsql-uffi)
     (:export #:postgresql-database)
     (:documentation "This is the CLSQL interface to PostgreSQL."))
 
 
 ;;; Field conversion functions
 
-(defun canonicalize-field-types (types num-fields res-ptr)
-  (cond
-   ((if (listp types)
-       (let ((length-types (length types))
-             (new-types '()))
-         (loop for i from 0 below num-fields
-             do
-               (if (>= i length-types)
-                   (push t new-types) ;; types is shorted than num-fields
-                 (push
-                  (case (nth i types)
-                    ((:int :long :double t)
-                     (nth i types))
-                    (t
-                     t))
-                  new-types)))
-         (nreverse new-types))))
-   ((eq types :auto)
-    (let ((new-types '()))
-      (dotimes (i num-fields)
-       (declare (fixnum i))
-       (let* ((type (PQftype res-ptr i)))
-         (push
-          (case type
-            ((#.pgsql-ftype#bytea
-              #.pgsql-ftype#int2
-              #.pgsql-ftype#int4)
-             :int)
-            ((#.pgsql-ftype#float4
-              #.pgsql-ftype#float8)
-             :double)
-            (otherwise
-             t))
-          new-types)))
+(defun make-type-list-for-auto (num-fields res-ptr)
+  (let ((new-types '()))
+    (dotimes (i num-fields)
+      (declare (fixnum i))
+      (let* ((type (PQftype res-ptr i)))
+       (push
+        (case type
+          ((#.pgsql-ftype#bytea
+            #.pgsql-ftype#int2
+            #.pgsql-ftype#int4)
+           :int32)
+          (#.pgsql-ftype#int8
+           :int64)
+          ((#.pgsql-ftype#float4
+            #.pgsql-ftype#float8)
+           :double)
+          (otherwise
+           t))
+        new-types)))
       (nreverse new-types)))
-   (t
-    nil)))
-
-
-(uffi:def-function "atoi"
-    ((str :cstring))
-  :returning :int)
-
-(uffi:def-function "atol"
-    ((str :cstring))
-  :returning :long)
-
-(uffi:def-function "atof"
-    ((str :cstring))
-  :returning :double)
-
-(defun convert-raw-field (char-ptr types index)
-  (let ((type (if (listp types)
-                 (nth index types)
-                 types)))
-    (case type
-      (:int
-       (atoi char-ptr))
-      (:long
-       (atol char-ptr))
-      (:double
-       (atof char-ptr))
-      (otherwise
-       (uffi:convert-from-foreign-string char-ptr)))))
 
+(defun canonicalize-types (types num-fields res-ptr)
+  (if (null types)
+      nil
+      (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
+       (cond
+         ((listp types)
+          (canonicalize-type-list types auto-list))
+         ((eq types :auto)
+          auto-list)
+         (t
+          nil)))))
 
 (defun tidy-error-message (message)
   (unless (stringp message)
   ((conn-ptr :accessor database-conn-ptr :initarg :conn-ptr
             :type pgsql-conn-def)))
 
+(defmethod database-type ((database postgresql-database))
+  :postgresql)
+
 (defmethod database-name-from-spec (connection-spec (database-type
                                                     (eql :postgresql)))
   (check-connection-spec connection-spec database-type
        (make-instance 'postgresql-database
                       :name (database-name-from-spec connection-spec
                                                      database-type)
+                      :connection-spec connection-spec
                       :conn-ptr connection)))))
 
 
   (setf (database-conn-ptr database) nil)
   t)
 
-(defmethod database-query (query-expression (database postgresql-database) field-types)
+(defmethod database-query (query-expression (database postgresql-database) types)
   (let ((conn-ptr (database-conn-ptr database)))
     (declare (type pgsql-conn-def conn-ptr))
     (uffi:with-cstring (query-native query-expression)
                nil)
               (#.pgsql-exec-status-type#tuples-ok
               (let ((num-fields (PQnfields result)))
-                (setq field-types
-                  (canonicalize-field-types field-types num-fields
+                (setq types
+                  (canonicalize-types types num-fields
                                             result))
                 (loop for tuple-index from 0 below (PQntuples result)
                       collect
                             (if (zerop (PQgetisnull result tuple-index i))
                                 (convert-raw-field
                                  (PQgetvalue result tuple-index i)
-                                 field-types i)
+                                 types i)
                                 nil)))))
               (t
                (error 'clsql-sql-error
 (defstruct postgresql-result-set
   (res-ptr (uffi:make-null-pointer 'pgsql-result) 
           :type pgsql-result-def)
-  (field-types nil) 
+  (types nil) 
   (num-tuples 0 :type integer)
   (num-fields 0 :type integer)
   (tuple-index 0 :type integer))
 
 (defmethod database-query-result-set (query-expression (database postgresql-database) 
-                                      &key full-set field-types)
+                                      &key full-set types)
   (let ((conn-ptr (database-conn-ptr database)))
     (declare (type pgsql-conn-def conn-ptr))
     (uffi:with-cstring (query-native query-expression)
                         :res-ptr result
                         :num-fields (PQnfields result)
                         :num-tuples (PQntuples result)
-                       :field-types (canonicalize-field-types 
-                                     field-types
+                       :types (canonicalize-types 
+                                     types
                                      (PQnfields result)
                                      result))))
             (if full-set
 (defmethod database-store-next-row (result-set (database postgresql-database) 
                                     list)
   (let ((result (postgresql-result-set-res-ptr result-set))
-       (field-types (postgresql-result-set-field-types result-set)))
+       (types (postgresql-result-set-types result-set)))
     (declare (type pgsql-result-def result))
     (if (>= (postgresql-result-set-tuple-index result-set)
            (postgresql-result-set-num-tuples result-set))
               (if (zerop (PQgetisnull result tuple-index i))
                   (convert-raw-field
                    (PQgetvalue result tuple-index i)
-                  field-types i)
+                  types i)
                 nil))
           finally
             (incf (postgresql-result-set-tuple-index result-set))
             (return list)))))
+
+;;; Large objects support (Marc B)
+
+(defmethod database-create-large-object ((database postgresql-database))
+  (lo-create (database-conn-ptr database)
+            (logior postgresql::+INV_WRITE+ postgresql::+INV_READ+)))
+
+
+#+mb-original
+(defmethod database-write-large-object (object-id (data string) (database postgresql-database))
+  (let ((ptr (database-conn-ptr database))
+       (length (length data))
+       (result nil)
+       (fd nil))
+    (with-transaction (:database database)
+       (unwind-protect
+         (progn 
+           (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
+           (when (>= fd 0)
+             (when (= (lo-write ptr fd data length) length)
+               (setf result t))))
+        (progn
+          (when (and fd (>= fd 0))
+            (lo-close ptr fd))
+          )))
+    result))
+
+(defmethod database-write-large-object (object-id (data string) (database postgresql-database))
+  (let ((ptr (database-conn-ptr database))
+       (length (length data))
+       (result nil)
+       (fd nil))
+    (database-execute-command "begin" database)
+    (unwind-protect
+       (progn 
+         (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
+         (when (>= fd 0)
+           (when (= (lo-write ptr fd data length) length)
+             (setf result t))))
+      (progn
+       (when (and fd (>= fd 0))
+         (lo-close ptr fd))
+       (database-execute-command (if result "commit" "rollback") database)))
+    result))
+
+;; (MB) the begin/commit/rollback stuff will be removed when with-transaction wil be implemented
+;; (KMR) Can't use with-transaction since that function is in high-level code
+(defmethod database-read-large-object (object-id (database postgresql-database))
+  (let ((ptr (database-conn-ptr database))
+       (buffer nil)
+       (result nil)
+       (length 0)
+       (fd nil))
+    (unwind-protect
+       (progn
+        (database-execute-command "begin" database)
+        (setf fd (lo-open ptr object-id postgresql::+INV_READ+))
+        (when (>= fd 0)
+          (setf length (lo-lseek ptr fd 0 2))
+          (lo-lseek ptr fd 0 0)
+          (when (> length 0)
+            (setf buffer (uffi:allocate-foreign-string 
+                          length :unsigned t))
+            (when (= (lo-read ptr fd buffer length) length)
+              (setf result (uffi:convert-from-foreign-string
+                            buffer :length length :null-terminated-p nil))))))
+      (progn
+       (when buffer (uffi:free-foreign-object buffer))
+       (when (and fd (>= fd 0)) (lo-close ptr fd))
+       (database-execute-command (if result "commit" "rollback") database)))
+    result))
+
+(defmethod database-delete-large-object (object-id (database postgresql-database))
+  (lo-unlink (database-conn-ptr database) object-id))
+
+(when (clsql-base-sys:database-type-library-loaded :postgresql)
+  (clsql-base-sys:initialize-database-type :database-type :postgresql)
+  (pushnew :postgresql cl:*features*))