[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: PgSQLConnection.php,v 1.21 2005/08/03 17:56:22 hlellelid Exp $ 4 * 5 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 6 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 7 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 8 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 9 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 10 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 11 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 12 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 13 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 14 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 15 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 16 * 17 * This software consists of voluntary contributions made by many individuals 18 * and is licensed under the LGPL. For more information please see 19 * <http://creole.phpdb.org>. 20 */ 21 22 require_once 'creole/Connection.php'; 23 require_once 'creole/common/ConnectionCommon.php'; 24 include_once 'creole/drivers/pgsql/PgSQLResultSet.php'; 25 26 /** 27 * PgSQL implementation of Connection. 28 * 29 * @author Hans Lellelid <hans@xmpl.org> (Creole) 30 * @author Stig Bakken <ssb@fast.no> (PEAR::DB) 31 * @author Lukas Smith (PEAR::MDB) 32 * @version $Revision: 1.21 $ 33 * @package creole.drivers.pgsql 34 */ 35 class PgSQLConnection extends ConnectionCommon implements Connection { 36 37 /** 38 * Affected Rows of last executed query. 39 * Postgres needs this for getUpdateCount() 40 * We used to store the entire result set 41 * instead but that can be a large dataset. 42 * @var int 43 */ 44 private $result_affected_rows; 45 46 /** 47 * Connect to a database and log in as the specified user. 48 * 49 * @param array $dsn The datasource hash. 50 * @param $flags Any connection flags. 51 * @access public 52 * @throws SQLException 53 * @return void 54 */ 55 function connect($dsninfo, $flags = 0) 56 { 57 global $php_errormsg; 58 59 if (!extension_loaded('pgsql')) { 60 throw new SQLException('pgsql extension not loaded'); 61 } 62 63 $this->dsn = $dsninfo; 64 $this->flags = $flags; 65 66 $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT); 67 68 $protocol = (isset($dsninfo['protocol'])) ? $dsninfo['protocol'] : 'tcp'; 69 $connstr = ''; 70 71 if ($protocol == 'tcp') { 72 if (!empty($dsninfo['hostspec'])) { 73 $connstr = 'host=' . $dsninfo['hostspec']; 74 } 75 if (!empty($dsninfo['port'])) { 76 $connstr .= ' port=' . $dsninfo['port']; 77 } 78 } 79 80 if (isset($dsninfo['database'])) { 81 $connstr .= ' dbname=\'' . addslashes($dsninfo['database']) . '\''; 82 } 83 if (!empty($dsninfo['username'])) { 84 $connstr .= ' user=\'' . addslashes($dsninfo['username']) . '\''; 85 } 86 if (!empty($dsninfo['password'])) { 87 $connstr .= ' password=\'' . addslashes($dsninfo['password']) . '\''; 88 } 89 if (!empty($dsninfo['options'])) { 90 $connstr .= ' options=' . $dsninfo['options']; 91 } 92 if (!empty($dsninfo['tty'])) { 93 $connstr .= ' tty=' . $dsninfo['tty']; 94 } 95 96 if ($persistent) { 97 $conn = @pg_pconnect($connstr); 98 } else { 99 $conn = @pg_connect($connstr); 100 } 101 102 if (!$conn) { 103 // hide the password from connstr 104 $cleanconnstr = preg_replace('/password=\'.*?\'($|\s)/', 'password=\'*********\'', $connstr); 105 throw new SQLException('Could not connect', $php_errormsg, $cleanconnstr); 106 } 107 108 $this->dblink = $conn; 109 } 110 111 /** 112 * @see Connection::applyLimit() 113 */ 114 public function applyLimit(&$sql, $offset, $limit) 115 { 116 if ( $limit > 0 ) { 117 $sql .= " LIMIT ".$limit; 118 } 119 if ( $offset > 0 ) { 120 $sql .= " OFFSET ".$offset; 121 } 122 } 123 124 /** 125 * @see Connection::disconnect() 126 */ 127 function close() 128 { 129 $ret = @pg_close($this->dblink); 130 $this->result_affected_rows = null; 131 $this->dblink = null; 132 return $ret; 133 } 134 135 /** 136 * @see Connection::simpleQuery() 137 */ 138 function executeQuery($sql, $fetchmode = null) 139 { 140 $result = @pg_query($this->dblink, $sql); 141 if (!$result) { 142 throw new SQLException('Could not execute query', pg_last_error($this->dblink), $sql); 143 } 144 $this->result_affected_rows = (int) @pg_affected_rows($result); 145 146 return new PgSQLResultSet($this, $result, $fetchmode); 147 } 148 149 /** 150 * @see Connection::simpleUpdate() 151 */ 152 function executeUpdate($sql) 153 { 154 $result = @pg_query($this->dblink, $sql); 155 if (!$result) { 156 throw new SQLException('Could not execute update', pg_last_error($this->dblink), $sql); 157 } 158 $this->result_affected_rows = (int) @pg_affected_rows($result); 159 160 return $this->result_affected_rows; 161 } 162 163 /** 164 * Start a database transaction. 165 * @throws SQLException 166 * @return void 167 */ 168 protected function beginTrans() 169 { 170 $result = @pg_query($this->dblink, "BEGIN"); 171 if (!$result) { 172 throw new SQLException('Could not begin transaction', pg_last_error($this->dblink)); 173 } 174 } 175 176 /** 177 * Commit the current transaction. 178 * @throws SQLException 179 * @return void 180 */ 181 protected function commitTrans() 182 { 183 $result = @pg_query($this->dblink, "COMMIT"); 184 if (!$result) { 185 throw new SQLException('Could not commit transaction', pg_last_error($this->dblink)); 186 } 187 } 188 189 /** 190 * Roll back (undo) the current transaction. 191 * @throws SQLException 192 * @return void 193 */ 194 protected function rollbackTrans() 195 { 196 $result = @pg_query($this->dblink, "ROLLBACK"); 197 if (!$result) { 198 throw new SQLException('Could not rollback transaction', pg_last_error($this->dblink)); 199 } 200 } 201 202 /** 203 * Gets the number of rows affected by the data manipulation 204 * query. 205 * @see Statement::getUpdateCount() 206 * @return int Number of rows affected by the last query. 207 */ 208 function getUpdateCount() 209 { 210 if ( $this->result_affected_rows === null ) { 211 throw new SQLException('getUpdateCount called before any sql queries were executed'); 212 } 213 return $this->result_affected_rows; 214 } 215 216 217 /** 218 * @see Connection::getDatabaseInfo() 219 */ 220 public function getDatabaseInfo() 221 { 222 require_once 'creole/drivers/pgsql/metadata/PgSQLDatabaseInfo.php'; 223 return new PgSQLDatabaseInfo($this); 224 } 225 226 /** 227 * @see Connection::getIdGenerator() 228 */ 229 public function getIdGenerator() 230 { 231 require_once 'creole/drivers/pgsql/PgSQLIdGenerator.php'; 232 return new PgSQLIdGenerator($this); 233 } 234 235 /** 236 * @see Connection::prepareStatement() 237 */ 238 public function prepareStatement($sql) 239 { 240 require_once 'creole/drivers/pgsql/PgSQLPreparedStatement.php'; 241 return new PgSQLPreparedStatement($this, $sql); 242 } 243 244 /** 245 * @see Connection::prepareCall() 246 */ 247 public function prepareCall($sql) { 248 throw new SQLException('PostgreSQL does not support stored procedures.'); 249 } 250 251 /** 252 * @see Connection::createStatement() 253 */ 254 public function createStatement() 255 { 256 require_once 'creole/drivers/pgsql/PgSQLStatement.php'; 257 return new PgSQLStatement($this); 258 } 259 260 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |