2 Mix-ins that provide the actual commands for the indexer for various indexing
5 # pylint: disable=C0111
7 class AbstractPlacexRunner:
8 """ Returns SQL commands for indexing of the placex table.
10 SELECT_SQL = 'SELECT place_id, (placex_prepare_update(placex)).* FROM placex'
12 def __init__(self, rank):
15 self._cached_index_sql = None
17 def _index_sql(self, num_places):
18 if num_places != self._sql_terms:
19 self._cached_index_sql = \
21 SET indexed_status = 0, address = v.addr
22 FROM (VALUES {}) as v(id, addr)
24 """.format(','.join(["(%s, %s::hstore)"] * num_places))
25 self._sql_terms = num_places
27 return self._cached_index_sql
30 def index_places(self, worker, places):
33 values.extend((place[x] for x in ('place_id', 'address')))
35 worker.perform(self._index_sql(len(places)), values)
38 class RankRunner(AbstractPlacexRunner):
39 """ Returns SQL commands for indexing one rank within the placex table.
43 return "rank {}".format(self.rank)
45 def sql_count_objects(self):
46 return """SELECT count(*) FROM placex
47 WHERE rank_address = {} and indexed_status > 0
50 def sql_get_objects(self):
51 return """{} WHERE indexed_status > 0 and rank_address = {}
52 ORDER BY geometry_sector
53 """.format(self.SELECT_SQL, self.rank)
56 class BoundaryRunner(AbstractPlacexRunner):
57 """ Returns SQL commands for indexing the administrative boundaries
62 return "boundaries rank {}".format(self.rank)
64 def sql_count_objects(self):
65 return """SELECT count(*) FROM placex
66 WHERE indexed_status > 0
68 AND class = 'boundary' and type = 'administrative'
71 def sql_get_objects(self):
72 return """{} WHERE indexed_status > 0 and rank_search = {}
73 and class = 'boundary' and type = 'administrative'
74 ORDER BY partition, admin_level
75 """.format(self.SELECT_SQL, self.rank)
78 class InterpolationRunner:
79 """ Returns SQL commands for indexing the address interpolation table
80 location_property_osmline.
85 return "interpolation lines (location_property_osmline)"
88 def sql_count_objects():
89 return """SELECT count(*) FROM location_property_osmline
90 WHERE indexed_status > 0"""
93 def sql_get_objects():
94 return """SELECT place_id FROM location_property_osmline
95 WHERE indexed_status > 0
96 ORDER BY geometry_sector"""
99 def index_places(worker, ids):
100 worker.perform(""" UPDATE location_property_osmline
101 SET indexed_status = 0 WHERE place_id IN ({})
102 """.format(','.join((str(i[0]) for i in ids))))
105 class PostcodeRunner:
106 """ Provides the SQL commands for indexing the location_postcode table.
111 return "postcodes (location_postcode)"
114 def sql_count_objects():
115 return 'SELECT count(*) FROM location_postcode WHERE indexed_status > 0'
118 def sql_get_objects():
119 return """SELECT place_id FROM location_postcode
120 WHERE indexed_status > 0
121 ORDER BY country_code, postcode"""
124 def index_places(worker, ids):
125 worker.perform(""" UPDATE location_postcode SET indexed_status = 0
126 WHERE place_id IN ({})
127 """.format(','.join((str(i[0]) for i in ids))))