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