r11418: 30 Dec 2006 Kevin Rosenberg <kevin@rosenberg.net>
[clsql.git] / db-postgresql / postgresql-sql.lisp
index 2d307c243f439db4848a38243618010cb718c159..a3f9b885f442c41b6296ebc1740afeb5d2567127 100644 (file)
@@ -16,7 +16,7 @@
 (in-package #:cl-user)
 
 (defpackage #:clsql-postgresql
-    (:use #:common-lisp #:clsql-sys #:postgresql #:clsql-uffi)
+    (:use #:common-lisp #:clsql-sys #:pgsql #:clsql-uffi)
     (:export #:postgresql-database)
     (:documentation "This is the CLSQL interface to PostgreSQL."))
 
        (declare (type pgsql-conn-def connection))
        (when (not (eq (PQstatus connection) 
                       pgsql-conn-status-type#connection-ok))
-         (error 'sql-connection-error
-                :database-type database-type
-                :connection-spec connection-spec
-                :error-id (PQstatus connection)
-                :message (tidy-error-message 
-                          (PQerrorMessage connection))))
+          (let ((pqstatus (PQstatus connection))
+                (pqmessage (tidy-error-message (PQerrorMessage connection))))
+            (PQfinish connection)
+            (error 'sql-connection-error
+                   :database-type database-type
+                   :connection-spec connection-spec
+                   :error-id pqstatus
+                   :message  pqmessage)))
        (make-instance 'postgresql-database
                       :name (database-name-from-spec connection-spec
                                                      database-type)
                (error 'sql-database-data-error
                       :database database
                       :expression sql-expression
-                      :error-id (PQresultStatus result)
+                      :error-id (PQresultErrorField result +PG-DIAG-SQLSTATE+)
                       :message (tidy-error-message
                                (PQresultErrorMessage result)))))
           (PQclear result))))))
 
 (defmethod database-create-large-object ((database postgresql-database))
   (lo-create (database-conn-ptr database)
-            (logior postgresql::+INV_WRITE+ postgresql::+INV_READ+)))
+            (logior pgsql::+INV_WRITE+ pgsql::+INV_READ+)))
 
 
 #+mb-original
     (with-transaction (:database database)
        (unwind-protect
          (progn 
-           (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
+           (setf fd (lo-open ptr object-id pgsql::+INV_WRITE+))
            (when (>= fd 0)
              (when (= (lo-write ptr fd data length) length)
                (setf result t))))
     (database-execute-command "begin" database)
     (unwind-protect
        (progn 
-         (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
+         (setf fd (lo-open ptr object-id pgsql::+INV_WRITE+))
          (when (>= fd 0)
            (when (= (lo-write ptr fd data length) length)
              (setf result t))))
     (unwind-protect
        (progn
         (database-execute-command "begin" database)
-        (setf fd (lo-open ptr object-id postgresql::+INV_READ+))
+        (setf fd (lo-open ptr object-id pgsql::+INV_READ+))
         (when (>= fd 0)
           (setf length (lo-lseek ptr fd 0 2))
           (lo-lseek ptr fd 0 0)
 
 (defmethod database-probe (connection-spec (type (eql :postgresql)))
   (when (find (second connection-spec) (database-list connection-spec type)
-             :key #'car :test #'string-equal)
+              :test #'string-equal)
     t))
 
 
       (coerce-string db)
       (coerce-string user)
       (let ((connection (PQsetdbLogin host port options tty db user password)))
-        (declare (type postgresql::pgsql-conn-ptr connection))
-        (unless (eq (PQstatus connection) :connection-ok)
+        (declare (type pgsql::pgsql-conn-ptr connection))
+        (unless (eq (PQstatus connection)
+                   pgsql-conn-status-type#connection-ok)
           ;; Connect failed
           (error 'sql-connection-error
                  :database-type :postgresql