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