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)))
219 (defmethod db-type-has-fancy-math? ((db-type (eql :postgresql)))
222 (defmethod db-type-default-case ((db-type (eql :postgresql)))