MySQLConnection.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MySQLConnection.php,v 1.16 2004/03/20 04:16:49 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/mysql/MySQLResultSet.php'; 00025 00036 class MySQLConnection extends ConnectionCommon implements Connection { 00037 00039 private $database; 00040 00050 function connect($dsninfo, $flags = 0) 00051 { 00052 if (!extension_loaded('mysql')) { 00053 throw new SQLException('mysql extension not loaded'); 00054 } 00055 00056 $this->dsn = $dsninfo; 00057 $this->flags = $flags; 00058 00059 $persistent = ($flags & Creole::PERSISTENT) === Creole::PERSISTENT; 00060 00061 if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') { 00062 $dbhost = ':' . $dsninfo['socket']; 00063 } else { 00064 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; 00065 if (!empty($dsninfo['port'])) { 00066 $dbhost .= ':' . $dsninfo['port']; 00067 } 00068 } 00069 $user = $dsninfo['username']; 00070 $pw = $dsninfo['password']; 00071 00072 $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect'; 00073 00074 @ini_set('track_errors', true); 00075 if ($dbhost && $user && $pw) { 00076 $conn = @$connect_function($dbhost, $user, $pw); 00077 } elseif ($dbhost && $user) { 00078 $conn = @$connect_function($dbhost, $user); 00079 } elseif ($dbhost) { 00080 $conn = @$connect_function($dbhost); 00081 } else { 00082 $conn = false; 00083 } 00084 @ini_restore('track_errors'); 00085 if (empty($conn)) { 00086 if (($err = @mysql_error()) != '') { 00087 throw new SQLException("connect failed", $err); 00088 } elseif (empty($php_errormsg)) { 00089 throw new SQLException("connect failed"); 00090 } else { 00091 throw new SQLException("connect failed", $php_errormsg); 00092 } 00093 } 00094 00095 if ($dsninfo['database']) { 00096 if (!@mysql_select_db($dsninfo['database'], $conn)) { 00097 switch(mysql_errno($conn)) { 00098 case 1049: 00099 $exc = new SQLException("no such database", mysql_error($conn)); 00100 break; 00101 case 1044: 00102 $exc = new SQLException("access violation", mysql_error($conn)); 00103 break; 00104 default: 00105 $exc = new SQLException("cannot select database", mysql_error($conn)); 00106 } 00107 00108 throw $exc; 00109 00110 } 00111 // fix to allow calls to different databases in the same script 00112 $this->database = $dsninfo['database']; 00113 } 00114 00115 $this->dblink = $conn; 00116 00117 } 00118 00122 public function getDatabaseInfo() 00123 { 00124 require_once 'creole/drivers/mysql/metadata/MySQLDatabaseInfo.php'; 00125 return new MySQLDatabaseInfo($this); 00126 } 00127 00131 public function getIdGenerator() 00132 { 00133 require_once 'creole/drivers/mysql/MySQLIdGenerator.php'; 00134 return new MySQLIdGenerator($this); 00135 } 00136 00140 public function prepareStatement($sql) 00141 { 00142 require_once 'creole/drivers/mysql/MySQLPreparedStatement.php'; 00143 return new MySQLPreparedStatement($this, $sql); 00144 } 00145 00149 public function prepareCall($sql) { 00150 throw new SQLException('MySQL does not support stored procedures.'); 00151 } 00152 00156 public function createStatement() 00157 { 00158 require_once 'creole/drivers/mysql/MySQLStatement.php'; 00159 return new MySQLStatement($this); 00160 } 00161 00165 function close() 00166 { 00167 $ret = mysql_close($this->dblink); 00168 $this->dblink = null; 00169 return $ret; 00170 } 00171 00175 function executeQuery($sql, $fetchmode = null) 00176 { 00177 $this->lastQuery = $sql; 00178 if ($this->database) { 00179 if (!@mysql_select_db($this->database, $this->dblink)) { 00180 throw new SQLException('No database selected', mysql_error($this->dblink)); 00181 } 00182 } 00183 $result = @mysql_query($sql, $this->dblink); 00184 if (!$result) { 00185 throw new SQLException('Could not execute query', mysql_error($this->dblink), $sql); 00186 } 00187 return new MySQLResultSet($this, $result, $fetchmode); 00188 } 00189 00193 function executeUpdate($sql) 00194 { 00195 $this->lastQuery = $sql; 00196 00197 if ($this->database) { 00198 if (!@mysql_select_db($this->database, $this->dblink)) { 00199 throw new SQLException('No database selected', mysql_error($this->dblink)); 00200 } 00201 } 00202 00203 if (!$this->autocommit) { 00204 if ($this->transactionOpcount == 0) { 00205 $result = @mysql_query('SET AUTOCOMMIT=0', $this->dblink); 00206 $result = @mysql_query('BEGIN', $this->dblink); 00207 if (!$result) { 00208 throw new SQLException('Could not begin transaction', mysql_error($this->dblink)); 00209 } 00210 } 00211 $this->transactionOpcount++; 00212 } 00213 00214 $result = @mysql_query($sql, $this->dblink); 00215 if (!$result) { 00216 throw new SQLException('Could not execute update', mysql_error($this->dblink), $sql); 00217 } 00218 return (int) mysql_affected_rows($this->dblink); 00219 } 00220 00224 function commit() 00225 { 00226 if ($this->transactionOpcount > 0) { 00227 if ($this->database) { 00228 if (!@mysql_select_db($this->database, $this->dblink)) { 00229 throw new SQLException('No database selected', mysql_error($this->dblink)); 00230 } 00231 } 00232 $result = @mysql_query('COMMIT', $this->dblink); 00233 $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink); 00234 $this->transactionOpcount = 0; 00235 if (!$result) { 00236 throw new SQLException('Can not commit transaction', mysql_error($this->dblink)); 00237 } 00238 } 00239 } 00240 00246 function rollback() 00247 { 00248 if ($this->transactionOpcount > 0) { 00249 if ($this->database) { 00250 if (!@mysql_select_db($this->database, $this->dblink)) { 00251 throw new SQLException('No database selected', mysql_error($this->dblink)); 00252 } 00253 } 00254 $result = @mysql_query('ROLLBACK', $this->dblink); 00255 $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink); 00256 $this->transactionOpcount = 0; 00257 if (!$result) { 00258 throw new SQLException('Could not rollback transaction', mysql_error($this->dblink)); 00259 } 00260 } 00261 } 00262 00269 function getUpdateCount() 00270 { 00271 return (int) @mysql_affected_rows($this->dblink); 00272 } 00273 00274 }

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


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS