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