]> git.openstreetmap.org Git - nominatim.git/commitdiff
provide wrapper function for DROP TABLE
authorSarah Hoffmann <lonvia@denofr.de>
Mon, 12 Jul 2021 18:32:46 +0000 (20:32 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Mon, 12 Jul 2021 18:32:46 +0000 (20:32 +0200)
Use psycopg2 formatting to ensure correct quoting.

nominatim/db/connection.py
nominatim/tools/exec_utils.py
nominatim/tools/freeze.py
nominatim/tools/refresh.py
nominatim/tools/special_phrases/sp_importer.py

index ac8d7c858090a725142fc9a3bf8546baf6caac8b..1eb3599bee64a8cdfbb8c08972138fc6c47e282a 100644 (file)
@@ -8,6 +8,7 @@ import os
 import psycopg2
 import psycopg2.extensions
 import psycopg2.extras
+from psycopg2 import sql as pysql
 
 from nominatim.errors import UsageError
 
@@ -37,6 +38,22 @@ class _Cursor(psycopg2.extras.DictCursor):
         return self.fetchone()[0]
 
 
+    def drop_table(self, name, if_exists=True, cascade=False):
+        """ Drop the table with the given name.
+            Set `if_exists` to False if a non-existant table should raise
+            an exception instead of just being ignored. If 'cascade' is set
+            to True then all dependent tables are deleted as well.
+        """
+        sql = 'DROP TABLE '
+        if if_exists:
+            sql += 'IF EXISTS '
+        sql += '{}'
+        if cascade:
+            sql += ' CASCADE'
+
+        self.execute(pysql.SQL(sql).format(pysql.Identifier(name)))
+
+
 class _Connection(psycopg2.extensions.connection):
     """ A connection that provides the specialised cursor by default and
         adds convenience functions for administrating the database.
@@ -75,14 +92,13 @@ class _Connection(psycopg2.extensions.connection):
         return True
 
 
-    def drop_table(self, name, if_exists=True):
+    def drop_table(self, name, if_exists=True, cascade=False):
         """ Drop the table with the given name.
             Set `if_exists` to False if a non-existant table should raise
             an exception instead of just being ignored.
         """
         with self.cursor() as cur:
-            cur.execute("""DROP TABLE {} "{}"
-                        """.format('IF EXISTS' if if_exists else '', name))
+            cur.drop_table(name, if_exists, cascade)
         self.commit()
 
 
index f91c56542759895b6d52d0a7a2f8ee6a2ceccd48..72d252b7a83abdeb434912729bd595cfb10b3f7a 100644 (file)
@@ -141,6 +141,6 @@ def get_url(url):
     try:
         with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response:
             return response.read().decode('utf-8')
-    except:
+    except Exception:
         LOG.fatal('Failed to load URL: %s', url)
         raise
index cc1bf97e09a92556cc1cc79cd47cfe03e605181c..ce0b5cde033345e00ca8894d946b85b1d7f6842c 100644 (file)
@@ -29,7 +29,7 @@ def drop_update_tables(conn):
         tables = [r[0] for r in cur]
 
         for table in tables:
-            cur.execute('DROP TABLE IF EXISTS "{}" CASCADE'.format(table))
+            cur.drop_table(table, cascade=True)
 
     conn.commit()
 
index 97e2e0374b644c4d7721ed30d38f6e573fab54b5..6b18f67d8e6b064e98c6c5019f688cf2753a3d37 100644 (file)
@@ -49,7 +49,7 @@ def load_address_levels(conn, table, levels):
         _add_address_level_rows_from_entry(rows, entry)
 
     with conn.cursor() as cur:
-        cur.execute('DROP TABLE IF EXISTS {}'.format(table))
+        cur.drop_table(table)
 
         cur.execute("""CREATE TABLE {} (country_code varchar(2),
                                         class TEXT,
index a26ea8a6201ae8b10891a85e457fbbe12778e902..791f4dc323eb61f4659b34c43af624ed37c4c1e9 100644 (file)
@@ -248,20 +248,14 @@ class SPImporter():
             Delete the place_classtype tables.
         """
         LOG.warning('Cleaning database...')
-        # Array containing all queries to execute.
-        # Contains tuples of format (query, parameters)
-        queries_parameters = []
 
         # Delete place_classtype tables corresponding to class/type which
         # are not on the wiki anymore.
-        for table in self.table_phrases_to_delete:
-            self.statistics_handler.notify_one_table_deleted()
-            query = SQL('DROP TABLE IF EXISTS {}').format(Identifier(table))
-            queries_parameters.append((query, ()))
-
         with self.db_connection.cursor() as db_cursor:
-            for query, parameters in queries_parameters:
-                db_cursor.execute(query, parameters)
+            for table in self.table_phrases_to_delete:
+                self.statistics_handler.notify_one_table_deleted()
+                db_cursor.drop_table(table)
+
 
     def _convert_php_settings_if_needed(self, file_path):
         """