00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once 'creole/Connection.php';
00023 require_once 'creole/common/ConnectionCommon.php';
00024 include_once 'creole/drivers/pgsql/PgSQLResultSet.php';
00025
00035 class PgSQLConnection extends ConnectionCommon implements Connection {
00036
00044 private $result_affected_rows;
00045
00055 function connect($dsninfo, $flags = 0)
00056 {
00057 global $php_errormsg;
00058
00059 if (!extension_loaded('pgsql')) {
00060 throw new SQLException('pgsql extension not loaded');
00061 }
00062
00063 $this->dsn = $dsninfo;
00064 $this->flags = $flags;
00065
00066 $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT);
00067
00068 $protocol = (isset($dsninfo['protocol'])) ? $dsninfo['protocol'] : 'tcp';
00069 $connstr = '';
00070
00071 if ($protocol == 'tcp') {
00072 if (!empty($dsninfo['hostspec'])) {
00073 $connstr = 'host=' . $dsninfo['hostspec'];
00074 }
00075 if (!empty($dsninfo['port'])) {
00076 $connstr .= ' port=' . $dsninfo['port'];
00077 }
00078 }
00079
00080 if (isset($dsninfo['database'])) {
00081 $connstr .= ' dbname=\'' . addslashes($dsninfo['database']) . '\'';
00082 }
00083 if (!empty($dsninfo['username'])) {
00084 $connstr .= ' user=\'' . addslashes($dsninfo['username']) . '\'';
00085 }
00086 if (!empty($dsninfo['password'])) {
00087 $connstr .= ' password=\'' . addslashes($dsninfo['password']) . '\'';
00088 }
00089 if (!empty($dsninfo['options'])) {
00090 $connstr .= ' options=' . $dsninfo['options'];
00091 }
00092 if (!empty($dsninfo['tty'])) {
00093 $connstr .= ' tty=' . $dsninfo['tty'];
00094 }
00095
00096 if ($persistent) {
00097 $conn = @pg_pconnect($connstr);
00098 } else {
00099 $conn = @pg_connect($connstr);
00100 }
00101
00102 if (!$conn) {
00103
00104 $cleanconnstr = preg_replace('/password=\'.*?\'($|\s)/', 'password=\'*********\'', $connstr);
00105 throw new SQLException('Could not connect', $php_errormsg, $cleanconnstr);
00106 }
00107
00108 $this->dblink = $conn;
00109 }
00110
00114 public function applyLimit(&$sql, $offset, $limit)
00115 {
00116 if ( $limit > 0 ) {
00117 $sql .= " LIMIT ".$limit;
00118 }
00119 if ( $offset > 0 ) {
00120 $sql .= " OFFSET ".$offset;
00121 }
00122 }
00123
00127 function close()
00128 {
00129 $ret = @pg_close($this->dblink);
00130 $this->result_affected_rows = null;
00131 $this->dblink = null;
00132 return $ret;
00133 }
00134
00138 function executeQuery($sql, $fetchmode = null)
00139 {
00140 $result = @pg_query($this->dblink, $sql);
00141 if (!$result) {
00142 throw new SQLException('Could not execute query', pg_last_error($this->dblink), $sql);
00143 }
00144 $this->result_affected_rows = (int) @pg_affected_rows($result);
00145
00146 return new PgSQLResultSet($this, $result, $fetchmode);
00147 }
00148
00152 function executeUpdate($sql)
00153 {
00154 $result = @pg_query($this->dblink, $sql);
00155 if (!$result) {
00156 throw new SQLException('Could not execute update', pg_last_error($this->dblink), $sql);
00157 }
00158 $this->result_affected_rows = (int) @pg_affected_rows($result);
00159
00160 return $this->result_affected_rows;
00161 }
00162
00168 protected function beginTrans()
00169 {
00170 $result = @pg_query($this->dblink, "BEGIN");
00171 if (!$result) {
00172 throw new SQLException('Could not begin transaction', pg_last_error($this->dblink));
00173 }
00174 }
00175
00181 protected function commitTrans()
00182 {
00183 $result = @pg_query($this->dblink, "COMMIT");
00184 if (!$result) {
00185 throw new SQLException('Could not commit transaction', pg_last_error($this->dblink));
00186 }
00187 }
00188
00194 protected function rollbackTrans()
00195 {
00196 $result = @pg_query($this->dblink, "ROLLBACK");
00197 if (!$result) {
00198 throw new SQLException('Could not rollback transaction', pg_last_error($this->dblink));
00199 }
00200 }
00201
00208 function getUpdateCount()
00209 {
00210 if ( $this->result_affected_rows === null ) {
00211 throw new SQLException('getUpdateCount called before any sql queries were executed');
00212 }
00213 return $this->result_affected_rows;
00214 }
00215
00216
00220 public function getDatabaseInfo()
00221 {
00222 require_once 'creole/drivers/pgsql/metadata/PgSQLDatabaseInfo.php';
00223 return new PgSQLDatabaseInfo($this);
00224 }
00225
00229 public function getIdGenerator()
00230 {
00231 require_once 'creole/drivers/pgsql/PgSQLIdGenerator.php';
00232 return new PgSQLIdGenerator($this);
00233 }
00234
00238 public function prepareStatement($sql)
00239 {
00240 require_once 'creole/drivers/pgsql/PgSQLPreparedStatement.php';
00241 return new PgSQLPreparedStatement($this, $sql);
00242 }
00243
00247 public function prepareCall($sql) {
00248 throw new SQLException('PostgreSQL does not support stored procedures.');
00249 }
00250
00254 public function createStatement()
00255 {
00256 require_once 'creole/drivers/pgsql/PgSQLStatement.php';
00257 return new PgSQLStatement($this);
00258 }
00259
00260 }