Adding versions to the Changelog
[clsql.git] / sql / generic-postgresql.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;;
4 ;;;; Generic postgresql layer, used by db-postgresql and db-postgresql-socket
5 ;;;;
6 ;;;; This file is part of CLSQL.
7 ;;;;
8 ;;;; CLSQL users are granted the rights to distribute and use this software
9 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
10 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
11 ;;;; *************************************************************************
12
13 (in-package #:clsql-sys)
14
15 (defclass generic-postgresql-database (database)
16   ((has-table-pg_roles :type boolean :reader has-table-pg_roles :initform nil))
17   (:documentation "Encapsulate same behavior across postgresql and postgresql-socket backends."))
18
19
20
21 ;; Object functions
22
23 (defmethod database-get-type-specifier (type args database
24                                         (db-type (eql :postgresql)))
25   (declare (ignore type args database))
26   "VARCHAR")
27
28 (defmethod database-get-type-specifier ((type (eql 'string)) args database
29                                         (db-type (eql :postgresql)))
30   (declare (ignore database))
31   (if args
32       (format nil "CHAR(~A)" (car args))
33     "VARCHAR"))
34
35 (defmethod database-get-type-specifier ((type (eql 'tinyint)) args database
36                                         (db-type (eql :postgresql)))
37   (declare (ignore args database))
38   "INT2")
39
40 (defmethod database-get-type-specifier ((type (eql 'smallint)) args database
41                                         (db-type (eql :postgresql)))
42   (declare (ignore args database))
43   "INT2")
44
45 (defmethod database-get-type-specifier ((type (eql 'wall-time)) args database
46                                         (db-type (eql :postgresql)))
47   (declare (ignore args database))
48   "TIMESTAMP WITHOUT TIME ZONE")
49
50 (defmethod database-get-type-specifier ((type (eql 'number)) args database
51                                         (db-type (eql :postgresql)))
52   (declare (ignore database))
53   (cond
54    ((and (consp args) (= (length args) 2))
55     (format nil "NUMERIC(~D,~D)" (first args) (second args)))
56    ((and (consp args) (= (length args) 1))
57     (format nil "NUMERIC(~D)" (first args)))
58    (t
59     "NUMERIC")))
60
61 ;;; Backend functions
62
63 (defun owner-clause (owner)
64   (cond
65    ((stringp owner)
66     (format
67      nil
68      " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))"
69      owner))
70    ((null owner)
71     (format nil " AND (relowner<>(SELECT usesysid FROM pg_user WHERE usename='postgres'))"))
72    (t "")))
73
74 (defun has-table (name database)
75   (let ((name-retrieved
76          (caar (database-query
77                 (format nil "SELECT relname FROM pg_class WHERE relname='~A'"
78                         name)
79                 database nil nil))))
80     (if (and (stringp name-retrieved) (plusp (length name-retrieved)))
81         t
82         nil)))
83
84 (defmethod slot-unbound (class (obj generic-postgresql-database)
85                          (slot (eql 'has-table-pg_roles)))
86   ;; Lazily cache slot value
87   (declare (ignore class))
88   (setf (slot-value obj 'has-table-pg_roles) (has-table "pg_roles" obj)))
89
90 (defun database-list-objects-of-type (database type owner)
91   (mapcar #'car
92           (database-query
93            (format nil
94                    (if (and (has-table-pg_roles database)
95                             (not (eq owner :all)))
96                        "
97  SELECT c.relname
98  FROM pg_catalog.pg_class c
99       LEFT JOIN pg_catalog.pg_roles r ON r.oid = c.relowner
100       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
101  WHERE c.relkind IN ('~A','')
102        AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
103        AND pg_catalog.pg_table_is_visible(c.oid)
104        ~A"
105                        "SELECT relname FROM pg_class WHERE (relkind =
106 '~A')~A")
107                    type
108                    (owner-clause owner))
109            database nil nil)))
110
111 (defmethod database-list-tables ((database generic-postgresql-database)
112                                  &key (owner nil))
113   (database-list-objects-of-type database "r" owner))
114
115 (defmethod database-list-views ((database generic-postgresql-database)
116                                 &key (owner nil))
117   (database-list-objects-of-type database "v" owner))
118
119 (defmethod database-list-indexes ((database generic-postgresql-database)
120                                   &key (owner nil))
121   (database-list-objects-of-type database "i" owner))
122
123
124 (defmethod database-list-table-indexes (table (database generic-postgresql-database)
125                                         &key (owner nil))
126   (let ((indexrelids
127          (database-query
128           (format
129            nil
130            "select indexrelid from pg_index where indrelid=(select relfilenode from pg_class where relname='~A'~A)"
131            (string-downcase table)
132            (owner-clause owner))
133           database :auto nil))
134         (result nil))
135     (dolist (indexrelid indexrelids (nreverse result))
136       (push
137        (caar (database-query
138               (format nil "select relname from pg_class where relfilenode='~A'"
139                       (car indexrelid))
140               database nil nil))
141        result))))
142
143 (defmethod database-list-attributes ((table string)
144                                      (database generic-postgresql-database)
145                                      &key (owner nil))
146   (let* ((owner-clause
147           (cond ((stringp owner)
148                  (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
149                 ((null owner) " AND (not (relowner=1))")
150                 (t "")))
151          (result
152           (mapcar #'car
153                   (database-query
154                    (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND attisdropped = FALSE AND relname='~A'~A"
155                            (string-downcase table)
156                            owner-clause)
157                    database nil nil))))
158     (if result
159         (remove-if #'(lambda (it) (member it '("cmin"
160                                                "cmax"
161                                                "xmax"
162                                                "xmin"
163                                                "oid"
164                                                "ctid"
165                                                ;; kmr -- added tableoid
166                                                "tableoid") :test #'equal))
167                    result))))
168
169 (defmethod database-attribute-type (attribute (table string)
170                                     (database generic-postgresql-database)
171                                     &key (owner nil))
172   (let ((row (car (database-query
173                    (format nil "SELECT pg_type.typname,pg_attribute.attlen,pg_attribute.atttypmod,pg_attribute.attnotnull 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"
174                            (string-downcase table)
175                            (string-downcase attribute)
176                            (owner-clause owner))
177                    database nil nil))))
178     (when row
179       (destructuring-bind (typname attlen atttypmod attnull) row
180
181         (setf attlen (parse-integer attlen :junk-allowed t)
182               atttypmod (parse-integer atttypmod :junk-allowed t))
183
184         (let ((coltype (ensure-keyword typname))
185               (colnull (if (string-equal "f" attnull) 1 0))
186               collen
187               colprec)
188            (setf (values collen colprec)
189                  (case coltype
190                    ((:numeric :decimal)
191                     (if (= -1 atttypmod)
192                         (values nil nil)
193                         (values (ash (- atttypmod 4) -16)
194                                 (boole boole-and (- atttypmod 4) #xffff))))
195                    (otherwise
196                     (values
197                      (cond ((and (= -1 attlen) (= -1 atttypmod)) nil)
198                            ((= -1 attlen) (- atttypmod 4))
199                            (t attlen))
200                      nil))))
201            (values coltype collen colprec colnull))))))
202
203 (defmethod database-create-sequence (sequence-name
204                                      (database generic-postgresql-database))
205   (database-execute-command
206    (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
207    database))
208
209 (defmethod database-drop-sequence (sequence-name
210                                    (database generic-postgresql-database))
211   (database-execute-command
212    (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
213
214 (defmethod database-list-sequences ((database generic-postgresql-database)
215                                     &key (owner nil))
216   (database-list-objects-of-type database "S" owner))
217
218 (defmethod database-set-sequence-position (name (position integer)
219                                                 (database generic-postgresql-database))
220   (values
221    (parse-integer
222     (caar
223      (database-query
224       (format nil "SELECT SETVAL ('~A', ~A)" name position)
225       database nil nil)))))
226
227 (defmethod database-sequence-next (sequence-name
228                                    (database generic-postgresql-database))
229   (values
230    (parse-integer
231     (caar
232      (database-query
233       (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
234       database nil nil)))))
235
236 (defmethod database-sequence-last (sequence-name (database generic-postgresql-database))
237   (values
238    (parse-integer
239     (caar
240      (database-query
241       (concatenate 'string "SELECT LAST_VALUE FROM " sequence-name)
242       database nil nil)))))
243
244 (defmethod database-last-auto-increment-id ((database generic-postgresql-database) table column)
245   (let (column-helper seq-name)
246     (typecase table
247       (sql-ident (setf table (slot-value table 'name)))
248       (standard-db-class (setf table (view-table table))))
249     (typecase column
250       (sql-ident (setf column-helper (slot-value column 'name)))
251       (view-class-slot-definition-mixin
252        (setf column-helper (view-class-slot-column column))))
253     (setq seq-name (or (view-class-slot-autoincrement-sequence column)
254                        (convert-to-db-default-case (format nil "~a_~a_seq" table column-helper) database)))
255     (first (clsql:query (format nil "SELECT currval ('~a')" seq-name)
256                         :flatp t
257                         :database database
258                         :result-types '(:int)))))
259
260 (defmethod database-generate-column-definition (class slotdef (database generic-postgresql-database))
261   ; handle autoincr slots special
262   (when (or (and (listp (view-class-slot-db-constraints slotdef))
263                  (member :auto-increment (view-class-slot-db-constraints slotdef)))
264             (eql :auto-increment (view-class-slot-db-constraints slotdef))
265             (slot-value slotdef 'autoincrement-sequence))
266     (let ((sequence-name (database-make-autoincrement-sequence class slotdef database)))
267       (setf (view-class-slot-autoincrement-sequence slotdef) sequence-name)
268       (cond ((listp (view-class-slot-db-constraints slotdef))
269              (setf (view-class-slot-db-constraints slotdef)
270                    (remove :auto-increment 
271                            (view-class-slot-db-constraints slotdef)))
272              (unless (member :default (view-class-slot-db-constraints slotdef))
273                (setf (view-class-slot-db-constraints slotdef)
274                      (append
275                       (list :default (format nil "nextval('~a')" sequence-name))
276                       (view-class-slot-db-constraints slotdef)))))
277             (t
278              (setf (view-class-slot-db-constraints slotdef)
279                    (list :default (format nil "nextval('~a')" sequence-name)))))))
280   (call-next-method class slotdef database))
281
282 (defmethod database-make-autoincrement-sequence (table column (database generic-postgresql-database))
283   (let* ((table-name (view-table table))
284          (column-name (view-class-slot-column column))
285          (sequence-name (or (slot-value column 'autoincrement-sequence)
286                             (convert-to-db-default-case 
287                              (format nil "~a_~a_SEQ" table-name column-name) database))))
288     (unless (sequence-exists-p sequence-name  :database database)
289       (database-create-sequence sequence-name database))
290     sequence-name))
291
292 (defun postgresql-database-list (connection-spec type)
293   (destructuring-bind (host name &rest other-args) connection-spec
294     (declare (ignore name))
295     (let ((database (database-connect (list* host "template1" other-args)
296                                       type)))
297       (unwind-protect
298            (progn
299              (setf (slot-value database 'clsql-sys::state) :open)
300              (mapcar #'car (database-query "select datname from pg_database"
301                                            database nil nil)))
302         (progn
303           (database-disconnect database)
304           (setf (slot-value database 'clsql-sys::state) :closed))))))
305
306 (defmethod database-list (connection-spec (type (eql :postgresql)))
307   (postgresql-database-list connection-spec type))
308
309 (defmethod database-list (connection-spec (type (eql :postgresql-socket)))
310   (postgresql-database-list connection-spec type))
311
312 #+nil
313 (defmethod database-describe-table ((database generic-postgresql-database) table)
314   ;; MTP: LIST-ATTRIBUTE-TYPES currently executes separate queries for
315   ;; each attribute. It would be more efficient to have a single SQL
316   ;; query return the type data for all attributes. This code is
317   ;; retained as an example of how to do this for PostgreSQL.
318   (database-query
319    (format nil "select a.attname, t.typname
320                                from pg_class c, pg_attribute a, pg_type t
321                                where c.relname = '~a'
322                                    and a.attnum > 0
323                                    and a.attrelid = c.oid
324                                    and a.atttypid = t.oid"
325            (sql-escape (string-downcase table)))
326    database :auto nil))
327
328 ;;; Prepared statements
329
330 (defvar *next-prepared-id-num* 0)
331 (defun next-prepared-id ()
332   (let ((num (incf *next-prepared-id-num*)))
333     (format nil "CLSQL_PS_~D" num)))
334
335 (defclass postgresql-stmt ()
336   ((database :initarg :database :reader database)
337    (id :initarg :id :reader id)
338    (bindings :initarg :bindings :reader bindings)
339    (field-names :initarg :field-names :accessor stmt-field-names)
340    (result-types :initarg :result-types :reader result-types)))
341
342 (defun clsql-type->postgresql-type (type)
343   (cond
344     ((in type :int :integer) "INT4")
345     ((in type :short) "INT2")
346     ((in type :bigint) "INT8")
347     ((in type :float :double :number) "NUMERIC")
348     ((and (consp type) (in (car type) :char :varchar)) "VARCHAR")
349     (t
350      (error 'sql-user-error
351             :message
352             (format nil "Unknown clsql type ~A." type)))))
353
354 (defun prepared-sql-to-postgresql-sql (sql)
355   ;; FIXME: Convert #\? to "$n". Don't convert within strings
356   (declare (simple-string sql))
357   (with-output-to-string (out)
358     (do ((len (length sql))
359          (param 0)
360          (in-str nil)
361          (pos 0 (1+ pos)))
362         ((= len pos))
363       (declare (fixnum len param pos))
364       (let ((c (schar sql pos)))
365         (declare (character c))
366         (cond
367          ((or (char= c #\") (char= c #\'))
368           (setq in-str (not in-str))
369           (write-char c out))
370          ((and (char= c #\?) (not in-str))
371           (write-char #\$ out)
372           (write-string (write-to-string (incf param)) out))
373          (t
374           (write-char c out)))))))
375
376 (defmethod database-prepare (sql-stmt types (database generic-postgresql-database) result-types field-names)
377   (let ((id (next-prepared-id)))
378     (database-execute-command
379      (format nil "PREPARE ~A (~{~A~^,~}) AS ~A"
380              id
381              (mapcar #'clsql-type->postgresql-type types)
382              (prepared-sql-to-postgresql-sql sql-stmt))
383      database)
384     (make-instance 'postgresql-stmt
385                    :id id
386                    :database database
387                    :result-types result-types
388                    :field-names field-names
389                    :bindings (make-list (length types)))))
390
391 (defmethod database-bind-parameter ((stmt postgresql-stmt) position value)
392   (setf (nth (1- position) (bindings stmt)) value))
393
394 (defun binding-to-param (binding)
395   (typecase binding
396     (string
397      (concatenate 'string "'" (sql-escape-quotes binding) "'"))
398     (t
399      binding)))
400
401 (defmethod database-run-prepared ((stmt postgresql-stmt))
402   (with-slots (database id bindings field-names result-types) stmt
403     (let ((query (format nil "EXECUTE ~A (~{~A~^,~})"
404                          id (mapcar #'binding-to-param bindings))))
405       (cond
406        ((and field-names (not (consp field-names)))
407         (multiple-value-bind (res names)
408             (database-query query database result-types field-names)
409           (setf field-names names)
410           (values res names)))
411        (field-names
412         (values (nth-value 0 (database-query query database result-types nil))
413                 field-names))
414        (t
415         (database-query query database result-types field-names))))))
416
417 ;;; Capabilities
418
419 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
420   t)
421
422 (defmethod db-type-default-case ((db-type (eql :postgresql)))
423   :lower)
424
425 (defmethod db-type-has-prepared-stmt? ((db-type (eql :postgresql)))
426   t)
427
428 (defmethod db-type-has-prepared-stmt? ((db-type (eql :postgresql-socket)))
429   t)
430
431 (defmethod db-type-has-auto-increment? ((db-type (eql :postgresql)))
432   t)