r11232: 16 Oct 2006 Kevin Rosenberg <kevin@rosenberg.net>
[clsql.git] / db-postgresql / postgresql-sql.lisp
index 6a8c7c83290c6852deff730f229e432e4b9661d8..82f03e2454a0d63fff2ddfb4822fd96f31169d9d 100644 (file)
        (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-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))