]> git.openstreetmap.org Git - nominatim.git/blob - lib/db.php
introduce chksql function for website code and move other fail functions
[nominatim.git] / lib / db.php
1 <?php
2         require_once('DB.php');
3
4         function &getDB($bNew = false, $bPersistent = false)
5         {
6                 // Get the database object
7                 $oDB = chksql(DB::connect(CONST_Database_DSN.($bNew?'?new_link=true':''), $bPersistent),
8                               "Failed to establish database connection");
9                 $oDB->setFetchMode(DB_FETCHMODE_ASSOC);
10                 $oDB->query("SET DateStyle TO 'sql,european'");
11                 $oDB->query("SET client_encoding TO 'utf-8'");
12                 $iMaxExecution = ini_get('max_execution_time') * 1000;
13                 if ($iMaxExecution > 0) $oDB->query("SET statement_timeout TO $iMaxExecution");
14                 return $oDB;
15         }
16
17         function getDBQuoted($s)
18         {
19                 return "'".pg_escape_string($s)."'";
20         }
21
22         function getPostgresVersion(&$oDB)
23         {
24                 $sVersionString = $oDB->getOne('select version()');
25                 preg_match('#PostgreSQL ([0-9]+)[.]([0-9]+)[^0-9]#', $sVersionString, $aMatches);
26                 return (float) ($aMatches[1].'.'.$aMatches[2]);
27         }
28
29         function getPostgisVersion(&$oDB)
30         {
31                 $sVersionString = $oDB->getOne('select postgis_full_version()');
32                 preg_match('#POSTGIS="([0-9]+)[.]([0-9]+)[.]([0-9]+)( r([0-9]+))?"#', $sVersionString, $aMatches);
33                 return (float) ($aMatches[1].'.'.$aMatches[2]);
34         }