blob: ccd110f793c9aaeafdbb2eec6bbd52db22e337ee [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 * MySQLi Database Adapter Class - MySQLi only works with PHP 5
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_mysqli_driver extends CI_DB {
32
33 var $dbdriver = 'mysqli';
Barry Mienydd671972010-10-04 16:33:58 +020034
Derek Allard2067d1a2008-11-13 22:59:24 +000035 // The character used for escaping
36 var $_escape_char = '`';
37
Derek Jonese4ed5832009-02-20 21:44:59 +000038 // 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 * The syntax to count rows is slightly different across different
44 * database engines, so this string appears in each driver and is
45 * used for the count_all() and count_all_results() functions.
46 */
47 var $_count_string = "SELECT COUNT(*) AS ";
48 var $_random_keyword = ' RAND()'; // database specific random keyword
49
50 /**
51 * Whether to use the MySQL "delete hack" which allows the number
52 * of affected rows to be shown. Uses a preg_replace when enabled,
53 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020054 */
Derek Allard2067d1a2008-11-13 22:59:24 +000055 var $delete_hack = TRUE;
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;
59
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // --------------------------------------------------------------------
61
62 /**
63 * Non-persistent database connection
64 *
65 * @access private called by the base class
66 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020067 */
Derek Allard2067d1a2008-11-13 22:59:24 +000068 function db_connect()
69 {
70 if ($this->port != '')
71 {
Barry Mienydd671972010-10-04 16:33:58 +020072 return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
Derek Allard2067d1a2008-11-13 22:59:24 +000073 }
74 else
75 {
76 return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
77 }
78
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Persistent database connection
85 *
86 * @access private called by the base class
87 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020088 */
Derek Allard2067d1a2008-11-13 22:59:24 +000089 function db_pconnect()
90 {
91 return $this->db_connect();
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 (mysqli_ping($this->conn_id) === FALSE)
108 {
109 $this->conn_id = FALSE;
110 }
111 }
112
113 // --------------------------------------------------------------------
114
115 /**
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 @mysqli_select_db($this->conn_id, $this->database);
124 }
125
126 // --------------------------------------------------------------------
127
128 /**
129 * Set client character set
130 *
131 * @access private
132 * @param string
133 * @param string
134 * @return resource
135 */
136 function _db_set_charset($charset, $collation)
137 {
Derek Jones3b9f88d2011-05-20 10:25:13 -0500138 if ( ! isset($this->use_set_names))
Derek Jones6ae70cc2011-04-19 16:13:48 -0500139 {
140 // mysqli_set_charset() requires MySQL >= 5.0.7, use SET NAMES as fallback
Derek Jones3b9f88d2011-05-20 10:25:13 -0500141 $this->use_set_names = (version_compare(mysqli_get_server_info($this->conn_id), '5.0.7', '>=')) ? FALSE : TRUE;
Derek Jones6ae70cc2011-04-19 16:13:48 -0500142 }
143
Derek Jones3b9f88d2011-05-20 10:25:13 -0500144 if ($this->use_set_names === TRUE)
Derek Jones6ae70cc2011-04-19 16:13:48 -0500145 {
146 return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
147 }
148 else
149 {
150 return @mysqli_set_charset($this->conn_id, $charset);
151 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
153
154 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 /**
157 * Version number query string
158 *
159 * @access public
160 * @return string
161 */
162 function _version()
163 {
164 return "SELECT version() AS ver";
165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Execute the query
171 *
172 * @access private called by the base class
173 * @param string an SQL query
174 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200175 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 function _execute($sql)
177 {
Barry Mienydd671972010-10-04 16:33:58 +0200178 $sql = $this->_prep_query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 $result = @mysqli_query($this->conn_id, $sql);
180 return $result;
181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
184
185 /**
186 * Prep the query
187 *
188 * If needed, each database adapter can prep the query string
189 *
190 * @access private called by execute()
191 * @param string an SQL query
192 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200193 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 function _prep_query($sql)
195 {
196 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
197 // the query so that it returns the number of affected rows
198 if ($this->delete_hack === TRUE)
199 {
200 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
201 {
202 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
203 }
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 return $sql;
207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Begin Transaction
213 *
214 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200215 * @return bool
216 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 function trans_begin($test_mode = FALSE)
218 {
219 if ( ! $this->trans_enabled)
220 {
221 return TRUE;
222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // When transactions are nested we only begin/commit/rollback the outermost ones
225 if ($this->_trans_depth > 0)
226 {
227 return TRUE;
228 }
229
230 // Reset the transaction failure flag.
231 // If the $test_mode flag is set to TRUE transactions will be rolled back
232 // even if the queries produce a successful result.
233 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
234
235 $this->simple_query('SET AUTOCOMMIT=0');
236 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
237 return TRUE;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Commit Transaction
244 *
245 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200246 * @return bool
247 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 function trans_commit()
249 {
250 if ( ! $this->trans_enabled)
251 {
252 return TRUE;
253 }
254
255 // When transactions are nested we only begin/commit/rollback the outermost ones
256 if ($this->_trans_depth > 0)
257 {
258 return TRUE;
259 }
260
261 $this->simple_query('COMMIT');
262 $this->simple_query('SET AUTOCOMMIT=1');
263 return TRUE;
264 }
265
266 // --------------------------------------------------------------------
267
268 /**
269 * Rollback Transaction
270 *
271 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200272 * @return bool
273 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 function trans_rollback()
275 {
276 if ( ! $this->trans_enabled)
277 {
278 return TRUE;
279 }
280
281 // When transactions are nested we only begin/commit/rollback the outermost ones
282 if ($this->_trans_depth > 0)
283 {
284 return TRUE;
285 }
286
287 $this->simple_query('ROLLBACK');
288 $this->simple_query('SET AUTOCOMMIT=1');
289 return TRUE;
290 }
291
292 // --------------------------------------------------------------------
293
294 /**
295 * Escape String
296 *
297 * @access public
298 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000299 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 * @return string
301 */
Barry Mienydd671972010-10-04 16:33:58 +0200302 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000304 if (is_array($str))
305 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500306 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200307 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000308 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200309 }
310
311 return $str;
312 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
315 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000316 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
318 elseif (function_exists('mysql_escape_string'))
319 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000320 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 }
322 else
323 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000324 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Jonese4ed5832009-02-20 21:44:59 +0000327 // escape LIKE condition wildcards
328 if ($like === TRUE)
329 {
330 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Jonese4ed5832009-02-20 21:44:59 +0000333 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 // --------------------------------------------------------------------
337
338 /**
339 * Affected Rows
340 *
341 * @access public
342 * @return integer
343 */
344 function affected_rows()
345 {
346 return @mysqli_affected_rows($this->conn_id);
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
350
351 /**
352 * Insert ID
353 *
354 * @access public
355 * @return integer
356 */
357 function insert_id()
358 {
359 return @mysqli_insert_id($this->conn_id);
360 }
361
362 // --------------------------------------------------------------------
363
364 /**
365 * "Count All" query
366 *
367 * Generates a platform-specific query string that counts all records in
368 * the specified database
369 *
370 * @access public
371 * @param string
372 * @return string
373 */
374 function count_all($table = '')
375 {
376 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000377 {
378 return 0;
379 }
380
381 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
382
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000384 {
385 return 0;
386 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000387
388 $row = $query->row();
Greg Aker448148b2011-08-20 14:23:14 -0500389 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000390 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
392
393 // --------------------------------------------------------------------
394
395 /**
396 * List table query
397 *
398 * Generates a platform-specific query string so that the table names can be fetched
399 *
400 * @access private
401 * @param boolean
402 * @return string
403 */
404 function _list_tables($prefix_limit = FALSE)
405 {
Barry Mienydd671972010-10-04 16:33:58 +0200406 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
409 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000410 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 return $sql;
414 }
415
416 // --------------------------------------------------------------------
417
418 /**
419 * Show column query
420 *
421 * Generates a platform-specific query string so that the column names can be fetched
422 *
423 * @access public
424 * @param string the table name
425 * @return string
426 */
427 function _list_columns($table = '')
428 {
Greg Aker1edde302010-01-26 00:17:01 +0000429 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 }
431
432 // --------------------------------------------------------------------
433
434 /**
435 * Field data query
436 *
437 * Generates a platform-specific query so that the column data can be retrieved
438 *
439 * @access public
440 * @param string the table name
441 * @return object
442 */
443 function _field_data($table)
444 {
danmontgomeryad4681f2011-08-21 15:31:22 -0400445 return "DESCRIBE ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
447
448 // --------------------------------------------------------------------
449
450 /**
451 * The error message string
452 *
453 * @access private
454 * @return string
455 */
456 function _error_message()
457 {
458 return mysqli_error($this->conn_id);
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
462
463 /**
464 * The error message number
465 *
466 * @access private
467 * @return integer
468 */
469 function _error_number()
470 {
471 return mysqli_errno($this->conn_id);
472 }
473
474 // --------------------------------------------------------------------
475
476 /**
477 * Escape the SQL Identifiers
478 *
479 * This function escapes column and table names
480 *
481 * @access private
482 * @param string
483 * @return string
484 */
485 function _escape_identifiers($item)
486 {
487 if ($this->_escape_char == '')
488 {
489 return $item;
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 foreach ($this->_reserved_identifiers as $id)
493 {
494 if (strpos($item, '.'.$id) !== FALSE)
495 {
Barry Mienydd671972010-10-04 16:33:58 +0200496 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // remove duplicates if the user already included the escape
499 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200500 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 if (strpos($item, '.') !== FALSE)
504 {
Barry Mienydd671972010-10-04 16:33:58 +0200505 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507 else
508 {
509 $str = $this->_escape_char.$item.$this->_escape_char;
510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // remove duplicates if the user already included the escape
513 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
517
518 /**
519 * From Tables
520 *
521 * This function implicitly groups FROM tables so there is no confusion
522 * about operator precedence in harmony with SQL standards
523 *
524 * @access public
525 * @param type
526 * @return type
527 */
528 function _from_tables($tables)
529 {
530 if ( ! is_array($tables))
531 {
532 $tables = array($tables);
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 return '('.implode(', ', $tables).')';
536 }
537
538 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 /**
541 * Insert statement
542 *
543 * Generates a platform-specific insert string from the supplied data
544 *
545 * @access public
546 * @param string the table name
547 * @param array the insert keys
548 * @param array the insert values
549 * @return string
550 */
551 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200552 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
554 }
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 // --------------------------------------------------------------------
557
558 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500559 * Insert_batch statement
560 *
561 * Generates a platform-specific insert string from the supplied data
562 *
563 * @access public
564 * @param string the table name
565 * @param array the insert keys
566 * @param array the insert values
567 * @return string
568 */
569 function _insert_batch($table, $keys, $values)
570 {
571 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
572 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500573
Pascal Kriete43ded232011-01-07 15:05:40 -0500574 // --------------------------------------------------------------------
575
576 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 * Update statement
578 *
579 * Generates a platform-specific update string from the supplied data
580 *
581 * @access public
582 * @param string the table name
583 * @param array the update data
584 * @param array the where clause
585 * @param array the orderby clause
586 * @param array the limit clause
587 * @return string
588 */
589 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
590 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500591 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 {
593 $valstr[] = $key." = ".$val;
594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200597
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200599
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
Barry Mienydd671972010-10-04 16:33:58 +0200601
Derek Allard2067d1a2008-11-13 22:59:24 +0000602 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200603
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 return $sql;
607 }
608
Pascal Kriete43ded232011-01-07 15:05:40 -0500609 // --------------------------------------------------------------------
610
611 /**
612 * Update_Batch statement
613 *
614 * Generates a platform-specific batch update string from the supplied data
615 *
616 * @access public
617 * @param string the table name
618 * @param array the update data
619 * @param array the where clause
620 * @return string
621 */
622 function _update_batch($table, $values, $index, $where = NULL)
623 {
624 $ids = array();
625 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
626
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500627 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500628 {
629 $ids[] = $val[$index];
630
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500631 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500632 {
633 if ($field != $index)
634 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500635 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500636 }
637 }
638 }
639
640 $sql = "UPDATE ".$table." SET ";
641 $cases = '';
642
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500643 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500644 {
645 $cases .= $k.' = CASE '."\n";
646 foreach ($v as $row)
647 {
648 $cases .= $row."\n";
649 }
650
651 $cases .= 'ELSE '.$k.' END, ';
652 }
653
654 $sql .= substr($cases, 0, -2);
655
656 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
657
658 return $sql;
659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 // --------------------------------------------------------------------
662
663 /**
664 * Truncate statement
665 *
666 * Generates a platform-specific truncate string from the supplied data
667 * If the database does not support the truncate() command
668 * This function maps to "DELETE FROM table"
669 *
670 * @access public
671 * @param string the table name
672 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200673 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000674 function _truncate($table)
675 {
676 return "TRUNCATE ".$table;
677 }
Barry Mienydd671972010-10-04 16:33:58 +0200678
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 // --------------------------------------------------------------------
680
681 /**
682 * Delete statement
683 *
684 * Generates a platform-specific delete string from the supplied data
685 *
686 * @access public
687 * @param string the table name
688 * @param array the where clause
689 * @param string the limit clause
690 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200691 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000692 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
693 {
694 $conditions = '';
695
696 if (count($where) > 0 OR count($like) > 0)
697 {
698 $conditions = "\nWHERE ";
699 $conditions .= implode("\n", $this->ar_where);
700
701 if (count($where) > 0 && count($like) > 0)
702 {
703 $conditions .= " AND ";
704 }
705 $conditions .= implode("\n", $like);
706 }
707
708 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 return "DELETE FROM ".$table.$conditions.$limit;
711 }
712
713 // --------------------------------------------------------------------
714
715 /**
716 * Limit string
717 *
718 * Generates a platform-specific LIMIT clause
719 *
720 * @access public
721 * @param string the sql query string
722 * @param integer the number of rows to limit the query to
723 * @param integer the offset value
724 * @return string
725 */
726 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200727 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000728 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200729
Derek Allard2067d1a2008-11-13 22:59:24 +0000730 if ($offset > 0)
731 {
732 $sql .= " OFFSET ".$offset;
733 }
Barry Mienydd671972010-10-04 16:33:58 +0200734
Derek Allard2067d1a2008-11-13 22:59:24 +0000735 return $sql;
736 }
737
738 // --------------------------------------------------------------------
739
740 /**
741 * Close DB Connection
742 *
743 * @access public
744 * @param resource
745 * @return void
746 */
747 function _close($conn_id)
748 {
749 @mysqli_close($conn_id);
750 }
751
752
753}
754
755
756/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000757/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */