avoid repeated patterns for table name

This commit is contained in:
Sarah Hoffmann
2021-07-12 11:33:09 +02:00
parent a08ef43e40
commit d5a1883b62

View File

@@ -20,6 +20,12 @@ from nominatim.errors import UsageError
from nominatim.tools.special_phrases.importer_statistics import SpecialPhrasesImporterStatistics from nominatim.tools.special_phrases.importer_statistics import SpecialPhrasesImporterStatistics
LOG = logging.getLogger() LOG = logging.getLogger()
def _classtype_table(phrase_class, phrase_type):
""" Return the name of the table for the given class and type.
"""
return f'place_classtype_{phrase_class}_{phrase_type}'
class SPImporter(): class SPImporter():
# pylint: disable-msg=too-many-instance-attributes # pylint: disable-msg=too-many-instance-attributes
""" """
@@ -164,7 +170,7 @@ class SPImporter():
phrase_class = pair[0] phrase_class = pair[0]
phrase_type = pair[1] phrase_type = pair[1]
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type) table_name = _classtype_table(phrase_class, phrase_type)
if table_name in self.table_phrases_to_delete: if table_name in self.table_phrases_to_delete:
self.statistics_handler.notify_one_table_ignored() self.statistics_handler.notify_one_table_ignored()
@@ -193,7 +199,7 @@ class SPImporter():
""" """
Create table place_classtype of the given phrase_class/phrase_type if doesn't exit. Create table place_classtype of the given phrase_class/phrase_type if doesn't exit.
""" """
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type) table_name = _classtype_table(phrase_class, phrase_type)
with self.db_connection.cursor() as db_cursor: with self.db_connection.cursor() as db_cursor:
db_cursor.execute(SQL(""" db_cursor.execute(SQL("""
CREATE TABLE IF NOT EXISTS {{}} {} CREATE TABLE IF NOT EXISTS {{}} {}
@@ -208,7 +214,7 @@ class SPImporter():
Create indexes on centroid and place_id for the place_classtype table. Create indexes on centroid and place_id for the place_classtype table.
""" """
index_prefix = 'idx_place_classtype_{}_{}_'.format(phrase_class, phrase_type) index_prefix = 'idx_place_classtype_{}_{}_'.format(phrase_class, phrase_type)
base_table = 'place_classtype_{}_{}'.format(phrase_class, phrase_type) base_table = _classtype_table(phrase_class, phrase_type)
#Index on centroid #Index on centroid
if not self.db_connection.index_exists(index_prefix + 'centroid'): if not self.db_connection.index_exists(index_prefix + 'centroid'):
with self.db_connection.cursor() as db_cursor: with self.db_connection.cursor() as db_cursor:
@@ -230,7 +236,7 @@ class SPImporter():
""" """
Grant access on read to the table place_classtype for the webuser. Grant access on read to the table place_classtype for the webuser.
""" """
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type) table_name = _classtype_table(phrase_class, phrase_type)
with self.db_connection.cursor() as db_cursor: with self.db_connection.cursor() as db_cursor:
db_cursor.execute(SQL("""GRANT SELECT ON {} TO {}""") db_cursor.execute(SQL("""GRANT SELECT ON {} TO {}""")
.format(Identifier(table_name), .format(Identifier(table_name),