27 private $dbConnection;
29 public function __construct($dbConnection)
31 $this->dbConnection = $dbConnection;
43 if ($namedatalen >= strlen($stmt)) {
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);
58 public function prepare($statementName, $sqlStatement)
60 return pg_prepare($this->dbConnection, $this->
identifierHash($statementName), $sqlStatement);
68 public function execute($statementName, $parameters)
70 return pg_execute($this->dbConnection, $this->
identifierHash($statementName), $parameters);
77 public function query($sqlStatement)
79 return pg_query($this->dbConnection, $sqlStatement);
87 return pg_connection_status($this->dbConnection) === PGSQL_CONNECTION_OK;
95 return pg_last_error($this->dbConnection);
104 return pg_free_result($res);
113 return pg_fetch_array($res, null, PGSQL_ASSOC);
122 if (pg_num_rows($res) == 0) {
125 return pg_fetch_all($res);
133 pg_query($this->dbConnection,
"BEGIN");
142 pg_query($this->dbConnection,
"COMMIT");
151 pg_query($this->dbConnection,
"ROLLBACK");
161 return $booleanValue ===
't';
170 return $booleanValue ?
't' :
'f';
179 return pg_escape_string($string);
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));
198 throw new \Exception(
'DB connection lost');
200 $row = pg_fetch_assoc($res);
201 pg_free_result($res);
202 return($row[
'cnt']>0);
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));
223 throw new \Exception(
'DB connection lost');
225 $row = pg_fetch_assoc($res);
226 pg_free_result($res);
227 return($row[
'cnt']>0);
239 $sql .=
" RETURNING $colName";
240 $stmt .=
".returning:$colName";
242 $res = $this->
execute($stmt,$params);
245 return $return[$colName];
prepare($statementName, $sqlStatement)
identifierHash($stmt)
PostgreSQL uses no more than NAMEDATALEN-1 characters of an identifier; hence long statementNames nee...
existsColumn($tableName, $columnName)
execute($statementName, $parameters)
insertPreparedAndReturn($stmt, $sql, $params, $colName)
booleanToDb($booleanValue)
booleanFromDb($booleanValue)