blob: dc020c624014420c8f33aabb8f34924ec07f08f4 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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 */
31class CI_DB_mysql_driver extends CI_DB {
32
33 var $dbdriver = 'mysql';
34
35 // The character used for escaping
36 var $_escape_char = '`';
Derek Jonese4ed5832009-02-20 21:44:59 +000037
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 Allard2067d1a2008-11-13 22:59:24 +000042 /**
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 Mienydd671972010-10-04 16:33:58 +020046 */
Derek Allard2067d1a2008-11-13 22:59:24 +000047 var $delete_hack = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +020048
Derek Allard2067d1a2008-11-13 22:59:24 +000049 /**
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 Jones3b9f88d2011-05-20 10:25:13 -050057 // whether SET NAMES must be used to set the character set
58 var $use_set_names;
RH Beckercfdb2322011-10-03 17:28:32 -070059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
61 * Non-persistent database connection
62 *
63 * @access private called by the base class
64 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020065 */
Derek Allard2067d1a2008-11-13 22:59:24 +000066 function db_connect()
67 {
68 if ($this->port != '')
69 {
70 $this->hostname .= ':'.$this->port;
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
74 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 // --------------------------------------------------------------------
77
78 /**
79 * Persistent database connection
80 *
81 * @access private called by the base class
82 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020083 */
Derek Allard2067d1a2008-11-13 22:59:24 +000084 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 Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // --------------------------------------------------------------------
95
96 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000097 * 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 Mienydd671972010-10-04 16:33:58 +0200114
Derek Jones87cbafc2009-02-27 16:29:59 +0000115 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 * Select the database
117 *
118 * @access private called by the base class
119 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200120 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 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 Beckercfdb2322011-10-03 17:28:32 -0700138 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 Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 /**
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 Mienydd671972010-10-04 16:33:58 +0200164 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 function _execute($sql)
166 {
167 $sql = $this->_prep_query($sql);
168 return @mysql_query($sql, $this->conn_id);
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // --------------------------------------------------------------------
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 Mienydd671972010-10-04 16:33:58 +0200181 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 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 Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 return $sql;
195 }
196
197 // --------------------------------------------------------------------
198
199 /**
200 * Begin Transaction
201 *
202 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200203 * @return bool
204 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 function trans_begin($test_mode = FALSE)
206 {
207 if ( ! $this->trans_enabled)
208 {
209 return TRUE;
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // 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 Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $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 Mienydd671972010-10-04 16:33:58 +0200234 * @return bool
235 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 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 Mienydd671972010-10-04 16:33:58 +0200260 * @return bool
261 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 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 Mienydd671972010-10-04 16:33:58 +0200279
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 // --------------------------------------------------------------------
281
282 /**
283 * Escape String
284 *
285 * @access public
286 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000287 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @return string
289 */
Barry Mienydd671972010-10-04 16:33:58 +0200290 function escape_str($str, $like = FALSE)
291 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 if (is_array($str))
293 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500294 foreach ($str as $key => $val)
Derek Jones37f4b9c2011-07-01 17:56:50 -0500295 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000296 $str[$key] = $this->escape_str($val, $like);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Jones37f4b9c2011-07-01 17:56:50 -0500299 return $str;
300 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000301
302 if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
303 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000304 $str = mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 }
306 elseif (function_exists('mysql_escape_string'))
307 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
310 else
311 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000312 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Jonese4ed5832009-02-20 21:44:59 +0000315 // escape LIKE condition wildcards
316 if ($like === TRUE)
317 {
318 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Jonese4ed5832009-02-20 21:44:59 +0000321 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
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 Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
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 Allarde37ab382009-02-03 16:13:57 +0000365 {
366 return 0;
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allarde37ab382009-02-03 16:13:57 +0000369 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000372 {
373 return 0;
374 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000375
376 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500377 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000378 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
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 Mienydd671972010-10-04 16:33:58 +0200394 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000395
396 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
397 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000398 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
400
401 return $sql;
402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // --------------------------------------------------------------------
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 Aker1edde302010-01-26 00:17:01 +0000417 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
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 {
danmontgomeryfc756452011-08-21 15:31:22 -0400433 return "DESCRIBE ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
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 Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 // --------------------------------------------------------------------
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 Mienydd671972010-10-04 16:33:58 +0200484 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 // remove duplicates if the user already included the escape
487 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200488 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 if (strpos($item, '.') !== FALSE)
492 {
Barry Mienydd671972010-10-04 16:33:58 +0200493 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 }
495 else
496 {
497 $str = $this->_escape_char.$item.$this->_escape_char;
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 // remove duplicates if the user already included the escape
501 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 // --------------------------------------------------------------------
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 Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 return '('.implode(', ', $tables).')';
524 }
525
526 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 /**
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 Mienydd671972010-10-04 16:33:58 +0200540 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000541 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 // --------------------------------------------------------------------
545
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Jones20aa2bd2010-03-02 17:28:07 -0600547 /**
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 Mienydd671972010-10-04 16:33:58 +0200559 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000560 return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Jones20aa2bd2010-03-02 17:28:07 -0600561 }
Barry Mienydd671972010-10-04 16:33:58 +0200562
Derek Jones20aa2bd2010-03-02 17:28:07 -0600563 // --------------------------------------------------------------------
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 Mienydd671972010-10-04 16:33:58 +0200577 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000578 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
Derek Jones20aa2bd2010-03-02 17:28:07 -0600579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Derek Jones20aa2bd2010-03-02 17:28:07 -0600581 // --------------------------------------------------------------------
582
583
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 /**
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 Krietec3a4a8d2011-02-14 13:40:08 -0500599 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000601 $valstr[] = $key . ' = ' . $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 }
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200607
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
609
610 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
611
612 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200613
Derek Allard2067d1a2008-11-13 22:59:24 +0000614 return $sql;
615 }
616
617 // --------------------------------------------------------------------
618
Derek Jones20aa2bd2010-03-02 17:28:07 -0600619
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 Krietec3a4a8d2011-02-14 13:40:08 -0500636 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600637 {
638 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200639
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500640 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200641 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600642 if ($field != $index)
643 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500644 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600645 }
646 }
647 }
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Jones20aa2bd2010-03-02 17:28:07 -0600649 $sql = "UPDATE ".$table." SET ";
650 $cases = '';
651
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500652 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600653 {
654 $cases .= $k.' = CASE '."\n";
655 foreach ($v as $row)
656 {
657 $cases .= $row."\n";
658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Jones20aa2bd2010-03-02 17:28:07 -0600660 $cases .= 'ELSE '.$k.' END, ';
661 }
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Jones20aa2bd2010-03-02 17:28:07 -0600663 $sql .= substr($cases, 0, -2);
Barry Mienydd671972010-10-04 16:33:58 +0200664
Derek Jones20aa2bd2010-03-02 17:28:07 -0600665 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Jones20aa2bd2010-03-02 17:28:07 -0600667 return $sql;
668 }
669
670 // --------------------------------------------------------------------
671
672
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 /**
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 Mienydd671972010-10-04 16:33:58 +0200683 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000684 function _truncate($table)
685 {
686 return "TRUNCATE ".$table;
687 }
Barry Mienydd671972010-10-04 16:33:58 +0200688
Derek Allard2067d1a2008-11-13 22:59:24 +0000689 // --------------------------------------------------------------------
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 Mienydd671972010-10-04 16:33:58 +0200701 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 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 Mienydd671972010-10-04 16:33:58 +0200719
Derek Allard2067d1a2008-11-13 22:59:24 +0000720 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 Mienydd671972010-10-04 16:33:58 +0200737 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000738 if ($offset == 0)
739 {
740 $offset = '';
741 }
742 else
743 {
744 $offset .= ", ";
745 }
Barry Mienydd671972010-10-04 16:33:58 +0200746
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 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 Mienydd671972010-10-04 16:33:58 +0200763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764}
765
766
767/* End of file mysql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000768/* Location: ./system/database/drivers/mysql/mysql_driver.php */