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 ;;;; Authors: Kevin M. Rosenberg based on original code by Pierre R. Mai
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2004 by Kevin M. Rosenberg
13 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
15 ;;;; CLSQL users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
20 (in-package #:cl-user)
22 (defpackage :clsql-postgresql-socket
23 (:use #:common-lisp #:clsql-base-sys #:postgresql-socket)
24 (:export #:postgresql-socket-database)
25 (:documentation "This is the CLSQL socket interface to PostgreSQL."))
27 (in-package #:clsql-postgresql-socket)
29 ;; interface foreign library loading routines
32 (clsql-base-sys:database-type-load-foreign :postgresql-socket)
35 ;; Field type conversion
37 (defun make-type-list-for-auto (cursor)
38 (let* ((fields (postgresql-cursor-fields cursor))
39 (num-fields (length fields))
41 (dotimes (i num-fields)
43 (push (canonical-field-type fields i) new-types))
44 (nreverse new-types)))
46 (defun canonical-field-type (fields index)
47 "Extracts canonical field type from fields list"
48 (let ((oid (cadr (nth index fields))))
56 ((#.pgsql-ftype#float4
62 (defun canonicalize-types (types cursor)
65 (let ((auto-list (make-type-list-for-auto cursor)))
68 (canonicalize-type-list types auto-list))
74 (defun canonicalize-type-list (types auto-list)
75 "Ensure a field type list meets expectations.
76 Duplicated from clsql-uffi package so that this interface
77 doesn't depend on UFFI."
78 (let ((length-types (length types))
80 (loop for i from 0 below (length auto-list)
82 (if (>= i length-types)
83 (push t new-types) ;; types is shorted than num-fields
87 (case (nth i auto-list)
95 (case (nth i auto-list)
103 (nreverse new-types)))
106 (defun convert-to-clsql-warning (database condition)
107 (warn 'clsql-database-warning :database database
108 :message (postgresql-condition-message condition)))
110 (defun convert-to-clsql-error (database expression condition)
111 (error 'clsql-sql-error :database database
112 :expression expression
113 :errno (type-of condition)
114 :error (postgresql-condition-message condition)))
116 (defmacro with-postgresql-handlers
117 ((database &optional expression)
119 (let ((database-var (gensym))
120 (expression-var (gensym)))
121 `(let ((,database-var ,database)
122 (,expression-var ,expression))
123 (handler-bind ((postgresql-warning
125 (convert-to-clsql-warning ,database-var c)))
128 (convert-to-clsql-error
129 ,database-var ,expression-var c))))
130 ;; KMR - removed double @@
133 (defmethod database-initialize-database-type ((database-type
134 (eql :postgresql-socket)))
137 (defclass postgresql-socket-database (database)
138 ((connection :accessor database-connection :initarg :connection
139 :type postgresql-connection)))
141 (defmethod database-type ((database postgresql-socket-database))
144 (defmethod database-name-from-spec (connection-spec
145 (database-type (eql :postgresql-socket)))
146 (check-connection-spec connection-spec database-type
147 (host db user password &optional port options tty))
148 (destructuring-bind (host db user password &optional port options tty)
150 (declare (ignore password options tty))
155 (pathname (namestring host))
161 (integer (write-to-string port))
165 (defmethod database-connect (connection-spec
166 (database-type (eql :postgresql-socket)))
167 (check-connection-spec connection-spec database-type
168 (host db user password &optional port options tty))
169 (destructuring-bind (host db user password &optional
170 (port +postgresql-server-default-port+)
171 (options "") (tty ""))
174 (handler-bind ((postgresql-warning
176 (warn 'clsql-simple-warning
179 (list (princ-to-string c))))))
180 (open-postgresql-connection :host host :port port
181 :options options :tty tty
182 :database db :user user
184 (postgresql-error (c)
186 (error 'clsql-connect-error
187 :database-type database-type
188 :connection-spec connection-spec
190 :error (postgresql-condition-message c)))
191 (:no-error (connection)
192 ;; Success, make instance
193 (make-instance 'postgresql-socket-database
194 :name (database-name-from-spec connection-spec
196 :database-type :postgresql-socket
197 :connection-spec connection-spec
198 :connection connection)))))
200 (defmethod database-disconnect ((database postgresql-socket-database))
201 (close-postgresql-connection (database-connection database))
204 (defmethod database-query (expression (database postgresql-socket-database) result-types field-names)
205 (let ((connection (database-connection database)))
206 (with-postgresql-handlers (database expression)
207 (start-query-execution connection expression)
208 (multiple-value-bind (status cursor)
209 (wait-for-query-results connection)
210 (unless (eq status :cursor)
211 (close-postgresql-connection connection)
212 (error 'clsql-sql-error
214 :expression expression
215 :errno 'missing-result
216 :error "Didn't receive result cursor for query."))
217 (setq result-types (canonicalize-types result-types cursor))
219 (loop for row = (read-cursor-row cursor result-types)
223 (unless (null (wait-for-query-results connection))
224 (close-postgresql-connection connection)
225 (error 'clsql-sql-error
227 :expression expression
228 :errno 'multiple-results
229 :error "Received multiple results for query.")))
231 (result-field-names cursor)))))))
233 (defun result-field-names (cursor)
234 "Return list of result field names."
235 ;; FIXME -- implement
238 (defmethod database-execute-command
239 (expression (database postgresql-socket-database))
240 (let ((connection (database-connection database)))
241 (with-postgresql-handlers (database expression)
242 (start-query-execution connection expression)
243 (multiple-value-bind (status result)
244 (wait-for-query-results connection)
245 (when (eq status :cursor)
247 (multiple-value-bind (row stuff)
248 (skip-cursor-row result)
250 (setq status :completed result stuff)
255 ((eq status :completed)
256 (unless (null (wait-for-query-results connection))
257 (close-postgresql-connection connection)
258 (error 'clsql-sql-error
260 :expression expression
261 :errno 'multiple-results
262 :error "Received multiple results for command."))
265 (close-postgresql-connection connection)
266 (error 'clsql-sql-error
268 :expression expression
269 :errno 'missing-result
270 :error "Didn't receive completion for command.")))))))
272 (defstruct postgresql-socket-result-set
277 (defmethod database-query-result-set ((expression string)
278 (database postgresql-socket-database)
279 &key full-set result-types)
280 (declare (ignore full-set))
281 (let ((connection (database-connection database)))
282 (with-postgresql-handlers (database expression)
283 (start-query-execution connection expression)
284 (multiple-value-bind (status cursor)
285 (wait-for-query-results connection)
286 (unless (eq status :cursor)
287 (close-postgresql-connection connection)
288 (error 'clsql-sql-error
290 :expression expression
291 :errno 'missing-result
292 :error "Didn't receive result cursor for query."))
293 (values (make-postgresql-socket-result-set
296 :types (canonicalize-types result-types cursor))
297 (length (postgresql-cursor-fields cursor)))))))
299 (defmethod database-dump-result-set (result-set
300 (database postgresql-socket-database))
301 (if (postgresql-socket-result-set-done result-set)
303 (with-postgresql-handlers (database)
304 (loop while (skip-cursor-row
305 (postgresql-socket-result-set-cursor result-set))
306 finally (setf (postgresql-socket-result-set-done result-set) t)))))
308 (defmethod database-store-next-row (result-set
309 (database postgresql-socket-database)
311 (let ((cursor (postgresql-socket-result-set-cursor result-set)))
312 (with-postgresql-handlers (database)
313 (if (copy-cursor-row cursor
315 (postgresql-socket-result-set-types
319 (setf (postgresql-socket-result-set-done result-set) t)
320 (wait-for-query-results (database-connection database)))))))
324 (defun owner-clause (owner)
329 " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))"
332 (format nil " AND (NOT (relowner=1))"))
335 (defun database-list-objects-of-type (database type owner)
339 "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
341 (owner-clause owner))
344 (defmethod database-list-tables ((database postgresql-socket-database)
346 (database-list-objects-of-type database "r" owner))
348 (defmethod database-list-views ((database postgresql-socket-database)
350 (database-list-objects-of-type database "v" owner))
352 (defmethod database-list-indexes ((database postgresql-socket-database)
354 (database-list-objects-of-type database "i" owner))
356 (defmethod database-list-table-indexes (table
357 (database postgresql-socket-database)
363 "select indexrelid from pg_index where indrelid=(select relfilenode from pg_class where relname='~A'~A)"
364 (string-downcase table)
365 (owner-clause owner))
368 (dolist (indexrelid indexrelids (nreverse result))
370 (caar (database-query
371 (format nil "select relname from pg_class where relfilenode='~A'"
377 (defmethod database-list-attributes ((table string)
378 (database postgresql-socket-database)
381 (cond ((stringp owner)
382 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
383 ((null owner) " AND (not (relowner=1))")
388 (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
389 (string-downcase table)
394 (remove-if #'(lambda (it) (member it '("cmin"
400 ;; kmr -- added tableoid
401 "tableoid") :test #'equal))
404 (defmethod database-attribute-type (attribute (table string)
405 (database postgresql-socket-database)
408 (cond ((stringp owner)
409 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
410 ((null owner) " AND (not (relowner=1))")
415 (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"
416 (string-downcase table)
417 (string-downcase attribute)
421 (intern (string-upcase (car result)) :keyword))))
423 (defmethod database-create-sequence (sequence-name
424 (database postgresql-socket-database))
425 (database-execute-command
426 (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
429 (defmethod database-drop-sequence (sequence-name
430 (database postgresql-socket-database))
431 (database-execute-command
432 (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
434 (defmethod database-list-sequences ((database postgresql-socket-database)
436 (database-list-objects-of-type database "S" owner))
438 (defmethod database-set-sequence-position (name (position integer)
439 (database postgresql-socket-database))
444 (format nil "SELECT SETVAL ('~A', ~A)" name position)
447 (defmethod database-sequence-next (sequence-name
448 (database postgresql-socket-database))
453 (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
456 (defmethod database-sequence-last (sequence-name (database postgresql-socket-database))
461 (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
465 (defmethod database-create (connection-spec (type (eql :postgresql-socket)))
466 (destructuring-bind (host name user password) connection-spec
467 (let ((database (database-connect (list host "template1" user password)
470 (execute-command (format nil "create database ~A" name))
471 (database-disconnect database)))))
473 (defmethod database-destroy (connection-spec (type (eql :postgresql-socket)))
474 (destructuring-bind (host name user password) connection-spec
475 (let ((database (database-connect (list host "template1" user password)
478 (execute-command (format nil "drop database ~A" name))
479 (database-disconnect database)))))
482 (defmethod database-probe (connection-spec (type (eql :postgresql-socket)))
483 (when (find (second connection-spec) (database-list connection-spec type)
484 :key #'car :test #'string-equal)
487 (defmethod database-list (connection-spec (type (eql :postgresql-socket)))
488 (destructuring-bind (host name user password) connection-spec
489 (declare (ignore name))
490 (let ((database (database-connect (list host "template1" user password)
494 (setf (slot-value database 'clsql-base-sys::state) :open)
495 (mapcar #'car (database-query "select datname from pg_database"
498 (database-disconnect database)
499 (setf (slot-value database 'clsql-base-sys::state) :closed))))))
501 (defmethod database-describe-table ((database postgresql-socket-database)
504 (format nil "select a.attname, t.typname
505 from pg_class c, pg_attribute a, pg_type t
506 where c.relname = '~a'
508 and a.attrelid = c.oid
509 and a.atttypid = t.oid"
510 (sql-escape (string-downcase table)))
514 ;; Database capabilities
516 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :postgresql-socket)))
519 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql-socket)))
522 (defmethod db-type-default-case ((db-type (eql :postgresql)))
525 (when (clsql-base-sys:database-type-library-loaded :postgresql-socket)
526 (clsql-base-sys:initialize-database-type :database-type :postgresql-socket))