1 # SPDX-License-Identifier: GPL-2.0-only
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2022 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Preprocessing of SQL files.
 
  10 from typing import Set, Dict, Any
 
  13 from nominatim.db.connection import Connection
 
  14 from nominatim.config import Configuration
 
  16 def _get_partitions(conn: Connection) -> Set[int]:
 
  17     """ Get the set of partitions currently in use.
 
  19     with conn.cursor() as cur:
 
  20         cur.execute('SELECT DISTINCT partition FROM country_name')
 
  23             partitions.add(row[0])
 
  28 def _get_tables(conn: Connection) -> Set[str]:
 
  29     """ Return the set of tables currently in use.
 
  30         Only includes non-partitioned
 
  32     with conn.cursor() as cur:
 
  33         cur.execute("SELECT tablename FROM pg_tables WHERE schemaname = 'public'")
 
  35         return set((row[0] for row in list(cur)))
 
  38 def _setup_tablespace_sql(config: Configuration) -> Dict[str, str]:
 
  39     """ Returns a dict with tablespace expressions for the different tablespace
 
  40         kinds depending on whether a tablespace is configured or not.
 
  43     for subset in ('ADDRESS', 'SEARCH', 'AUX'):
 
  44         for kind in ('DATA', 'INDEX'):
 
  45             tspace = getattr(config, f'TABLESPACE_{subset}_{kind}')
 
  47                 tspace = f'TABLESPACE "{tspace}"'
 
  48             out[f'{subset.lower()}_{kind.lower()}'] = tspace
 
  53 def _setup_postgresql_features(conn: Connection) -> Dict[str, Any]:
 
  54     """ Set up a dictionary with various optional Postgresql/Postgis features that
 
  55         depend on the database version.
 
  57     pg_version = conn.server_version_tuple()
 
  58     postgis_version = conn.postgis_version_tuple()
 
  60         'has_index_non_key_column': pg_version >= (11, 0, 0),
 
  61         'spgist_geom' : 'SPGIST' if postgis_version >= (3, 0) else 'GIST'
 
  64 class SQLPreprocessor:
 
  65     """ A environment for preprocessing SQL files from the
 
  68         The preprocessor provides a number of default filters and variables.
 
  69         The variables may be overwritten when rendering an SQL file.
 
  71         The preprocessing is currently based on the jinja2 templating library
 
  72         and follows its syntax.
 
  75     def __init__(self, conn: Connection, config: Configuration) -> None:
 
  76         self.env = jinja2.Environment(autoescape=False,
 
  77                                       loader=jinja2.FileSystemLoader(str(config.lib_dir.sql)))
 
  79         db_info: Dict[str, Any] = {}
 
  80         db_info['partitions'] = _get_partitions(conn)
 
  81         db_info['tables'] = _get_tables(conn)
 
  82         db_info['reverse_only'] = 'search_name' not in db_info['tables']
 
  83         db_info['tablespace'] = _setup_tablespace_sql(config)
 
  85         self.env.globals['config'] = config
 
  86         self.env.globals['db'] = db_info
 
  87         self.env.globals['postgres'] = _setup_postgresql_features(conn)
 
  90     def run_sql_file(self, conn: Connection, name: str, **kwargs: Any) -> None:
 
  91         """ Execute the given SQL file on the connection. The keyword arguments
 
  92             may supply additional parameters for preprocessing.
 
  94         sql = self.env.get_template(name).render(**kwargs)
 
  96         with conn.cursor() as cur: