Public Member Functions | |
connect ($dsninfo, $flags=0) | |
Connect to a database and log in as the specified user. | |
getDatabaseInfo () | |
getIdGenerator () | |
prepareStatement ($sql) | |
prepareCall ($sql) | |
createStatement () | |
close () | |
applyLimit (&$sql, $offset, $limit) | |
executeQuery ($sql, $fetchmode=null) | |
executeUpdate ($sql) | |
getUpdateCount () | |
Gets the number of rows affected by the data manipulation query. | |
Protected Member Functions | |
beginTrans () | |
Start a database transaction. | |
commitTrans () | |
Commit the current transaction. | |
rollbackTrans () | |
Roll back (undo) the current transaction. | |
Private Attributes | |
$database | |
Current database (used in mysql_select_db()). |
Definition at line 36 of file MySQLConnection.php.
MySQLConnection::applyLimit | ( | &$ | sql, | |
$ | offset, | |||
$ | limit | |||
) |
Implements Connection.
Definition at line 180 of file MySQLConnection.php.
00181 { 00182 if ( $limit > 0 ) { 00183 $sql .= " LIMIT " . ($offset > 0 ? $offset . ", " : "") . $limit; 00184 } else if ( $offset > 0 ) { 00185 $sql .= " LIMIT " . $offset . ", 18446744073709551615"; 00186 } 00187 }
MySQLConnection::beginTrans | ( | ) | [protected] |
Start a database transaction.
SQLException |
Reimplemented from ConnectionCommon.
Definition at line 232 of file MySQLConnection.php.
References $result.
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 }
MySQLConnection::close | ( | ) |
Implements Connection.
Definition at line 170 of file MySQLConnection.php.
00171 { 00172 $ret = mysql_close($this->dblink); 00173 $this->dblink = null; 00174 return $ret; 00175 }
MySQLConnection::commitTrans | ( | ) | [protected] |
Commit the current transaction.
SQLException |
Reimplemented from ConnectionCommon.
Definition at line 246 of file MySQLConnection.php.
References $result.
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 }
MySQLConnection::connect | ( | $ | dsninfo, | |
$ | flags = 0 | |||
) |
Connect to a database and log in as the specified user.
$dsn | the data source name (see DB::parseDSN for syntax) | |
$flags | Any conneciton flags. public |
SQLException |
Implements Connection.
Definition at line 50 of file MySQLConnection.php.
References ConnectionCommon::$flags, executeUpdate(), and Creole::PERSISTENT.
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 // fix to allow calls to different databases in the same script 00114 $this->database = $dsninfo['database']; 00115 } 00116 00117 $this->dblink = $conn; 00118 00119 if ($encoding) { 00120 $this->executeUpdate("SET NAMES " . $encoding); 00121 } 00122 }
MySQLConnection::createStatement | ( | ) |
Implements Connection.
Definition at line 161 of file MySQLConnection.php.
00162 { 00163 require_once 'creole/drivers/mysql/MySQLStatement.php'; 00164 return new MySQLStatement($this); 00165 }
MySQLConnection::executeQuery | ( | $ | sql, | |
$ | fetchmode = null | |||
) |
Implements Connection.
Definition at line 192 of file MySQLConnection.php.
References $result.
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 }
MySQLConnection::executeUpdate | ( | $ | sql | ) |
Implements Connection.
Definition at line 210 of file MySQLConnection.php.
References $result.
Referenced by connect().
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 }
MySQLConnection::getDatabaseInfo | ( | ) |
Implements Connection.
Definition at line 127 of file MySQLConnection.php.
00128 { 00129 require_once 'creole/drivers/mysql/metadata/MySQLDatabaseInfo.php'; 00130 return new MySQLDatabaseInfo($this); 00131 }
MySQLConnection::getIdGenerator | ( | ) |
Implements Connection.
Definition at line 136 of file MySQLConnection.php.
00137 { 00138 require_once 'creole/drivers/mysql/MySQLIdGenerator.php'; 00139 return new MySQLIdGenerator($this); 00140 }
MySQLConnection::getUpdateCount | ( | ) |
Gets the number of rows affected by the data manipulation query.
Implements Connection.
Definition at line 285 of file MySQLConnection.php.
MySQLConnection::prepareCall | ( | $ | sql | ) |
Reimplemented from ConnectionCommon.
Definition at line 154 of file MySQLConnection.php.
00154 { 00155 throw new SQLException('MySQL does not support stored procedures.'); 00156 }
MySQLConnection::prepareStatement | ( | $ | sql | ) |
Implements Connection.
Definition at line 145 of file MySQLConnection.php.
00146 { 00147 require_once 'creole/drivers/mysql/MySQLPreparedStatement.php'; 00148 return new MySQLPreparedStatement($this, $sql); 00149 }
MySQLConnection::rollbackTrans | ( | ) | [protected] |
Roll back (undo) the current transaction.
SQLException |
Reimplemented from ConnectionCommon.
Definition at line 265 of file MySQLConnection.php.
References $result.
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 }
MySQLConnection::$database [private] |