1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: postgresql-socket-sql.sql
6 ;;;; Purpose: High-level PostgreSQL interface using socket
7 ;;;; Programmers: Kevin M. Rosenberg based on
8 ;;;; Original code by Pierre R. Mai
9 ;;;; Date Started: Feb 2002
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
16 ;;;; CLSQL users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
18 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
19 ;;;; *************************************************************************
21 (in-package #:cl-user)
23 (defpackage :clsql-postgresql-socket
24 (:use #:common-lisp #:clsql-base-sys #:postgresql-socket)
25 (:export #:postgresql-socket-database)
26 (:documentation "This is the CLSQL socket interface to PostgreSQL."))
28 (in-package #:clsql-postgresql-socket)
30 ;; interface foreign library loading routines
33 (clsql-base-sys:database-type-load-foreign :postgresql-socket)
36 ;; Field type conversion
38 (defun make-type-list-for-auto (cursor)
39 (let* ((fields (postgresql-cursor-fields cursor))
40 (num-fields (length fields))
42 (dotimes (i num-fields)
44 (push (canonical-field-type fields i) new-types))
45 (nreverse new-types)))
47 (defun canonical-field-type (fields index)
48 "Extracts canonical field type from fields list"
49 (let ((oid (cadr (nth index fields))))
57 ((#.pgsql-ftype#float4
63 (defun canonicalize-types (types cursor)
66 (let ((auto-list (make-type-list-for-auto cursor)))
69 (canonicalize-type-list types auto-list))
75 (defun canonicalize-type-list (types auto-list)
76 "Ensure a field type list meets expectations.
77 Duplicated from clsql-uffi package so that this interface
78 doesn't depend on UFFI."
79 (let ((length-types (length types))
81 (loop for i from 0 below (length auto-list)
83 (if (>= i length-types)
84 (push t new-types) ;; types is shorted than num-fields
88 (case (nth i auto-list)
96 (case (nth i auto-list)
104 (nreverse new-types)))
107 (defun convert-to-clsql-warning (database condition)
108 (warn 'clsql-database-warning :database database
109 :message (postgresql-condition-message condition)))
111 (defun convert-to-clsql-error (database expression condition)
112 (error 'clsql-sql-error :database database
113 :expression expression
114 :errno (type-of condition)
115 :error (postgresql-condition-message condition)))
117 (defmacro with-postgresql-handlers
118 ((database &optional expression)
120 (let ((database-var (gensym))
121 (expression-var (gensym)))
122 `(let ((,database-var ,database)
123 (,expression-var ,expression))
124 (handler-bind ((postgresql-warning
126 (convert-to-clsql-warning ,database-var c)))
129 (convert-to-clsql-error
130 ,database-var ,expression-var c))))
131 ;; KMR - removed double @@
134 (defmethod database-initialize-database-type ((database-type
135 (eql :postgresql-socket)))
138 (defclass postgresql-socket-database (database)
139 ((connection :accessor database-connection :initarg :connection
140 :type postgresql-connection)))
142 (defmethod database-type ((database postgresql-socket-database))
145 (defmethod database-name-from-spec (connection-spec
146 (database-type (eql :postgresql-socket)))
147 (check-connection-spec connection-spec database-type
148 (host db user password &optional port options tty))
149 (destructuring-bind (host db user password &optional port options tty)
151 (declare (ignore password options tty))
154 (pathname (namestring host))
160 (integer (write-to-string port))
164 (defmethod database-connect (connection-spec
165 (database-type (eql :postgresql-socket)))
166 (check-connection-spec connection-spec database-type
167 (host db user password &optional port options tty))
168 (destructuring-bind (host db user password &optional
169 (port +postgresql-server-default-port+)
170 (options "") (tty ""))
173 (handler-bind ((postgresql-warning
175 (warn 'clsql-simple-warning
178 (list (princ-to-string c))))))
179 (open-postgresql-connection :host host :port port
180 :options options :tty tty
181 :database db :user user
183 (postgresql-error (c)
185 (error 'clsql-connect-error
186 :database-type database-type
187 :connection-spec connection-spec
189 :error (postgresql-condition-message c)))
190 (:no-error (connection)
191 ;; Success, make instance
192 (make-instance 'postgresql-socket-database
193 :name (database-name-from-spec connection-spec
195 :connection-spec connection-spec
196 :connection connection)))))
198 (defmethod database-disconnect ((database postgresql-socket-database))
199 (close-postgresql-connection (database-connection database))
202 (defmethod database-query (expression (database postgresql-socket-database) result-types)
203 (let ((connection (database-connection database)))
204 (with-postgresql-handlers (database expression)
205 (start-query-execution connection expression)
206 (multiple-value-bind (status cursor)
207 (wait-for-query-results connection)
208 (unless (eq status :cursor)
209 (close-postgresql-connection connection)
210 (error 'clsql-sql-error
212 :expression expression
213 :errno 'missing-result
214 :error "Didn't receive result cursor for query."))
215 (setq result-types (canonicalize-types result-types cursor))
216 (loop for row = (read-cursor-row cursor result-types)
220 (unless (null (wait-for-query-results connection))
221 (close-postgresql-connection connection)
222 (error 'clsql-sql-error
224 :expression expression
225 :errno 'multiple-results
226 :error "Received multiple results for query.")))))))
228 (defmethod database-execute-command
229 (expression (database postgresql-socket-database))
230 (let ((connection (database-connection database)))
231 (with-postgresql-handlers (database expression)
232 (start-query-execution connection expression)
233 (multiple-value-bind (status result)
234 (wait-for-query-results connection)
235 (when (eq status :cursor)
237 (multiple-value-bind (row stuff)
238 (skip-cursor-row result)
240 (setq status :completed result stuff)
245 ((eq status :completed)
246 (unless (null (wait-for-query-results connection))
247 (close-postgresql-connection connection)
248 (error 'clsql-sql-error
250 :expression expression
251 :errno 'multiple-results
252 :error "Received multiple results for command."))
255 (close-postgresql-connection connection)
256 (error 'clsql-sql-error
258 :expression expression
259 :errno 'missing-result
260 :error "Didn't receive completion for command.")))))))
262 (defstruct postgresql-socket-result-set
267 (defmethod database-query-result-set ((expression string)
268 (database postgresql-socket-database)
269 &key full-set result-types)
270 (declare (ignore full-set))
271 (let ((connection (database-connection database)))
272 (with-postgresql-handlers (database expression)
273 (start-query-execution connection expression)
274 (multiple-value-bind (status cursor)
275 (wait-for-query-results connection)
276 (unless (eq status :cursor)
277 (close-postgresql-connection connection)
278 (error 'clsql-sql-error
280 :expression expression
281 :errno 'missing-result
282 :error "Didn't receive result cursor for query."))
283 (values (make-postgresql-socket-result-set
286 :types (canonicalize-types result-types cursor))
287 (length (postgresql-cursor-fields cursor)))))))
289 (defmethod database-dump-result-set (result-set
290 (database postgresql-socket-database))
291 (if (postgresql-socket-result-set-done result-set)
293 (with-postgresql-handlers (database)
294 (loop while (skip-cursor-row
295 (postgresql-socket-result-set-cursor result-set))
296 finally (setf (postgresql-socket-result-set-done result-set) t)))))
298 (defmethod database-store-next-row (result-set
299 (database postgresql-socket-database)
301 (let ((cursor (postgresql-socket-result-set-cursor result-set)))
302 (with-postgresql-handlers (database)
303 (if (copy-cursor-row cursor
305 (postgresql-socket-result-set-types
309 (setf (postgresql-socket-result-set-done result-set) t)
310 (wait-for-query-results (database-connection database)))))))
314 (defmethod database-list-objects-of-type ((database postgresql-socket-database)
317 (cond ((stringp owner)
318 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))" owner))
320 (format nil " AND (NOT (relowner=1))"))
325 "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
330 (defmethod database-list-tables ((database postgresql-socket-database)
332 (database-list-objects-of-type database "r" owner))
334 (defmethod database-list-views ((database postgresql-socket-database)
336 (database-list-objects-of-type database "v" owner))
338 (defmethod database-list-indexes ((database postgresql-socket-database)
340 (database-list-objects-of-type database "i" owner))
342 (defmethod database-list-attributes ((table string)
343 (database postgresql-socket-database)
346 (cond ((stringp owner)
347 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
348 ((null owner) " AND (not (relowner=1))")
353 (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
354 (string-downcase table)
359 (remove-if #'(lambda (it) (member it '("cmin"
365 ;; kmr -- added tableoid
366 "tableoid") :test #'equal))
369 (defmethod database-attribute-type (attribute (table string)
370 (database postgresql-socket-database)
373 (cond ((stringp owner)
374 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
375 ((null owner) " AND (not (relowner=1))")
380 (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"
381 (string-downcase table)
382 (string-downcase attribute)
386 (intern (string-upcase (car result)) :keyword))))
388 (defmethod database-create-sequence (sequence-name
389 (database postgresql-socket-database))
390 (database-execute-command
391 (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
394 (defmethod database-drop-sequence (sequence-name
395 (database postgresql-socket-database))
396 (database-execute-command
397 (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
399 (defmethod database-list-sequences ((database postgresql-socket-database)
401 (database-list-objects-of-type database "S" owner))
403 (defmethod database-set-sequence-position (name (position integer)
404 (database postgresql-socket-database))
409 (format nil "SELECT SETVAL ('~A', ~A)" name position)
412 (defmethod database-sequence-next (sequence-name
413 (database postgresql-socket-database))
418 (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
421 (defmethod database-sequence-last (sequence-name (database postgresql-socket-database))
426 (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
430 ;; Functions depending upon high-level CommonSQL classes/functions
432 (defmethod database-output-sql ((expr clsql-sys::sql-typecast-exp)
433 (database postgresql-socket-database))
434 (with-slots (clsql-sys::modifier clsql-sys::components)
436 (if clsql-sys::modifier
438 (clsql-sys::output-sql clsql-sys::components database)
439 (write-char #\: clsql-sys::*sql-stream*)
440 (write-char #\: clsql-sys::*sql-stream*)
441 (write-string (symbol-name clsql-sys::modifier)
442 clsql-sys::*sql-stream*)))))
444 (defmethod database-output-sql-as-type ((type (eql 'integer)) val
445 (database postgresql-socket-database))
446 (when val ;; typecast it so it uses the indexes
447 (make-instance 'clsql-sys::sql-typecast-exp
452 (when (clsql-base-sys:database-type-library-loaded :postgresql-socket)
453 (clsql-base-sys:initialize-database-type :database-type :postgresql-socket))