Commits

Kirill Simonov committed 70a5eaa

pgsql introspector: skip tables the current user has no access to

  • Participants
  • Parent commits 8dd7662

Comments (0)

Files changed (6)

File src/htsql_pgsql/introspect.py

         connection = connect()
         cursor = connection.cursor()
         self.pg_namespace = self.fetch(cursor, 'pg_catalog.pg_namespace')
-        self.pg_class = self.fetch(cursor, 'pg_catalog.pg_class')
+        self.pg_class = self.fetch(cursor, 'pg_catalog.pg_class',
+                    extra="HAS_TABLE_PRIVILEGE(oid, 'SELECT') AS has_access")
         self.pg_class_by_namespace = self.group(self.pg_class,
                                                 self.pg_namespace,
                                                 'relnamespace')
         self.pg_constraint_by_class = self.group(self.pg_constraint,
                                                  self.pg_class, 'conrelid')
         self.pg_rewrite = self.fetch(cursor, 'pg_rewrite')
+        self.skip_list = []
+        for oid in sorted(self.pg_class):
+            rel = self.pg_class[oid]
+            if not rel.has_access:
+                schema_name = self.pg_namespace[rel.relnamespace].nspname
+                table_name = rel.relname
+                self.skip_list.append((schema_name, table_name))
 
-    def fetch(self, cursor, table_name, key_names=('oid',)):
+    def fetch(self, cursor, table_name, key_names=('oid',), extra=None):
         rows = {}
-        cursor.execute("SELECT %s, * FROM %s" % (", ".join(key_names),
-                                                 table_name))
+        select = "%s, *" % ", ".join(key_names)
+        if extra is not None:
+            select += ", %s" % extra
+        sql ="SELECT %s FROM %s" % (select, table_name)
+        cursor.execute(sql)
         for items in cursor.fetchall():
             key = tuple(items[idx] for idx in range(len(key_names)))
             if len(key) == 1:
         return True
 
     def permit_table(self, schema_name, table_name):
+        if (schema_name, table_name) in self.meta.skip_list:
+            return False
         return True
 
     def permit_column(self, schema_name, table_name, column_name):

File test/regress/output/mssql.yaml

           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(course?department='psych'&no=610).prerequisite
                                                     ^^^^^^^^^^^^
         - uri: /(course?department='psych'&no=610).course_classification
           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(prerequisite?of_department='capmrk'&of_course=818 &on_department='acc'&on_course=315).course
                                                                                                         ^^^^^^
         - uri: /(classification?code='modlanguage').part_of

File test/regress/output/mysql.yaml

           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(course?department='psych'&no=610).prerequisite
                                                     ^^^^^^^^^^^^
         - uri: /(course?department='psych'&no=610).course_classification
           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(prerequisite?of_department='capmrk'&of_course=818 &on_department='acc'&on_course=315).course
                                                                                                         ^^^^^^
         - uri: /(classification?code='modlanguage').part_of

File test/regress/output/oracle.yaml

           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(course?department='psych'&no=610).prerequisite
                                                     ^^^^^^^^^^^^
         - uri: /(course?department='psych'&no=610).course_classification
           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(prerequisite?of_department='capmrk'&of_course=818 &on_department='acc'&on_course=315).course
                                                                                                         ^^^^^^
         - uri: /(classification?code='modlanguage').part_of

File test/regress/output/pgsql.yaml

           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(course?department='psych'&no=610).prerequisite
                                                     ^^^^^^^^^^^^
         - uri: /(course?department='psych'&no=610).course_classification
           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(prerequisite?of_department='capmrk'&of_course=818 &on_department='acc'&on_course=315).course
                                                                                                         ^^^^^^
         - uri: /(classification?code='modlanguage').part_of

File test/regress/output/sqlite.yaml

           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(course?department='psych'&no=610).prerequisite
                                                     ^^^^^^^^^^^^
         - uri: /(course?department='psych'&no=610).course_classification
           headers:
           - [Content-Type, text/plain; charset=UTF-8]
           body: |
-            bind error: unable to resolve an identifier:
+            bind error: ambiguous name:
                 /(prerequisite?of_department='capmrk'&of_course=818 &on_department='acc'&on_course=315).course
                                                                                                         ^^^^^^
         - uri: /(classification?code='modlanguage').part_of