MySQLConnection.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: MySQLConnection.php,v 1.2 2004/03/29 18:46:46 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/common/ConnectionCommon.php'; 00023 include_once 'creole/drivers/mysql/MySQLResultSet.php'; 00024 00036 class MySQLConnection extends ConnectionCommon 00037 { 00039 var $database; 00040 00049 function connect($dsninfo, $flags = 0) 00050 { 00051 if (! extension_loaded('mysql')) { 00052 return new SQLException(CREOLE_ERROR_EXTENSION_NOT_FOUND, 'mysql extension not loaded'); 00053 } 00054 00055 $this->dsn = $dsninfo; 00056 $this->flags = $flags; 00057 00058 $persistent = ($flags & Creole::PERSISTENT()) === Creole::PERSISTENT(); 00059 00060 if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') { 00061 $dbhost = ':' . $dsninfo['socket']; 00062 } 00063 else { 00064 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; 00065 if (!empty($dsninfo['port'])) { 00066 $dbhost .= ':' . $dsninfo['port']; 00067 } 00068 } 00069 00070 $user = $dsninfo['username']; 00071 $pw = $dsninfo['password']; 00072 00073 $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect'; 00074 00075 @ini_set('track_errors', true); 00076 if ($dbhost && $user && $pw) { 00077 $conn = @$connect_function($dbhost, $user, $pw); 00078 } elseif ($dbhost && $user) { 00079 $conn = @$connect_function($dbhost, $user); 00080 } elseif ($dbhost) { 00081 $conn = @$connect_function($dbhost); 00082 } else { 00083 $conn = false; 00084 } 00085 00086 @ini_restore('track_errors'); 00087 if (empty($conn)) 00088 { 00089 if (($err = @mysql_error()) != '') { 00090 return new SQLException(CREOLE_ERROR_CONNECT_FAILED, "connect failed", $err); 00091 } elseif (empty($php_errormsg)) { 00092 return new SQLException(CREOLE_ERROR_CONNECT_FAILED, "connect failed"); 00093 } else { 00094 return new SQLException(CREOLE_ERROR_CONNECT_FAILED, "connect failed", $php_errormsg); 00095 } 00096 } 00097 00098 if ($dsninfo['database']) 00099 { 00100 if (! @mysql_select_db($dsninfo['database'], $conn)) 00101 { 00102 switch(mysql_errno($conn)) 00103 { 00104 case 1049: 00105 $exc = new SQLException(CREOLE_ERROR_NOSUCHDB, "no such database", mysql_error($conn)); 00106 break; 00107 case 1044: 00108 $exc = new SQLException(CREOLE_ERROR_ACCESS_VIOLATION, "access violation", mysql_error($conn)); 00109 break; 00110 default: 00111 $exc = new SQLException(CREOLE_ERROR, "cannot select database", mysql_error($conn)); 00112 } 00113 00114 return $exc; 00115 } 00116 00117 // fix to allow calls to different databases in the same script 00118 $this->database = $dsninfo['database']; 00119 } 00120 00121 $this->dblink = $conn; 00122 return true; 00123 } 00124 00128 function & getDatabaseInfo() 00129 { 00130 require_once 'creole/drivers/mysql/metadata/MySQLDatabaseInfo.php'; 00131 return new MySQLDatabaseInfo($this); 00132 } 00133 00137 function & getIdGenerator() 00138 { 00139 require_once 'creole/drivers/mysql/MySQLIdGenerator.php'; 00140 return new MySQLIdGenerator($this); 00141 } 00142 00146 function & prepareStatement(&$sql) 00147 { 00148 require_once 'creole/drivers/mysql/MySQLPreparedStatement.php'; 00149 return new MySQLPreparedStatement($this, $sql); 00150 } 00151 00155 function & prepareCall(&$sql) 00156 { 00157 return new SQLException(CREOLE_ERROR_UNSUPPORTED, 'MySQL does not support stored procedures.'); 00158 } 00159 00163 function & createStatement() 00164 { 00165 require_once 'creole/drivers/mysql/MySQLStatement.php'; 00166 return new MySQLStatement($this); 00167 } 00168 00172 function close() 00173 { 00174 $ret = mysql_close($this->dblink); 00175 $this->dblink = null; 00176 return $ret; 00177 } 00178 00182 function & executeQuery($sql, $fetchmode = null) 00183 { 00184 $this->lastQuery = $sql; 00185 if ($this->database) { 00186 if (! @mysql_select_db($this->database, $this->dblink)) { 00187 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected', mysql_error($this->dblink)); 00188 } 00189 } 00190 00191 $result = @mysql_query($sql, $this->dblink); 00192 00193 if ($result === false) { 00194 return new SQLException(CREOLE_ERROR, 'Could not execute query', mysql_error($this->dblink), $sql); 00195 } 00196 00197 return new MySQLResultSet($this, $result, $fetchmode); 00198 } 00199 00203 function & executeUpdate($sql) 00204 { 00205 $this->lastQuery = $sql; 00206 00207 if ($this->database) { 00208 if (!@mysql_select_db($this->database, $this->dblink)) { 00209 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected', mysql_error($this->dblink)); 00210 } 00211 } 00212 00213 if (!$this->autocommit) 00214 { 00215 if ($this->transactionOpcount == 0) 00216 { 00217 $result = @mysql_query('SET AUTOCOMMIT=0', $this->dblink); 00218 $result = @mysql_query('BEGIN', $this->dblink); 00219 if ($result === false) { 00220 return new SQLException(CREOLE_ERROR, 'Could not begin transaction', mysql_error($this->dblink)); 00221 } 00222 } 00223 00224 $this->transactionOpcount++; 00225 } 00226 00227 $result = @mysql_query($sql, $this->dblink); 00228 00229 if ($result === false) { 00230 return new SQLException(CREOLE_ERROR, 'Could not execute update', mysql_error($this->dblink), $sql); 00231 } 00232 00233 return (int) mysql_affected_rows($this->dblink); 00234 } 00235 00242 function commit() 00243 { 00244 if ($this->transactionOpcount > 0) 00245 { 00246 if ($this->database) { 00247 if (!@mysql_select_db($this->database, $this->dblink)) { 00248 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected', mysql_error($this->dblink)); 00249 } 00250 } 00251 00252 $result = @mysql_query('COMMIT', $this->dblink); 00253 $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink); 00254 $this->transactionOpcount = 0; 00255 00256 if ($result === false) { 00257 return new SQLException(CREOLE_ERROR, 'Can not commit transaction', mysql_error($this->dblink)); 00258 } 00259 } 00260 00261 return true; 00262 } 00263 00268 function rollback() 00269 { 00270 if ($this->transactionOpcount > 0) 00271 { 00272 if ($this->database) { 00273 if (!@mysql_select_db($this->database, $this->dblink)) { 00274 return new SQLException(CREOLE_ERROR_NODBSELECTED, 'No database selected', mysql_error($this->dblink)); 00275 } 00276 } 00277 00278 $result = @mysql_query('ROLLBACK', $this->dblink); 00279 $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink); 00280 $this->transactionOpcount = 0; 00281 00282 if ($result === false) { 00283 return new SQLException(CREOLE_ERROR, 'Could not rollback transaction', mysql_error($this->dblink)); 00284 } 00285 } 00286 00287 return true; 00288 } 00289 00296 function getUpdateCount() 00297 { 00298 return (int) @mysql_affected_rows($this->dblink); 00299 } 00300 00301 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS