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/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 $encoding = !empty($dsninfo['encoding']) ? $dsninfo['encoding'] : null;
00073
00074 $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
00075
00076 @ini_set('track_errors', true);
00077 if ($dbhost && $user && $pw) {
00078 $conn = @$connect_function($dbhost, $user, $pw);
00079 } elseif ($dbhost && $user) {
00080 $conn = @$connect_function($dbhost, $user);
00081 } elseif ($dbhost) {
00082 $conn = @$connect_function($dbhost);
00083 } else {
00084 $conn = false;
00085 }
00086 @ini_restore('track_errors');
00087 if (empty($conn)) {
00088 if (($err = @mysql_error()) != '') {
00089 throw new SQLException("connect failed", $err);
00090 } elseif (empty($php_errormsg)) {
00091 throw new SQLException("connect failed");
00092 } else {
00093 throw new SQLException("connect failed", $php_errormsg);
00094 }
00095 }
00096
00097 if ($dsninfo['database']) {
00098 if (!@mysql_select_db($dsninfo['database'], $conn)) {
00099 switch(mysql_errno($conn)) {
00100 case 1049:
00101 $exc = new SQLException("no such database", mysql_error($conn));
00102 break;
00103 case 1044:
00104 $exc = new SQLException("access violation", mysql_error($conn));
00105 break;
00106 default:
00107 $exc = new SQLException("cannot select database", mysql_error($conn));
00108 }
00109
00110 throw $exc;
00111
00112 }
00113
00114 $this->database = $dsninfo['database'];
00115 }
00116
00117 $this->dblink = $conn;
00118
00119 if ($encoding) {
00120 $this->executeUpdate("SET NAMES " . $encoding);
00121 }
00122 }
00123
00127 public function getDatabaseInfo()
00128 {
00129 require_once 'creole/drivers/mysql/metadata/MySQLDatabaseInfo.php';
00130 return new MySQLDatabaseInfo($this);
00131 }
00132
00136 public function getIdGenerator()
00137 {
00138 require_once 'creole/drivers/mysql/MySQLIdGenerator.php';
00139 return new MySQLIdGenerator($this);
00140 }
00141
00145 public function prepareStatement($sql)
00146 {
00147 require_once 'creole/drivers/mysql/MySQLPreparedStatement.php';
00148 return new MySQLPreparedStatement($this, $sql);
00149 }
00150
00154 public function prepareCall($sql) {
00155 throw new SQLException('MySQL does not support stored procedures.');
00156 }
00157
00161 public function createStatement()
00162 {
00163 require_once 'creole/drivers/mysql/MySQLStatement.php';
00164 return new MySQLStatement($this);
00165 }
00166
00170 function close()
00171 {
00172 $ret = mysql_close($this->dblink);
00173 $this->dblink = null;
00174 return $ret;
00175 }
00176
00180 public function applyLimit(&$sql, $offset, $limit)
00181 {
00182 if ( $limit > 0 ) {
00183 $sql .= " LIMIT " . ($offset > 0 ? $offset . ", " : "") . $limit;
00184 } else if ( $offset > 0 ) {
00185 $sql .= " LIMIT " . $offset . ", 18446744073709551615";
00186 }
00187 }
00188
00192 function executeQuery($sql, $fetchmode = null)
00193 {
00194 $this->lastQuery = $sql;
00195 if ($this->database) {
00196 if (!@mysql_select_db($this->database, $this->dblink)) {
00197 throw new SQLException('No database selected', mysql_error($this->dblink));
00198 }
00199 }
00200 $result = @mysql_query($sql, $this->dblink);
00201 if (!$result) {
00202 throw new SQLException('Could not execute query', mysql_error($this->dblink), $sql);
00203 }
00204 return new MySQLResultSet($this, $result, $fetchmode);
00205 }
00206
00210 function executeUpdate($sql)
00211 {
00212 $this->lastQuery = $sql;
00213
00214 if ($this->database) {
00215 if (!@mysql_select_db($this->database, $this->dblink)) {
00216 throw new SQLException('No database selected', mysql_error($this->dblink));
00217 }
00218 }
00219
00220 $result = @mysql_query($sql, $this->dblink);
00221 if (!$result) {
00222 throw new SQLException('Could not execute update', mysql_error($this->dblink), $sql);
00223 }
00224 return (int) mysql_affected_rows($this->dblink);
00225 }
00226
00232 protected function beginTrans()
00233 {
00234 $result = @mysql_query('SET AUTOCOMMIT=0', $this->dblink);
00235 $result = @mysql_query('BEGIN', $this->dblink);
00236 if (!$result) {
00237 throw new SQLException('Could not begin transaction', mysql_error($this->dblink));
00238 }
00239 }
00240
00246 protected function commitTrans()
00247 {
00248 if ($this->database) {
00249 if (!@mysql_select_db($this->database, $this->dblink)) {
00250 throw new SQLException('No database selected', mysql_error($this->dblink));
00251 }
00252 }
00253 $result = @mysql_query('COMMIT', $this->dblink);
00254 $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink);
00255 if (!$result) {
00256 throw new SQLException('Can not commit transaction', mysql_error($this->dblink));
00257 }
00258 }
00259
00265 protected function rollbackTrans()
00266 {
00267 if ($this->database) {
00268 if (!@mysql_select_db($this->database, $this->dblink)) {
00269 throw new SQLException('No database selected', mysql_error($this->dblink));
00270 }
00271 }
00272 $result = @mysql_query('ROLLBACK', $this->dblink);
00273 $result = @mysql_query('SET AUTOCOMMIT=1', $this->dblink);
00274 if (!$result) {
00275 throw new SQLException('Could not rollback transaction', mysql_error($this->dblink));
00276 }
00277 }
00278
00285 function getUpdateCount()
00286 {
00287 return (int) @mysql_affected_rows($this->dblink);
00288 }
00289
00290 }