| [ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 /* 3 * $Id: MySQLiConnection.php,v 1.7 2004/09/18 09:29:22 sb 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/mysqli/MySQLiResultSet.php'; 25 26 /** 27 * MySQLi implementation of Connection. 28 * 29 * 30 * @author Sebastian Bergmann <sb@sebastian-bergmann.de> 31 * @version $Revision: 1.7 $ 32 * @package creole.drivers.mysqli 33 */ 34 class MySQLiConnection extends ConnectionCommon implements Connection { 35 /** Current database (used in mysqli_select_db()). */ 36 private $database; 37 38 /** 39 * Connect to a database and log in as the specified user. 40 * 41 * @param $dsn the data source name (see DB::parseDSN for syntax) 42 * @param $flags Any conneciton flags. 43 * @access public 44 * @throws SQLException 45 * @return void 46 */ 47 public function connect($dsninfo, $flags = 0) 48 { 49 if (!extension_loaded('mysqli')) { 50 throw new SQLException('mysqli extension not loaded'); 51 } 52 53 $this->dsn = $dsninfo; 54 $this->flags = $flags; 55 56 $dbhost = null; 57 58 59 if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') { 60 $dbhost = ':' . $dsninfo['socket']; 61 } else { 62 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; 63 64 if (!empty($dsninfo['port'])) { 65 $dbhost .= ':' . $dsninfo['port']; 66 } 67 } 68 69 $host = !empty($dsninfo['hostspec']) ? $dsninfo['hostspec'] : null; 70 $user = !empty($dsninfo['username']) ? $dsninfo['username'] : null; 71 $pw = !empty($dsninfo['password']) ? $dsninfo['password'] : null; 72 $port = !empty($dsninfo['port']) ? $dsninfo['port'] : null; 73 $socket = !empty($dsninfo['socket']) ? $dsninfo['socket'] : null; 74 $database = !empty($dsninfo['database']) ? $dsninfo['database'] : null; 75 76 $encoding = !empty($dsninfo['encoding']) ? $dsninfo['encoding'] : null; 77 78 @ini_set('track_errors', true); 79 80 $conn = mysqli_connect($host, $user, $pw, $database, $port, $socket); 81 82 @ini_restore('track_errors'); 83 84 if (empty($conn)) { 85 if (($err = @mysqli_error()) != '') { 86 throw new SQLException("connect failed", $err); 87 } elseif (empty($php_errormsg)) { 88 throw new SQLException("connect failed"); 89 } else { 90 throw new SQLException("connect failed", $php_errormsg); 91 } 92 } 93 94 if ($dsninfo['database']) { 95 if (!@mysqli_select_db($conn, $dsninfo['database'])) { 96 switch(mysqli_errno($conn)) { 97 case 1049: 98 $exc = new SQLException("no such database", mysqli_error($conn)); 99 break; 100 case 1044: 101 $exc = new SQLException("access violation", mysqli_error($conn)); 102 break; 103 default: 104 $exc = new SQLException("cannot select database", mysqli_error($conn)); 105 } 106 107 throw $exc; 108 109 } 110 111 // fix to allow calls to different databases in the same script 112 $this->database = $dsninfo['database']; 113 } 114 115 $this->dblink = $conn; 116 117 if ($encoding) { 118 $this->executeUpdate("SET NAMES " . $encoding); 119 } 120 } 121 122 /** 123 * @see Connection::getDatabaseInfo() 124 */ 125 public function getDatabaseInfo() 126 { 127 require_once 'creole/drivers/mysqli/metadata/MySQLiDatabaseInfo.php'; 128 return new MySQLiDatabaseInfo($this); 129 } 130 131 /** 132 * @see Connection::getIdGenerator() 133 */ 134 public function getIdGenerator() 135 { 136 require_once 'creole/drivers/mysqli/MySQLiIdGenerator.php'; 137 return new MySQLiIdGenerator($this); 138 } 139 140 /** 141 * @see Connection::prepareStatement() 142 */ 143 public function prepareStatement($sql) 144 { 145 require_once 'creole/drivers/mysqli/MySQLiPreparedStatement.php'; 146 return new MySQLiPreparedStatement($this, $sql); 147 } 148 149 /** 150 * @see Connection::prepareCall() 151 */ 152 public function prepareCall($sql) { 153 throw new SQLException('MySQL does not support stored procedures.'); 154 } 155 156 /** 157 * @see Connection::createStatement() 158 */ 159 public function createStatement() 160 { 161 require_once 'creole/drivers/mysqli/MySQLiStatement.php'; 162 return new MySQLiStatement($this); 163 } 164 165 /** 166 * @see Connection::disconnect() 167 */ 168 public function close() 169 { 170 $ret = mysqli_close($this->dblink); 171 $this->dblink = null; 172 return $ret; 173 } 174 175 /** 176 * @see Connection::applyLimit() 177 */ 178 public function applyLimit(&$sql, $offset, $limit) 179 { 180 if ( $limit > 0 ) { 181 $sql .= " LIMIT " . ($offset > 0 ? $offset . ", " : "") . $limit; 182 } else if ( $offset > 0 ) { 183 $sql .= " LIMIT " . $offset . ", 18446744073709551615"; 184 } 185 } 186 187 /** 188 * @see Connection::executeQuery() 189 */ 190 public function executeQuery($sql, $fetchmode = null) 191 { 192 $this->lastQuery = $sql; 193 194 if ($this->database) { 195 if (!@mysqli_select_db($this->dblink, $this->database)) { 196 throw new SQLException('No database selected', mysqli_error($this->dblink)); 197 } 198 } 199 200 $result = @mysqli_query($this->dblink, $sql); 201 202 if (!$result) { 203 throw new SQLException('Could not execute query', mysqli_error($this->dblink), $sql); 204 } 205 206 return new MySQLiResultSet($this, $result, $fetchmode); 207 } 208 209 /** 210 * @see Connection::executeUpdate() 211 */ 212 public function executeUpdate($sql) 213 { 214 $this->lastQuery = $sql; 215 216 if ($this->database) { 217 if (!@mysqli_select_db($this->dblink, $this->database)) { 218 throw new SQLException('No database selected', mysqli_error($this->dblink)); 219 } 220 } 221 222 $result = @mysqli_query($this->dblink, $sql); 223 224 if (!$result) { 225 throw new SQLException('Could not execute update', mysqli_error($this->dblink), $sql); 226 } 227 228 return (int) mysqli_affected_rows($this->dblink); 229 } 230 231 /** 232 * Start a database transaction. 233 * @throws SQLException 234 * @return void 235 */ 236 protected function beginTrans() 237 { 238 if (!mysqli_autocommit($this->dblink, FALSE)) { 239 throw new SQLException('Could not begin transaction', mysqli_error($this->dblink)); 240 } 241 } 242 243 /** 244 * Commit the current transaction. 245 * @throws SQLException 246 * @return void 247 */ 248 protected function commitTrans() 249 { 250 if ($this->database) { 251 if (!@mysqli_select_db($this->dblink, $this->database)) { 252 throw new SQLException('No database selected', mysqli_error($this->dblink)); 253 } 254 } 255 256 if (!mysqli_commit($this->dblink)) { 257 throw new SQLException('Can not commit transaction', mysqli_error($this->dblink)); 258 } 259 260 mysqli_autocommit($this->dblink, TRUE); 261 } 262 263 /** 264 * Roll back (undo) the current transaction. 265 * @throws SQLException 266 * @return void 267 */ 268 protected function rollbackTrans() 269 { 270 if ($this->database) { 271 if (!@mysqli_select_db($this->dblink, $this->database)) { 272 throw new SQLException('No database selected', mysqli_error($this->dblink)); 273 } 274 } 275 276 if (!mysqli_rollback($this->dblink)) { 277 throw new SQLException('Could not rollback transaction', mysqli_error($this->dblink)); 278 } 279 280 mysqli_autocommit($this->dblink, TRUE); 281 } 282 283 /** 284 * Gets the number of rows affected by the data manipulation 285 * query. 286 * 287 * @return int Number of rows affected by the last query. 288 */ 289 public function getUpdateCount() 290 { 291 return (int) @mysqli_affected_rows($this->dblink); 292 } 293 }
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 |