FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
Postgres.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 Authors: Steffen Weber, Andreas Würl
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\Db\Driver;
21 
23 
24 class Postgres implements Driver
25 {
26 
27  private $dbConnection;
28 
29  public function __construct($dbConnection)
30  {
31  $this->dbConnection = $dbConnection;
32  }
33 
40  private function identifierHash($stmt)
41  {
42  $namedatalen = 63;
43  if ($namedatalen >= strlen($stmt)) {
44  return $stmt;
45  }
46  $hash = substr($stmt, 0, $namedatalen);
47  for ($i = $namedatalen; $i < strlen($stmt); $i ++) {
48  $hash[$i%$namedatalen] = chr((ord($hash[$i%$namedatalen])+ord($stmt[$i])-32)%96+32);
49  }
50  return $hash;
51  }
52 
58  public function prepare($statementName, $sqlStatement)
59  {
60  return pg_prepare($this->dbConnection, $this->identifierHash($statementName), $sqlStatement);
61  }
62 
68  public function execute($statementName, $parameters)
69  {
70  return pg_execute($this->dbConnection, $this->identifierHash($statementName), $parameters);
71  }
72 
77  public function query($sqlStatement)
78  {
79  return pg_query($this->dbConnection, $sqlStatement);
80  }
81 
85  public function isConnected()
86  {
87  return pg_connection_status($this->dbConnection) === PGSQL_CONNECTION_OK;
88  }
89 
93  public function getLastError()
94  {
95  return pg_last_error($this->dbConnection);
96  }
97 
102  public function freeResult($res)
103  {
104  return pg_free_result($res);
105  }
106 
111  public function fetchArray($res)
112  {
113  return pg_fetch_array($res, null, PGSQL_ASSOC);
114  }
115 
120  public function fetchAll($res)
121  {
122  if (pg_num_rows($res) == 0) {
123  return array();
124  }
125  return pg_fetch_all($res);
126  }
127 
131  public function begin()
132  {
133  pg_query($this->dbConnection, "BEGIN");
134  return;
135  }
136 
140  public function commit()
141  {
142  pg_query($this->dbConnection, "COMMIT");
143  return;
144  }
145 
149  public function rollback()
150  {
151  pg_query($this->dbConnection, "ROLLBACK");
152  return;
153  }
154 
159  public function booleanFromDb($booleanValue)
160  {
161  return $booleanValue === 't';
162  }
163 
168  public function booleanToDb($booleanValue)
169  {
170  return $booleanValue ? 't' : 'f';
171  }
172 
177  public function escapeString($string)
178  {
179  return pg_escape_string($string);
180  }
181 
187  public function existsTable($tableName)
188  {
189  $dbName = pg_dbname($this->dbConnection);
190  $sql = "SELECT count(*) cnt
191  FROM information_schema.tables
192  WHERE table_catalog='$dbName'
193  AND table_name='". strtolower($tableName) . "'";
194  $res = pg_query($this->dbConnection, $sql);
195  if (!$res && pg_connection_status($this->dbConnection) === PGSQL_CONNECTION_OK) {
196  throw new \Exception(pg_last_error($this->dbConnection));
197  } else if (! $res) {
198  throw new \Exception('DB connection lost');
199  }
200  $row = pg_fetch_assoc($res);
201  pg_free_result($res);
202  return($row['cnt']>0);
203  }
204 
211  public function existsColumn($tableName, $columnName)
212  {
213  $dbName = pg_dbname($this->dbConnection);
214  $sql = "SELECT count(*) cnt
215  FROM information_schema.columns
216  WHERE table_catalog='$dbName'
217  AND table_name='". strtolower($tableName) . "'
218  AND column_name='". strtolower($columnName) . "'";
219  $res = pg_query($this->dbConnection, $sql);
220  if (!$res && pg_connection_status($this->dbConnection) === PGSQL_CONNECTION_OK) {
221  throw new \Exception(pg_last_error($this->dbConnection));
222  } else if (! $res) {
223  throw new \Exception('DB connection lost');
224  }
225  $row = pg_fetch_assoc($res);
226  pg_free_result($res);
227  return($row['cnt']>0);
228  }
229 
237  public function insertPreparedAndReturn($stmt, $sql, $params, $colName)
238  {
239  $sql .= " RETURNING $colName";
240  $stmt .= ".returning:$colName";
241  $this->prepare($stmt,$sql);
242  $res = $this->execute($stmt,$params);
243  $return = $this->fetchArray($res);
244  $this->freeResult($res);
245  return $return[$colName];
246  }
247 }
prepare($statementName, $sqlStatement)
Definition: Postgres.php:58
identifierHash($stmt)
PostgreSQL uses no more than NAMEDATALEN-1 characters of an identifier; hence long statementNames nee...
Definition: Postgres.php:40
existsColumn($tableName, $columnName)
Definition: Postgres.php:211
execute($statementName, $parameters)
Definition: Postgres.php:68
insertPreparedAndReturn($stmt, $sql, $params, $colName)
Definition: Postgres.php:237