r8885: pre 2.3.4
[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
84 (defmethod database-type ((database postgresql-database))
85   :postgresql)
86
87 (defmethod database-name-from-spec (connection-spec (database-type
88                                                      (eql :postgresql)))
89   (check-connection-spec connection-spec database-type
90                          (host db user password &optional port options tty))
91   (destructuring-bind (host db user password &optional port options tty)
92       connection-spec
93     (declare (ignore password options tty))
94     (concatenate 'string 
95       (etypecase host
96         (pathname (namestring host))
97         (string host))
98       (when port 
99         (concatenate 'string
100                      ":"
101                      (etypecase port
102                        (integer (write-to-string port))
103                        (string port))))
104       "/" db "/" user)))
105
106
107 (defmethod database-connect (connection-spec (database-type (eql :postgresql)))
108   (check-connection-spec connection-spec database-type
109                          (host db user password &optional port options tty))
110   (destructuring-bind (host db user password &optional port options tty)
111       connection-spec
112     (uffi:with-cstrings ((host-native host)
113                          (user-native user)
114                          (password-native password)
115                          (db-native db)
116                          (port-native port)
117                          (options-native options)
118                          (tty-native tty))
119       (let ((connection (PQsetdbLogin host-native port-native
120                                       options-native tty-native
121                                       db-native user-native
122                                       password-native)))
123         (declare (type pgsql-conn-def connection))
124         (when (not (eq (PQstatus connection) 
125                        pgsql-conn-status-type#connection-ok))
126           (error 'clsql-connect-error
127                  :database-type database-type
128                  :connection-spec connection-spec
129                  :errno (PQstatus connection)
130                  :error (tidy-error-message 
131                          (PQerrorMessage connection))))
132         (make-instance 'postgresql-database
133                        :name (database-name-from-spec connection-spec
134                                                       database-type)
135                        :connection-spec connection-spec
136                        :conn-ptr connection)))))
137
138
139 (defmethod database-disconnect ((database postgresql-database))
140   (PQfinish (database-conn-ptr database))
141   (setf (database-conn-ptr database) nil)
142   t)
143
144 (defmethod database-query (query-expression (database postgresql-database) result-types)
145   (let ((conn-ptr (database-conn-ptr database)))
146     (declare (type pgsql-conn-def conn-ptr))
147     (uffi:with-cstring (query-native query-expression)
148       (let ((result (PQexec conn-ptr query-native)))
149         (when (uffi:null-pointer-p result)
150           (error 'clsql-sql-error
151                  :database database
152                  :expression query-expression
153                  :errno nil
154                  :error (tidy-error-message (PQerrorMessage conn-ptr))))
155         (unwind-protect
156             (case (PQresultStatus result)
157               (#.pgsql-exec-status-type#empty-query
158                nil)
159               (#.pgsql-exec-status-type#tuples-ok
160                (let ((num-fields (PQnfields result)))
161                  (setq result-types
162                    (canonicalize-types result-types num-fields
163                                              result))
164                  (loop for tuple-index from 0 below (PQntuples result)
165                        collect
166                        (loop for i from 0 below num-fields
167                              collect
168                              (if (zerop (PQgetisnull result tuple-index i))
169                                  (convert-raw-field
170                                   (PQgetvalue result tuple-index i)
171                                   result-types i)
172                                  nil)))))
173               (t
174                (error 'clsql-sql-error
175                       :database database
176                       :expression query-expression
177                       :errno (PQresultStatus result)
178                       :error (tidy-error-message
179                               (PQresultErrorMessage result)))))
180           (PQclear result))))))
181
182 (defmethod database-execute-command (sql-expression
183                                      (database postgresql-database))
184   (let ((conn-ptr (database-conn-ptr database)))
185     (declare (type pgsql-conn-def conn-ptr))
186     (uffi:with-cstring (sql-native sql-expression)
187       (let ((result (PQexec conn-ptr sql-native)))
188         (when (uffi:null-pointer-p result)
189           (error 'clsql-sql-error
190                  :database database
191                  :expression sql-expression
192                  :errno nil
193                  :error (tidy-error-message (PQerrorMessage conn-ptr))))
194         (unwind-protect
195             (case (PQresultStatus result)
196               (#.pgsql-exec-status-type#command-ok
197                t)
198               ((#.pgsql-exec-status-type#empty-query
199                 #.pgsql-exec-status-type#tuples-ok)
200                (warn "Strange result...")
201                t)
202               (t
203                (error 'clsql-sql-error
204                       :database database
205                       :expression sql-expression
206                       :errno (PQresultStatus result)
207                       :error (tidy-error-message
208                               (PQresultErrorMessage result)))))
209           (PQclear result))))))
210
211 (defstruct postgresql-result-set
212   (res-ptr (uffi:make-null-pointer 'pgsql-result) 
213            :type pgsql-result-def)
214   (types nil) 
215   (num-tuples 0 :type integer)
216   (num-fields 0 :type integer)
217   (tuple-index 0 :type integer))
218
219 (defmethod database-query-result-set ((query-expression string)
220                                       (database postgresql-database) 
221                                       &key full-set result-types)
222   (let ((conn-ptr (database-conn-ptr database)))
223     (declare (type pgsql-conn-def conn-ptr))
224     (uffi:with-cstring (query-native query-expression)
225       (let ((result (PQexec conn-ptr query-native)))
226         (when (uffi:null-pointer-p result)
227           (error 'clsql-sql-error
228                  :database database
229                  :expression query-expression
230                  :errno nil
231                  :error (tidy-error-message (PQerrorMessage conn-ptr))))
232         (case (PQresultStatus result)
233           ((#.pgsql-exec-status-type#empty-query
234             #.pgsql-exec-status-type#tuples-ok)
235            (let ((result-set (make-postgresql-result-set
236                         :res-ptr result
237                         :num-fields (PQnfields result)
238                         :num-tuples (PQntuples result)
239                         :types (canonicalize-types 
240                                       result-types
241                                       (PQnfields result)
242                                       result))))
243              (if full-set
244                  (values result-set
245                          (PQnfields result)
246                          (PQntuples result))
247                  (values result-set
248                          (PQnfields result)))))
249           (t
250            (unwind-protect
251                (error 'clsql-sql-error
252                       :database database
253                       :expression query-expression
254                       :errno (PQresultStatus result)
255                       :error (tidy-error-message
256                               (PQresultErrorMessage result)))
257              (PQclear result))))))))
258   
259 (defmethod database-dump-result-set (result-set (database postgresql-database))
260   (let ((res-ptr (postgresql-result-set-res-ptr result-set))) 
261     (declare (type pgsql-result-def res-ptr))
262     (PQclear res-ptr)
263     t))
264
265 (defmethod database-store-next-row (result-set (database postgresql-database) 
266                                     list)
267   (let ((result (postgresql-result-set-res-ptr result-set))
268         (types (postgresql-result-set-types result-set)))
269     (declare (type pgsql-result-def result))
270     (if (>= (postgresql-result-set-tuple-index result-set)
271             (postgresql-result-set-num-tuples result-set))
272         nil
273       (loop with tuple-index = (postgresql-result-set-tuple-index result-set)
274           for i from 0 below (postgresql-result-set-num-fields result-set)
275           for rest on list
276           do
277             (setf (car rest)
278               (if (zerop (PQgetisnull result tuple-index i))
279                   (convert-raw-field
280                    (PQgetvalue result tuple-index i)
281                    types i)
282                 nil))
283           finally
284             (incf (postgresql-result-set-tuple-index result-set))
285             (return list)))))
286
287 ;;; Large objects support (Marc B)
288
289 (defmethod database-create-large-object ((database postgresql-database))
290   (lo-create (database-conn-ptr database)
291              (logior postgresql::+INV_WRITE+ postgresql::+INV_READ+)))
292
293
294 #+mb-original
295 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
296   (let ((ptr (database-conn-ptr database))
297         (length (length data))
298         (result nil)
299         (fd nil))
300     (with-transaction (:database database)
301        (unwind-protect
302           (progn 
303             (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
304             (when (>= fd 0)
305               (when (= (lo-write ptr fd data length) length)
306                 (setf result t))))
307          (progn
308            (when (and fd (>= fd 0))
309              (lo-close ptr fd))
310            )))
311     result))
312
313 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
314   (let ((ptr (database-conn-ptr database))
315         (length (length data))
316         (result nil)
317         (fd nil))
318     (database-execute-command "begin" database)
319     (unwind-protect
320         (progn 
321           (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+))
322           (when (>= fd 0)
323             (when (= (lo-write ptr fd data length) length)
324               (setf result t))))
325       (progn
326         (when (and fd (>= fd 0))
327           (lo-close ptr fd))
328         (database-execute-command (if result "commit" "rollback") database)))
329     result))
330
331 ;; (MB) the begin/commit/rollback stuff will be removed when with-transaction wil be implemented
332 ;; (KMR) Can't use with-transaction since that function is in high-level code
333 (defmethod database-read-large-object (object-id (database postgresql-database))
334   (let ((ptr (database-conn-ptr database))
335         (buffer nil)
336         (result nil)
337         (length 0)
338         (fd nil))
339     (unwind-protect
340        (progn
341          (database-execute-command "begin" database)
342          (setf fd (lo-open ptr object-id postgresql::+INV_READ+))
343          (when (>= fd 0)
344            (setf length (lo-lseek ptr fd 0 2))
345            (lo-lseek ptr fd 0 0)
346            (when (> length 0)
347              (setf buffer (uffi:allocate-foreign-string 
348                            length :unsigned t))
349              (when (= (lo-read ptr fd buffer length) length)
350                (setf result (uffi:convert-from-foreign-string
351                              buffer :length length :null-terminated-p nil))))))
352       (progn
353         (when buffer (uffi:free-foreign-object buffer))
354         (when (and fd (>= fd 0)) (lo-close ptr fd))
355         (database-execute-command (if result "commit" "rollback") database)))
356     result))
357
358 (defmethod database-delete-large-object (object-id (database postgresql-database))
359   (lo-unlink (database-conn-ptr database) object-id))
360
361
362 ;;; Object listing
363
364 (defmethod database-list-objects-of-type ((database postgresql-database)
365                                           type owner)
366   (let ((owner-clause
367          (cond ((stringp owner)
368                 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))" owner))
369                ((null owner)
370                 (format nil " AND (NOT (relowner=1))"))
371                (t ""))))
372     (mapcar #'car
373             (database-query
374              (format nil
375                      "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
376                      type
377                      owner-clause)
378              database nil))))
379     
380 (defmethod database-list-tables ((database postgresql-database)
381                                  &key (owner nil))
382   (database-list-objects-of-type database "r" owner))
383   
384 (defmethod database-list-views ((database postgresql-database)
385                                 &key (owner nil))
386   (database-list-objects-of-type database "v" owner))
387   
388 (defmethod database-list-indexes ((database postgresql-database)
389                                   &key (owner nil))
390   (database-list-objects-of-type database "i" owner))
391   
392 (defmethod database-list-attributes ((table string)
393                                      (database postgresql-database)
394                                      &key (owner nil))
395   (let* ((owner-clause
396           (cond ((stringp owner)
397                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
398                 ((null owner) " AND (not (relowner=1))")
399                 (t "")))
400          (result
401           (mapcar #'car
402                   (database-query
403                    (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
404                            (string-downcase table)
405                            owner-clause)
406                    database nil))))
407     (if result
408         (reverse
409          (remove-if #'(lambda (it) (member it '("cmin"
410                                                 "cmax"
411                                                 "xmax"
412                                                 "xmin"
413                                                 "oid"
414                                                 "ctid"
415                                                 ;; kmr -- added tableoid
416                                                 "tableoid") :test #'equal)) 
417                     result)))))
418
419 (defmethod database-attribute-type (attribute (table string)
420                                     (database postgresql-database)
421                                     &key (owner nil))
422   (let* ((owner-clause
423           (cond ((stringp owner)
424                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
425                 ((null owner) " AND (not (relowner=1))")
426                 (t "")))
427          (result
428           (mapcar #'car
429                   (database-query
430                    (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"
431                            (string-downcase table)
432                            (string-downcase attribute)
433                            owner-clause)
434                    database nil))))
435     (when result
436       (intern (string-upcase (car result)) :keyword))))
437
438 (defmethod database-create-sequence (sequence-name
439                                      (database postgresql-database))
440   (database-execute-command
441    (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
442    database))
443
444 (defmethod database-drop-sequence (sequence-name
445                                    (database postgresql-database))
446   (database-execute-command
447    (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
448
449 (defmethod database-list-sequences ((database postgresql-database)
450                                     &key (owner nil))
451   (database-list-objects-of-type database "S" owner))
452
453 (defmethod database-set-sequence-position (name (position integer)
454                                                 (database postgresql-database))
455   (values
456    (parse-integer
457     (caar
458      (database-query
459       (format nil "SELECT SETVAL ('~A', ~A)" name position)
460       database nil)))))
461
462 (defmethod database-sequence-next (sequence-name 
463                                    (database postgresql-database))
464   (values
465    (parse-integer
466     (caar
467      (database-query
468       (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
469       database nil)))))
470
471 (defmethod database-sequence-last (sequence-name (database postgresql-database))
472   (values
473    (parse-integer
474     (caar
475      (database-query
476       (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
477       database nil)))))
478   
479
480 ;; Functions depending upon high-level CommonSQL classes/functions
481 #|
482 (defmethod database-output-sql ((expr clsql-sys::sql-typecast-exp) 
483                                 (database postgresql-database))
484   (with-slots (clsql-sys::modifier clsql-sys::components)
485     expr
486     (if clsql-sys::modifier
487         (progn
488           (clsql-sys::output-sql clsql-sys::components database)
489           (write-char #\: clsql-sys::*sql-stream*)
490           (write-char #\: clsql-sys::*sql-stream*)
491           (write-string (symbol-name clsql-sys::modifier) 
492                         clsql-sys::*sql-stream*)))))
493
494 (defmethod database-output-sql-as-type ((type (eql 'integer)) val
495                                         (database postgresql-database))
496   (when val   ;; typecast it so it uses the indexes
497     (make-instance 'clsql-sys::sql-typecast-exp
498                    :modifier 'int8
499                    :components val)))
500 |#
501
502 (when (clsql-base-sys:database-type-library-loaded :postgresql)
503   (clsql-base-sys:initialize-database-type :database-type :postgresql))