r9403: Rework conditions to be CommonSQL backward compatible
[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-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 'sql-connection-error
131                  :database-type database-type
132                  :connection-spec connection-spec
133                  :error-id (PQstatus connection)
134                  :message (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 field-names)
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 'sql-database-data-error
156                  :database database
157                  :expression query-expression
158                  :message (tidy-error-message (PQerrorMessage conn-ptr))))
159         (unwind-protect
160             (case (PQresultStatus result)
161               (#.pgsql-exec-status-type#empty-query
162                nil)
163               (#.pgsql-exec-status-type#tuples-ok
164                (let ((num-fields (PQnfields result)))
165                  (setq result-types
166                    (canonicalize-types result-types num-fields
167                                              result))
168                  (values
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                   (when field-names
179                     (result-field-names num-fields result)))))
180               (t
181                (error 'sql-database-data-error
182                       :database database
183                       :expression query-expression
184                       :error-id (PQresultStatus result)
185                       :message (tidy-error-message
186                                 (PQresultErrorMessage result)))))
187           (PQclear result))))))
188
189 (defun result-field-names (num-fields result)
190   "Return list of result field names."
191   (let ((names '()))
192     (dotimes (i num-fields (nreverse names))
193       (declare (fixnum i))
194       (push (uffi:convert-from-cstring (PQfname result i)) names))))
195
196 (defmethod database-execute-command (sql-expression
197                                      (database postgresql-database))
198   (let ((conn-ptr (database-conn-ptr database)))
199     (declare (type pgsql-conn-def conn-ptr))
200     (uffi:with-cstring (sql-native sql-expression)
201       (let ((result (PQexec conn-ptr sql-native)))
202         (when (uffi:null-pointer-p result)
203           (error 'sql-database-data-error
204                  :database database
205                  :expression sql-expression
206                  :message (tidy-error-message (PQerrorMessage conn-ptr))))
207         (unwind-protect
208             (case (PQresultStatus result)
209               (#.pgsql-exec-status-type#command-ok
210                t)
211               ((#.pgsql-exec-status-type#empty-query
212                 #.pgsql-exec-status-type#tuples-ok)
213                (warn "Strange result...")
214                t)
215               (t
216                (error 'sql-database-data-error
217                       :database database
218                       :expression sql-expression
219                       :error-id (PQresultStatus result)
220                       :message (tidy-error-message
221                                 (PQresultErrorMessage result)))))
222           (PQclear result))))))
223
224 (defstruct postgresql-result-set
225   (res-ptr (uffi:make-null-pointer 'pgsql-result) 
226            :type pgsql-result-def)
227   (types nil) 
228   (num-tuples 0 :type integer)
229   (num-fields 0 :type integer)
230   (tuple-index 0 :type integer))
231
232 (defmethod database-query-result-set ((query-expression string)
233                                       (database postgresql-database) 
234                                       &key full-set result-types)
235   (let ((conn-ptr (database-conn-ptr database)))
236     (declare (type pgsql-conn-def conn-ptr))
237     (uffi:with-cstring (query-native query-expression)
238       (let ((result (PQexec conn-ptr query-native)))
239         (when (uffi:null-pointer-p result)
240           (error 'sql-database-data-error
241                  :database database
242                  :expression query-expression
243                  :message (tidy-error-message (PQerrorMessage conn-ptr))))
244         (case (PQresultStatus result)
245           ((#.pgsql-exec-status-type#empty-query
246             #.pgsql-exec-status-type#tuples-ok)
247            (let ((result-set (make-postgresql-result-set
248                         :res-ptr result
249                         :num-fields (PQnfields result)
250                         :num-tuples (PQntuples result)
251                         :types (canonicalize-types 
252                                       result-types
253                                       (PQnfields result)
254                                       result))))
255              (if full-set
256                  (values result-set
257                          (PQnfields result)
258                          (PQntuples result))
259                  (values result-set
260                          (PQnfields result)))))
261           (t
262            (unwind-protect
263                (error 'sql-database-data-error
264                       :database database
265                       :expression query-expression
266                       :error-id (PQresultStatus result)
267                       :message (tidy-error-message
268                                 (PQresultErrorMessage result)))
269              (PQclear result))))))))
270   
271 (defmethod database-dump-result-set (result-set (database postgresql-database))
272   (let ((res-ptr (postgresql-result-set-res-ptr result-set))) 
273     (declare (type pgsql-result-def res-ptr))
274     (PQclear res-ptr)
275     t))
276
277 (defmethod database-store-next-row (result-set (database postgresql-database) 
278                                     list)
279   (let ((result (postgresql-result-set-res-ptr result-set))
280         (types (postgresql-result-set-types result-set)))
281     (declare (type pgsql-result-def result))
282     (if (>= (postgresql-result-set-tuple-index result-set)
283             (postgresql-result-set-num-tuples result-set))
284         nil
285       (loop with tuple-index = (postgresql-result-set-tuple-index result-set)
286           for i from 0 below (postgresql-result-set-num-fields result-set)
287           for rest on list
288           do
289             (setf (car rest)
290               (if (zerop (PQgetisnull result tuple-index i))
291                   (convert-raw-field
292                    (PQgetvalue result tuple-index i)
293                    types i)
294                 nil))
295           finally
296             (incf (postgresql-result-set-tuple-index result-set))
297             (return list)))))
298
299 ;;; Large objects support (Marc B)
300
301 (defmethod database-create-large-object ((database postgresql-database))
302   (lo-create (database-conn-ptr database)
303              (logior postgresql::+INV_WRITE+ postgresql::+INV_READ+)))
304
305
306 #+mb-original
307 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
308   (let ((ptr (database-conn-ptr database))
309         (length (length data))
310         (result nil)
311         (fd nil))
312     (with-transaction (:database database)
313        (unwind-protect
314           (progn 
315             (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
316             (when (>= fd 0)
317               (when (= (lo-write ptr fd data length) length)
318                 (setf result t))))
319          (progn
320            (when (and fd (>= fd 0))
321              (lo-close ptr fd))
322            )))
323     result))
324
325 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
326   (let ((ptr (database-conn-ptr database))
327         (length (length data))
328         (result nil)
329         (fd nil))
330     (database-execute-command "begin" database)
331     (unwind-protect
332         (progn 
333           (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
334           (when (>= fd 0)
335             (when (= (lo-write ptr fd data length) length)
336               (setf result t))))
337       (progn
338         (when (and fd (>= fd 0))
339           (lo-close ptr fd))
340         (database-execute-command (if result "commit" "rollback") database)))
341     result))
342
343 ;; (MB) the begin/commit/rollback stuff will be removed when with-transaction wil be implemented
344 ;; (KMR) Can't use with-transaction since that function is in high-level code
345 (defmethod database-read-large-object (object-id (database postgresql-database))
346   (let ((ptr (database-conn-ptr database))
347         (buffer nil)
348         (result nil)
349         (length 0)
350         (fd nil))
351     (unwind-protect
352        (progn
353          (database-execute-command "begin" database)
354          (setf fd (lo-open ptr object-id postgresql::+INV_READ+))
355          (when (>= fd 0)
356            (setf length (lo-lseek ptr fd 0 2))
357            (lo-lseek ptr fd 0 0)
358            (when (> length 0)
359              (setf buffer (uffi:allocate-foreign-string 
360                            length :unsigned t))
361              (when (= (lo-read ptr fd buffer length) length)
362                (setf result (uffi:convert-from-foreign-string
363                              buffer :length length :null-terminated-p nil))))))
364       (progn
365         (when buffer (uffi:free-foreign-object buffer))
366         (when (and fd (>= fd 0)) (lo-close ptr fd))
367         (database-execute-command (if result "commit" "rollback") database)))
368     result))
369
370 (defmethod database-delete-large-object (object-id (database postgresql-database))
371   (lo-unlink (database-conn-ptr database) object-id))
372
373
374 ;;; Object listing
375
376 (defun owner-clause (owner)
377   (cond 
378    ((stringp owner)
379     (format
380      nil
381      " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))" 
382      owner))
383    ((null owner)
384     (format nil " AND (NOT (relowner=1))"))
385    (t "")))
386
387 (defun database-list-objects-of-type (database type owner)
388   (mapcar #'car
389           (database-query
390            (format nil
391                    "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
392                    type
393                    (owner-clause owner))
394            database nil nil)))
395
396 (defmethod database-list-tables ((database postgresql-database)
397                                  &key (owner nil))
398   (database-list-objects-of-type database "r" owner))
399   
400 (defmethod database-list-views ((database postgresql-database)
401                                 &key (owner nil))
402   (database-list-objects-of-type database "v" owner))
403   
404 (defmethod database-list-indexes ((database postgresql-database)
405                                   &key (owner nil))
406   (database-list-objects-of-type database "i" owner))
407
408
409 (defmethod database-list-table-indexes (table (database postgresql-database)
410                                         &key (owner nil))
411   (let ((indexrelids
412          (database-query
413           (format 
414            nil
415            "select indexrelid from pg_index where indrelid=(select relfilenode from pg_class where relname='~A'~A)"
416            (string-downcase table)
417            (owner-clause owner))
418           database :auto nil))
419         (result nil))
420     (dolist (indexrelid indexrelids (nreverse result))
421       (push 
422        (caar (database-query
423               (format nil "select relname from pg_class where relfilenode='~A'"
424                       (car indexrelid))
425               database nil nil))
426        result))))
427
428 (defmethod database-list-attributes ((table string)
429                                      (database postgresql-database)
430                                      &key (owner nil))
431   (let* ((owner-clause
432           (cond ((stringp owner)
433                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
434                 ((null owner) " AND (not (relowner=1))")
435                 (t "")))
436          (result
437           (mapcar #'car
438                   (database-query
439                    (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
440                            (string-downcase table)
441                            owner-clause)
442                    database nil nil))))
443     (if result
444         (remove-if #'(lambda (it) (member it '("cmin"
445                                                "cmax"
446                                                "xmax"
447                                                "xmin"
448                                                "oid"
449                                                "ctid"
450                                                ;; kmr -- added tableoid
451                                                "tableoid") :test #'equal)) 
452                    result))))
453
454 (defmethod database-attribute-type (attribute (table string)
455                                     (database postgresql-database)
456                                     &key (owner nil))
457   (let ((row (car (database-query
458                    (format nil "SELECT pg_type.typname,pg_attribute.attlen,pg_attribute.atttypmod,pg_attribute.attnotnull 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"
459                            (string-downcase table)
460                            (string-downcase attribute)
461                            (owner-clause owner))
462                    database nil nil))))
463     (when row
464       (values
465        (ensure-keyword (first row))
466        (if (string= "-1" (second row))
467            (- (parse-integer (third row) :junk-allowed t) 4)
468          (parse-integer (second row)))
469        nil
470        (if (string-equal "f" (fourth row))
471            1
472          0)))))
473
474 (defmethod database-create-sequence (sequence-name
475                                      (database postgresql-database))
476   (database-execute-command
477    (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
478    database))
479
480 (defmethod database-drop-sequence (sequence-name
481                                    (database postgresql-database))
482   (database-execute-command
483    (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
484
485 (defmethod database-list-sequences ((database postgresql-database)
486                                     &key (owner nil))
487   (database-list-objects-of-type database "S" owner))
488
489 (defmethod database-set-sequence-position (name (position integer)
490                                                 (database postgresql-database))
491   (values
492    (parse-integer
493     (caar
494      (database-query
495       (format nil "SELECT SETVAL ('~A', ~A)" name position)
496       database nil nil)))))
497
498 (defmethod database-sequence-next (sequence-name 
499                                    (database postgresql-database))
500   (values
501    (parse-integer
502     (caar
503      (database-query
504       (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
505       database nil nil)))))
506
507 (defmethod database-sequence-last (sequence-name (database postgresql-database))
508   (values
509    (parse-integer
510     (caar
511      (database-query
512       (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
513       database nil nil)))))
514   
515 (defmethod database-create (connection-spec (type (eql :postgresql)))
516   (destructuring-bind (host name user password) connection-spec
517     (declare (ignore user password))
518     (multiple-value-bind (output status)
519         (clsql-sys:command-output "createdb -h~A ~A"
520                                        (if host host "localhost")
521                                        name)
522       (if (or (not (zerop status))
523               (search "database creation failed: ERROR:" output))
524           (error 'sql-database-error
525                  :message
526                  (format nil "createdb failed for postgresql backend with connection spec ~A."
527                          connection-spec))
528         t))))
529
530 (defmethod database-destroy (connection-spec (type (eql :postgresql)))
531   (destructuring-bind (host name user password) connection-spec
532     (declare (ignore user password))
533     (multiple-value-bind (output status)
534         (clsql-sys:command-output "dropdb -h~A ~A"
535                                        (if host host "localhost")
536                                        name)
537       (if (or (not (zerop status))
538               (search "database removal failed: ERROR:" output))
539           (error 'sql-database-error
540                  :message
541                  (format nil "dropdb failed for postgresql backend with connection spec ~A."
542                          connection-spec))
543         t))))
544
545
546 (defmethod database-probe (connection-spec (type (eql :postgresql)))
547   (when (find (second connection-spec) (database-list connection-spec type)
548               :key #'car :test #'string-equal)
549     t))
550
551 (defmethod database-list (connection-spec (type (eql :postgresql)))
552   (destructuring-bind (host name user password) connection-spec
553     (declare (ignore name))
554     (let ((database (database-connect (list host "template1" user password)
555                                       type)))
556       (unwind-protect
557            (progn
558              (setf (slot-value database 'clsql-sys::state) :open)
559              (mapcar #'car (database-query "select datname from pg_database" 
560                                            database nil nil)))
561         (progn
562           (database-disconnect database)
563           (setf (slot-value database 'clsql-sys::state) :closed))))))
564
565 (defmethod database-describe-table ((database postgresql-database) table)
566   (database-query 
567    (format nil "select a.attname, t.typname
568                                from pg_class c, pg_attribute a, pg_type t
569                                where c.relname = '~a'
570                                    and a.attnum > 0
571                                    and a.attrelid = c.oid
572                                    and a.atttypid = t.oid"
573            (sql-escape (string-downcase table)))
574    database :auto nil))
575
576 (defun %pg-database-connection (connection-spec)
577   (check-connection-spec connection-spec :postgresql
578                          (host db user password &optional port options tty))
579   (macrolet ((coerce-string (var)
580                `(unless (typep ,var 'simple-base-string)
581                  (setf ,var (coerce ,var 'simple-base-string)))))
582     (destructuring-bind (host db user password &optional port options tty)
583         connection-spec
584       (coerce-string db)
585       (coerce-string user)
586       (let ((connection (PQsetdbLogin host port options tty db user password)))
587         (declare (type postgresql::pgsql-conn-ptr connection))
588         (unless (eq (PQstatus connection) :connection-ok)
589           ;; Connect failed
590           (error 'sql-connection-error
591                  :database-type :postgresql
592                  :connection-spec connection-spec
593                  :error-id (PQstatus connection)
594                  :message (PQerrorMessage connection)))
595         connection))))
596
597 (defmethod database-reconnect ((database postgresql-database))
598   (let ((lock (database-lock database)))
599     (with-process-lock (lock "Reconnecting")
600       (with-slots (connection-spec conn-ptr)
601           database
602         (setf conn-ptr (%pg-database-connection connection-spec))
603         database))))
604
605 ;;; Database capabilities
606
607 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
608   t)
609
610 (defmethod db-type-default-case ((db-type (eql :postgresql)))
611   :lower)
612
613 (when (clsql-sys:database-type-library-loaded :postgresql)
614   (clsql-sys:initialize-database-type :database-type :postgresql))