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 'tinyint)) args database
38 (db-type (eql :postgresql)))
39 (declare (ignore args database))
42 (defmethod database-get-type-specifier ((type (eql 'smallint)) args database
43 (db-type (eql :postgresql)))
44 (declare (ignore args database))
47 (defmethod database-get-type-specifier ((type (eql 'wall-time)) args database
48 (db-type (eql :postgresql)))
49 (declare (ignore args database))
50 "TIMESTAMP WITHOUT TIME ZONE")
55 (defun owner-clause (owner)
60 " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))"
63 (format nil " AND (NOT (relowner=1))"))
66 (defun database-list-objects-of-type (database type owner)
70 "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
75 (defmethod database-list-tables ((database generic-postgresql-database)
77 (database-list-objects-of-type database "r" owner))
79 (defmethod database-list-views ((database generic-postgresql-database)
81 (database-list-objects-of-type database "v" owner))
83 (defmethod database-list-indexes ((database generic-postgresql-database)
85 (database-list-objects-of-type database "i" owner))
88 (defmethod database-list-table-indexes (table (database generic-postgresql-database)
94 "select indexrelid from pg_index where indrelid=(select relfilenode from pg_class where relname='~A'~A)"
95 (string-downcase table)
99 (dolist (indexrelid indexrelids (nreverse result))
101 (caar (database-query
102 (format nil "select relname from pg_class where relfilenode='~A'"
107 (defmethod database-list-attributes ((table string)
108 (database generic-postgresql-database)
111 (cond ((stringp owner)
112 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
113 ((null owner) " AND (not (relowner=1))")
118 (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND attisdropped = FALSE AND relname='~A'~A"
119 (string-downcase table)
123 (remove-if #'(lambda (it) (member it '("cmin"
129 ;; kmr -- added tableoid
130 "tableoid") :test #'equal))
133 (defmethod database-attribute-type (attribute (table string)
134 (database generic-postgresql-database)
136 (let ((row (car (database-query
137 (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"
138 (string-downcase table)
139 (string-downcase attribute)
140 (owner-clause owner))
144 (ensure-keyword (first row))
145 (if (string= "-1" (second row))
146 (- (parse-integer (third row) :junk-allowed t) 4)
147 (parse-integer (second row)))
149 (if (string-equal "f" (fourth row))
153 (defmethod database-create-sequence (sequence-name
154 (database generic-postgresql-database))
155 (database-execute-command
156 (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
159 (defmethod database-drop-sequence (sequence-name
160 (database generic-postgresql-database))
161 (database-execute-command
162 (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
164 (defmethod database-list-sequences ((database generic-postgresql-database)
166 (database-list-objects-of-type database "S" owner))
168 (defmethod database-set-sequence-position (name (position integer)
169 (database generic-postgresql-database))
174 (format nil "SELECT SETVAL ('~A', ~A)" name position)
175 database nil nil)))))
177 (defmethod database-sequence-next (sequence-name
178 (database generic-postgresql-database))
183 (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
184 database nil nil)))))
186 (defmethod database-sequence-last (sequence-name (database generic-postgresql-database))
191 (concatenate 'string "SELECT LAST_VALUE FROM " sequence-name)
192 database nil nil)))))
194 (defun postgresql-database-list (connection-spec type)
195 (destructuring-bind (host name user password) connection-spec
196 (declare (ignore name))
197 (let ((database (database-connect (list host "template1" user password)
201 (setf (slot-value database 'clsql-sys::state) :open)
202 (mapcar #'car (database-query "select datname from pg_database"
205 (database-disconnect database)
206 (setf (slot-value database 'clsql-sys::state) :closed))))))
208 (defmethod database-list (connection-spec (type (eql :postgresql)))
209 (postgresql-database-list connection-spec type))
211 (defmethod database-list (connection-spec (type (eql :postgresql-socket)))
212 (postgresql-database-list connection-spec type))
215 (defmethod database-describe-table ((database generic-postgresql-database) table)
216 ;; MTP: LIST-ATTRIBUTE-TYPES currently executes separate queries for
217 ;; each attribute. It would be more efficient to have a single SQL
218 ;; query return the type data for all attributes. This code is
219 ;; retained as an example of how to do this for PostgreSQL.
221 (format nil "select a.attname, t.typname
222 from pg_class c, pg_attribute a, pg_type t
223 where c.relname = '~a'
225 and a.attrelid = c.oid
226 and a.atttypid = t.oid"
227 (sql-escape (string-downcase table)))
230 ;;; Prepared statements
232 (defvar *next-prepared-id-num* 0)
233 (defun next-prepared-id ()
234 (let ((num (incf *next-prepared-id-num*)))
235 (format nil "CLSQL_PS_~D" num)))
237 (defclass postgresql-stmt ()
238 ((database :initarg :database :reader database)
239 (id :initarg :id :reader id)
240 (bindings :initarg :bindings :reader bindings)
241 (field-names :initarg :field-names :accessor stmt-field-names)
242 (result-types :initarg :result-types :reader result-types)))
244 (defun clsql-type->postgresql-type (type)
246 ((in type :int :integer) "INT4")
247 ((in type :short) "INT2")
248 ((in type :bigint) "INT8")
249 ((in type :float :double :number) "NUMERIC")
250 ((and (consp type) (in (car type) :char :varchar)) "VARCHAR")
252 (error 'sql-user-error
254 (format nil "Unknown clsql type ~A." type)))))
256 (defun prepared-sql-to-postgresql-sql (sql)
257 ;; FIXME: Convert #\? to "$n". Don't convert within strings
258 (declare (simple-string sql))
259 (with-output-to-string (out)
260 (do ((len (length sql))
265 (declare (fixnum len param pos))
266 (let ((c (schar sql pos)))
267 (declare (character c))
269 ((or (char= c #\") (char= c #\'))
270 (setq in-str (not in-str))
272 ((and (char= c #\?) (not in-str))
274 (write-string (write-to-string (incf param)) out))
276 (write-char c out)))))))
278 (defmethod database-prepare (sql-stmt types (database generic-postgresql-database) result-types field-names)
279 (let ((id (next-prepared-id)))
280 (database-execute-command
281 (format nil "PREPARE ~A (~{~A~^,~}) AS ~A"
283 (mapcar #'clsql-type->postgresql-type types)
284 (prepared-sql-to-postgresql-sql sql-stmt))
286 (make-instance 'postgresql-stmt
289 :result-types result-types
290 :field-names field-names
291 :bindings (make-list (length types)))))
293 (defmethod database-bind-parameter ((stmt postgresql-stmt) position value)
294 (setf (nth (1- position) (bindings stmt)) value))
296 (defun binding-to-param (binding)
299 (concatenate 'string "'" (sql-escape-quotes binding) "'"))
303 (defmethod database-run-prepared ((stmt postgresql-stmt))
304 (with-slots (database id bindings field-names result-types) stmt
305 (let ((query (format nil "EXECUTE ~A (~{~A~^,~})"
306 id (mapcar #'binding-to-param bindings))))
308 ((and field-names (not (consp field-names)))
309 (multiple-value-bind (res names)
310 (database-query query database result-types field-names)
311 (setf field-names names)
314 (values (nth-value 0 (database-query query database result-types nil))
317 (database-query query database result-types field-names))))))
321 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
324 (defmethod db-type-default-case ((db-type (eql :postgresql)))
327 (defmethod db-type-has-prepared-stmt? ((db-type (eql :postgresql)))
330 (defmethod db-type-has-prepared-stmt? ((db-type (eql :postgresql-socket)))