1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; Generic postgresql layer, used by db-postgresql and db-postgresql-socket
8 ;;;; This file is part of CLSQL.
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
15 (in-package #:clsql-sys)
17 (defclass generic-postgresql-database (database)
19 (:documentation "Encapsulate same behavior across postgresql and postgresql-socket backends."))
25 (defmethod database-get-type-specifier (type args database
26 (db-type (eql :postgresql)))
27 (declare (ignore type args database))
30 (defmethod database-get-type-specifier ((type (eql 'string)) args database
31 (db-type (eql :postgresql)))
32 (declare (ignore database))
34 (format nil "CHAR(~A)" (car args))
37 (defmethod database-get-type-specifier ((type (eql 'wall-time)) args database
38 (db-type (eql :postgresql)))
39 (declare (ignore args database))
40 "TIMESTAMP WITHOUT TIME ZONE")
45 (defun owner-clause (owner)
50 " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))"
53 (format nil " AND (NOT (relowner=1))"))
56 (defun database-list-objects-of-type (database type owner)
60 "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
65 (defmethod database-list-tables ((database generic-postgresql-database)
67 (database-list-objects-of-type database "r" owner))
69 (defmethod database-list-views ((database generic-postgresql-database)
71 (database-list-objects-of-type database "v" owner))
73 (defmethod database-list-indexes ((database generic-postgresql-database)
75 (database-list-objects-of-type database "i" owner))
78 (defmethod database-list-table-indexes (table (database generic-postgresql-database)
84 "select indexrelid from pg_index where indrelid=(select relfilenode from pg_class where relname='~A'~A)"
85 (string-downcase table)
89 (dolist (indexrelid indexrelids (nreverse result))
92 (format nil "select relname from pg_class where relfilenode='~A'"
97 (defmethod database-list-attributes ((table string)
98 (database generic-postgresql-database)
101 (cond ((stringp owner)
102 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
103 ((null owner) " AND (not (relowner=1))")
108 (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
109 (string-downcase table)
113 (remove-if #'(lambda (it) (member it '("cmin"
119 ;; kmr -- added tableoid
120 "tableoid") :test #'equal))
123 (defmethod database-attribute-type (attribute (table string)
124 (database generic-postgresql-database)
126 (let ((row (car (database-query
127 (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"
128 (string-downcase table)
129 (string-downcase attribute)
130 (owner-clause owner))
134 (ensure-keyword (first row))
135 (if (string= "-1" (second row))
136 (- (parse-integer (third row) :junk-allowed t) 4)
137 (parse-integer (second row)))
139 (if (string-equal "f" (fourth row))
143 (defmethod database-create-sequence (sequence-name
144 (database generic-postgresql-database))
145 (database-execute-command
146 (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
149 (defmethod database-drop-sequence (sequence-name
150 (database generic-postgresql-database))
151 (database-execute-command
152 (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
154 (defmethod database-list-sequences ((database generic-postgresql-database)
156 (database-list-objects-of-type database "S" owner))
158 (defmethod database-set-sequence-position (name (position integer)
159 (database generic-postgresql-database))
164 (format nil "SELECT SETVAL ('~A', ~A)" name position)
165 database nil nil)))))
167 (defmethod database-sequence-next (sequence-name
168 (database generic-postgresql-database))
173 (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
174 database nil nil)))))
176 (defmethod database-sequence-last (sequence-name (database generic-postgresql-database))
181 (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
182 database nil nil)))))
184 (defun postgresql-database-list (connection-spec type)
185 (destructuring-bind (host name user password) connection-spec
186 (declare (ignore name))
187 (let ((database (database-connect (list host "template1" user password)
191 (setf (slot-value database 'clsql-sys::state) :open)
192 (mapcar #'car (database-query "select datname from pg_database"
195 (database-disconnect database)
196 (setf (slot-value database 'clsql-sys::state) :closed))))))
198 (defmethod database-list (connection-spec (type (eql :postgresql)))
199 (postgresql-database-list connection-spec type))
201 (defmethod database-list (connection-spec (type (eql :postgresql-socket)))
202 (postgresql-database-list connection-spec type))
205 (defmethod database-describe-table ((database generic-postgresql-database) table)
207 (format nil "select a.attname, t.typname
208 from pg_class c, pg_attribute a, pg_type t
209 where c.relname = '~a'
211 and a.attrelid = c.oid
212 and a.atttypid = t.oid"
213 (sql-escape (string-downcase table)))
216 ;;; Prepared statements
218 (defvar *next-prepared-id-num* 0)
219 (defun next-prepared-id ()
220 (let ((num (incf *next-prepared-id-num*)))
221 (format nil "CLSQL_PS_~D" num)))
223 (defclass postgresql-stmt ()
224 ((database :initarg :database :reader database)
225 (id :initarg :id :reader id)
226 (bindings :initarg :bindings :reader bindings)
227 (field-names :initarg :field-names :accessor stmt-field-names)
228 (result-types :initarg :result-types :reader result-types)))
230 (defun clsql-type->postgresql-type (type)
233 ((:int :integer) "INT4")
235 ((:number :numeric :float) "NUMERIC")
238 (defun prepared-sql-to-postgresql-sql (sql)
239 ;; FIXME: Convert #\? to "$n". Don't convert within strings
240 (declare (simple-string sql))
241 (with-output-to-string (out)
242 (do ((len (length sql))
247 (declare (fixnum len param pos))
248 (let ((c (schar sql pos)))
249 (declare (character c))
251 ((or (char= c #\") (char= c #\'))
252 (setq in-str (not in-str))
254 ((and (char= c #\?) (not in-str))
256 (write-string (write-to-string (incf param)) out))
258 (write-char c out)))))))
260 (defmethod database-prepare (sql-stmt types (database generic-postgresql-database) result-types field-names)
261 (let ((id (next-prepared-id)))
262 (database-execute-command
263 (format nil "PREPARE ~A (~{~A~^,~}) AS ~A"
265 (mapcar #'clsql-type->postgresql-type types)
266 (prepared-sql-to-postgresql-sql sql-stmt))
268 (make-instance 'postgresql-stmt
271 :result-types result-types
272 :field-names field-names
273 :bindings (make-list (length types)))))
275 (defmethod database-bind-parameter ((stmt postgresql-stmt) position value)
276 (setf (nth (1- position) (bindings stmt)) value))
278 (defun binding-to-param (binding)
281 (concatenate 'string "'" (sql-escape-quotes binding) "'"))
285 (defmethod database-run-prepared ((stmt postgresql-stmt))
286 (with-slots (database id bindings field-names result-types) stmt
287 (let ((query (format nil "EXECUTE ~A (~{~A~^,~})"
288 id (mapcar #'binding-to-param bindings))))
290 ((and field-names (not (consp field-names)))
291 (multiple-value-bind (res names)
292 (database-query query database result-types field-names)
293 (setf field-names names)
296 (values (nth-value 0 (database-query query database result-types nil))
299 (database-query query database result-types field-names))))))
303 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
306 (defmethod db-type-default-case ((db-type (eql :postgresql)))
309 (defmethod db-type-has-prepared-stmt? ((db-type (eql :postgresql)))
312 (defmethod db-type-has-prepared-stmt? ((db-type (eql :postgresql-socket)))