FOSSology  4.4.0
Open Source License Compliance by Open Source Software
Postgres.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4  Authors: Steffen Weber, Andreas Würl
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
10 
12 
13 class Postgres implements Driver
14 {
15 
16  private $dbConnection;
17 
18  public function __construct($dbConnection)
19  {
20  $this->dbConnection = $dbConnection;
21  }
22 
29  private function identifierHash($stmt)
30  {
31  $namedatalen = 63;
32  if ($namedatalen >= strlen($stmt)) {
33  return $stmt;
34  }
35  $hash = substr($stmt, 0, $namedatalen);
36  for ($i = $namedatalen; $i < strlen($stmt); $i ++) {
37  $hash[$i%$namedatalen] = chr((ord($hash[$i%$namedatalen])+ord($stmt[$i])-32)%96+32);
38  }
39  return $hash;
40  }
41 
47  public function prepare($statementName, $sqlStatement)
48  {
49  return pg_prepare($this->dbConnection, $this->identifierHash($statementName), $sqlStatement);
50  }
51 
57  public function execute($statementName, $parameters)
58  {
59  return pg_execute($this->dbConnection, $this->identifierHash($statementName), $parameters);
60  }
61 
66  public function query($sqlStatement)
67  {
68  return pg_query($this->dbConnection, $sqlStatement);
69  }
70 
74  public function isConnected()
75  {
76  return pg_connection_status($this->dbConnection) === PGSQL_CONNECTION_OK;
77  }
78 
82  public function getLastError()
83  {
84  return pg_last_error($this->dbConnection);
85  }
86 
91  public function freeResult($res)
92  {
93  return pg_free_result($res);
94  }
95 
100  public function fetchArray($res)
101  {
102  return pg_fetch_array($res, null, PGSQL_ASSOC);
103  }
104 
109  public function fetchAll($res)
110  {
111  if (pg_num_rows($res) == 0) {
112  return array();
113  }
114  return pg_fetch_all($res);
115  }
116 
120  public function begin()
121  {
122  pg_query($this->dbConnection, "BEGIN");
123  return;
124  }
125 
129  public function commit()
130  {
131  pg_query($this->dbConnection, "COMMIT");
132  return;
133  }
134 
138  public function rollback()
139  {
140  pg_query($this->dbConnection, "ROLLBACK");
141  return;
142  }
143 
148  public function booleanFromDb($booleanValue)
149  {
150  return $booleanValue === 't';
151  }
152 
157  public function booleanToDb($booleanValue)
158  {
159  return $booleanValue ? 't' : 'f';
160  }
161 
166  public function escapeString($string)
167  {
168  return pg_escape_string($string);
169  }
170 
176  public function existsTable($tableName)
177  {
178  $dbName = pg_dbname($this->dbConnection);
179  $sql = "SELECT count(*) cnt
180  FROM information_schema.tables
181  WHERE table_catalog='$dbName'
182  AND table_name='". strtolower($tableName) . "'";
183  $res = pg_query($this->dbConnection, $sql);
184  if (!$res && pg_connection_status($this->dbConnection) === PGSQL_CONNECTION_OK) {
185  throw new \Exception(pg_last_error($this->dbConnection));
186  } else if (! $res) {
187  throw new \Exception('DB connection lost');
188  }
189  $row = pg_fetch_assoc($res);
190  pg_free_result($res);
191  return($row['cnt']>0);
192  }
193 
200  public function existsColumn($tableName, $columnName)
201  {
202  $dbName = pg_dbname($this->dbConnection);
203  $sql = "SELECT count(*) cnt
204  FROM information_schema.columns
205  WHERE table_catalog='$dbName'
206  AND table_name='". strtolower($tableName) . "'
207  AND column_name='". strtolower($columnName) . "'";
208  $res = pg_query($this->dbConnection, $sql);
209  if (!$res && pg_connection_status($this->dbConnection) === PGSQL_CONNECTION_OK) {
210  throw new \Exception(pg_last_error($this->dbConnection));
211  } else if (! $res) {
212  throw new \Exception('DB connection lost');
213  }
214  $row = pg_fetch_assoc($res);
215  pg_free_result($res);
216  return($row['cnt']>0);
217  }
218 
226  public function insertPreparedAndReturn($stmt, $sql, $params, $colName)
227  {
228  $sql .= " RETURNING $colName";
229  $stmt .= ".returning:$colName";
230  $this->prepare($stmt,$sql);
231  $res = $this->execute($stmt,$params);
232  $return = $this->fetchArray($res);
233  $this->freeResult($res);
234  return $return[$colName];
235  }
236 }
insertPreparedAndReturn($stmt, $sql, $params, $colName)
Definition: Postgres.php:226
existsColumn($tableName, $columnName)
Definition: Postgres.php:200
execute($statementName, $parameters)
Definition: Postgres.php:57
prepare($statementName, $sqlStatement)
Definition: Postgres.php:47
identifierHash($stmt)
PostgreSQL uses no more than NAMEDATALEN-1 characters of an identifier; hence long statementNames nee...
Definition: Postgres.php:29