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