Use psycopg2 formatting to ensure correct quoting.
import psycopg2
import psycopg2.extensions
import psycopg2.extras
import psycopg2
import psycopg2.extensions
import psycopg2.extras
+from psycopg2 import sql as pysql
from nominatim.errors import UsageError
from nominatim.errors import UsageError
return self.fetchone()[0]
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.
class _Connection(psycopg2.extensions.connection):
""" A connection that provides the specialised cursor by default and
adds convenience functions for administrating the database.
- 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:
""" 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)
try:
with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response:
return response.read().decode('utf-8')
try:
with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response:
return response.read().decode('utf-8')
LOG.fatal('Failed to load URL: %s', url)
raise
LOG.fatal('Failed to load URL: %s', url)
raise
tables = [r[0] for r in cur]
for table in tables:
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)
_add_address_level_rows_from_entry(rows, entry)
with conn.cursor() as cur:
_add_address_level_rows_from_entry(rows, entry)
with conn.cursor() as cur:
- cur.execute('DROP TABLE IF EXISTS {}'.format(table))
cur.execute("""CREATE TABLE {} (country_code varchar(2),
class TEXT,
cur.execute("""CREATE TABLE {} (country_code varchar(2),
class TEXT,
Delete the place_classtype tables.
"""
LOG.warning('Cleaning database...')
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.
# 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:
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):
"""
def _convert_php_settings_if_needed(self, file_path):
"""