r9088: Automated commit for Debian build of clsql upstream-version-2.8.0
[clsql.git] / db-postgresql / postgresql-sql.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          postgresql-sql.lisp
6 ;;;; Purpose:       High-level PostgreSQL interface using UFFI
7 ;;;; Date Started:  Feb 2002
8 ;;;;
9 ;;;; $Id$
10 ;;;;
11 ;;;; CLSQL users are granted the rights to distribute and use this software
12 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
13 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
14 ;;;; *************************************************************************
15
16 (in-package #:cl-user)
17
18 (defpackage #:clsql-postgresql
19     (:use #:common-lisp #:clsql-base-sys #:postgresql #:clsql-uffi)
20     (:export #:postgresql-database)
21     (:documentation "This is the CLSQL interface to PostgreSQL."))
22
23 (in-package #:clsql-postgresql)
24
25 ;;; Field conversion functions
26
27 (defun make-type-list-for-auto (num-fields res-ptr)
28   (let ((new-types '()))
29     (dotimes (i num-fields)
30       (declare (fixnum i))
31       (let* ((type (PQftype res-ptr i)))
32         (push
33          (case type
34            ((#.pgsql-ftype#bytea
35              #.pgsql-ftype#int2
36              #.pgsql-ftype#int4)
37             :int32)
38            (#.pgsql-ftype#int8
39             :int64)
40            ((#.pgsql-ftype#float4
41              #.pgsql-ftype#float8)
42             :double)
43            (otherwise
44             t))
45          new-types)))
46       (nreverse new-types)))
47
48 (defun canonicalize-types (types num-fields res-ptr)
49   (if (null types)
50       nil
51       (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
52         (cond
53           ((listp types)
54            (canonicalize-type-list types auto-list))
55           ((eq types :auto)
56            auto-list)
57           (t
58            nil)))))
59
60 (defun tidy-error-message (message)
61   (unless (stringp message)
62     (setq message (uffi:convert-from-foreign-string message)))
63   (let ((message (string-right-trim '(#\Return #\Newline) message)))
64     (cond
65       ((< (length message) (length "ERROR:"))
66        message)
67       ((string= message "ERROR:" :end1 6)
68        (string-left-trim '(#\Space) (subseq message 6)))
69       (t
70        message))))
71
72 (defmethod database-initialize-database-type ((database-type
73                                                (eql :postgresql)))
74   t)
75
76 (uffi:def-type pgsql-conn-def pgsql-conn)
77 (uffi:def-type pgsql-result-def pgsql-result)
78
79
80 (defclass postgresql-database (database)
81   ((conn-ptr :accessor database-conn-ptr :initarg :conn-ptr
82              :type pgsql-conn-def)
83    (lock
84     :accessor database-lock
85     :initform (make-process-lock "conn"))))
86
87 (defmethod database-type ((database postgresql-database))
88   :postgresql)
89
90 (defmethod database-name-from-spec (connection-spec (database-type
91                                                      (eql :postgresql)))
92   (check-connection-spec connection-spec database-type
93                          (host db user password &optional port options tty))
94   (destructuring-bind (host db user password &optional port options tty)
95       connection-spec
96     (declare (ignore password options tty))
97     (concatenate 'string 
98       (etypecase host
99         (null "localhost")
100         (pathname (namestring host))
101         (string host))
102       (when port 
103         (concatenate 'string
104                      ":"
105                      (etypecase port
106                        (integer (write-to-string port))
107                        (string port))))
108       "/" db "/" user)))
109
110
111 (defmethod database-connect (connection-spec (database-type (eql :postgresql)))
112   (check-connection-spec connection-spec database-type
113                          (host db user password &optional port options tty))
114   (destructuring-bind (host db user password &optional port options tty)
115       connection-spec
116     (uffi:with-cstrings ((host-native host)
117                          (user-native user)
118                          (password-native password)
119                          (db-native db)
120                          (port-native port)
121                          (options-native options)
122                          (tty-native tty))
123       (let ((connection (PQsetdbLogin host-native port-native
124                                       options-native tty-native
125                                       db-native user-native
126                                       password-native)))
127         (declare (type pgsql-conn-def connection))
128         (when (not (eq (PQstatus connection) 
129                        pgsql-conn-status-type#connection-ok))
130           (error 'clsql-connect-error
131                  :database-type database-type
132                  :connection-spec connection-spec
133                  :errno (PQstatus connection)
134                  :error (tidy-error-message 
135                          (PQerrorMessage connection))))
136         (make-instance 'postgresql-database
137                        :name (database-name-from-spec connection-spec
138                                                       database-type)
139                        :database-type :postgresql
140                        :connection-spec connection-spec
141                        :conn-ptr connection)))))
142
143
144 (defmethod database-disconnect ((database postgresql-database))
145   (PQfinish (database-conn-ptr database))
146   (setf (database-conn-ptr database) nil)
147   t)
148
149 (defmethod database-query (query-expression (database postgresql-database) result-types)
150   (let ((conn-ptr (database-conn-ptr database)))
151     (declare (type pgsql-conn-def conn-ptr))
152     (uffi:with-cstring (query-native query-expression)
153       (let ((result (PQexec conn-ptr query-native)))
154         (when (uffi:null-pointer-p result)
155           (error 'clsql-sql-error
156                  :database database
157                  :expression query-expression
158                  :errno nil
159                  :error (tidy-error-message (PQerrorMessage conn-ptr))))
160         (unwind-protect
161             (case (PQresultStatus result)
162               (#.pgsql-exec-status-type#empty-query
163                nil)
164               (#.pgsql-exec-status-type#tuples-ok
165                (let ((num-fields (PQnfields result)))
166                  (setq result-types
167                    (canonicalize-types result-types num-fields
168                                              result))
169                  (loop for tuple-index from 0 below (PQntuples result)
170                        collect
171                        (loop for i from 0 below num-fields
172                              collect
173                              (if (zerop (PQgetisnull result tuple-index i))
174                                  (convert-raw-field
175                                   (PQgetvalue result tuple-index i)
176                                   result-types i)
177                                  nil)))))
178               (t
179                (error 'clsql-sql-error
180                       :database database
181                       :expression query-expression
182                       :errno (PQresultStatus result)
183                       :error (tidy-error-message
184                               (PQresultErrorMessage result)))))
185           (PQclear result))))))
186
187 (defmethod database-execute-command (sql-expression
188                                      (database postgresql-database))
189   (let ((conn-ptr (database-conn-ptr database)))
190     (declare (type pgsql-conn-def conn-ptr))
191     (uffi:with-cstring (sql-native sql-expression)
192       (let ((result (PQexec conn-ptr sql-native)))
193         (when (uffi:null-pointer-p result)
194           (error 'clsql-sql-error
195                  :database database
196                  :expression sql-expression
197                  :errno nil
198                  :error (tidy-error-message (PQerrorMessage conn-ptr))))
199         (unwind-protect
200             (case (PQresultStatus result)
201               (#.pgsql-exec-status-type#command-ok
202                t)
203               ((#.pgsql-exec-status-type#empty-query
204                 #.pgsql-exec-status-type#tuples-ok)
205                (warn "Strange result...")
206                t)
207               (t
208                (error 'clsql-sql-error
209                       :database database
210                       :expression sql-expression
211                       :errno (PQresultStatus result)
212                       :error (tidy-error-message
213                               (PQresultErrorMessage result)))))
214           (PQclear result))))))
215
216 (defstruct postgresql-result-set
217   (res-ptr (uffi:make-null-pointer 'pgsql-result) 
218            :type pgsql-result-def)
219   (types nil) 
220   (num-tuples 0 :type integer)
221   (num-fields 0 :type integer)
222   (tuple-index 0 :type integer))
223
224 (defmethod database-query-result-set ((query-expression string)
225                                       (database postgresql-database) 
226                                       &key full-set result-types)
227   (let ((conn-ptr (database-conn-ptr database)))
228     (declare (type pgsql-conn-def conn-ptr))
229     (uffi:with-cstring (query-native query-expression)
230       (let ((result (PQexec conn-ptr query-native)))
231         (when (uffi:null-pointer-p result)
232           (error 'clsql-sql-error
233                  :database database
234                  :expression query-expression
235                  :errno nil
236                  :error (tidy-error-message (PQerrorMessage conn-ptr))))
237         (case (PQresultStatus result)
238           ((#.pgsql-exec-status-type#empty-query
239             #.pgsql-exec-status-type#tuples-ok)
240            (let ((result-set (make-postgresql-result-set
241                         :res-ptr result
242                         :num-fields (PQnfields result)
243                         :num-tuples (PQntuples result)
244                         :types (canonicalize-types 
245                                       result-types
246                                       (PQnfields result)
247                                       result))))
248              (if full-set
249                  (values result-set
250                          (PQnfields result)
251                          (PQntuples result))
252                  (values result-set
253                          (PQnfields result)))))
254           (t
255            (unwind-protect
256                (error 'clsql-sql-error
257                       :database database
258                       :expression query-expression
259                       :errno (PQresultStatus result)
260                       :error (tidy-error-message
261                               (PQresultErrorMessage result)))
262              (PQclear result))))))))
263   
264 (defmethod database-dump-result-set (result-set (database postgresql-database))
265   (let ((res-ptr (postgresql-result-set-res-ptr result-set))) 
266     (declare (type pgsql-result-def res-ptr))
267     (PQclear res-ptr)
268     t))
269
270 (defmethod database-store-next-row (result-set (database postgresql-database) 
271                                     list)
272   (let ((result (postgresql-result-set-res-ptr result-set))
273         (types (postgresql-result-set-types result-set)))
274     (declare (type pgsql-result-def result))
275     (if (>= (postgresql-result-set-tuple-index result-set)
276             (postgresql-result-set-num-tuples result-set))
277         nil
278       (loop with tuple-index = (postgresql-result-set-tuple-index result-set)
279           for i from 0 below (postgresql-result-set-num-fields result-set)
280           for rest on list
281           do
282             (setf (car rest)
283               (if (zerop (PQgetisnull result tuple-index i))
284                   (convert-raw-field
285                    (PQgetvalue result tuple-index i)
286                    types i)
287                 nil))
288           finally
289             (incf (postgresql-result-set-tuple-index result-set))
290             (return list)))))
291
292 ;;; Large objects support (Marc B)
293
294 (defmethod database-create-large-object ((database postgresql-database))
295   (lo-create (database-conn-ptr database)
296              (logior postgresql::+INV_WRITE+ postgresql::+INV_READ+)))
297
298
299 #+mb-original
300 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
301   (let ((ptr (database-conn-ptr database))
302         (length (length data))
303         (result nil)
304         (fd nil))
305     (with-transaction (:database database)
306        (unwind-protect
307           (progn 
308             (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
309             (when (>= fd 0)
310               (when (= (lo-write ptr fd data length) length)
311                 (setf result t))))
312          (progn
313            (when (and fd (>= fd 0))
314              (lo-close ptr fd))
315            )))
316     result))
317
318 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
319   (let ((ptr (database-conn-ptr database))
320         (length (length data))
321         (result nil)
322         (fd nil))
323     (database-execute-command "begin" database)
324     (unwind-protect
325         (progn 
326           (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
327           (when (>= fd 0)
328             (when (= (lo-write ptr fd data length) length)
329               (setf result t))))
330       (progn
331         (when (and fd (>= fd 0))
332           (lo-close ptr fd))
333         (database-execute-command (if result "commit" "rollback") database)))
334     result))
335
336 ;; (MB) the begin/commit/rollback stuff will be removed when with-transaction wil be implemented
337 ;; (KMR) Can't use with-transaction since that function is in high-level code
338 (defmethod database-read-large-object (object-id (database postgresql-database))
339   (let ((ptr (database-conn-ptr database))
340         (buffer nil)
341         (result nil)
342         (length 0)
343         (fd nil))
344     (unwind-protect
345        (progn
346          (database-execute-command "begin" database)
347          (setf fd (lo-open ptr object-id postgresql::+INV_READ+))
348          (when (>= fd 0)
349            (setf length (lo-lseek ptr fd 0 2))
350            (lo-lseek ptr fd 0 0)
351            (when (> length 0)
352              (setf buffer (uffi:allocate-foreign-string 
353                            length :unsigned t))
354              (when (= (lo-read ptr fd buffer length) length)
355                (setf result (uffi:convert-from-foreign-string
356                              buffer :length length :null-terminated-p nil))))))
357       (progn
358         (when buffer (uffi:free-foreign-object buffer))
359         (when (and fd (>= fd 0)) (lo-close ptr fd))
360         (database-execute-command (if result "commit" "rollback") database)))
361     result))
362
363 (defmethod database-delete-large-object (object-id (database postgresql-database))
364   (lo-unlink (database-conn-ptr database) object-id))
365
366
367 ;;; Object listing
368
369 (defmethod database-list-objects-of-type ((database postgresql-database)
370                                           type owner)
371   (let ((owner-clause
372          (cond ((stringp owner)
373                 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))" owner))
374                ((null owner)
375                 (format nil " AND (NOT (relowner=1))"))
376                (t ""))))
377     (mapcar #'car
378             (database-query
379              (format nil
380                      "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
381                      type
382                      owner-clause)
383              database nil))))
384     
385 (defmethod database-list-tables ((database postgresql-database)
386                                  &key (owner nil))
387   (database-list-objects-of-type database "r" owner))
388   
389 (defmethod database-list-views ((database postgresql-database)
390                                 &key (owner nil))
391   (database-list-objects-of-type database "v" owner))
392   
393 (defmethod database-list-indexes ((database postgresql-database)
394                                   &key (owner nil))
395   (database-list-objects-of-type database "i" owner))
396   
397 (defmethod database-list-attributes ((table string)
398                                      (database postgresql-database)
399                                      &key (owner nil))
400   (let* ((owner-clause
401           (cond ((stringp owner)
402                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
403                 ((null owner) " AND (not (relowner=1))")
404                 (t "")))
405          (result
406           (mapcar #'car
407                   (database-query
408                    (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
409                            (string-downcase table)
410                            owner-clause)
411                    database nil))))
412     (if result
413         (reverse
414          (remove-if #'(lambda (it) (member it '("cmin"
415                                                 "cmax"
416                                                 "xmax"
417                                                 "xmin"
418                                                 "oid"
419                                                 "ctid"
420                                                 ;; kmr -- added tableoid
421                                                 "tableoid") :test #'equal)) 
422                     result)))))
423
424 (defmethod database-attribute-type (attribute (table string)
425                                     (database postgresql-database)
426                                     &key (owner nil))
427   (let* ((owner-clause
428           (cond ((stringp owner)
429                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
430                 ((null owner) " AND (not (relowner=1))")
431                 (t "")))
432          (result
433           (mapcar #'car
434                   (database-query
435                    (format nil "SELECT pg_type.typname FROM pg_type,pg_class,pg_attribute WHERE pg_class.oid=pg_attribute.attrelid AND pg_class.relname='~A' AND pg_attribute.attname='~A' AND pg_attribute.atttypid=pg_type.oid~A"
436                            (string-downcase table)
437                            (string-downcase attribute)
438                            owner-clause)
439                    database nil))))
440     (when result
441       (intern (string-upcase (car result)) :keyword))))
442
443 (defmethod database-create-sequence (sequence-name
444                                      (database postgresql-database))
445   (database-execute-command
446    (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
447    database))
448
449 (defmethod database-drop-sequence (sequence-name
450                                    (database postgresql-database))
451   (database-execute-command
452    (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
453
454 (defmethod database-list-sequences ((database postgresql-database)
455                                     &key (owner nil))
456   (database-list-objects-of-type database "S" owner))
457
458 (defmethod database-set-sequence-position (name (position integer)
459                                                 (database postgresql-database))
460   (values
461    (parse-integer
462     (caar
463      (database-query
464       (format nil "SELECT SETVAL ('~A', ~A)" name position)
465       database nil)))))
466
467 (defmethod database-sequence-next (sequence-name 
468                                    (database postgresql-database))
469   (values
470    (parse-integer
471     (caar
472      (database-query
473       (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
474       database nil)))))
475
476 (defmethod database-sequence-last (sequence-name (database postgresql-database))
477   (values
478    (parse-integer
479     (caar
480      (database-query
481       (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
482       database nil)))))
483   
484 (defmethod database-create (connection-spec (type (eql :postgresql)))
485   (destructuring-bind (host name user password) connection-spec
486     (declare (ignore user password))
487     (multiple-value-bind (output status)
488         (clsql-base-sys:command-output "createdb -h~A ~A"
489                                        (if host host "localhost")
490                                        name)
491       (if (or (not (zerop status))
492               (search "database creation failed: ERROR:" output))
493           (error 'clsql-access-error
494                  :connection-spec connection-spec
495                  :database-type type
496                  :error 
497                  (format nil "database-create failed: ~A" 
498                          output))
499         t))))
500
501 (defmethod database-destroy (connection-spec (type (eql :postgresql)))
502   (destructuring-bind (host name user password) connection-spec
503     (declare (ignore user password))
504     (multiple-value-bind (output status)
505         (clsql-base-sys:command-output "dropdb -h~A ~A"
506                                        (if host host "localhost")
507                                        name)
508       (if (or (not (zerop status))
509               (search "database removal failed: ERROR:" output))
510           (error 'clsql-access-error
511                  :connection-spec connection-spec
512                  :database-type type
513                  :error 
514                  (format nil "database-destory failed: ~A" 
515                          output))
516         t))))
517
518
519 (defmethod database-probe (connection-spec (type (eql :postgresql)))
520   (when (find (second connection-spec) (database-list connection-spec type)
521               :key #'car :test #'string-equal)
522     t))
523
524 (defmethod database-list (connection-spec (type (eql :postgresql)))
525   (destructuring-bind (host name user password) connection-spec
526     (declare (ignore name))
527     (let ((database (database-connect (list host "template1" user password)
528                                       type)))
529       (unwind-protect
530            (progn
531              (setf (slot-value database 'clsql-base-sys::state) :open)
532              (mapcar #'car (database-query "select datname from pg_database" 
533                                            database :auto)))
534         (progn
535           (database-disconnect database)
536           (setf (slot-value database 'clsql-base-sys::state) :closed))))))
537
538 (defmethod database-describe-table ((database postgresql-database) table)
539   (database-query 
540    (format nil "select a.attname, t.typname
541                                from pg_class c, pg_attribute a, pg_type t
542                                where c.relname = '~a'
543                                    and a.attnum > 0
544                                    and a.attrelid = c.oid
545                                    and a.atttypid = t.oid"
546            (sql-escape (string-downcase table)))
547    database :auto))
548
549 (defun %pg-database-connection (connection-spec)
550   (check-connection-spec connection-spec :postgresql
551                          (host db user password &optional port options tty))
552   (macrolet ((coerce-string (var)
553                `(unless (typep ,var 'simple-base-string)
554                  (setf ,var (coerce ,var 'simple-base-string)))))
555     (destructuring-bind (host db user password &optional port options tty)
556         connection-spec
557       (coerce-string db)
558       (coerce-string user)
559       (let ((connection (pqsetdblogin host port options tty db user password)))
560         (declare (type postgresql::pgsql-conn-ptr connection))
561         (unless (eq (pqstatus connection) :connection-ok)
562           ;; Connect failed
563           (error 'clsql-connect-error
564                  :database-type :postgresql
565                  :connection-spec connection-spec
566                  :errno (pqstatus connection)
567                  :error (pqerrormessage connection)))
568         connection))))
569
570 (defmethod database-reconnect ((database postgresql-database))
571   (let ((lock (database-lock database)))
572     (with-process-lock (lock "Reconnecting")
573       (with-slots (connection-spec conn-ptr)
574           database
575         (setf conn-ptr (%pg-database-connection connection-spec))
576         database))))
577
578 (when (clsql-base-sys:database-type-library-loaded :postgresql)
579   (clsql-base-sys:initialize-database-type :database-type :postgresql))