1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: postgresql-sql.lisp
6 ;;;; Purpose: High-level PostgreSQL interface using UFFI
7 ;;;; Date Started: Feb 2002
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 ;;;; *************************************************************************
14 (in-package #:cl-user)
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."))
21 (in-package #:clsql-postgresql)
23 ;;; Field conversion functions
25 (defun make-type-list-for-auto (num-fields res-ptr)
26 (let ((new-types '()))
27 (dotimes (i num-fields)
29 (let* ((type (PQftype res-ptr i)))
38 ((#.pgsql-ftype#float4
44 (nreverse new-types)))
46 (defun canonicalize-types (types num-fields res-ptr)
49 (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
52 (canonicalize-type-list types auto-list))
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)))
63 ((< (length message) (length "ERROR:"))
65 ((string= message "ERROR:" :end1 6)
66 (string-left-trim '(#\Space) (subseq message 6)))
70 (defmethod database-initialize-database-type ((database-type
74 (uffi:def-type pgsql-conn-def pgsql-conn)
75 (uffi:def-type pgsql-result-def pgsql-result)
78 (defclass postgresql-database (generic-postgresql-database)
79 ((conn-ptr :accessor database-conn-ptr :initarg :conn-ptr
82 :accessor database-lock
83 :initform (make-process-lock "conn"))))
85 (defmethod database-type ((database postgresql-database))
88 (defmethod database-name-from-spec (connection-spec (database-type
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)
94 (declare (ignore password options tty))
98 (pathname (namestring host))
104 (integer (write-to-string port))
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)
114 (uffi:with-cstrings ((host-native host)
116 (password-native password)
119 (options-native options)
121 (let ((connection (PQsetdbLogin host-native port-native
122 options-native tty-native
123 db-native user-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
135 :message pqmessage)))
136 (make-instance 'postgresql-database
137 :name (database-name-from-spec connection-spec
139 :database-type :postgresql
140 :connection-spec connection-spec
141 :conn-ptr connection)))))
144 (defmethod database-disconnect ((database postgresql-database))
145 (PQfinish (database-conn-ptr database))
146 (setf (database-conn-ptr database) nil)
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
157 :expression query-expression
158 :message (tidy-error-message (PQerrorMessage conn-ptr) (encoding database))))
160 (case (PQresultStatus result)
161 ;; User gave a command rather than a query
162 (#.pgsql-exec-status-type#command-ok
164 (#.pgsql-exec-status-type#empty-query
166 (#.pgsql-exec-status-type#tuples-ok
167 (let ((num-fields (PQnfields result)))
170 (canonicalize-types result-types num-fields
172 (let ((res (loop for tuple-index from 0 below (PQntuples result)
174 (loop for i from 0 below num-fields
176 (if (zerop (PQgetisnull result tuple-index i))
178 (PQgetvalue result tuple-index i)
180 :encoding (encoding database))
183 (values res (result-field-names num-fields result))
186 (error 'sql-database-data-error
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))))))
195 (defun result-field-names (num-fields result)
196 "Return list of result field names."
198 (dotimes (i num-fields (nreverse names))
200 (push (uffi:convert-from-cstring (PQfname result i)) names))))
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
211 :expression sql-expression
212 :message (tidy-error-message (PQerrorMessage conn-ptr)
213 (encoding databse))))
215 (case (PQresultStatus result)
216 (#.pgsql-exec-status-type#command-ok
218 ((#.pgsql-exec-status-type#empty-query
219 #.pgsql-exec-status-type#tuples-ok)
220 (warn "Strange result...")
223 (error 'sql-database-data-error
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))))))
232 (defstruct postgresql-result-set
233 (res-ptr (uffi:make-null-pointer 'pgsql-result)
234 :type pgsql-result-def)
236 (num-tuples 0 :type integer)
237 (num-fields 0 :type integer)
238 (tuple-index 0 :type integer))
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
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
258 :num-fields (PQnfields result)
259 :num-tuples (PQntuples result)
260 :types (canonicalize-types
269 (PQnfields result)))))
272 (error 'sql-database-data-error
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))))))))
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))
287 (defmethod database-store-next-row (result-set (database postgresql-database)
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))
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)
300 (if (zerop (PQgetisnull result tuple-index i))
302 (PQgetvalue result tuple-index i)
304 :encoding (encoding database))
307 (incf (postgresql-result-set-tuple-index result-set))
310 ;;; Large objects support (Marc B)
312 (defmethod database-create-large-object ((database postgresql-database))
313 (lo-create (database-conn-ptr database)
314 (logior pgsql::+INV_WRITE+ pgsql::+INV_READ+)))
318 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
319 (let ((ptr (database-conn-ptr database))
320 (length (length data))
323 (with-transaction (:database database)
326 (setf fd (lo-open ptr object-id pgsql::+INV_WRITE+))
328 (when (= (lo-write ptr fd data length) length)
331 (when (and fd (>= fd 0))
336 (defmethod database-write-large-object (object-id (data string) (database postgresql-database))
337 (let ((ptr (database-conn-ptr database))
338 (length (length data))
341 (database-execute-command "begin" database)
344 (setf fd (lo-open ptr object-id pgsql::+INV_WRITE+))
346 (when (= (lo-write ptr fd data length) length)
349 (when (and fd (>= fd 0))
351 (database-execute-command (if result "commit" "rollback") database)))
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))
364 (database-execute-command "begin" database)
365 (setf fd (lo-open ptr object-id pgsql::+INV_READ+))
367 (setf length (lo-lseek ptr fd 0 2))
368 (lo-lseek ptr fd 0 0)
370 (setf buffer (uffi:allocate-foreign-string
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)))))))
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)))
382 (defmethod database-delete-large-object (object-id (database postgresql-database))
383 (lo-unlink (database-conn-ptr database) object-id))
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)
394 (setf (slot-value database 'clsql-sys::state) :open)
396 (database-execute-command (format nil "create database ~A" name) database)
397 (database-disconnect database)))))
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)
403 (setf (slot-value database 'clsql-sys::state) :open)
405 (database-execute-command (format nil "drop database ~A" name) database)
406 (database-disconnect database)))))
409 (defmethod database-probe (connection-spec (type (eql :postgresql)))
410 (when (find (second connection-spec) (database-list connection-spec type)
411 :test #'string-equal)
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)
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)
430 (error 'sql-connection-error
431 :database-type :postgresql
432 :connection-spec connection-spec
433 :error-id (PQstatus connection)
434 :message (PQerrorMessage connection)))
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)
442 (setf conn-ptr (%pg-database-connection connection-spec))
445 ;;; Database capabilities
447 (when (clsql-sys:database-type-library-loaded :postgresql)
448 (clsql-sys:initialize-database-type :database-type :postgresql))