]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/db/sqlalchemy_functions.py
27eec794d0e80a8c3ed6b8bb3d0ed7b5352c1a82
[nominatim.git] / nominatim / db / sqlalchemy_functions.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Custom functions and expressions for SQLAlchemy.
9 """
10
11 import sqlalchemy as sa
12
13 def select_index_placex_geometry_reverse_lookuppolygon(table: str) -> 'sa.TextClause':
14     """ Create an expression with the necessary conditions over a placex
15         table that the index 'idx_placex_geometry_reverse_lookupPolygon'
16         can be used.
17     """
18     return sa.text(f"ST_GeometryType({table}.geometry) in ('ST_Polygon', 'ST_MultiPolygon')"
19                    f" AND {table}.rank_address between 4 and 25"
20                    f" AND {table}.type != 'postcode'"
21                    f" AND {table}.name is not null"
22                    f" AND {table}.indexed_status = 0"
23                    f" AND {table}.linked_place_id is null")
24
25 def select_index_placex_geometry_reverse_lookupplacenode(table: str) -> 'sa.TextClause':
26     """ Create an expression with the necessary conditions over a placex
27         table that the index 'idx_placex_geometry_reverse_lookupPlaceNode'
28         can be used.
29     """
30     return sa.text(f"{table}.rank_address between 4 and 25"
31                    f" AND {table}.type != 'postcode'"
32                    f" AND {table}.name is not null"
33                    f" AND {table}.linked_place_id is null"
34                    f" AND {table}.osm_type = 'N'")