X-Git-Url: http://git.kpe.io/?p=clsql.git;a=blobdiff_plain;f=db-postgresql%2Fpostgresql-sql.lisp;h=a3f9b885f442c41b6296ebc1740afeb5d2567127;hp=bcfda5ecb5ae6ee8b8e295927b3eeb8b8969b076;hb=ab37892bfa71e0d66021cc73f28cd189be30c81c;hpb=7308bdf188da6424e615ca14096ef53cfb845a90 diff --git a/db-postgresql/postgresql-sql.lisp b/db-postgresql/postgresql-sql.lisp index bcfda5e..a3f9b88 100644 --- a/db-postgresql/postgresql-sql.lisp +++ b/db-postgresql/postgresql-sql.lisp @@ -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.")) @@ -127,12 +127,14 @@ (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) @@ -158,25 +160,29 @@ :message (tidy-error-message (PQerrorMessage conn-ptr)))) (unwind-protect (case (PQresultStatus result) + ;; User gave a command rather than a query + (#.pgsql-exec-status-type#command-ok + nil) (#.pgsql-exec-status-type#empty-query nil) (#.pgsql-exec-status-type#tuples-ok (let ((num-fields (PQnfields result))) - (setq result-types - (canonicalize-types result-types num-fields - result)) - (values - (loop for tuple-index from 0 below (PQntuples result) - collect - (loop for i from 0 below num-fields - collect - (if (zerop (PQgetisnull result tuple-index i)) - (convert-raw-field - (PQgetvalue result tuple-index i) - result-types i) - nil))) - (when field-names - (result-field-names num-fields result))))) + (when result-types + (setq result-types + (canonicalize-types result-types num-fields + result))) + (let ((res (loop for tuple-index from 0 below (PQntuples result) + collect + (loop for i from 0 below num-fields + collect + (if (zerop (PQgetisnull result tuple-index i)) + (convert-raw-field + (PQgetvalue result tuple-index i) + result-types i) + nil))))) + (if field-names + (values res (result-field-names num-fields result)) + res)))) (t (error 'sql-database-data-error :database database @@ -216,7 +222,7 @@ (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)))))) @@ -300,7 +306,7 @@ (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 @@ -312,7 +318,7 @@ (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)))) @@ -330,7 +336,7 @@ (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)))) @@ -351,7 +357,7 @@ (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) @@ -408,7 +414,7 @@ (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)) @@ -423,8 +429,9 @@ (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 @@ -443,11 +450,5 @@ ;;; Database capabilities -(defmethod db-type-has-fancy-math? ((db-type (eql :postgresql))) - t) - -(defmethod db-type-default-case ((db-type (eql :postgresql))) - :lower) - (when (clsql-sys:database-type-library-loaded :postgresql) (clsql-sys:initialize-database-type :database-type :postgresql))