00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 require_once 'creole/Connection.php';
00023 require_once 'creole/common/ConnectionCommon.php';
00024 include_once 'creole/drivers/mysqli/MySQLiResultSet.php';
00025
00034 class MySQLiConnection extends ConnectionCommon implements Connection {
00035
00045 public function connect($dsninfo, $flags = 0)
00046 {
00047 if (!extension_loaded('mysqli')) {
00048 throw new SQLException('mysqli extension not loaded');
00049 }
00050
00051 $this->dsn = $dsninfo;
00052 $this->flags = $flags;
00053
00054 $dbhost = null;
00055
00056 if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') {
00057 $dbhost = ':' . $dsninfo['socket'];
00058 } else {
00059 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
00060
00061 if (!empty($dsninfo['port'])) {
00062 $dbhost .= ':' . $dsninfo['port'];
00063 }
00064 }
00065
00066 $host = !empty($dsninfo['hostspec']) ? $dsninfo['hostspec'] : null;
00067 $user = !empty($dsninfo['username']) ? $dsninfo['username'] : null;
00068 $pw = !empty($dsninfo['password']) ? $dsninfo['password'] : null;
00069 $port = !empty($dsninfo['port']) ? $dsninfo['port'] : null;
00070 $socket = !empty($dsninfo['socket']) ? $dsninfo['socket'] : null;
00071 $database = !empty($dsninfo['database']) ? $dsninfo['database'] : null;
00072
00073 $encoding = !empty($dsninfo['encoding']) ? $dsninfo['encoding'] : null;
00074
00075 @ini_set('track_errors', true);
00076
00077 $conn = mysqli_connect($host, $user, $pw, $database, $port, $socket);
00078
00079 @ini_restore('track_errors');
00080
00081 if (!$conn) {
00082 if (($err = @mysqli_error()) != '') {
00083 throw new SQLException("connect failed", $err);
00084 } elseif (empty($php_errormsg)) {
00085 throw new SQLException("connect failed");
00086 } else {
00087 throw new SQLException("connect failed", $php_errormsg);
00088 }
00089 }
00090
00091 $this->dblink = $conn;
00092
00093 if ($encoding) {
00094 $this->executeUpdate("SET NAMES " . $encoding);
00095 }
00096 }
00097
00101 public function getDatabaseInfo()
00102 {
00103 require_once 'creole/drivers/mysqli/metadata/MySQLiDatabaseInfo.php';
00104 return new MySQLiDatabaseInfo($this);
00105 }
00106
00110 public function getIdGenerator()
00111 {
00112 require_once 'creole/drivers/mysqli/MySQLiIdGenerator.php';
00113 return new MySQLiIdGenerator($this);
00114 }
00115
00119 public function prepareStatement($sql)
00120 {
00121 require_once 'creole/drivers/mysqli/MySQLiPreparedStatement.php';
00122 return new MySQLiPreparedStatement($this, $sql);
00123 }
00124
00128 public function prepareCall($sql) {
00129 throw new SQLException('MySQL does not support stored procedures.');
00130 }
00131
00135 public function createStatement()
00136 {
00137 require_once 'creole/drivers/mysqli/MySQLiStatement.php';
00138 return new MySQLiStatement($this);
00139 }
00140
00144 public function close()
00145 {
00146 $ret = mysqli_close($this->dblink);
00147 $this->dblink = null;
00148 return $ret;
00149 }
00150
00154 public function applyLimit(&$sql, $offset, $limit)
00155 {
00156 if ( $limit > 0 ) {
00157 $sql .= " LIMIT " . ($offset > 0 ? $offset . ", " : "") . $limit;
00158 } else if ( $offset > 0 ) {
00159 $sql .= " LIMIT " . $offset . ", 18446744073709551615";
00160 }
00161 }
00162
00166 public function executeQuery($sql, $fetchmode = null)
00167 {
00168 $this->lastQuery = $sql;
00169
00170 $result = @mysqli_query($this->dblink, $sql);
00171
00172 if (!$result) {
00173 throw new SQLException('Could not execute query', mysqli_error($this->dblink), $sql);
00174 }
00175
00176 return new MySQLiResultSet($this, $result, $fetchmode);
00177 }
00178
00182 public function executeUpdate($sql)
00183 {
00184 $this->lastQuery = $sql;
00185
00186 $result = @mysqli_query($this->dblink, $sql);
00187
00188 if (!$result) {
00189 throw new SQLException('Could not execute update', mysqli_error($this->dblink), $sql);
00190 }
00191
00192 return (int) mysqli_affected_rows($this->dblink);
00193 }
00194
00200 protected function beginTrans()
00201 {
00202 if (!mysqli_autocommit($this->dblink, FALSE)) {
00203 throw new SQLException('Could not begin transaction', mysqli_error($this->dblink));
00204 }
00205 }
00206
00212 protected function commitTrans()
00213 {
00214 if (!mysqli_commit($this->dblink)) {
00215 throw new SQLException('Can not commit transaction', mysqli_error($this->dblink));
00216 }
00217 mysqli_autocommit($this->dblink, TRUE);
00218 }
00219
00225 protected function rollbackTrans()
00226 {
00227 if (!mysqli_rollback($this->dblink)) {
00228 throw new SQLException('Could not rollback transaction', mysqli_error($this->dblink));
00229 }
00230 mysqli_autocommit($this->dblink, TRUE);
00231 }
00232
00239 public function getUpdateCount()
00240 {
00241 return (int) @mysqli_affected_rows($this->dblink);
00242 }
00243 }