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