PgSQLConnection.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: PgSQLConnection.php,v 1.15 2004/03/20 04:16:50 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 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 00042 private $result; 00043 00053 function connect($dsninfo, $flags = 0) 00054 { 00055 global $php_errormsg; 00056 00057 if (!extension_loaded('pgsql')) { 00058 throw new SQLException('pgsql extension not loaded'); 00059 } 00060 00061 $this->dsn = $dsninfo; 00062 $this->flags = $flags; 00063 00064 $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT); 00065 00066 $protocol = (isset($dsninfo['protocol'])) ? $dsninfo['protocol'] : 'tcp'; 00067 $connstr = ''; 00068 00069 if ($protocol == 'tcp') { 00070 if (!empty($dsninfo['hostspec'])) { 00071 $connstr = 'host=' . $dsninfo['hostspec']; 00072 } 00073 if (!empty($dsninfo['port'])) { 00074 $connstr .= ' port=' . $dsninfo['port']; 00075 } 00076 } 00077 00078 if (isset($dsninfo['database'])) { 00079 $connstr .= ' dbname=\'' . addslashes($dsninfo['database']) . '\''; 00080 } 00081 if (!empty($dsninfo['username'])) { 00082 $connstr .= ' user=\'' . addslashes($dsninfo['username']) . '\''; 00083 } 00084 if (!empty($dsninfo['password'])) { 00085 $connstr .= ' password=\'' . addslashes($dsninfo['password']) . '\''; 00086 } 00087 if (!empty($dsninfo['options'])) { 00088 $connstr .= ' options=' . $dsninfo['options']; 00089 } 00090 if (!empty($dsninfo['tty'])) { 00091 $connstr .= ' tty=' . $dsninfo['tty']; 00092 } 00093 00094 if ($persistent) { 00095 $conn = @pg_pconnect($connstr); 00096 } else { 00097 $conn = @pg_connect($connstr); 00098 } 00099 00100 if (!$conn) { 00101 throw new SQLException('Could not connect', $php_errormsg, $connstr); 00102 } 00103 00104 $this->dblink = $conn; 00105 } 00106 00110 function close() 00111 { 00112 $ret = @pg_close($this->dblink); 00113 $this->dblink = null; 00114 return $ret; 00115 } 00116 00120 function executeQuery($sql, $fetchmode = null) 00121 { 00122 $this->result = @pg_query($this->dblink, $sql); 00123 if (!$this->result) { 00124 throw new SQLException('Could not execute query', pg_last_error($this->dblink), $sql); 00125 } 00126 return new PgSQLResultSet($this, $this->result, $fetchmode); 00127 } 00128 00132 function executeUpdate($sql) 00133 { 00134 if (!$this->autocommit) { 00135 if ($this->transactionOpcount == 0) { 00136 $result = @pg_exec($this->dblink, "begin;"); 00137 if (!$result) { 00138 throw new SQLException('Could not begin transaction', pg_last_error($this->dblink)); 00139 } 00140 } 00141 $this->transactionOpcount++; 00142 } 00143 00144 $this->result = @pg_query($this->dblink, $sql); 00145 if (!$this->result) { 00146 throw new SQLException('Could not execute update', pg_last_error($this->dblink), $sql); 00147 } 00148 00149 return (int) @pg_cmdtuples($this->result); 00150 } 00151 00155 function commit() 00156 { 00157 if ($this->transactionOpcount > 0) { 00158 $result = @pg_query($this->dblink, "end;"); 00159 $this->transactionOpcount = 0; 00160 if (!$result) { 00161 throw new SQLException('Could not commit transaction', pg_last_error($this->dblink)); 00162 } 00163 } 00164 } 00165 00171 function rollback() 00172 { 00173 if ($this->transactionOpcount > 0) { 00174 $result = @pg_query($this->dblink, "abort;"); 00175 $this->transactionOpcount = 0; 00176 if (!$result) { 00177 throw new SQLException('Could not rollback transaction', pg_last_error($this->dblink)); 00178 } 00179 } 00180 } 00181 00188 function getUpdateCount() 00189 { 00190 return (int) @pg_cmdtuples($this->result); 00191 } 00192 00193 00197 public function getDatabaseInfo() 00198 { 00199 require_once 'creole/drivers/pgsql/metadata/PgSQLDatabaseInfo.php'; 00200 return new PgSQLDatabaseInfo($this); 00201 } 00202 00206 public function getIdGenerator() 00207 { 00208 require_once 'creole/drivers/pgsql/PgSQLIdGenerator.php'; 00209 return new PgSQLIdGenerator($this); 00210 } 00211 00215 public function prepareStatement($sql) 00216 { 00217 require_once 'creole/drivers/pgsql/PgSQLPreparedStatement.php'; 00218 return new PgSQLPreparedStatement($this, $sql); 00219 } 00220 00224 public function prepareCall($sql) { 00225 throw new SQLException('PostgreSQL does not support stored procedures.'); 00226 } 00227 00231 public function createStatement() 00232 { 00233 require_once 'creole/drivers/pgsql/PgSQLStatement.php'; 00234 return new PgSQLStatement($this); 00235 } 00236 00237 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS