pgtoolkit/pgbrowser.py
changeset 31 c2e6e24b83d9
parent 12 203be9022b46
child 32 d59c473c9ad7
--- a/pgtoolkit/pgbrowser.py	Mon Feb 27 15:12:40 2012 +0100
+++ b/pgtoolkit/pgbrowser.py	Mon Mar 05 18:36:46 2012 +0100
@@ -149,7 +149,7 @@
         return self._schemas
     schemas = property(getschemas)
                     
-    def _query(self, query, *args):
+    def _query(self, query, args):
         try:
             curs = self.conn.cursor()
             curs.execute(query, args)
@@ -177,7 +177,7 @@
             FROM pg_catalog.pg_database d
             JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid
             ORDER BY 1;
-            ''')
+            ''', [])
 
     def list_schemas(self):
         return self._query('''
@@ -193,7 +193,7 @@
                 END AS "system"
             FROM pg_catalog.pg_namespace n
             ORDER BY 1;
-            ''')
+            ''', [])
 
     def list_tables(self, schema='public'):
         return self._query('''
@@ -206,7 +206,7 @@
             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
             WHERE n.nspname = %s AND c.relkind IN ('r','s','')
             ORDER BY 1;
-            ''', schema)
+            ''', [schema])
 
     def list_columns(self, table, schema='public', order=2):
         return self._query('''
@@ -223,7 +223,7 @@
             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
             LEFT JOIN pg_catalog.pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE n.nspname = %s AND c.relname = %s AND a.attnum > 0 AND NOT a.attisdropped
-            ORDER BY ''' + str(order), schema, table)
+            ORDER BY ''' + str(order), [schema, table])
 
     def list_constraints(self, table, schema='public'):
         return self._query('''
@@ -236,7 +236,7 @@
             JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
             WHERE n.nspname = %s AND c.relname = %s
             ORDER BY 1
-            ''', schema, table)
+            ''', [schema, table])
 
     def list_indexes(self, table, schema='public'):
         return self._query('''
@@ -246,13 +246,27 @@
                 i.indisunique as "unique",
                 i.indisclustered as "clustered",
                 i.indisvalid as "valid",
-                pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) as "definition"
+                pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) as "definition",
+                ARRAY(SELECT a.attname FROM pg_catalog.pg_attribute a WHERE a.attrelid = c2.oid ORDER BY attnum) AS "columns"
                 --c2.reltablespace as "tablespace_oid"
             FROM pg_catalog.pg_class c
             JOIN pg_catalog.pg_index i ON c.oid = i.indrelid
             JOIN pg_catalog.pg_class c2 ON i.indexrelid = c2.oid
             JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
-            WHERE n.nspname = %s AND c.relname = %s
+            WHERE n.nspname = %(schema)s AND c.relname = %(table)s
             ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname
-            ''', schema, table)
+            ''', {'schema': schema, 'table': table})
 
+    def list_column_usage(self, table, column, schema='public'):
+        '''List objects using the column.
+        
+        These objects may block alteration of column. Currently only views are listed.
+        
+        '''
+        return self._query('''
+            SELECT
+                'view' AS type, view_schema AS schema, view_name AS name
+            FROM information_schema.view_column_usage
+            WHERE table_schema=%(schema)s AND table_name=%(table)s AND column_name=%(column)s
+            ''', {'schema':schema, 'table':table, 'column':column})
+