]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/db/sql_preprocessor.py
introduce jinja2 for preprocessing SQL
[nominatim.git] / nominatim / db / sql_preprocessor.py
1 """
2 Preprocessing of SQL files.
3 """
4 import jinja2
5
6
7 def _get_partitions(conn):
8     """ Get the set of partitions currently in use.
9     """
10     with conn.cursor() as cur:
11         cur.execute('SELECT DISTINCT partition FROM country_name')
12         partitions = set([0])
13         for row in cur:
14             partitions.add(row[0])
15
16     return partitions
17
18
19 def _get_tables(conn):
20     """ Return the set of tables currently in use.
21         Only includes non-partitioned
22     """
23     with conn.cursor() as cur:
24         cur.execute("SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
25
26         return set((row[0] for row in list(cur)))
27
28 class SQLPreprocessor: # pylint: disable=too-few-public-methods
29     """ A environment for preprocessing SQL files from the
30         lib-sql directory.
31
32         The preprocessor provides a number of default filters and variables.
33         The variables may be overwritten when rendering an SQL file.
34
35         The preprocessing is currently based on the jinja2 templating library
36         and follows its syntax.
37     """
38
39     def __init__(self, conn, config, sqllib_dir):
40         self.env = jinja2.Environment(autoescape=False,
41                                       loader=jinja2.FileSystemLoader(str(sqllib_dir)))
42
43         db_info = {}
44         db_info['partitions'] = _get_partitions(conn)
45         db_info['tables'] = _get_tables(conn)
46         db_info['reverse_only'] = 'search_name' not in db_info['tables']
47
48         self.env.globals['config'] = config
49         self.env.globals['db'] = db_info
50         self.env.globals['modulepath'] = config.DATABASE_MODULE_PATH or \
51                                          str((config.project_dir / 'module').resolve())
52
53
54     def run_sql_file(self, conn, name, **kwargs):
55         """ Execute the given SQL file on the connection. The keyword arguments
56             may supply additional parameters for preprocessing.
57         """
58         sql = self.env.get_template(name).render(**kwargs)
59
60         with conn.cursor() as cur:
61             cur.execute(sql)
62         conn.commit()