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.9 2002/03/29 08:28:14 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)
70 (let ((auto-list (make-type-list-for-auto cursor)))
73 (canonicalize-type-list types auto-list))
79 (defun canonicalize-type-list (types auto-list)
80 "Ensure a field type list meets expectations.
81 Duplicated from clsql-uffi package so that this interface
82 doesn't depend on UFFI."
83 (let ((length-types (length types))
85 (loop for i from 0 below (length auto-list)
87 (if (>= i length-types)
88 (push t new-types) ;; types is shorted than num-fields
92 (case (nth i auto-list)
100 (case (nth i auto-list)
108 (nreverse new-types)))
111 (defun convert-to-clsql-warning (database condition)
112 (warn 'clsql-database-warning :database database
113 :message (postgresql-condition-message condition)))
115 (defun convert-to-clsql-error (database expression condition)
116 (error 'clsql-sql-error :database database
117 :expression expression
118 :errno (type-of condition)
119 :error (postgresql-condition-message condition)))
121 (defmacro with-postgresql-handlers
122 ((database &optional expression)
124 (let ((database-var (gensym))
125 (expression-var (gensym)))
126 `(let ((,database-var ,database)
127 (,expression-var ,expression))
128 (handler-bind ((postgresql-warning
130 (convert-to-clsql-warning ,database-var c)))
133 (convert-to-clsql-error
134 ,database-var ,expression-var c))))
135 ;; KMR - removed double @@
138 (defmethod database-initialize-database-type
139 ((database-type (eql :postgresql-socket)))
142 (defclass postgresql-socket-database (database)
143 ((connection :accessor database-connection :initarg :connection
144 :type postgresql-connection)))
146 (defmethod database-name-from-spec
147 (connection-spec (database-type (eql :postgresql-socket)))
148 (check-connection-spec connection-spec database-type
149 (host db user password &optional port options tty))
150 (destructuring-bind (host db user password &optional port options tty)
152 (declare (ignore password options tty))
153 (concatenate 'string host (if port ":") (if port port) "/" db "/" user)))
155 (defmethod database-connect
156 (connection-spec (database-type (eql :postgresql-socket)))
157 (check-connection-spec connection-spec database-type
158 (host db user password &optional port options tty))
159 (destructuring-bind (host db user password &optional
160 (port +postgresql-server-default-port+)
161 (options "") (tty ""))
164 (handler-bind ((postgresql-warning
166 (warn 'clsql-simple-warning
169 (list (princ-to-string c))))))
170 (open-postgresql-connection :host host :port port
171 :options options :tty tty
172 :database db :user user
174 (:no-error (connection)
175 ;; Success, make instance
176 (make-instance 'postgresql-socket-database
177 :name (database-name-from-spec connection-spec
179 :connection connection))
180 (postgresql-error (c)
182 (error 'clsql-connect-error
183 :database-type database-type
184 :connection-spec connection-spec
186 :error (postgresql-condition-message c))))))
188 (defmethod database-disconnect ((database postgresql-socket-database))
189 (close-postgresql-connection (database-connection database))
192 (defmethod database-query (expression (database postgresql-socket-database) types)
193 (let ((connection (database-connection database)))
194 (with-postgresql-handlers (database expression)
195 (start-query-execution connection expression)
196 (multiple-value-bind (status cursor)
197 (wait-for-query-results connection)
198 (unless (eq status :cursor)
199 (close-postgresql-connection connection)
200 (error 'clsql-sql-error
202 :expression expression
203 :errno 'missing-result
204 :error "Didn't receive result cursor for query."))
205 (setq types (canonicalize-types types cursor))
206 (loop for row = (read-cursor-row cursor types)
210 (unless (null (wait-for-query-results connection))
211 (close-postgresql-connection connection)
212 (error 'clsql-sql-error
214 :expression expression
215 :errno 'multiple-results
216 :error "Received multiple results for query.")))))))
218 (defmethod database-execute-command
219 (expression (database postgresql-socket-database))
220 (let ((connection (database-connection database)))
221 (with-postgresql-handlers (database expression)
222 (start-query-execution connection expression)
223 (multiple-value-bind (status result)
224 (wait-for-query-results connection)
225 (when (eq status :cursor)
227 (multiple-value-bind (row stuff)
228 (skip-cursor-row result)
230 (setq status :completed result stuff)
235 ((eq status :completed)
236 (unless (null (wait-for-query-results connection))
237 (close-postgresql-connection connection)
238 (error 'clsql-sql-error
240 :expression expression
241 :errno 'multiple-results
242 :error "Received multiple results for command."))
245 (close-postgresql-connection connection)
246 (error 'clsql-sql-error
248 :expression expression
249 :errno 'missing-result
250 :error "Didn't receive completion for command.")))))))
252 (defstruct postgresql-socket-result-set
257 (defmethod database-query-result-set (expression (database postgresql-socket-database)
260 (declare (ignore full-set))
261 (let ((connection (database-connection database)))
262 (with-postgresql-handlers (database expression)
263 (start-query-execution connection expression)
264 (multiple-value-bind (status cursor)
265 (wait-for-query-results connection)
266 (unless (eq status :cursor)
267 (close-postgresql-connection connection)
268 (error 'clsql-sql-error
270 :expression expression
271 :errno 'missing-result
272 :error "Didn't receive result cursor for query."))
273 (values (make-postgresql-socket-result-set
276 :types (canonicalize-types types cursor))
277 (length (postgresql-cursor-fields cursor)))))))
279 (defmethod database-dump-result-set (result-set
280 (database postgresql-socket-database))
281 (if (postgresql-socket-result-set-done result-set)
283 (with-postgresql-handlers (database)
284 (loop while (skip-cursor-row
285 (postgresql-socket-result-set-cursor result-set))
286 finally (setf (postgresql-socket-result-set-done result-set) t)))))
288 (defmethod database-store-next-row (result-set
289 (database postgresql-socket-database)
291 (let ((cursor (postgresql-socket-result-set-cursor result-set)))
292 (with-postgresql-handlers (database)
293 (if (copy-cursor-row cursor
295 (postgresql-socket-result-set-types
299 (setf (postgresql-socket-result-set-done result-set) t)
300 (wait-for-query-results (database-connection database)))))))