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
11 ;;;; $Id: postgresql-socket-sql.cl,v 1.10 2002/03/29 09:37:24 kevin Exp $
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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
24 (defpackage :clsql-postgresql-socket
25 (:use :common-lisp :clsql-sys :postgresql-socket)
26 (:export #:postgresql-socket-database)
27 (:documentation "This is the CLSQL socket interface to PostgreSQL."))
29 (in-package :clsql-postgresql-socket)
31 ;; interface foreign library loading routines
33 (defmethod database-type-library-loaded ((database-type (eql :postgresql-socket)))
36 (defmethod clsql-sys:database-type-load-foreign ((database-type (eql :postgresql-socket)))
39 (clsql-sys:database-type-load-foreign :postgresql-socket)
42 ;; Field type conversion
44 (defun make-type-list-for-auto (cursor)
45 (let* ((fields (postgresql-cursor-fields cursor))
46 (num-fields (length fields))
48 (dotimes (i num-fields)
50 (push (canonical-field-type fields i) new-types))
51 (nreverse new-types)))
53 (defun canonical-field-type (fields index)
54 "Extracts canonical field type from fields list"
55 (let ((oid (cadr (nth index fields))))
63 ((#.pgsql-ftype#float4
69 (defun canonicalize-types (types cursor)
72 (let ((auto-list (make-type-list-for-auto cursor)))
75 (canonicalize-type-list types auto-list))
81 (defun canonicalize-type-list (types auto-list)
82 "Ensure a field type list meets expectations.
83 Duplicated from clsql-uffi package so that this interface
84 doesn't depend on UFFI."
85 (let ((length-types (length types))
87 (loop for i from 0 below (length auto-list)
89 (if (>= i length-types)
90 (push t new-types) ;; types is shorted than num-fields
94 (case (nth i auto-list)
102 (case (nth i auto-list)
110 (nreverse new-types)))
113 (defun convert-to-clsql-warning (database condition)
114 (warn 'clsql-database-warning :database database
115 :message (postgresql-condition-message condition)))
117 (defun convert-to-clsql-error (database expression condition)
118 (error 'clsql-sql-error :database database
119 :expression expression
120 :errno (type-of condition)
121 :error (postgresql-condition-message condition)))
123 (defmacro with-postgresql-handlers
124 ((database &optional expression)
126 (let ((database-var (gensym))
127 (expression-var (gensym)))
128 `(let ((,database-var ,database)
129 (,expression-var ,expression))
130 (handler-bind ((postgresql-warning
132 (convert-to-clsql-warning ,database-var c)))
135 (convert-to-clsql-error
136 ,database-var ,expression-var c))))
137 ;; KMR - removed double @@
140 (defmethod database-initialize-database-type
141 ((database-type (eql :postgresql-socket)))
144 (defclass postgresql-socket-database (database)
145 ((connection :accessor database-connection :initarg :connection
146 :type postgresql-connection)))
148 (defmethod database-name-from-spec
149 (connection-spec (database-type (eql :postgresql-socket)))
150 (check-connection-spec connection-spec database-type
151 (host db user password &optional port options tty))
152 (destructuring-bind (host db user password &optional port options tty)
154 (declare (ignore password options tty))
155 (concatenate 'string host (if port ":") (if port port) "/" db "/" user)))
157 (defmethod database-connect
158 (connection-spec (database-type (eql :postgresql-socket)))
159 (check-connection-spec connection-spec database-type
160 (host db user password &optional port options tty))
161 (destructuring-bind (host db user password &optional
162 (port +postgresql-server-default-port+)
163 (options "") (tty ""))
166 (handler-bind ((postgresql-warning
168 (warn 'clsql-simple-warning
171 (list (princ-to-string c))))))
172 (open-postgresql-connection :host host :port port
173 :options options :tty tty
174 :database db :user user
176 (:no-error (connection)
177 ;; Success, make instance
178 (make-instance 'postgresql-socket-database
179 :name (database-name-from-spec connection-spec
181 :connection connection))
182 (postgresql-error (c)
184 (error 'clsql-connect-error
185 :database-type database-type
186 :connection-spec connection-spec
188 :error (postgresql-condition-message c))))))
190 (defmethod database-disconnect ((database postgresql-socket-database))
191 (close-postgresql-connection (database-connection database))
194 (defmethod database-query (expression (database postgresql-socket-database) types)
195 (let ((connection (database-connection database)))
196 (with-postgresql-handlers (database expression)
197 (start-query-execution connection expression)
198 (multiple-value-bind (status cursor)
199 (wait-for-query-results connection)
200 (unless (eq status :cursor)
201 (close-postgresql-connection connection)
202 (error 'clsql-sql-error
204 :expression expression
205 :errno 'missing-result
206 :error "Didn't receive result cursor for query."))
207 (setq types (canonicalize-types types cursor))
208 (loop for row = (read-cursor-row cursor types)
212 (unless (null (wait-for-query-results connection))
213 (close-postgresql-connection connection)
214 (error 'clsql-sql-error
216 :expression expression
217 :errno 'multiple-results
218 :error "Received multiple results for query.")))))))
220 (defmethod database-execute-command
221 (expression (database postgresql-socket-database))
222 (let ((connection (database-connection database)))
223 (with-postgresql-handlers (database expression)
224 (start-query-execution connection expression)
225 (multiple-value-bind (status result)
226 (wait-for-query-results connection)
227 (when (eq status :cursor)
229 (multiple-value-bind (row stuff)
230 (skip-cursor-row result)
232 (setq status :completed result stuff)
237 ((eq status :completed)
238 (unless (null (wait-for-query-results connection))
239 (close-postgresql-connection connection)
240 (error 'clsql-sql-error
242 :expression expression
243 :errno 'multiple-results
244 :error "Received multiple results for command."))
247 (close-postgresql-connection connection)
248 (error 'clsql-sql-error
250 :expression expression
251 :errno 'missing-result
252 :error "Didn't receive completion for command.")))))))
254 (defstruct postgresql-socket-result-set
259 (defmethod database-query-result-set (expression (database postgresql-socket-database)
262 (declare (ignore full-set))
263 (let ((connection (database-connection database)))
264 (with-postgresql-handlers (database expression)
265 (start-query-execution connection expression)
266 (multiple-value-bind (status cursor)
267 (wait-for-query-results connection)
268 (unless (eq status :cursor)
269 (close-postgresql-connection connection)
270 (error 'clsql-sql-error
272 :expression expression
273 :errno 'missing-result
274 :error "Didn't receive result cursor for query."))
275 (values (make-postgresql-socket-result-set
278 :types (canonicalize-types types cursor))
279 (length (postgresql-cursor-fields cursor)))))))
281 (defmethod database-dump-result-set (result-set
282 (database postgresql-socket-database))
283 (if (postgresql-socket-result-set-done result-set)
285 (with-postgresql-handlers (database)
286 (loop while (skip-cursor-row
287 (postgresql-socket-result-set-cursor result-set))
288 finally (setf (postgresql-socket-result-set-done result-set) t)))))
290 (defmethod database-store-next-row (result-set
291 (database postgresql-socket-database)
293 (let ((cursor (postgresql-socket-result-set-cursor result-set)))
294 (with-postgresql-handlers (database)
295 (if (copy-cursor-row cursor
297 (postgresql-socket-result-set-types
301 (setf (postgresql-socket-result-set-done result-set) t)
302 (wait-for-query-results (database-connection database)))))))