blob: b7d547cc0e40f7950d7e500c9fa5adaeeefaec71 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -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
57 /**
58 * Non-persistent database connection
59 *
60 * @access private called by the base class
61 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020062 */
Derek Allard2067d1a2008-11-13 22:59:24 +000063 function db_connect()
64 {
65 if ($this->port != '')
66 {
67 $this->hostname .= ':'.$this->port;
68 }
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
71 }
Barry Mienydd671972010-10-04 16:33:58 +020072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 // --------------------------------------------------------------------
74
75 /**
76 * Persistent database connection
77 *
78 * @access private called by the base class
79 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020080 */
Derek Allard2067d1a2008-11-13 22:59:24 +000081 function db_pconnect()
82 {
83 if ($this->port != '')
84 {
85 $this->hostname .= ':'.$this->port;
86 }
87
88 return @mysql_pconnect($this->hostname, $this->username, $this->password);
89 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 // --------------------------------------------------------------------
92
93 /**
Derek Jones87cbafc2009-02-27 16:29:59 +000094 * Reconnect
95 *
96 * Keep / reestablish the db connection if no queries have been
97 * sent for a length of time exceeding the server's idle timeout
98 *
99 * @access public
100 * @return void
101 */
102 function reconnect()
103 {
104 if (mysql_ping($this->conn_id) === FALSE)
105 {
106 $this->conn_id = FALSE;
107 }
108 }
109
110 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Jones87cbafc2009-02-27 16:29:59 +0000112 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * Select the database
114 *
115 * @access private called by the base class
116 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200117 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 function db_select()
119 {
120 return @mysql_select_db($this->database, $this->conn_id);
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
126 * Set client character set
127 *
128 * @access public
129 * @param string
130 * @param string
131 * @return resource
132 */
133 function db_set_charset($charset, $collation)
134 {
Derek Jones6ae70cc2011-04-19 16:13:48 -0500135 static $use_set_names;
Derek Jones4b9c6292011-07-01 17:40:48 -0500136
Derek Jones6ae70cc2011-04-19 16:13:48 -0500137 if ( ! isset($use_set_names))
138 {
139 // mysql_set_charset() requires PHP >= 5.2.3 and MySQL >= 5.0.7, use SET NAMES as fallback
140 $use_set_names = (version_compare(PHP_VERSION, '5.2.3', '>=') && version_compare(mysql_get_server_info(), '5.0.7', '>=')) ? FALSE : TRUE;
141 }
142
143 if ($use_set_names)
144 {
145 return @mysql_query("SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'", $this->conn_id);
146 }
147 else
148 {
149 return @mysql_set_charset($charset, $this->conn_id);
150 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152
153 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 /**
156 * Version number query string
157 *
158 * @access public
159 * @return string
160 */
161 function _version()
162 {
163 return "SELECT version() AS ver";
164 }
165
166 // --------------------------------------------------------------------
167
168 /**
169 * Execute the query
170 *
171 * @access private called by the base class
172 * @param string an SQL query
173 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200174 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 function _execute($sql)
176 {
177 $sql = $this->_prep_query($sql);
178 return @mysql_query($sql, $this->conn_id);
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 // --------------------------------------------------------------------
182
183 /**
184 * Prep the query
185 *
186 * If needed, each database adapter can prep the query string
187 *
188 * @access private called by execute()
189 * @param string an SQL query
190 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200191 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 function _prep_query($sql)
193 {
194 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
195 // the query so that it returns the number of affected rows
196 if ($this->delete_hack === TRUE)
197 {
198 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
199 {
200 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
201 }
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 return $sql;
205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Begin Transaction
211 *
212 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200213 * @return bool
214 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 function trans_begin($test_mode = FALSE)
216 {
217 if ( ! $this->trans_enabled)
218 {
219 return TRUE;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 // When transactions are nested we only begin/commit/rollback the outermost ones
223 if ($this->_trans_depth > 0)
224 {
225 return TRUE;
226 }
227
228 // Reset the transaction failure flag.
229 // If the $test_mode flag is set to TRUE transactions will be rolled back
230 // even if the queries produce a successful result.
231 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $this->simple_query('SET AUTOCOMMIT=0');
234 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
235 return TRUE;
236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Commit Transaction
242 *
243 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200244 * @return bool
245 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 function trans_commit()
247 {
248 if ( ! $this->trans_enabled)
249 {
250 return TRUE;
251 }
252
253 // When transactions are nested we only begin/commit/rollback the outermost ones
254 if ($this->_trans_depth > 0)
255 {
256 return TRUE;
257 }
258
259 $this->simple_query('COMMIT');
260 $this->simple_query('SET AUTOCOMMIT=1');
261 return TRUE;
262 }
263
264 // --------------------------------------------------------------------
265
266 /**
267 * Rollback Transaction
268 *
269 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200270 * @return bool
271 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 function trans_rollback()
273 {
274 if ( ! $this->trans_enabled)
275 {
276 return TRUE;
277 }
278
279 // When transactions are nested we only begin/commit/rollback the outermost ones
280 if ($this->_trans_depth > 0)
281 {
282 return TRUE;
283 }
284
285 $this->simple_query('ROLLBACK');
286 $this->simple_query('SET AUTOCOMMIT=1');
287 return TRUE;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
291
292 /**
293 * Escape String
294 *
295 * @access public
296 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000297 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 * @return string
299 */
Barry Mienydd671972010-10-04 16:33:58 +0200300 function escape_str($str, $like = FALSE)
301 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 if (is_array($str))
303 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500304 foreach ($str as $key => $val)
Derek Jones4b9c6292011-07-01 17:40:48 -0500305 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000306 $str[$key] = $this->escape_str($val, $like);
Derek Jones4b9c6292011-07-01 17:40:48 -0500307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Jones4b9c6292011-07-01 17:40:48 -0500309 return $str;
310 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000311
312 if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
313 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000314 $str = mysql_real_escape_string($str, $this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 }
316 elseif (function_exists('mysql_escape_string'))
317 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000318 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
320 else
321 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000322 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Jonese4ed5832009-02-20 21:44:59 +0000325 // escape LIKE condition wildcards
326 if ($like === TRUE)
327 {
328 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Jonese4ed5832009-02-20 21:44:59 +0000331 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 // --------------------------------------------------------------------
335
336 /**
337 * Affected Rows
338 *
339 * @access public
340 * @return integer
341 */
342 function affected_rows()
343 {
344 return @mysql_affected_rows($this->conn_id);
345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // --------------------------------------------------------------------
348
349 /**
350 * Insert ID
351 *
352 * @access public
353 * @return integer
354 */
355 function insert_id()
356 {
357 return @mysql_insert_id($this->conn_id);
358 }
359
360 // --------------------------------------------------------------------
361
362 /**
363 * "Count All" query
364 *
365 * Generates a platform-specific query string that counts all records in
366 * the specified database
367 *
368 * @access public
369 * @param string
370 * @return string
371 */
372 function count_all($table = '')
373 {
374 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000375 {
376 return 0;
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allarde37ab382009-02-03 16:13:57 +0000379 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000382 {
383 return 0;
384 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000385
386 $row = $query->row();
Derek Allarde37ab382009-02-03 16:13:57 +0000387 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
389
390 // --------------------------------------------------------------------
391
392 /**
393 * List table query
394 *
395 * Generates a platform-specific query string so that the table names can be fetched
396 *
397 * @access private
398 * @param boolean
399 * @return string
400 */
401 function _list_tables($prefix_limit = FALSE)
402 {
Barry Mienydd671972010-10-04 16:33:58 +0200403 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000404
405 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
406 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000407 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
410 return $sql;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 // --------------------------------------------------------------------
414
415 /**
416 * Show column query
417 *
418 * Generates a platform-specific query string so that the column names can be fetched
419 *
420 * @access public
421 * @param string the table name
422 * @return string
423 */
424 function _list_columns($table = '')
425 {
Greg Aker1edde302010-01-26 00:17:01 +0000426 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428
429 // --------------------------------------------------------------------
430
431 /**
432 * Field data query
433 *
434 * Generates a platform-specific query so that the column data can be retrieved
435 *
436 * @access public
437 * @param string the table name
438 * @return object
439 */
440 function _field_data($table)
441 {
442 return "SELECT * FROM ".$table." LIMIT 1";
443 }
444
445 // --------------------------------------------------------------------
446
447 /**
448 * The error message string
449 *
450 * @access private
451 * @return string
452 */
453 function _error_message()
454 {
455 return mysql_error($this->conn_id);
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // --------------------------------------------------------------------
459
460 /**
461 * The error message number
462 *
463 * @access private
464 * @return integer
465 */
466 function _error_number()
467 {
468 return mysql_errno($this->conn_id);
469 }
470
471 // --------------------------------------------------------------------
472
473 /**
474 * Escape the SQL Identifiers
475 *
476 * This function escapes column and table names
477 *
478 * @access private
479 * @param string
480 * @return string
481 */
482 function _escape_identifiers($item)
483 {
484 if ($this->_escape_char == '')
485 {
486 return $item;
487 }
488
489 foreach ($this->_reserved_identifiers as $id)
490 {
491 if (strpos($item, '.'.$id) !== FALSE)
492 {
Barry Mienydd671972010-10-04 16:33:58 +0200493 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 // remove duplicates if the user already included the escape
496 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200497 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 if (strpos($item, '.') !== FALSE)
501 {
Barry Mienydd671972010-10-04 16:33:58 +0200502 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 }
504 else
505 {
506 $str = $this->_escape_char.$item.$this->_escape_char;
507 }
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 // remove duplicates if the user already included the escape
510 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 // --------------------------------------------------------------------
514
515 /**
516 * From Tables
517 *
518 * This function implicitly groups FROM tables so there is no confusion
519 * about operator precedence in harmony with SQL standards
520 *
521 * @access public
522 * @param type
523 * @return type
524 */
525 function _from_tables($tables)
526 {
527 if ( ! is_array($tables))
528 {
529 $tables = array($tables);
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 return '('.implode(', ', $tables).')';
533 }
534
535 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 /**
538 * Insert statement
539 *
540 * Generates a platform-specific insert string from the supplied data
541 *
542 * @access public
543 * @param string the table name
544 * @param array the insert keys
545 * @param array the insert values
546 * @return string
547 */
548 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200549 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000550 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 // --------------------------------------------------------------------
554
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Jones20aa2bd2010-03-02 17:28:07 -0600556 /**
557 * Replace statement
558 *
559 * Generates a platform-specific replace string from the supplied data
560 *
561 * @access public
562 * @param string the table name
563 * @param array the insert keys
564 * @param array the insert values
565 * @return string
566 */
567 function _replace($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200568 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000569 return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
Derek Jones20aa2bd2010-03-02 17:28:07 -0600570 }
Barry Mienydd671972010-10-04 16:33:58 +0200571
Derek Jones20aa2bd2010-03-02 17:28:07 -0600572 // --------------------------------------------------------------------
573
574 /**
575 * Insert_batch statement
576 *
577 * Generates a platform-specific insert string from the supplied data
578 *
579 * @access public
580 * @param string the table name
581 * @param array the insert keys
582 * @param array the insert values
583 * @return string
584 */
585 function _insert_batch($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200586 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000587 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
Derek Jones20aa2bd2010-03-02 17:28:07 -0600588 }
Barry Mienydd671972010-10-04 16:33:58 +0200589
Derek Jones20aa2bd2010-03-02 17:28:07 -0600590 // --------------------------------------------------------------------
591
592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 /**
594 * Update statement
595 *
596 * Generates a platform-specific update string from the supplied data
597 *
598 * @access public
599 * @param string the table name
600 * @param array the update data
601 * @param array the where clause
602 * @param array the orderby clause
603 * @param array the limit clause
604 * @return string
605 */
606 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
607 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500608 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Phil Sturgeona12216b2011-02-09 16:09:31 +0000610 $valstr[] = $key . ' = ' . $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 }
Barry Mienydd671972010-10-04 16:33:58 +0200612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
618
619 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
620
621 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200622
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 return $sql;
624 }
625
626 // --------------------------------------------------------------------
627
Derek Jones20aa2bd2010-03-02 17:28:07 -0600628
629 /**
630 * Update_Batch statement
631 *
632 * Generates a platform-specific batch update string from the supplied data
633 *
634 * @access public
635 * @param string the table name
636 * @param array the update data
637 * @param array the where clause
638 * @return string
639 */
640 function _update_batch($table, $values, $index, $where = NULL)
641 {
642 $ids = array();
643 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
644
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500645 foreach ($values as $key => $val)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600646 {
647 $ids[] = $val[$index];
Barry Mienydd671972010-10-04 16:33:58 +0200648
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500649 foreach (array_keys($val) as $field)
Barry Mienydd671972010-10-04 16:33:58 +0200650 {
Derek Jones20aa2bd2010-03-02 17:28:07 -0600651 if ($field != $index)
652 {
Derek Jones4b9c6292011-07-01 17:40:48 -0500653 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Derek Jones20aa2bd2010-03-02 17:28:07 -0600654 }
655 }
656 }
Barry Mienydd671972010-10-04 16:33:58 +0200657
Derek Jones20aa2bd2010-03-02 17:28:07 -0600658 $sql = "UPDATE ".$table." SET ";
659 $cases = '';
660
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500661 foreach ($final as $k => $v)
Derek Jones20aa2bd2010-03-02 17:28:07 -0600662 {
663 $cases .= $k.' = CASE '."\n";
664 foreach ($v as $row)
665 {
666 $cases .= $row."\n";
667 }
Barry Mienydd671972010-10-04 16:33:58 +0200668
Derek Jones20aa2bd2010-03-02 17:28:07 -0600669 $cases .= 'ELSE '.$k.' END, ';
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Jones20aa2bd2010-03-02 17:28:07 -0600672 $sql .= substr($cases, 0, -2);
Barry Mienydd671972010-10-04 16:33:58 +0200673
Derek Jones20aa2bd2010-03-02 17:28:07 -0600674 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Jones20aa2bd2010-03-02 17:28:07 -0600676 return $sql;
677 }
678
679 // --------------------------------------------------------------------
680
681
Derek Allard2067d1a2008-11-13 22:59:24 +0000682 /**
683 * Truncate statement
684 *
685 * Generates a platform-specific truncate string from the supplied data
686 * If the database does not support the truncate() command
687 * This function maps to "DELETE FROM table"
688 *
689 * @access public
690 * @param string the table name
691 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200692 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 function _truncate($table)
694 {
695 return "TRUNCATE ".$table;
696 }
Barry Mienydd671972010-10-04 16:33:58 +0200697
Derek Allard2067d1a2008-11-13 22:59:24 +0000698 // --------------------------------------------------------------------
699
700 /**
701 * Delete statement
702 *
703 * Generates a platform-specific delete string from the supplied data
704 *
705 * @access public
706 * @param string the table name
707 * @param array the where clause
708 * @param string the limit clause
709 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200710 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
712 {
713 $conditions = '';
714
715 if (count($where) > 0 OR count($like) > 0)
716 {
717 $conditions = "\nWHERE ";
718 $conditions .= implode("\n", $this->ar_where);
719
720 if (count($where) > 0 && count($like) > 0)
721 {
722 $conditions .= " AND ";
723 }
724 $conditions .= implode("\n", $like);
725 }
726
727 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200728
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 return "DELETE FROM ".$table.$conditions.$limit;
730 }
731
732 // --------------------------------------------------------------------
733
734 /**
735 * Limit string
736 *
737 * Generates a platform-specific LIMIT clause
738 *
739 * @access public
740 * @param string the sql query string
741 * @param integer the number of rows to limit the query to
742 * @param integer the offset value
743 * @return string
744 */
745 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200746 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000747 if ($offset == 0)
748 {
749 $offset = '';
750 }
751 else
752 {
753 $offset .= ", ";
754 }
Barry Mienydd671972010-10-04 16:33:58 +0200755
Derek Allard2067d1a2008-11-13 22:59:24 +0000756 return $sql."LIMIT ".$offset.$limit;
757 }
758
759 // --------------------------------------------------------------------
760
761 /**
762 * Close DB Connection
763 *
764 * @access public
765 * @param resource
766 * @return void
767 */
768 function _close($conn_id)
769 {
770 @mysql_close($conn_id);
771 }
Barry Mienydd671972010-10-04 16:33:58 +0200772
Derek Allard2067d1a2008-11-13 22:59:24 +0000773}
774
775
776/* End of file mysql_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000777/* Location: ./system/database/drivers/mysql/mysql_driver.php */