r11510: disconnect pooled session already removed from stack with
[clsql.git] / sql / generic-postgresql.lisp
index 4958eaa72b74eb9effd8e3bd693850efdacccf3b..a2dd4372daf6d9c30d8fd925b48b99a4fefa6bd6 100644 (file)
@@ -15,7 +15,7 @@
 (in-package #:clsql-sys)
 
 (defclass generic-postgresql-database (database)
-  ()
+  ((has-table-pg_roles :type boolean :reader has-table-pg_roles :initform nil))
   (:documentation "Encapsulate same behavior across postgresql and postgresql-socket backends."))
 
 
      " AND (relowner=(SELECT usesysid FROM pg_user WHERE (usename='~A')))"
      owner))
    ((null owner)
-    (format nil " AND (NOT (relowner=1))"))
+    (format nil " AND (relowner<>(SELECT usesysid FROM pg_user WHERE usename='postgres'))"))
    (t "")))
 
+(defun has-table (name database)
+  (let ((name-retrieved
+         (caar (database-query
+                (format nil "SELECT relname FROM pg_class WHERE relname='~A'"
+                        name)
+                database nil nil))))
+    (if (and (stringp name-retrieved) (plusp (length name-retrieved)))
+        t
+        nil)))
+
+(defmethod slot-unbound (class (obj generic-postgresql-database)
+                         (slot (eql 'has-table-pg_roles)))
+  ;; Lazily cache slot value
+  (declare (ignore class))
+  (setf (slot-value obj 'has-table-pg_roles) (has-table "pg_roles" obj)))
+
 (defun database-list-objects-of-type (database type owner)
   (mapcar #'car
          (database-query
           (format nil
-                  "SELECT relname FROM pg_class WHERE (relkind = '~A')~A"
-                   #+nil
-                   (if (not (eq owner :all))
-                       ;; The below query fails on versions of postgresql
-                       ;; (such as 7.4) that lack the pg_roles table
+                   (if (and (has-table-pg_roles database)
+                            (not (eq owner :all)))
                        "
  SELECT c.relname
  FROM pg_catalog.pg_class c