+ if ($this->hasHousenumber()) {
+ $sHouseNumberRegex = $oDB->getDBQuoted('\\\\m'.$this->sHouseNumber.'\\\\M');
+
+ // Housenumbers on streets and places.
+ $sPlacexSql = 'SELECT array_agg(place_id) FROM placex';
+ $sPlacexSql .= ' WHERE parent_place_id = sin.place_id AND sin.address_rank < 30';
+ $sPlacexSql .= $this->oContext->excludeSQL(' AND place_id');
+ $sPlacexSql .= ' and housenumber ~* E'.$sHouseNumberRegex;
+
+ // Interpolations on streets and places.
+ $sInterpolSql = 'null';
+ $sTigerSql = 'null';
+ if (preg_match('/^[0-9]+$/', $this->sHouseNumber)) {
+ $sIpolHnr = 'WHERE parent_place_id = sin.place_id ';
+ $sIpolHnr .= ' AND startnumber is not NULL AND sin.address_rank < 30';
+ $sIpolHnr .= ' AND '.$this->sHouseNumber.' between startnumber and endnumber';
+ $sIpolHnr .= ' AND ('.$this->sHouseNumber.' - startnumber) % step = 0';
+
+ $sInterpolSql = 'SELECT array_agg(place_id) FROM location_property_osmline '.$sIpolHnr;
+ if (CONST_Use_US_Tiger_Data) {
+ $sTigerSql = 'SELECT array_agg(place_id) FROM location_property_tiger '.$sIpolHnr;
+ $sTigerSql .= " and sin.country_code = 'us'";
+ }
+ }
+
+ if ($this->sClass) {
+ $iLimit = 40;
+ }
+
+ $sSelfHnr = 'SELECT * FROM placex WHERE place_id = search_name.place_id';
+ $sSelfHnr .= ' AND housenumber ~* E'.$sHouseNumberRegex;
+
+ $aTerms[] = '(address_rank < 30 or exists('.$sSelfHnr.'))';
+
+
+ $sSQL = 'SELECT sin.*, ';
+ $sSQL .= '('.$sPlacexSql.') as placex_hnr, ';
+ $sSQL .= '('.$sInterpolSql.') as interpol_hnr, ';
+ $sSQL .= '('.$sTigerSql.') as tiger_hnr ';
+ $sSQL .= ' FROM (';
+ $sSQL .= ' SELECT place_id, address_rank, country_code,'.$sExactMatchSQL.',';
+ $sSQL .= ' CASE WHEN importance = 0 OR importance IS NULL';
+ $sSQL .= ' THEN 0.75001-(search_rank::float/40) ELSE importance END as importance';
+ $sSQL .= ' FROM search_name';
+ $sSQL .= ' WHERE '.join(' and ', $aTerms);
+ $sSQL .= ' ORDER BY '.join(', ', $aOrder);
+ $sSQL .= ' LIMIT 40000';
+ $sSQL .= ') as sin';
+ $sSQL .= ' ORDER BY address_rank = 30 desc, placex_hnr, interpol_hnr, tiger_hnr,';
+ $sSQL .= ' importance';
+ $sSQL .= ' LIMIT '.$iLimit;
+ } else {
+ if ($this->sClass) {
+ $iLimit = 40;
+ }