summaryrefslogtreecommitdiff
path: root/db-postgresql-socket3/sql.lisp
blob: 3172e6dbd7b2881aaebc1f9c2008fe1aa6aa1eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:     postgresql-socket-sql.sql
;;;; Purpose:  High-level PostgreSQL interface using socket
;;;; Authors:  Kevin M. Rosenberg based on original code by Pierre R. Mai
;;;; Created:  Feb 2002
;;;;
;;;; $Id$
;;;;
;;;; This file, part of CLSQL, is Copyright (c) 2002-2007 by Kevin M. Rosenberg
;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
;;;;
;;;; CLSQL users are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser GNU Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
;;;; *************************************************************************

(in-package #:cl-user)

(defpackage :clsql-postgresql-socket3
    (:use #:common-lisp #:clsql-sys #:postgresql-socket3)
    (:export #:postgresql-socket3-database)
    (:documentation "This is the CLSQL socket interface (protocol version 3) to PostgreSQL."))

(in-package #:clsql-postgresql-socket3)

(defvar *sqlreader* (cl-postgres:copy-sql-readtable))
(let ((dt-fn (lambda (useconds-since-2000)
	       (let ((sec (truncate
			   (/ useconds-since-2000
			      1000000)))
		     (usec (mod useconds-since-2000
				1000000)))
		 (clsql:make-time :year 2000 :second sec :usec usec)))))
  (cl-postgres:set-sql-datetime-readers
   :table *sqlreader*
   :date (lambda (days-since-2000)
	   (clsql:make-date :year 2000 :day (+ 1 days-since-2000)))
   :timestamp dt-fn
   :timestamp-with-timezone dt-fn))



;; interface foreign library loading routines

(clsql-sys:database-type-load-foreign :postgresql-socket3)


(defmethod database-initialize-database-type ((database-type
                                               (eql :postgresql-socket3)))
  t)


;; Field type conversion
(defun convert-to-clsql-warning (database condition)
  (ecase *backend-warning-behavior*
    (:warn
     (warn 'sql-database-warning :database database
           :message (cl-postgres:database-error-message condition)))
    (:error
     (error 'sql-database-error :database database
            :message (format nil "Warning upgraded to error: ~A"
                             (cl-postgres:database-error-message condition))))
    ((:ignore nil)
     ;; do nothing
     )))

(defun convert-to-clsql-error (database expression condition)
  (error 'sql-database-data-error
         :database database
         :expression expression
         :error-id (type-of condition)
         :message (cl-postgres:database-error-message condition)))

(defmacro with-postgresql-handlers
    ((database &optional expression)
     &body body)
  (let ((database-var (gensym))
        (expression-var (gensym)))
    `(let ((,database-var ,database)
           (,expression-var ,expression))
       (handler-bind ((postgresql-warning
                       (lambda (c)
                         (convert-to-clsql-warning ,database-var c)))
                      (cl-postgres:database-error
                       (lambda (c)
                         (convert-to-clsql-error
                          ,database-var ,expression-var c))))
         ,@body))))



(defclass postgresql-socket3-database (generic-postgresql-database)
  ((connection :accessor database-connection :initarg :connection
               :type cl-postgres:database-connection)))

(defmethod database-type ((database postgresql-socket3-database))
  :postgresql-socket3)

(defmethod database-name-from-spec (connection-spec (database-type (eql :postgresql-socket3)))
  (check-connection-spec connection-spec database-type
                         (host db user password &optional port options tty))
  (destructuring-bind (host db user password &optional port options tty)
      connection-spec
    (declare (ignore password options tty))
    (concatenate 'string
      (etypecase host
        (null
         "localhost")
        (keyword "unix")
        (pathname (namestring host))
        (string host))
      (when port
        (concatenate 'string
                     ":"
                     (etypecase port
                       (integer (write-to-string port))
                       (string port))))
      "/" db "/" user)))

(defmethod database-connect (connection-spec
                             (database-type (eql :postgresql-socket3)))
  (check-connection-spec connection-spec database-type
                         (host db user password &optional port options tty))
  (destructuring-bind (host db user password &optional
                            (port +postgresql-server-default-port+)
                            (options "") (tty ""))
      connection-spec
    (declare (ignore options tty))
    (handler-case
        (handler-bind ((warning
                        (lambda (c)
                          (warn 'sql-warning
                                :format-control "~A"
                                :format-arguments
                                (list (princ-to-string c))))))
          (cl-postgres:open-database db user password host port))
      (cl-postgres:database-error (c)
        ;; Connect failed
        (error 'sql-connection-error
               :database-type database-type
               :connection-spec connection-spec
               :error-id (type-of c)
               :message (cl-postgres:database-error-message c)))
      (:no-error (connection)
                 ;; Success, make instance
                 (make-instance 'postgresql-socket3-database
                                :name (database-name-from-spec connection-spec database-type)
                                :database-type :postgresql-socket3
                                :connection-spec connection-spec
                                :connection connection)))))

(defmethod database-disconnect ((database postgresql-socket3-database))
  (cl-postgres:close-database (database-connection database))
  t)

(defvar *include-field-names* nil)


;; THE FOLLOWING MACRO EXPANDS TO THE FUNCTION BELOW IT,
;; BUT TO GET null CONVENTIONS CORRECT I NEEDED TO TWEAK THE EXPANSION
;;
;; (cl-postgres:def-row-reader clsql-default-row-reader (fields)
;;   (values (loop :while (cl-postgres:next-row)
;; 		:collect (loop :for field :across fields
;; 			       :collect (cl-postgres:next-field field)))
;; 	  (when *include-field-names*
;; 	    (loop :for field :across fields
;; 		  :collect (cl-postgres:field-name field)))))



(defun clsql-default-row-reader (stream fields)
  (declare (type stream stream)
           (type (simple-array cl-postgres::field-description) fields))
  (flet ((cl-postgres:next-row ()
	   (cl-postgres::look-for-row stream))
	 (cl-postgres:next-field (cl-postgres::field)
	   (declare (type cl-postgres::field-description cl-postgres::field))
	   (let ((cl-postgres::size (cl-postgres::read-int4 stream)))
	     (declare (type (signed-byte 32) cl-postgres::size))
	     (if (eq cl-postgres::size -1)
		 nil
		 (funcall (cl-postgres::field-interpreter cl-postgres::field)
			  stream cl-postgres::size)))))
    (let ((results (loop :while (cl-postgres:next-row)
			 :collect (loop :for field :across fields
					:collect (cl-postgres:next-field field))))
	  (col-names (when *include-field-names*
		       (loop :for field :across fields
			     :collect (cl-postgres:field-name field)))))
      ;;multiple return values were not working here
      (list results col-names))))

(defmethod database-query ((expression string) (database postgresql-socket3-database) result-types field-names)
  (let ((connection (database-connection database))
	(cl-postgres:*sql-readtable* *sqlreader*))
    (with-postgresql-handlers (database expression)
      (let ((*include-field-names* field-names))
	(apply #'values (cl-postgres:exec-query connection expression #'clsql-default-row-reader)))
      )))

(defmethod query ((obj command-object) &key (database *default-database*)
                  (result-types :auto) (flatp nil) (field-names t))
  (clsql-sys::record-sql-command
   (format nil "~&~A~&{Params: ~{~A~^, ~}}"
           (expression obj)
           (parameters obj))
   database)
  (multiple-value-bind (rows names)
      (database-query obj database result-types field-names)
    (let ((result (if (and flatp (= 1 (length (car rows))))
                      (mapcar #'car rows)
		      rows)))
      (clsql-sys::record-sql-result result database)
      (if field-names
          (values result names)
	  result))))

(defmethod database-query ((obj command-object) (database postgresql-socket3-database) result-types field-names)
  (let ((connection (database-connection database))
	(cl-postgres:*sql-readtable* *sqlreader*))
    (with-postgresql-handlers (database obj)
      (let ((*include-field-names* field-names))
	(unless (has-been-prepared obj)
	  (cl-postgres:prepare-query connection (prepared-name obj) (expression obj))
	  (setf (has-been-prepared obj) T))
	(apply #'values (cl-postgres:exec-prepared
			 connection
			 (prepared-name obj)
			 (parameters obj)
			 #'clsql-default-row-reader))))))

(defmethod database-execute-command
    ((expression string) (database postgresql-socket3-database))
  (let ((connection (database-connection database)))
    (with-postgresql-handlers (database expression)
      ;; return row count?
      (second (multiple-value-list (cl-postgres:exec-query connection expression))))))

(defmethod execute-command ((obj command-object)
                            &key (database *default-database*))
  (clsql-sys::record-sql-command (expression obj) database)
  (let ((res (database-execute-command obj database)))
    (clsql-sys::record-sql-result res database)
    ;; return row count?
    res))

(defmethod database-execute-command
    ((obj command-object) (database postgresql-socket3-database))
  (let ((connection (database-connection database)))
    (with-postgresql-handlers (database obj)
      (unless (has-been-prepared obj)
	(cl-postgres:prepare-query connection (prepared-name obj) (expression obj))
	(setf (has-been-prepared obj) T))
      (second (multiple-value-list (cl-postgres:exec-prepared connection (prepared-name obj) (parameters obj)))))))

;;;; Cursoring interface


(defmethod database-query-result-set ((expression string)
                                      (database postgresql-socket3-database)
                                      &key full-set result-types)
  (declare (ignore result-types))
  (declare (ignore full-set))
  (error "Cursoring interface is not supported for postgresql-socket3-database try cl-postgres:exec-query with a custom row-reader"))

(defmethod database-dump-result-set (result-set
                                     (database postgresql-socket3-database))
  (error "Cursoring interface is not supported for postgresql-socket3-database try cl-postgres:exec-query with a custom row-reader")
  T)

(defmethod database-store-next-row (result-set
                                    (database postgresql-socket3-database)
                                    list)
  (error "Cursoring interface is not supported for postgresql-socket3-database try cl-postgres:exec-query with a custom row-reader"))


;;;;;;;;;;;;;;;;;;;;;;;;;;


(defmethod database-create (connection-spec (type (eql :postgresql-socket3)))
  (destructuring-bind (host name user password &optional port options tty) connection-spec
    (declare (ignore port options tty))
    (let ((database (database-connect (list host "postgres" user password)
                                      type)))
      (setf (slot-value database 'clsql-sys::state) :open)
      (unwind-protect
           (database-execute-command (format nil "create database ~A" name) database)
        (database-disconnect database)))))

(defmethod database-destroy (connection-spec (type (eql :postgresql-socket3)))
  (destructuring-bind (host name user password &optional port options tty) connection-spec
    (declare (ignore port options tty))
    (let ((database (database-connect (list host "postgres" user password)
                                      type)))
      (setf (slot-value database 'clsql-sys::state) :open)
      (unwind-protect
          (database-execute-command (format nil "drop database ~A" name) database)
        (database-disconnect database)))))


(defmethod database-probe (connection-spec (type (eql :postgresql-socket3)))
  (when (find (second connection-spec) (database-list connection-spec type)
              :test #'string-equal)
    t))


;; Database capabilities

(defmethod db-backend-has-create/destroy-db? ((db-type (eql :postgresql-socket3)))
  nil)

(defmethod db-type-has-fancy-math? ((db-type (eql :postgresql-socket3)))
  t)

(defmethod db-type-default-case ((db-type (eql :postgresql-socket3)))
  :lower)

(defmethod database-underlying-type ((database postgresql-socket3-database))
  :postgresql)

(when (clsql-sys:database-type-library-loaded :postgresql-socket3)
  (clsql-sys:initialize-database-type :database-type :postgresql-socket3))