]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/db/properties.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / db / properties.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Query and access functions for the in-database property table.
9 """
10 from typing import Optional, cast
11
12 from nominatim.db.connection import Connection
13
14 def set_property(conn: Connection, name: str, value: str) -> None:
15     """ Add or replace the property with the given name.
16     """
17     with conn.cursor() as cur:
18         cur.execute('SELECT value FROM nominatim_properties WHERE property = %s',
19                     (name, ))
20
21         if cur.rowcount == 0:
22             sql = 'INSERT INTO nominatim_properties (value, property) VALUES (%s, %s)'
23         else:
24             sql = 'UPDATE nominatim_properties SET value = %s WHERE property = %s'
25
26         cur.execute(sql, (value, name))
27     conn.commit()
28
29
30 def get_property(conn: Connection, name: str) -> Optional[str]:
31     """ Return the current value of the given property or None if the property
32         is not set.
33     """
34     if not conn.table_exists('nominatim_properties'):
35         return None
36
37     with conn.cursor() as cur:
38         cur.execute('SELECT value FROM nominatim_properties WHERE property = %s',
39                     (name, ))
40
41         if cur.rowcount == 0:
42             return None
43
44         result = cur.fetchone()
45         assert result is not None
46
47         return cast(Optional[str], result[0])