r9133: case handling, test report summarizing, documentation additions
[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 (defun owner-clause (owner)
370   (cond 
371    ((stringp owner)
372     (format
373      nil
374      " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))" 
375      owner))
376    ((null owner)
377     (format nil " AND (NOT (relowner=1))"))
378    (t "")))
379
380 (defmethod database-list-objects-of-type ((database postgresql-database)
381                                           type owner)
382   (mapcar #'car
383           (database-query
384            (format nil
385                    "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
386                    type
387                    (owner-clause owner))
388            database nil)))
389
390 (defmethod database-list-tables ((database postgresql-database)
391                                  &key (owner nil))
392   (database-list-objects-of-type database "r" owner))
393   
394 (defmethod database-list-views ((database postgresql-database)
395                                 &key (owner nil))
396   (database-list-objects-of-type database "v" owner))
397   
398 (defmethod database-list-indexes ((database postgresql-database)
399                                   &key (owner nil))
400   (database-list-objects-of-type database "i" owner))
401
402
403 (defmethod database-list-table-indexes (table (database postgresql-database)
404                                         &key (owner nil))
405   (let ((indexrelids
406          (database-query
407           (format 
408            nil
409            "select indexrelid from pg_index where indrelid=(select relfilenode from pg_class where relname='~A'~A)"
410            (string-downcase table)
411            (owner-clause owner))
412           database :auto))
413         (result nil))
414     (dolist (indexrelid indexrelids (nreverse result))
415       (push 
416        (caar (database-query
417               (format nil "select relname from pg_class where relfilenode='~A'"
418                       (car indexrelid))
419               database
420               nil))
421        result))))
422
423 (defmethod database-list-attributes ((table string)
424                                      (database postgresql-database)
425                                      &key (owner nil))
426   (let* ((owner-clause
427           (cond ((stringp owner)
428                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
429                 ((null owner) " AND (not (relowner=1))")
430                 (t "")))
431          (result
432           (mapcar #'car
433                   (database-query
434                    (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
435                            (string-downcase table)
436                            owner-clause)
437                    database nil))))
438     (if result
439         (reverse
440          (remove-if #'(lambda (it) (member it '("cmin"
441                                                 "cmax"
442                                                 "xmax"
443                                                 "xmin"
444                                                 "oid"
445                                                 "ctid"
446                                                 ;; kmr -- added tableoid
447                                                 "tableoid") :test #'equal)) 
448                     result)))))
449
450 (defmethod database-attribute-type (attribute (table string)
451                                     (database postgresql-database)
452                                     &key (owner nil))
453   (let* ((owner-clause
454           (cond ((stringp owner)
455                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
456                 ((null owner) " AND (not (relowner=1))")
457                 (t "")))
458          (result
459           (mapcar #'car
460                   (database-query
461                    (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"
462                            (string-downcase table)
463                            (string-downcase attribute)
464                            owner-clause)
465                    database nil))))
466     (when result
467       (intern (string-upcase (car result)) :keyword))))
468
469 (defmethod database-create-sequence (sequence-name
470                                      (database postgresql-database))
471   (database-execute-command
472    (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
473    database))
474
475 (defmethod database-drop-sequence (sequence-name
476                                    (database postgresql-database))
477   (database-execute-command
478    (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
479
480 (defmethod database-list-sequences ((database postgresql-database)
481                                     &key (owner nil))
482   (database-list-objects-of-type database "S" owner))
483
484 (defmethod database-set-sequence-position (name (position integer)
485                                                 (database postgresql-database))
486   (values
487    (parse-integer
488     (caar
489      (database-query
490       (format nil "SELECT SETVAL ('~A', ~A)" name position)
491       database nil)))))
492
493 (defmethod database-sequence-next (sequence-name 
494                                    (database postgresql-database))
495   (values
496    (parse-integer
497     (caar
498      (database-query
499       (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
500       database nil)))))
501
502 (defmethod database-sequence-last (sequence-name (database postgresql-database))
503   (values
504    (parse-integer
505     (caar
506      (database-query
507       (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
508       database nil)))))
509   
510 (defmethod database-create (connection-spec (type (eql :postgresql)))
511   (destructuring-bind (host name user password) connection-spec
512     (declare (ignore user password))
513     (multiple-value-bind (output status)
514         (clsql-base-sys:command-output "createdb -h~A ~A"
515                                        (if host host "localhost")
516                                        name)
517       (if (or (not (zerop status))
518               (search "database creation failed: ERROR:" output))
519           (error 'clsql-access-error
520                  :connection-spec connection-spec
521                  :database-type type
522                  :error 
523                  (format nil "database-create failed: ~A" 
524                          output))
525         t))))
526
527 (defmethod database-destroy (connection-spec (type (eql :postgresql)))
528   (destructuring-bind (host name user password) connection-spec
529     (declare (ignore user password))
530     (multiple-value-bind (output status)
531         (clsql-base-sys:command-output "dropdb -h~A ~A"
532                                        (if host host "localhost")
533                                        name)
534       (if (or (not (zerop status))
535               (search "database removal failed: ERROR:" output))
536           (error 'clsql-access-error
537                  :connection-spec connection-spec
538                  :database-type type
539                  :error 
540                  (format nil "database-destory failed: ~A" 
541                          output))
542         t))))
543
544
545 (defmethod database-probe (connection-spec (type (eql :postgresql)))
546   (when (find (second connection-spec) (database-list connection-spec type)
547               :key #'car :test #'string-equal)
548     t))
549
550 (defmethod database-list (connection-spec (type (eql :postgresql)))
551   (destructuring-bind (host name user password) connection-spec
552     (declare (ignore name))
553     (let ((database (database-connect (list host "template1" user password)
554                                       type)))
555       (unwind-protect
556            (progn
557              (setf (slot-value database 'clsql-base-sys::state) :open)
558              (mapcar #'car (database-query "select datname from pg_database" 
559                                            database nil)))
560         (progn
561           (database-disconnect database)
562           (setf (slot-value database 'clsql-base-sys::state) :closed))))))
563
564 (defmethod database-describe-table ((database postgresql-database) table)
565   (database-query 
566    (format nil "select a.attname, t.typname
567                                from pg_class c, pg_attribute a, pg_type t
568                                where c.relname = '~a'
569                                    and a.attnum > 0
570                                    and a.attrelid = c.oid
571                                    and a.atttypid = t.oid"
572            (sql-escape (string-downcase table)))
573    database :auto))
574
575 (defun %pg-database-connection (connection-spec)
576   (check-connection-spec connection-spec :postgresql
577                          (host db user password &optional port options tty))
578   (macrolet ((coerce-string (var)
579                `(unless (typep ,var 'simple-base-string)
580                  (setf ,var (coerce ,var 'simple-base-string)))))
581     (destructuring-bind (host db user password &optional port options tty)
582         connection-spec
583       (coerce-string db)
584       (coerce-string user)
585       (let ((connection (pqsetdblogin host port options tty db user password)))
586         (declare (type postgresql::pgsql-conn-ptr connection))
587         (unless (eq (pqstatus connection) :connection-ok)
588           ;; Connect failed
589           (error 'clsql-connect-error
590                  :database-type :postgresql
591                  :connection-spec connection-spec
592                  :errno (pqstatus connection)
593                  :error (pqerrormessage connection)))
594         connection))))
595
596 (defmethod database-reconnect ((database postgresql-database))
597   (let ((lock (database-lock database)))
598     (with-process-lock (lock "Reconnecting")
599       (with-slots (connection-spec conn-ptr)
600           database
601         (setf conn-ptr (%pg-database-connection connection-spec))
602         database))))
603
604 ;;; Database capabilities
605
606 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
607   t)
608
609 (defmethod db-type-default-case ((db-type (eql :postgresql)))
610   :lower)
611
612 (when (clsql-base-sys:database-type-library-loaded :postgresql)
613   (clsql-base-sys:initialize-database-type :database-type :postgresql))