Check the character case of table and schema names
authorKarl O. Pinc <kop@karlpinc.com>
Tue, 2 Feb 2021 22:50:54 +0000 (16:50 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Tue, 2 Feb 2021 22:50:54 +0000 (16:50 -0600)
src/pgwui_upload_core/views/upload.py

index 1eca47239bb1334a0663edaef4b5a2385b2f6490..dacd51dd6c7ce8b6d3450da391450f59a2afafb1 100644 (file)
@@ -145,7 +145,7 @@ class BaseTableUploadHandler(TabularFileUploadHandler):
         self.write_double_key(response)
         return response
 
-    def resolve_table(self, qualified_table):
+    def resolve_normalized_table(self, qualified_table):
         '''Return (schema, table) tuple of table name, or raise exception
         if not resolvable.
         '''
@@ -174,6 +174,33 @@ class BaseTableUploadHandler(TabularFileUploadHandler):
                 raise
         return self.cur.fetchone()
 
+    def resolve_table(self, qualified_table):
+        '''Return (schema, table) tuple of table name or raise exception
+        if character case is wrong
+        '''
+        (schema, table) = self.resolve_normalized_table(qualified_table)
+        norm_qualified_table = qualified_table.lower()
+        if schema:
+            norm_schema = schema.lower()
+            len_schema = len(schema)
+            if (norm_qualified_table[0:len_schema] == norm_schema
+                    and qualified_table[0:len_schema] != schema):
+                raise upload_ex.MissingSchemaError(
+                    'No such schema',
+                    (f'The schema ({qualified_table[0:len_schema]}) does '
+                     'not exist, but the same-but-for-character-case '
+                     f'schema ({schema}) does'))
+        norm_table = table.lower()
+        len_table = len(table)
+        if (norm_qualified_table[- len_table:] == norm_table
+                and qualified_table[- len_table:] != table):
+            raise upload_ex.MissingTableError(
+                'No such table or view',
+                (f'The table ({qualified_table[- len_table:]}) does '
+                 'not exist, but the same-but-for-character-case '
+                 f'table ({table}) does'))
+        return (schema, table)
+
     def good_table(self, schema, table):
         '''Is the supplied table or view insertable?
         '''