Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * MySQL Database Adapter Class |
| 20 | * |
| 21 | * Note: _DB is an extender class that the app controller |
| 22 | * creates dynamically based on whether the active record |
| 23 | * class is being used or not. |
| 24 | * |
| 25 | * @package CodeIgniter |
| 26 | * @subpackage Drivers |
| 27 | * @category Database |
| 28 | * @author ExpressionEngine Dev Team |
| 29 | * @link http://codeigniter.com/user_guide/database/ |
| 30 | */ |
| 31 | class CI_DB_mysql_driver extends CI_DB { |
| 32 | |
| 33 | var $dbdriver = 'mysql'; |
| 34 | |
| 35 | // The character used for escaping |
| 36 | var $_escape_char = '`'; |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 37 | |
| 38 | // clause and character used for LIKE escape sequences - not used in MySQL |
| 39 | var $_like_escape_str = ''; |
| 40 | var $_like_escape_chr = ''; |
| 41 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Whether to use the MySQL "delete hack" which allows the number |
| 44 | * of affected rows to be shown. Uses a preg_replace when enabled, |
| 45 | * adding a bit more processing to all queries. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 46 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | var $delete_hack = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 48 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | /** |
| 50 | * The syntax to count rows is slightly different across different |
| 51 | * database engines, so this string appears in each driver and is |
| 52 | * used for the count_all() and count_all_results() functions. |
| 53 | */ |
| 54 | var $_count_string = 'SELECT COUNT(*) AS '; |
| 55 | var $_random_keyword = ' RAND()'; // database specific random keyword |
| 56 | |
| 57 | /** |
| 58 | * Non-persistent database connection |
| 59 | * |
| 60 | * @access private called by the base class |
| 61 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 62 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | function db_connect() |
| 64 | { |
| 65 | if ($this->port != '') |
| 66 | { |
| 67 | $this->hostname .= ':'.$this->port; |
| 68 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | return @mysql_connect($this->hostname, $this->username, $this->password, TRUE); |
| 71 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 72 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | // -------------------------------------------------------------------- |
| 74 | |
| 75 | /** |
| 76 | * Persistent database connection |
| 77 | * |
| 78 | * @access private called by the base class |
| 79 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 81 | function db_pconnect() |
| 82 | { |
| 83 | if ($this->port != '') |
| 84 | { |
| 85 | $this->hostname .= ':'.$this->port; |
| 86 | } |
| 87 | |
| 88 | return @mysql_pconnect($this->hostname, $this->username, $this->password); |
| 89 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | // -------------------------------------------------------------------- |
| 92 | |
| 93 | /** |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 94 | * Reconnect |
| 95 | * |
| 96 | * Keep / reestablish the db connection if no queries have been |
| 97 | * sent for a length of time exceeding the server's idle timeout |
| 98 | * |
| 99 | * @access public |
| 100 | * @return void |
| 101 | */ |
| 102 | function reconnect() |
| 103 | { |
| 104 | if (mysql_ping($this->conn_id) === FALSE) |
| 105 | { |
| 106 | $this->conn_id = FALSE; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Derek Jones | 87cbafc | 2009-02-27 16:29:59 +0000 | [diff] [blame] | 112 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | * Select the database |
| 114 | * |
| 115 | * @access private called by the base class |
| 116 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | function db_select() |
| 119 | { |
| 120 | return @mysql_select_db($this->database, $this->conn_id); |
| 121 | } |
| 122 | |
| 123 | // -------------------------------------------------------------------- |
| 124 | |
| 125 | /** |
| 126 | * Set client character set |
| 127 | * |
| 128 | * @access public |
| 129 | * @param string |
| 130 | * @param string |
| 131 | * @return resource |
| 132 | */ |
| 133 | function db_set_charset($charset, $collation) |
| 134 | { |
Derek Jones | 6ae70cc | 2011-04-19 16:13:48 -0500 | [diff] [blame] | 135 | static $use_set_names; |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 136 | |
Derek Jones | 6ae70cc | 2011-04-19 16:13:48 -0500 | [diff] [blame] | 137 | if ( ! isset($use_set_names)) |
| 138 | { |
| 139 | // mysql_set_charset() requires PHP >= 5.2.3 and MySQL >= 5.0.7, use SET NAMES as fallback |
| 140 | $use_set_names = (version_compare(PHP_VERSION, '5.2.3', '>=') && version_compare(mysql_get_server_info(), '5.0.7', '>=')) ? FALSE : TRUE; |
| 141 | } |
| 142 | |
| 143 | if ($use_set_names) |
| 144 | { |
| 145 | return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | return @mysql_set_charset($charset, $this->conn_id); |
| 150 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | /** |
| 156 | * Version number query string |
| 157 | * |
| 158 | * @access public |
| 159 | * @return string |
| 160 | */ |
| 161 | function _version() |
| 162 | { |
| 163 | return "SELECT version() AS ver"; |
| 164 | } |
| 165 | |
| 166 | // -------------------------------------------------------------------- |
| 167 | |
| 168 | /** |
| 169 | * Execute the query |
| 170 | * |
| 171 | * @access private called by the base class |
| 172 | * @param string an SQL query |
| 173 | * @return resource |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | function _execute($sql) |
| 176 | { |
| 177 | $sql = $this->_prep_query($sql); |
| 178 | return @mysql_query($sql, $this->conn_id); |
| 179 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
| 184 | * Prep the query |
| 185 | * |
| 186 | * If needed, each database adapter can prep the query string |
| 187 | * |
| 188 | * @access private called by execute() |
| 189 | * @param string an SQL query |
| 190 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | function _prep_query($sql) |
| 193 | { |
| 194 | // "DELETE FROM TABLE" returns 0 affected rows This hack modifies |
| 195 | // the query so that it returns the number of affected rows |
| 196 | if ($this->delete_hack === TRUE) |
| 197 | { |
| 198 | if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql)) |
| 199 | { |
| 200 | $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql); |
| 201 | } |
| 202 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | return $sql; |
| 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
| 209 | /** |
| 210 | * Begin Transaction |
| 211 | * |
| 212 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | * @return bool |
| 214 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | function trans_begin($test_mode = FALSE) |
| 216 | { |
| 217 | if ( ! $this->trans_enabled) |
| 218 | { |
| 219 | return TRUE; |
| 220 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 223 | if ($this->_trans_depth > 0) |
| 224 | { |
| 225 | return TRUE; |
| 226 | } |
| 227 | |
| 228 | // Reset the transaction failure flag. |
| 229 | // If the $test_mode flag is set to TRUE transactions will be rolled back |
| 230 | // even if the queries produce a successful result. |
| 231 | $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | $this->simple_query('SET AUTOCOMMIT=0'); |
| 234 | $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK |
| 235 | return TRUE; |
| 236 | } |
| 237 | |
| 238 | // -------------------------------------------------------------------- |
| 239 | |
| 240 | /** |
| 241 | * Commit Transaction |
| 242 | * |
| 243 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | * @return bool |
| 245 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | function trans_commit() |
| 247 | { |
| 248 | if ( ! $this->trans_enabled) |
| 249 | { |
| 250 | return TRUE; |
| 251 | } |
| 252 | |
| 253 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 254 | if ($this->_trans_depth > 0) |
| 255 | { |
| 256 | return TRUE; |
| 257 | } |
| 258 | |
| 259 | $this->simple_query('COMMIT'); |
| 260 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 261 | return TRUE; |
| 262 | } |
| 263 | |
| 264 | // -------------------------------------------------------------------- |
| 265 | |
| 266 | /** |
| 267 | * Rollback Transaction |
| 268 | * |
| 269 | * @access public |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | * @return bool |
| 271 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | function trans_rollback() |
| 273 | { |
| 274 | if ( ! $this->trans_enabled) |
| 275 | { |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
| 279 | // When transactions are nested we only begin/commit/rollback the outermost ones |
| 280 | if ($this->_trans_depth > 0) |
| 281 | { |
| 282 | return TRUE; |
| 283 | } |
| 284 | |
| 285 | $this->simple_query('ROLLBACK'); |
| 286 | $this->simple_query('SET AUTOCOMMIT=1'); |
| 287 | return TRUE; |
| 288 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | // -------------------------------------------------------------------- |
| 291 | |
| 292 | /** |
| 293 | * Escape String |
| 294 | * |
| 295 | * @access public |
| 296 | * @param string |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 297 | * @param bool whether or not the string will be used in a LIKE condition |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | * @return string |
| 299 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 300 | function escape_str($str, $like = FALSE) |
| 301 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 302 | if (is_array($str)) |
| 303 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 304 | foreach ($str as $key => $val) |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 305 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 306 | $str[$key] = $this->escape_str($val, $like); |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 307 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 309 | return $str; |
| 310 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 311 | |
| 312 | if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id)) |
| 313 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 314 | $str = mysql_real_escape_string($str, $this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | } |
| 316 | elseif (function_exists('mysql_escape_string')) |
| 317 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 318 | $str = mysql_escape_string($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | } |
| 320 | else |
| 321 | { |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 322 | $str = addslashes($str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 324 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 325 | // escape LIKE condition wildcards |
| 326 | if ($like === TRUE) |
| 327 | { |
| 328 | $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); |
| 329 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | |
Derek Jones | e4ed583 | 2009-02-20 21:44:59 +0000 | [diff] [blame] | 331 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 334 | // -------------------------------------------------------------------- |
| 335 | |
| 336 | /** |
| 337 | * Affected Rows |
| 338 | * |
| 339 | * @access public |
| 340 | * @return integer |
| 341 | */ |
| 342 | function affected_rows() |
| 343 | { |
| 344 | return @mysql_affected_rows($this->conn_id); |
| 345 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 346 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | // -------------------------------------------------------------------- |
| 348 | |
| 349 | /** |
| 350 | * Insert ID |
| 351 | * |
| 352 | * @access public |
| 353 | * @return integer |
| 354 | */ |
| 355 | function insert_id() |
| 356 | { |
| 357 | return @mysql_insert_id($this->conn_id); |
| 358 | } |
| 359 | |
| 360 | // -------------------------------------------------------------------- |
| 361 | |
| 362 | /** |
| 363 | * "Count All" query |
| 364 | * |
| 365 | * Generates a platform-specific query string that counts all records in |
| 366 | * the specified database |
| 367 | * |
| 368 | * @access public |
| 369 | * @param string |
| 370 | * @return string |
| 371 | */ |
| 372 | function count_all($table = '') |
| 373 | { |
| 374 | if ($table == '') |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 375 | { |
| 376 | return 0; |
| 377 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 379 | $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); |
| 380 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | if ($query->num_rows() == 0) |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 382 | { |
| 383 | return 0; |
| 384 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | |
| 386 | $row = $query->row(); |
Derek Allard | e37ab38 | 2009-02-03 16:13:57 +0000 | [diff] [blame] | 387 | return (int) $row->numrows; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // -------------------------------------------------------------------- |
| 391 | |
| 392 | /** |
| 393 | * List table query |
| 394 | * |
| 395 | * Generates a platform-specific query string so that the table names can be fetched |
| 396 | * |
| 397 | * @access private |
| 398 | * @param boolean |
| 399 | * @return string |
| 400 | */ |
| 401 | function _list_tables($prefix_limit = FALSE) |
| 402 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | |
| 405 | if ($prefix_limit !== FALSE AND $this->dbprefix != '') |
| 406 | { |
Derek Jones | 3c11b6f | 2009-02-20 22:36:27 +0000 | [diff] [blame] | 407 | $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | return $sql; |
| 411 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 412 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | // -------------------------------------------------------------------- |
| 414 | |
| 415 | /** |
| 416 | * Show column query |
| 417 | * |
| 418 | * Generates a platform-specific query string so that the column names can be fetched |
| 419 | * |
| 420 | * @access public |
| 421 | * @param string the table name |
| 422 | * @return string |
| 423 | */ |
| 424 | function _list_columns($table = '') |
| 425 | { |
Greg Aker | 1edde30 | 2010-01-26 00:17:01 +0000 | [diff] [blame] | 426 | return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | // -------------------------------------------------------------------- |
| 430 | |
| 431 | /** |
| 432 | * Field data query |
| 433 | * |
| 434 | * Generates a platform-specific query so that the column data can be retrieved |
| 435 | * |
| 436 | * @access public |
| 437 | * @param string the table name |
| 438 | * @return object |
| 439 | */ |
| 440 | function _field_data($table) |
| 441 | { |
| 442 | return "SELECT * FROM ".$table." LIMIT 1"; |
| 443 | } |
| 444 | |
| 445 | // -------------------------------------------------------------------- |
| 446 | |
| 447 | /** |
| 448 | * The error message string |
| 449 | * |
| 450 | * @access private |
| 451 | * @return string |
| 452 | */ |
| 453 | function _error_message() |
| 454 | { |
| 455 | return mysql_error($this->conn_id); |
| 456 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 457 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | // -------------------------------------------------------------------- |
| 459 | |
| 460 | /** |
| 461 | * The error message number |
| 462 | * |
| 463 | * @access private |
| 464 | * @return integer |
| 465 | */ |
| 466 | function _error_number() |
| 467 | { |
| 468 | return mysql_errno($this->conn_id); |
| 469 | } |
| 470 | |
| 471 | // -------------------------------------------------------------------- |
| 472 | |
| 473 | /** |
| 474 | * Escape the SQL Identifiers |
| 475 | * |
| 476 | * This function escapes column and table names |
| 477 | * |
| 478 | * @access private |
| 479 | * @param string |
| 480 | * @return string |
| 481 | */ |
| 482 | function _escape_identifiers($item) |
| 483 | { |
| 484 | if ($this->_escape_char == '') |
| 485 | { |
| 486 | return $item; |
| 487 | } |
| 488 | |
| 489 | foreach ($this->_reserved_identifiers as $id) |
| 490 | { |
| 491 | if (strpos($item, '.'.$id) !== FALSE) |
| 492 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 493 | $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); |
| 494 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 495 | // remove duplicates if the user already included the escape |
| 496 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | if (strpos($item, '.') !== FALSE) |
| 501 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 502 | $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | } |
| 504 | else |
| 505 | { |
| 506 | $str = $this->_escape_char.$item.$this->_escape_char; |
| 507 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 509 | // remove duplicates if the user already included the escape |
| 510 | return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); |
| 511 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 512 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 513 | // -------------------------------------------------------------------- |
| 514 | |
| 515 | /** |
| 516 | * From Tables |
| 517 | * |
| 518 | * This function implicitly groups FROM tables so there is no confusion |
| 519 | * about operator precedence in harmony with SQL standards |
| 520 | * |
| 521 | * @access public |
| 522 | * @param type |
| 523 | * @return type |
| 524 | */ |
| 525 | function _from_tables($tables) |
| 526 | { |
| 527 | if ( ! is_array($tables)) |
| 528 | { |
| 529 | $tables = array($tables); |
| 530 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 531 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 532 | return '('.implode(', ', $tables).')'; |
| 533 | } |
| 534 | |
| 535 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 536 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 537 | /** |
| 538 | * Insert statement |
| 539 | * |
| 540 | * Generates a platform-specific insert string from the supplied data |
| 541 | * |
| 542 | * @access public |
| 543 | * @param string the table name |
| 544 | * @param array the insert keys |
| 545 | * @param array the insert values |
| 546 | * @return string |
| 547 | */ |
| 548 | function _insert($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 549 | { |
Phil Sturgeon | a12216b | 2011-02-09 16:09:31 +0000 | [diff] [blame] | 550 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 552 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | // -------------------------------------------------------------------- |
| 554 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 555 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 556 | /** |
| 557 | * Replace statement |
| 558 | * |
| 559 | * Generates a platform-specific replace string from the supplied data |
| 560 | * |
| 561 | * @access public |
| 562 | * @param string the table name |
| 563 | * @param array the insert keys |
| 564 | * @param array the insert values |
| 565 | * @return string |
| 566 | */ |
| 567 | function _replace($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 568 | { |
Phil Sturgeon | a12216b | 2011-02-09 16:09:31 +0000 | [diff] [blame] | 569 | return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 570 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 571 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 572 | // -------------------------------------------------------------------- |
| 573 | |
| 574 | /** |
| 575 | * Insert_batch statement |
| 576 | * |
| 577 | * Generates a platform-specific insert string from the supplied data |
| 578 | * |
| 579 | * @access public |
| 580 | * @param string the table name |
| 581 | * @param array the insert keys |
| 582 | * @param array the insert values |
| 583 | * @return string |
| 584 | */ |
| 585 | function _insert_batch($table, $keys, $values) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 586 | { |
Phil Sturgeon | a12216b | 2011-02-09 16:09:31 +0000 | [diff] [blame] | 587 | return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 588 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 589 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 590 | // -------------------------------------------------------------------- |
| 591 | |
| 592 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 593 | /** |
| 594 | * Update statement |
| 595 | * |
| 596 | * Generates a platform-specific update string from the supplied data |
| 597 | * |
| 598 | * @access public |
| 599 | * @param string the table name |
| 600 | * @param array the update data |
| 601 | * @param array the where clause |
| 602 | * @param array the orderby clause |
| 603 | * @param array the limit clause |
| 604 | * @return string |
| 605 | */ |
| 606 | function _update($table, $values, $where, $orderby = array(), $limit = FALSE) |
| 607 | { |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 608 | foreach ($values as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 609 | { |
Phil Sturgeon | a12216b | 2011-02-09 16:09:31 +0000 | [diff] [blame] | 610 | $valstr[] = $key . ' = ' . $val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 611 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 612 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 613 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 614 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 615 | $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 616 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 617 | $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); |
| 618 | |
| 619 | $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; |
| 620 | |
| 621 | $sql .= $orderby.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 622 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 623 | return $sql; |
| 624 | } |
| 625 | |
| 626 | // -------------------------------------------------------------------- |
| 627 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * Update_Batch statement |
| 631 | * |
| 632 | * Generates a platform-specific batch update string from the supplied data |
| 633 | * |
| 634 | * @access public |
| 635 | * @param string the table name |
| 636 | * @param array the update data |
| 637 | * @param array the where clause |
| 638 | * @return string |
| 639 | */ |
| 640 | function _update_batch($table, $values, $index, $where = NULL) |
| 641 | { |
| 642 | $ids = array(); |
| 643 | $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; |
| 644 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 645 | foreach ($values as $key => $val) |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 646 | { |
| 647 | $ids[] = $val[$index]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 648 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 649 | foreach (array_keys($val) as $field) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 650 | { |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 651 | if ($field != $index) |
| 652 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 653 | $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 657 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 658 | $sql = "UPDATE ".$table." SET "; |
| 659 | $cases = ''; |
| 660 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 661 | foreach ($final as $k => $v) |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 662 | { |
| 663 | $cases .= $k.' = CASE '."\n"; |
| 664 | foreach ($v as $row) |
| 665 | { |
| 666 | $cases .= $row."\n"; |
| 667 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 668 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 669 | $cases .= 'ELSE '.$k.' END, '; |
| 670 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 671 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 672 | $sql .= substr($cases, 0, -2); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 673 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 674 | $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 675 | |
Derek Jones | 20aa2bd | 2010-03-02 17:28:07 -0600 | [diff] [blame] | 676 | return $sql; |
| 677 | } |
| 678 | |
| 679 | // -------------------------------------------------------------------- |
| 680 | |
| 681 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 682 | /** |
| 683 | * Truncate statement |
| 684 | * |
| 685 | * Generates a platform-specific truncate string from the supplied data |
| 686 | * If the database does not support the truncate() command |
| 687 | * This function maps to "DELETE FROM table" |
| 688 | * |
| 689 | * @access public |
| 690 | * @param string the table name |
| 691 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 692 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 693 | function _truncate($table) |
| 694 | { |
| 695 | return "TRUNCATE ".$table; |
| 696 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 697 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 698 | // -------------------------------------------------------------------- |
| 699 | |
| 700 | /** |
| 701 | * Delete statement |
| 702 | * |
| 703 | * Generates a platform-specific delete string from the supplied data |
| 704 | * |
| 705 | * @access public |
| 706 | * @param string the table name |
| 707 | * @param array the where clause |
| 708 | * @param string the limit clause |
| 709 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 710 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 711 | function _delete($table, $where = array(), $like = array(), $limit = FALSE) |
| 712 | { |
| 713 | $conditions = ''; |
| 714 | |
| 715 | if (count($where) > 0 OR count($like) > 0) |
| 716 | { |
| 717 | $conditions = "\nWHERE "; |
| 718 | $conditions .= implode("\n", $this->ar_where); |
| 719 | |
| 720 | if (count($where) > 0 && count($like) > 0) |
| 721 | { |
| 722 | $conditions .= " AND "; |
| 723 | } |
| 724 | $conditions .= implode("\n", $like); |
| 725 | } |
| 726 | |
| 727 | $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 728 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 729 | return "DELETE FROM ".$table.$conditions.$limit; |
| 730 | } |
| 731 | |
| 732 | // -------------------------------------------------------------------- |
| 733 | |
| 734 | /** |
| 735 | * Limit string |
| 736 | * |
| 737 | * Generates a platform-specific LIMIT clause |
| 738 | * |
| 739 | * @access public |
| 740 | * @param string the sql query string |
| 741 | * @param integer the number of rows to limit the query to |
| 742 | * @param integer the offset value |
| 743 | * @return string |
| 744 | */ |
| 745 | function _limit($sql, $limit, $offset) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 746 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 747 | if ($offset == 0) |
| 748 | { |
| 749 | $offset = ''; |
| 750 | } |
| 751 | else |
| 752 | { |
| 753 | $offset .= ", "; |
| 754 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 755 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 756 | return $sql."LIMIT ".$offset.$limit; |
| 757 | } |
| 758 | |
| 759 | // -------------------------------------------------------------------- |
| 760 | |
| 761 | /** |
| 762 | * Close DB Connection |
| 763 | * |
| 764 | * @access public |
| 765 | * @param resource |
| 766 | * @return void |
| 767 | */ |
| 768 | function _close($conn_id) |
| 769 | { |
| 770 | @mysql_close($conn_id); |
| 771 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 772 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | |
| 776 | /* End of file mysql_driver.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 777 | /* Location: ./system/database/drivers/mysql/mysql_driver.php */ |