forked from hans/Nominatim
Merge pull request #1359 from mtmail/fix-export-script
utils/export.php broke after switch to PDO DB abstraction
This commit is contained in:
58
lib/DB.php
58
lib/DB.php
@@ -74,12 +74,7 @@ class DB
|
|||||||
public function getRow($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
public function getRow($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (isset($aInputVars)) {
|
$stmt = $this->getQueryStatement($sSQL, $aInputVars, $sErrMessage);
|
||||||
$stmt = $this->connection->prepare($sSQL);
|
|
||||||
$stmt->execute($aInputVars);
|
|
||||||
} else {
|
|
||||||
$stmt = $this->connection->query($sSQL);
|
|
||||||
}
|
|
||||||
$row = $stmt->fetch();
|
$row = $stmt->fetch();
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
throw new \Nominatim\DatabaseError($sErrMessage, 500, null, $e, $sSQL);
|
throw new \Nominatim\DatabaseError($sErrMessage, 500, null, $e, $sSQL);
|
||||||
@@ -98,12 +93,7 @@ class DB
|
|||||||
public function getOne($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
public function getOne($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (isset($aInputVars)) {
|
$stmt = $this->getQueryStatement($sSQL, $aInputVars, $sErrMessage);
|
||||||
$stmt = $this->connection->prepare($sSQL);
|
|
||||||
$stmt->execute($aInputVars);
|
|
||||||
} else {
|
|
||||||
$stmt = $this->connection->query($sSQL);
|
|
||||||
}
|
|
||||||
$row = $stmt->fetch(\PDO::FETCH_NUM);
|
$row = $stmt->fetch(\PDO::FETCH_NUM);
|
||||||
if ($row === false) return false;
|
if ($row === false) return false;
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
@@ -123,12 +113,7 @@ class DB
|
|||||||
public function getAll($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
public function getAll($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (isset($aInputVars)) {
|
$stmt = $this->getQueryStatement($sSQL, $aInputVars, $sErrMessage);
|
||||||
$stmt = $this->connection->prepare($sSQL);
|
|
||||||
$stmt->execute($aInputVars);
|
|
||||||
} else {
|
|
||||||
$stmt = $this->connection->query($sSQL);
|
|
||||||
}
|
|
||||||
$rows = $stmt->fetchAll();
|
$rows = $stmt->fetchAll();
|
||||||
} catch (\PDOException $e) {
|
} catch (\PDOException $e) {
|
||||||
throw new \Nominatim\DatabaseError($sErrMessage, 500, null, $e, $sSQL);
|
throw new \Nominatim\DatabaseError($sErrMessage, 500, null, $e, $sSQL);
|
||||||
@@ -148,12 +133,8 @@ class DB
|
|||||||
{
|
{
|
||||||
$aVals = array();
|
$aVals = array();
|
||||||
try {
|
try {
|
||||||
if (isset($aInputVars)) {
|
$stmt = $this->getQueryStatement($sSQL, $aInputVars, $sErrMessage);
|
||||||
$stmt = $this->connection->prepare($sSQL);
|
|
||||||
$stmt->execute($aInputVars);
|
|
||||||
} else {
|
|
||||||
$stmt = $this->connection->query($sSQL);
|
|
||||||
}
|
|
||||||
while ($val = $stmt->fetchColumn(0)) { // returns first column or false
|
while ($val = $stmt->fetchColumn(0)) { // returns first column or false
|
||||||
$aVals[] = $val;
|
$aVals[] = $val;
|
||||||
}
|
}
|
||||||
@@ -174,12 +155,8 @@ class DB
|
|||||||
public function getAssoc($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
public function getAssoc($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (isset($aInputVars)) {
|
$stmt = $this->getQueryStatement($sSQL, $aInputVars, $sErrMessage);
|
||||||
$stmt = $this->connection->prepare($sSQL);
|
|
||||||
$stmt->execute($aInputVars);
|
|
||||||
} else {
|
|
||||||
$stmt = $this->connection->query($sSQL);
|
|
||||||
}
|
|
||||||
$aList = array();
|
$aList = array();
|
||||||
while ($aRow = $stmt->fetch(\PDO::FETCH_NUM)) {
|
while ($aRow = $stmt->fetch(\PDO::FETCH_NUM)) {
|
||||||
$aList[$aRow[0]] = $aRow[1];
|
$aList[$aRow[0]] = $aRow[1];
|
||||||
@@ -190,6 +167,27 @@ class DB
|
|||||||
return $aList;
|
return $aList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes query. Returns a PDO statement to iterate over.
|
||||||
|
*
|
||||||
|
* @param string $sSQL
|
||||||
|
*
|
||||||
|
* @return PDOStatement
|
||||||
|
*/
|
||||||
|
public function getQueryStatement($sSQL, $aInputVars = null, $sErrMessage = 'Database query failed')
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (isset($aInputVars)) {
|
||||||
|
$stmt = $this->connection->prepare($sSQL);
|
||||||
|
$stmt->execute($aInputVars);
|
||||||
|
} else {
|
||||||
|
$stmt = $this->connection->query($sSQL);
|
||||||
|
}
|
||||||
|
} catch (\PDOException $e) {
|
||||||
|
throw new \Nominatim\DatabaseError($sErrMessage, 500, null, $e, $sSQL);
|
||||||
|
}
|
||||||
|
return $stmt;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* St. John's Way => 'St. John\'s Way'
|
* St. John's Way => 'St. John\'s Way'
|
||||||
|
|||||||
@@ -116,10 +116,8 @@
|
|||||||
$sOsmId = $aCMDResult['restrict-to-osm-relation'];
|
$sOsmId = $aCMDResult['restrict-to-osm-relation'];
|
||||||
}
|
}
|
||||||
if ($sOsmType) {
|
if ($sOsmType) {
|
||||||
$sSQL = 'select place_id from placex where';
|
$sSQL = 'select place_id from placex where osm_type = :osm_type and osm_id = :osm_id';
|
||||||
$sSQL .= ' osm_type = '.$oDB->getDBQuoted($sOsmType);
|
$sParentId = $oDB->getOne($sSQL, array('osm_type' => $sOsmType, 'osm_id' => $sOsmId));
|
||||||
$sSQL .= ' and osm_id = '.$sOsmId;
|
|
||||||
$sParentId = $oDB->getOne($sSQL);
|
|
||||||
if (!$sParentId) fail('Could not find place '.$sOsmType.' '.$sOsmId);
|
if (!$sParentId) fail('Could not find place '.$sOsmType.' '.$sOsmId);
|
||||||
}
|
}
|
||||||
if ($sParentId) {
|
if ($sParentId) {
|
||||||
@@ -131,15 +129,15 @@
|
|||||||
// Iterate over placeids
|
// Iterate over placeids
|
||||||
// to get further hierarchical information
|
// to get further hierarchical information
|
||||||
//var_dump($sPlacexSQL);
|
//var_dump($sPlacexSQL);
|
||||||
$aRes =& $oDB->query($sPlacexSQL);
|
$oResults = $oDB->getQueryStatement($sPlacexSQL);
|
||||||
$fOutstream = fopen('php://output', 'w');
|
$fOutstream = fopen('php://output', 'w');
|
||||||
while ($aRes->fetchInto($aRow)) {
|
while ($aRow = $oResults->fetch()) {
|
||||||
//var_dump($aRow);
|
//var_dump($aRow);
|
||||||
$iPlaceID = $aRow['place_id'];
|
$iPlaceID = $aRow['place_id'];
|
||||||
$sSQL = "select rank_address,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata($iPlaceID, -1)";
|
$sSQL = "select rank_address,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata(:place_id, -1)";
|
||||||
$sSQL .= ' WHERE isaddress';
|
$sSQL .= ' WHERE isaddress';
|
||||||
$sSQL .= ' order by rank_address desc,isaddress desc';
|
$sSQL .= ' order by rank_address desc,isaddress desc';
|
||||||
$aAddressLines = $oDB->getAll($sSQL);
|
$aAddressLines = $oDB->getAll($sSQL, array('place_id' => $iPlaceID));
|
||||||
|
|
||||||
$aOutput = array_fill(0, $iNumCol, '');
|
$aOutput = array_fill(0, $iNumCol, '');
|
||||||
// output address parts
|
// output address parts
|
||||||
@@ -154,9 +152,9 @@
|
|||||||
$sSQL = 'select array_agg(px.postcode) from placex px join place_addressline pa ';
|
$sSQL = 'select array_agg(px.postcode) from placex px join place_addressline pa ';
|
||||||
$sSQL .= 'on px.place_id = pa.address_place_id ';
|
$sSQL .= 'on px.place_id = pa.address_place_id ';
|
||||||
$sSQL .= 'where pa.cached_rank_address in (5,11) ';
|
$sSQL .= 'where pa.cached_rank_address in (5,11) ';
|
||||||
$sSQL .= 'and pa.place_id in (select place_id from place_addressline where address_place_id in ('.substr($aRow['place_ids'], 1, -1).')) ';
|
$sSQL .= 'and pa.place_id in (select place_id from place_addressline where address_place_id in (:first_place_id)) ';
|
||||||
$sSQL .= 'group by postcode order by count(*) desc limit 1';
|
$sSQL .= 'group by postcode order by count(*) desc limit 1';
|
||||||
$sRes = $oDB->getOne($sSQL);
|
$sRes = $oDB->getOne($sSQL, array('first_place_id' => substr($aRow['place_ids'], 1, -1)));
|
||||||
|
|
||||||
$aOutput[$aColumnMapping['postcode']] = substr($sRes, 1, -1);
|
$aOutput[$aColumnMapping['postcode']] = substr($sRes, 1, -1);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user