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 'simple-base-string)) args database
31 (db-type (eql :postgresql)))
32 (declare (ignore database))
34 (format nil "VARCHAR(~A)" (car args))
37 (defmethod database-get-type-specifier ((type (eql 'simple-string)) args database
38 (db-type (eql :postgresql)))
39 (declare (ignore database))
41 (format nil "VARCHAR(~A)" (car args))
44 (defmethod database-get-type-specifier ((type (eql 'string)) args database
45 (db-type (eql :postgresql)))
46 (declare (ignore database))
48 (format nil "VARCHAR(~A)" (car args))
51 (defmethod database-get-type-specifier ((type (eql 'wall-time)) args database
52 (db-type (eql :postgresql)))
53 (declare (ignore args database))
54 "TIMESTAMP WITHOUT TIME ZONE")
59 (defun owner-clause (owner)
64 " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))"
67 (format nil " AND (NOT (relowner=1))"))
70 (defun database-list-objects-of-type (database type owner)
74 "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
79 (defmethod database-list-tables ((database generic-postgresql-database)
81 (database-list-objects-of-type database "r" owner))
83 (defmethod database-list-views ((database generic-postgresql-database)
85 (database-list-objects-of-type database "v" owner))
87 (defmethod database-list-indexes ((database generic-postgresql-database)
89 (database-list-objects-of-type database "i" owner))
92 (defmethod database-list-table-indexes (table (database generic-postgresql-database)
98 "select indexrelid from pg_index where indrelid=(select relfilenode from pg_class where relname='~A'~A)"
99 (string-downcase table)
100 (owner-clause owner))
103 (dolist (indexrelid indexrelids (nreverse result))
105 (caar (database-query
106 (format nil "select relname from pg_class where relfilenode='~A'"
111 (defmethod database-list-attributes ((table string)
112 (database generic-postgresql-database)
115 (cond ((stringp owner)
116 (format nil " AND (relowner=(SELECT usesysid FROM pg_user WHERE usename='~A'))" owner))
117 ((null owner) " AND (not (relowner=1))")
122 (format nil "SELECT attname FROM pg_class,pg_attribute WHERE pg_class.oid=attrelid AND relname='~A'~A"
123 (string-downcase table)
127 (remove-if #'(lambda (it) (member it '("cmin"
133 ;; kmr -- added tableoid
134 "tableoid") :test #'equal))
137 (defmethod database-attribute-type (attribute (table string)
138 (database generic-postgresql-database)
140 (let ((row (car (database-query
141 (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"
142 (string-downcase table)
143 (string-downcase attribute)
144 (owner-clause owner))
148 (ensure-keyword (first row))
149 (if (string= "-1" (second row))
150 (- (parse-integer (third row) :junk-allowed t) 4)
151 (parse-integer (second row)))
153 (if (string-equal "f" (fourth row))
157 (defmethod database-create-sequence (sequence-name
158 (database generic-postgresql-database))
159 (database-execute-command
160 (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name))
163 (defmethod database-drop-sequence (sequence-name
164 (database generic-postgresql-database))
165 (database-execute-command
166 (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) database))
168 (defmethod database-list-sequences ((database generic-postgresql-database)
170 (database-list-objects-of-type database "S" owner))
172 (defmethod database-set-sequence-position (name (position integer)
173 (database generic-postgresql-database))
178 (format nil "SELECT SETVAL ('~A', ~A)" name position)
179 database nil nil)))))
181 (defmethod database-sequence-next (sequence-name
182 (database generic-postgresql-database))
187 (concatenate 'string "SELECT NEXTVAL ('" (sql-escape sequence-name) "')")
188 database nil nil)))))
190 (defmethod database-sequence-last (sequence-name (database generic-postgresql-database))
195 (concatenate 'string "SELECT LAST_VALUE ('" sequence-name "')")
196 database nil nil)))))
198 (defun postgresql-database-list (connection-spec type)
199 (destructuring-bind (host name user password) connection-spec
200 (declare (ignore name))
201 (let ((database (database-connect (list host "template1" user password)
205 (setf (slot-value database 'clsql-sys::state) :open)
206 (mapcar #'car (database-query "select datname from pg_database"
209 (database-disconnect database)
210 (setf (slot-value database 'clsql-sys::state) :closed))))))
212 (defmethod database-list (connection-spec (type (eql :postgresql)))
213 (postgresql-database-list connection-spec type))
215 (defmethod database-list (connection-spec (type (eql :postgresql-socket)))
216 (postgresql-database-list connection-spec type))
219 (defmethod database-describe-table ((database generic-postgresql-database) table)
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)))
233 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
236 (defmethod db-type-default-case ((db-type (eql :postgresql)))