PgSQLConnection.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: PgSQLConnection.php,v 1.2 2004/04/28 19:33:53 micha 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 00037 class PgSQLConnection extends ConnectionCommon 00038 { 00044 var $result; 00045 00054 function connect($dsninfo, $flags = 0) 00055 { 00056 if (!extension_loaded('pgsql')) { 00057 return new SQLException(CREOLE_ERROR_EXTENSION_NOT_FOUND, 'pgsql extension not loaded'); 00058 } 00059 00060 $this->dsn = $dsninfo; 00061 $this->flags = $flags; 00062 00063 $persistent = ($flags & Creole::PERSISTENT() === Creole::PERSISTENT()); 00064 00065 $protocol = (isset($dsninfo['protocol'])) ? $dsninfo['protocol'] : 'tcp'; 00066 $connstr = ''; 00067 00068 if ($protocol == 'tcp') { 00069 if (!empty($dsninfo['hostspec'])) { 00070 $connstr = 'host=' . $dsninfo['hostspec']; 00071 } 00072 if (!empty($dsninfo['port'])) { 00073 $connstr .= ' port=' . $dsninfo['port']; 00074 } 00075 } 00076 00077 if (isset($dsninfo['database'])) { 00078 $connstr .= ' dbname=\'' . addslashes($dsninfo['database']) . '\''; 00079 } 00080 if (!empty($dsninfo['username'])) { 00081 $connstr .= ' user=\'' . addslashes($dsninfo['username']) . '\''; 00082 } 00083 if (!empty($dsninfo['password'])) { 00084 $connstr .= ' password=\'' . addslashes($dsninfo['password']) . '\''; 00085 } 00086 if (!empty($dsninfo['options'])) { 00087 $connstr .= ' options=' . $dsninfo['options']; 00088 } 00089 if (!empty($dsninfo['tty'])) { 00090 $connstr .= ' tty=' . $dsninfo['tty']; 00091 } 00092 00093 if ($persistent) { 00094 $conn = @pg_pconnect($connstr); 00095 } else { 00096 $conn = @pg_connect($connstr); 00097 } 00098 00099 if (!$conn) { 00100 return new SQLException(CREOLE_ERROR_CONNECT_FAILED, 'Could not connect', $php_errormsg, $connstr); 00101 } 00102 00103 $this->dblink = $conn; 00104 return true; 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 return new SQLException(CREOLE_ERROR, '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 return new SQLException(CREOLE_ERROR, '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 return new SQLException(CREOLE_ERROR, 'Could not execute update', pg_last_error($this->dblink), $sql); 00147 } 00148 00149 return (int) @pg_cmdtuples($this->result); 00150 } 00151 00156 function commit() 00157 { 00158 if ($this->transactionOpcount > 0) { 00159 $result = @pg_query($this->dblink, "end;"); 00160 $this->transactionOpcount = 0; 00161 if (!$result) { 00162 return new SQLException('Could not commit transaction', pg_last_error($this->dblink)); 00163 } 00164 } 00165 00166 return true; 00167 } 00168 00173 function rollback() 00174 { 00175 if ($this->transactionOpcount > 0) { 00176 $result = @pg_query($this->dblink, "abort;"); 00177 $this->transactionOpcount = 0; 00178 if (!$result) { 00179 return new SQLException(CREOLE_ERROR, 'Could not rollback transaction', pg_last_error($this->dblink)); 00180 } 00181 } 00182 00183 return true; 00184 } 00185 00192 function getUpdateCount() 00193 { 00194 return (int) @pg_cmdtuples($this->result); 00195 } 00196 00197 00201 function & getDatabaseInfo() 00202 { 00203 require_once 'creole/drivers/pgsql/metadata/PgSQLDatabaseInfo.php'; 00204 return new PgSQLDatabaseInfo($this); 00205 } 00206 00210 function & getIdGenerator() 00211 { 00212 require_once 'creole/drivers/pgsql/PgSQLIdGenerator.php'; 00213 return new PgSQLIdGenerator($this); 00214 } 00215 00219 function & prepareStatement($sql) 00220 { 00221 require_once 'creole/drivers/pgsql/PgSQLPreparedStatement.php'; 00222 return new PgSQLPreparedStatement($this, $sql); 00223 } 00224 00228 function prepareCall($sql) { 00229 return new SQLException(CREOLE_ERROR_UNSUPPORTED, 'PostgreSQL does not support stored procedures.'); 00230 } 00231 00235 function & createStatement() 00236 { 00237 require_once 'creole/drivers/pgsql/PgSQLStatement.php'; 00238 return new PgSQLStatement($this); 00239 } 00240 00241 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS