Further internationalization.
[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 ;;;; CLSQL users are granted the rights to distribute and use this software
10 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
11 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
12 ;;;; *************************************************************************
13
14 (in-package #:cl-user)
15
16 (defpackage #:clsql-postgresql
17     (:use #:common-lisp #:clsql-sys #:pgsql #:clsql-uffi)
18     (:export #:postgresql-database)
19     (:documentation "This is the CLSQL interface to PostgreSQL."))
20
21 (in-package #:clsql-postgresql)
22
23 ;;; Field conversion functions
24
25 (defun make-type-list-for-auto (num-fields res-ptr)
26   (let ((new-types '()))
27     (dotimes (i num-fields)
28       (declare (fixnum i))
29       (let* ((type (PQftype res-ptr i)))
30         (push
31          (case type
32            ((#.pgsql-ftype#bytea
33              #.pgsql-ftype#int2
34              #.pgsql-ftype#int4)
35             :int32)
36            (#.pgsql-ftype#int8
37             :int64)
38            ((#.pgsql-ftype#float4
39              #.pgsql-ftype#float8)
40             :double)
41            (otherwise
42             t))
43          new-types)))
44       (nreverse new-types)))
45
46 (defun canonicalize-types (types num-fields res-ptr)
47   (if (null types)
48       nil
49       (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
50         (cond
51           ((listp types)
52            (canonicalize-type-list types auto-list))
53           ((eq types :auto)
54            auto-list)
55           (t
56            nil)))))
57
58 (defun tidy-error-message (message &optional encoding)
59   (unless (stringp message)
60     (setq message (uffi:convert-from-foreign-string message :encoding encoding)))
61   (let ((message (string-right-trim '(#\Return #\Newline) message)))
62     (cond
63       ((< (length message) (length "ERROR:"))
64        message)
65       ((string= message "ERROR:" :end1 6)
66        (string-left-trim '(#\Space) (subseq message 6)))
67       (t
68        message))))
69
70 (defmethod database-initialize-database-type ((database-type
71                                                (eql :postgresql)))
72   t)
73
74 (uffi:def-type pgsql-conn-def pgsql-conn)
75 (uffi:def-type pgsql-result-def pgsql-result)
76
77
78 (defclass postgresql-database (generic-postgresql-database)
79   ((conn-ptr :accessor database-conn-ptr :initarg :conn-ptr
80              :type pgsql-conn-def)
81    (lock
82     :accessor database-lock
83     :initform (make-process-lock "conn"))))
84
85 (defmethod database-type ((database postgresql-database))
86   :postgresql)
87
88 (defmethod database-name-from-spec (connection-spec (database-type
89                                                      (eql :postgresql)))
90   (check-connection-spec connection-spec database-type
91                          (host db user password &optional port options tty))
92   (destructuring-bind (host db user password &optional port options tty)
93       connection-spec
94     (declare (ignore password options tty))
95     (concatenate 'string
96       (etypecase host
97         (null "localhost")
98         (pathname (namestring host))
99         (string host))
100       (when port
101         (concatenate 'string
102                      ":"
103                      (etypecase port
104                        (integer (write-to-string port))
105                        (string port))))
106       "/" db "/" user)))
107
108
109 (defmethod database-connect (connection-spec (database-type (eql :postgresql)))
110   (check-connection-spec connection-spec database-type
111                          (host db user password &optional port options tty))
112   (destructuring-bind (host db user password &optional port options tty)
113       connection-spec
114     (uffi:with-cstrings ((host-native host)
115                          (user-native user)
116                          (password-native password)
117                          (db-native db)
118                          (port-native port)
119                          (options-native options)
120                          (tty-native tty))
121       (let ((connection (PQsetdbLogin host-native port-native
122                                       options-native tty-native
123                                       db-native user-native
124                                       password-native)))
125         (declare (type pgsql-conn-def connection))
126         (when (not (eq (PQstatus connection)
127                        pgsql-conn-status-type#connection-ok))
128           (let ((pqstatus (PQstatus connection))
129                 (pqmessage (tidy-error-message (PQerrorMessage connection))))
130             (PQfinish connection)
131             (error 'sql-connection-error
132                    :database-type database-type
133                    :connection-spec connection-spec
134                    :error-id pqstatus
135                    :message  pqmessage)))
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) (encoding database))))
159         (unwind-protect
160             (case (PQresultStatus result)
161               ;; User gave a command rather than a query
162               (#.pgsql-exec-status-type#command-ok
163                nil)
164               (#.pgsql-exec-status-type#empty-query
165                nil)
166               (#.pgsql-exec-status-type#tuples-ok
167                (let ((num-fields (PQnfields result)))
168                  (when result-types
169                    (setq result-types
170                      (canonicalize-types result-types num-fields
171                                          result)))
172                  (let ((res (loop for tuple-index from 0 below (PQntuples result)
173                                 collect
174                                   (loop for i from 0 below num-fields
175                                       collect
176                                         (if (zerop (PQgetisnull result tuple-index i))
177                                             (convert-raw-field
178                                              (PQgetvalue result tuple-index i)
179                                              (nth i result-types)
180                                              :encoding (encoding database))
181                                           nil)))))
182                    (if field-names
183                        (values res (result-field-names num-fields result))
184                      res))))
185               (t
186                (error 'sql-database-data-error
187                       :database database
188                       :expression query-expression
189                       :error-id (PQresultErrorField result +PG-DIAG-SQLSTATE+)
190                       :message (tidy-error-message
191                                 (PQresultErrorMessage result)
192                                 (encoding database)))))
193           (PQclear result))))))
194
195 (defun result-field-names (num-fields result)
196   "Return list of result field names."
197   (let ((names '()))
198     (dotimes (i num-fields (nreverse names))
199       (declare (fixnum i))
200       (push (uffi:convert-from-cstring (PQfname result i)) names))))
201
202 (defmethod database-execute-command (sql-expression
203                                      (database postgresql-database))
204   (let ((conn-ptr (database-conn-ptr database)))
205     (declare (type pgsql-conn-def conn-ptr))
206     (uffi:with-cstring (sql-native sql-expression)
207       (let ((result (PQexec conn-ptr sql-native)))
208         (when (uffi:null-pointer-p result)
209           (error 'sql-database-data-error
210                  :database database
211                  :expression sql-expression
212                  :message (tidy-error-message (PQerrorMessage conn-ptr)
213                                               (encoding databse))))
214         (unwind-protect
215             (case (PQresultStatus result)
216               (#.pgsql-exec-status-type#command-ok
217                t)
218               ((#.pgsql-exec-status-type#empty-query
219                 #.pgsql-exec-status-type#tuples-ok)
220                (warn "Strange result...")
221                t)
222               (t
223                (error 'sql-database-data-error
224                       :database database
225                       :expression sql-expression
226                       :error-id (PQresultErrorField result +PG-DIAG-SQLSTATE+)
227                       :message (tidy-error-message
228                                 (PQresultErrorMessage result)
229                                 (encoding database)))))
230           (PQclear result))))))
231
232 (defstruct postgresql-result-set
233   (res-ptr (uffi:make-null-pointer 'pgsql-result)
234            :type pgsql-result-def)
235   (types nil)
236   (num-tuples 0 :type integer)
237   (num-fields 0 :type integer)
238   (tuple-index 0 :type integer))
239
240 (defmethod database-query-result-set ((query-expression string)
241                                       (database postgresql-database)
242                                       &key full-set result-types)
243   (let ((conn-ptr (database-conn-ptr database)))
244     (declare (type pgsql-conn-def conn-ptr))
245     (uffi:with-cstring (query-native query-expression)
246       (let ((result (PQexec conn-ptr query-native)))
247         (when (uffi:null-pointer-p result)
248           (error 'sql-database-data-error
249                  :database database
250                  :expression query-expression
251                  :message (tidy-error-message (PQerrorMessage conn-ptr)
252                                               (encoding database))))
253         (case (PQresultStatus result)
254           ((#.pgsql-exec-status-type#empty-query
255             #.pgsql-exec-status-type#tuples-ok)
256            (let ((result-set (make-postgresql-result-set
257                         :res-ptr result
258                         :num-fields (PQnfields result)
259                         :num-tuples (PQntuples result)
260                         :types (canonicalize-types
261                                       result-types
262                                       (PQnfields result)
263                                       result))))
264              (if full-set
265                  (values result-set
266                          (PQnfields result)
267                          (PQntuples result))
268                  (values result-set
269                          (PQnfields result)))))
270           (t
271            (unwind-protect
272                (error 'sql-database-data-error
273                       :database database
274                       :expression query-expression
275                       :error-id (PQresultErrorField result +PG-DIAG-SQLSTATE+)
276                       :message (tidy-error-message
277                                 (PQresultErrorMessage result)
278                                 (encoding database)))
279              (PQclear result))))))))
280
281 (defmethod database-dump-result-set (result-set (database postgresql-database))
282   (let ((res-ptr (postgresql-result-set-res-ptr result-set)))
283     (declare (type pgsql-result-def res-ptr))
284     (PQclear res-ptr)
285     t))
286
287 (defmethod database-store-next-row (result-set (database postgresql-database)
288                                     list)
289   (let ((result (postgresql-result-set-res-ptr result-set))
290         (types (postgresql-result-set-types result-set)))
291     (declare (type pgsql-result-def result))
292     (if (>= (postgresql-result-set-tuple-index result-set)
293             (postgresql-result-set-num-tuples result-set))
294         nil
295       (loop with tuple-index = (postgresql-result-set-tuple-index result-set)
296           for i from 0 below (postgresql-result-set-num-fields result-set)
297           for rest on list
298           do
299             (setf (car rest)
300               (if (zerop (PQgetisnull result tuple-index i))
301                   (convert-raw-field
302                    (PQgetvalue result tuple-index i)
303                    (nth i types)
304                    :encoding (encoding database))
305                 nil))
306           finally
307             (incf (postgresql-result-set-tuple-index result-set))
308             (return list)))))
309
310 ;;; Large objects support (Marc B)
311
312 (defmethod database-create-large-object ((database postgresql-database))
313   (lo-create (database-conn-ptr database)
314              (logior pgsql::+INV_WRITE+ pgsql::+INV_READ+)))
315
316
317 #+mb-original
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     (with-transaction (:database database)
324        (unwind-protect
325           (progn
326             (setf fd (lo-open ptr object-id pgsql::+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            )))
334     result))
335
336 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
337   (let ((ptr (database-conn-ptr database))
338         (length (length data))
339         (result nil)
340         (fd nil))
341     (database-execute-command "begin" database)
342     (unwind-protect
343         (progn
344           (setf fd (lo-open ptr object-id pgsql::+INV_WRITE+))
345           (when (>= fd 0)
346             (when (= (lo-write ptr fd data length) length)
347               (setf result t))))
348       (progn
349         (when (and fd (>= fd 0))
350           (lo-close ptr fd))
351         (database-execute-command (if result "commit" "rollback") database)))
352     result))
353
354 ;; (MB) the begin/commit/rollback stuff will be removed when with-transaction wil be implemented
355 ;; (KMR) Can't use with-transaction since that function is in high-level code
356 (defmethod database-read-large-object (object-id (database postgresql-database))
357   (let ((ptr (database-conn-ptr database))
358         (buffer nil)
359         (result nil)
360         (length 0)
361         (fd nil))
362     (unwind-protect
363        (progn
364          (database-execute-command "begin" database)
365          (setf fd (lo-open ptr object-id pgsql::+INV_READ+))
366          (when (>= fd 0)
367            (setf length (lo-lseek ptr fd 0 2))
368            (lo-lseek ptr fd 0 0)
369            (when (> length 0)
370              (setf buffer (uffi:allocate-foreign-string
371                            length :unsigned t))
372              (when (= (lo-read ptr fd buffer length) length)
373                (setf result (uffi:convert-from-foreign-string
374                              buffer :length length :null-terminated-p nil
375                              :encoding (encoding database)))))))
376       (progn
377         (when buffer (uffi:free-foreign-object buffer))
378         (when (and fd (>= fd 0)) (lo-close ptr fd))
379         (database-execute-command (if result "commit" "rollback") database)))
380     result))
381
382 (defmethod database-delete-large-object (object-id (database postgresql-database))
383   (lo-unlink (database-conn-ptr database) object-id))
384
385
386 ;;; Object listing
387
388
389
390 (defmethod database-create (connection-spec (type (eql :postgresql)))
391   (destructuring-bind (host name user password) connection-spec
392     (let ((database (database-connect (list host "postgres" user password)
393                                       type)))
394       (setf (slot-value database 'clsql-sys::state) :open)
395       (unwind-protect
396            (database-execute-command (format nil "create database ~A" name) database)
397         (database-disconnect database)))))
398
399 (defmethod database-destroy (connection-spec (type (eql :postgresql)))
400   (destructuring-bind (host name user password) connection-spec
401     (let ((database (database-connect (list host "postgres" user password)
402                                       type)))
403       (setf (slot-value database 'clsql-sys::state) :open)
404       (unwind-protect
405            (database-execute-command (format nil "drop database ~A" name) database)
406         (database-disconnect database)))))
407
408
409 (defmethod database-probe (connection-spec (type (eql :postgresql)))
410   (when (find (second connection-spec) (database-list connection-spec type)
411               :test #'string-equal)
412     t))
413
414
415 (defun %pg-database-connection (connection-spec)
416   (check-connection-spec connection-spec :postgresql
417                          (host db user password &optional port options tty))
418   (macrolet ((coerce-string (var)
419                `(unless (typep ,var 'simple-base-string)
420                  (setf ,var (coerce ,var 'simple-base-string)))))
421     (destructuring-bind (host db user password &optional port options tty)
422         connection-spec
423       (coerce-string db)
424       (coerce-string user)
425       (let ((connection (PQsetdbLogin host port options tty db user password)))
426         (declare (type pgsql::pgsql-conn-ptr connection))
427         (unless (eq (PQstatus connection)
428                     pgsql-conn-status-type#connection-ok)
429           ;; Connect failed
430           (error 'sql-connection-error
431                  :database-type :postgresql
432                  :connection-spec connection-spec
433                  :error-id (PQstatus connection)
434                  :message (PQerrorMessage connection)))
435         connection))))
436
437 (defmethod database-reconnect ((database postgresql-database))
438   (let ((lock (database-lock database)))
439     (with-process-lock (lock "Reconnecting")
440       (with-slots (connection-spec conn-ptr)
441           database
442         (setf conn-ptr (%pg-database-connection connection-spec))
443         database))))
444
445 ;;; Database capabilities
446
447 (when (clsql-sys:database-type-library-loaded :postgresql)
448   (clsql-sys:initialize-database-type :database-type :postgresql))