2 Preprocessing of SQL files.
7 def _get_partitions(conn):
8 """ Get the set of partitions currently in use.
10 with conn.cursor() as cur:
11 cur.execute('SELECT DISTINCT partition FROM country_name')
14 partitions.add(row[0])
19 def _get_tables(conn):
20 """ Return the set of tables currently in use.
21 Only includes non-partitioned
23 with conn.cursor() as cur:
24 cur.execute("SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
26 return set((row[0] for row in list(cur)))
28 class SQLPreprocessor: # pylint: disable=too-few-public-methods
29 """ A environment for preprocessing SQL files from the
32 The preprocessor provides a number of default filters and variables.
33 The variables may be overwritten when rendering an SQL file.
35 The preprocessing is currently based on the jinja2 templating library
36 and follows its syntax.
39 def __init__(self, conn, config, sqllib_dir):
40 self.env = jinja2.Environment(autoescape=False,
41 loader=jinja2.FileSystemLoader(str(sqllib_dir)))
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']
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())
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.
58 sql = self.env.get_template(name).render(**kwargs)
60 with conn.cursor() as cur: