blob: 4af08c8a952bb990787d37940f35f0fab3abd4fa [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * MySQLi Database Adapter Class - MySQLi only works with PHP 5
32 *
33 * Note: _DB is an extender class that the app controller
34 * creates dynamically based on whether the active record
35 * class is being used or not.
36 *
37 * @package CodeIgniter
38 * @subpackage Drivers
39 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050040 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000041 * @link http://codeigniter.com/user_guide/database/
42 */
43class CI_DB_mysqli_driver extends CI_DB {
44
45 var $dbdriver = 'mysqli';
Barry Mienydd671972010-10-04 16:33:58 +020046
Derek Allard2067d1a2008-11-13 22:59:24 +000047 // The character used for escaping
48 var $_escape_char = '`';
49
Derek Jonese4ed5832009-02-20 21:44:59 +000050 // clause and character used for LIKE escape sequences - not used in MySQL
51 var $_like_escape_str = '';
52 var $_like_escape_chr = '';
53
Derek Allard2067d1a2008-11-13 22:59:24 +000054 /**
55 * The syntax to count rows is slightly different across different
56 * database engines, so this string appears in each driver and is
57 * used for the count_all() and count_all_results() functions.
58 */
59 var $_count_string = "SELECT COUNT(*) AS ";
60 var $_random_keyword = ' RAND()'; // database specific random keyword
61
62 /**
63 * Whether to use the MySQL "delete hack" which allows the number
64 * of affected rows to be shown. Uses a preg_replace when enabled,
65 * adding a bit more processing to all queries.
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Derek Allard2067d1a2008-11-13 22:59:24 +000067 var $delete_hack = TRUE;
68
Derek Jones3b9f88d2011-05-20 10:25:13 -050069 // whether SET NAMES must be used to set the character set
70 var $use_set_names;
RH Beckercfdb2322011-10-03 17:28:32 -070071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 // --------------------------------------------------------------------
73
74 /**
75 * Non-persistent database connection
76 *
77 * @access private called by the base class
78 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +020079 */
Derek Allard2067d1a2008-11-13 22:59:24 +000080 function db_connect()
81 {
82 if ($this->port != '')
83 {
Barry Mienydd671972010-10-04 16:33:58 +020084 return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
86 else
87 {
88 return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
89 }
90
91 }
92
93 // --------------------------------------------------------------------
94
95 /**
96 * Persistent database connection
97 *
98 * @access private called by the base class
99 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200100 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 function db_pconnect()
102 {
103 return $this->db_connect();
104 }
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
Derek Jones87cbafc2009-02-27 16:29:59 +0000109 * Reconnect
110 *
111 * Keep / reestablish the db connection if no queries have been
112 * sent for a length of time exceeding the server's idle timeout
113 *
114 * @access public
115 * @return void
116 */
117 function reconnect()
118 {
119 if (mysqli_ping($this->conn_id) === FALSE)
120 {
121 $this->conn_id = FALSE;
122 }
123 }
124
125 // --------------------------------------------------------------------
126
127 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 * Select the database
129 *
130 * @access private called by the base class
131 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200132 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 function db_select()
134 {
135 return @mysqli_select_db($this->conn_id, $this->database);
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Set client character set
142 *
143 * @access private
144 * @param string
145 * @param string
146 * @return resource
147 */
148 function _db_set_charset($charset, $collation)
149 {
RH Beckercfdb2322011-10-03 17:28:32 -0700150 return function_exists('mysqli_set_charset')
151 ? @mysqli_set_charset($this->conn_id, $charset)
152 : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154
155 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 /**
158 * Version number query string
159 *
160 * @access public
161 * @return string
162 */
163 function _version()
164 {
165 return "SELECT version() AS ver";
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Execute the query
172 *
173 * @access private called by the base class
174 * @param string an SQL query
175 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200176 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 function _execute($sql)
178 {
Barry Mienydd671972010-10-04 16:33:58 +0200179 $sql = $this->_prep_query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 $result = @mysqli_query($this->conn_id, $sql);
181 return $result;
182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
185
186 /**
187 * Prep the query
188 *
189 * If needed, each database adapter can prep the query string
190 *
191 * @access private called by execute()
192 * @param string an SQL query
193 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200194 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 function _prep_query($sql)
196 {
197 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
198 // the query so that it returns the number of affected rows
199 if ($this->delete_hack === TRUE)
200 {
201 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
202 {
203 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
204 }
205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 return $sql;
208 }
209
210 // --------------------------------------------------------------------
211
212 /**
213 * Begin Transaction
214 *
215 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200216 * @return bool
217 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 function trans_begin($test_mode = FALSE)
219 {
220 if ( ! $this->trans_enabled)
221 {
222 return TRUE;
223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // When transactions are nested we only begin/commit/rollback the outermost ones
226 if ($this->_trans_depth > 0)
227 {
228 return TRUE;
229 }
230
231 // Reset the transaction failure flag.
232 // If the $test_mode flag is set to TRUE transactions will be rolled back
233 // even if the queries produce a successful result.
234 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
235
236 $this->simple_query('SET AUTOCOMMIT=0');
237 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
238 return TRUE;
239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Commit Transaction
245 *
246 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200247 * @return bool
248 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 function trans_commit()
250 {
251 if ( ! $this->trans_enabled)
252 {
253 return TRUE;
254 }
255
256 // When transactions are nested we only begin/commit/rollback the outermost ones
257 if ($this->_trans_depth > 0)
258 {
259 return TRUE;
260 }
261
262 $this->simple_query('COMMIT');
263 $this->simple_query('SET AUTOCOMMIT=1');
264 return TRUE;
265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Rollback Transaction
271 *
272 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200273 * @return bool
274 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 function trans_rollback()
276 {
277 if ( ! $this->trans_enabled)
278 {
279 return TRUE;
280 }
281
282 // When transactions are nested we only begin/commit/rollback the outermost ones
283 if ($this->_trans_depth > 0)
284 {
285 return TRUE;
286 }
287
288 $this->simple_query('ROLLBACK');
289 $this->simple_query('SET AUTOCOMMIT=1');
290 return TRUE;
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Escape String
297 *
298 * @access public
299 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000300 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 * @return string
302 */
Barry Mienydd671972010-10-04 16:33:58 +0200303 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000305 if (is_array($str))
306 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500307 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200308 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000309 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200310 }
311
312 return $str;
313 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
316 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000317 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 }
319 elseif (function_exists('mysql_escape_string'))
320 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000321 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323 else
324 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000325 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Jonese4ed5832009-02-20 21:44:59 +0000328 // escape LIKE condition wildcards
329 if ($like === TRUE)
330 {
331 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Jonese4ed5832009-02-20 21:44:59 +0000334 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
338
339 /**
340 * Affected Rows
341 *
342 * @access public
343 * @return integer
344 */
345 function affected_rows()
346 {
347 return @mysqli_affected_rows($this->conn_id);
348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
351
352 /**
353 * Insert ID
354 *
355 * @access public
356 * @return integer
357 */
358 function insert_id()
359 {
360 return @mysqli_insert_id($this->conn_id);
361 }
362
363 // --------------------------------------------------------------------
364
365 /**
366 * "Count All" query
367 *
368 * Generates a platform-specific query string that counts all records in
369 * the specified database
370 *
371 * @access public
372 * @param string
373 * @return string
374 */
375 function count_all($table = '')
376 {
377 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000378 {
379 return 0;
380 }
381
382 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000385 {
386 return 0;
387 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000388
389 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500390 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000391 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * List table query
398 *
399 * Generates a platform-specific query string so that the table names can be fetched
400 *
401 * @access private
402 * @param boolean
403 * @return string
404 */
405 function _list_tables($prefix_limit = FALSE)
406 {
Barry Mienydd671972010-10-04 16:33:58 +0200407 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
410 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000411 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 return $sql;
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Show column query
421 *
422 * Generates a platform-specific query string so that the column names can be fetched
423 *
424 * @access public
425 * @param string the table name
426 * @return string
427 */
428 function _list_columns($table = '')
429 {
Greg Aker1edde302010-01-26 00:17:01 +0000430 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 }
432
433 // --------------------------------------------------------------------
434
435 /**
436 * Field data query
437 *
438 * Generates a platform-specific query so that the column data can be retrieved
439 *
440 * @access public
441 * @param string the table name
442 * @return object
443 */
444 function _field_data($table)
445 {
danmontgomeryfc756452011-08-21 15:31:22 -0400446 return "DESCRIBE ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 }
448
449 // --------------------------------------------------------------------
450
451 /**
452 * The error message string
453 *
454 * @access private
455 * @return string
456 */
457 function _error_message()
458 {
459 return mysqli_error($this->conn_id);
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
463
464 /**
465 * The error message number
466 *
467 * @access private
468 * @return integer
469 */
470 function _error_number()
471 {
472 return mysqli_errno($this->conn_id);
473 }
474
475 // --------------------------------------------------------------------
476
477 /**
478 * Escape the SQL Identifiers
479 *
480 * This function escapes column and table names
481 *
482 * @access private
483 * @param string
484 * @return string
485 */
486 function _escape_identifiers($item)
487 {
488 if ($this->_escape_char == '')
489 {
490 return $item;
491 }
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 foreach ($this->_reserved_identifiers as $id)
494 {
495 if (strpos($item, '.'.$id) !== FALSE)
496 {
Barry Mienydd671972010-10-04 16:33:58 +0200497 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 // remove duplicates if the user already included the escape
500 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200501 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 if (strpos($item, '.') !== FALSE)
505 {
Barry Mienydd671972010-10-04 16:33:58 +0200506 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 }
508 else
509 {
510 $str = $this->_escape_char.$item.$this->_escape_char;
511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 // remove duplicates if the user already included the escape
514 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 // --------------------------------------------------------------------
518
519 /**
520 * From Tables
521 *
522 * This function implicitly groups FROM tables so there is no confusion
523 * about operator precedence in harmony with SQL standards
524 *
525 * @access public
526 * @param type
527 * @return type
528 */
529 function _from_tables($tables)
530 {
531 if ( ! is_array($tables))
532 {
533 $tables = array($tables);
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 return '('.implode(', ', $tables).')';
537 }
538
539 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 /**
542 * Insert statement
543 *
544 * Generates a platform-specific insert string from the supplied data
545 *
546 * @access public
547 * @param string the table name
548 * @param array the insert keys
549 * @param array the insert values
550 * @return string
551 */
552 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200553 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
555 }
Barry Mienydd671972010-10-04 16:33:58 +0200556
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 // --------------------------------------------------------------------
558
559 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500560 * Insert_batch statement
561 *
562 * Generates a platform-specific insert string from the supplied data
563 *
564 * @access public
565 * @param string the table name
566 * @param array the insert keys
567 * @param array the insert values
568 * @return string
569 */
570 function _insert_batch($table, $keys, $values)
571 {
572 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
573 }
RH Beckercfdb2322011-10-03 17:28:32 -0700574
Pascal Kriete43ded232011-01-07 15:05:40 -0500575 // --------------------------------------------------------------------
576
577 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 * Update statement
579 *
580 * Generates a platform-specific update string from the supplied data
581 *
582 * @access public
583 * @param string the table name
584 * @param array the update data
585 * @param array the where clause
586 * @param array the orderby clause
587 * @param array the limit clause
588 * @return string
589 */
590 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
591 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500592 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 {
594 $valstr[] = $key." = ".$val;
595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200606
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 return $sql;
608 }
609
Pascal Kriete43ded232011-01-07 15:05:40 -0500610 // --------------------------------------------------------------------
611
612 /**
613 * Update_Batch statement
614 *
615 * Generates a platform-specific batch update string from the supplied data
616 *
617 * @access public
618 * @param string the table name
619 * @param array the update data
620 * @param array the where clause
621 * @return string
622 */
623 function _update_batch($table, $values, $index, $where = NULL)
624 {
625 $ids = array();
626 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
627
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500628 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500629 {
630 $ids[] = $val[$index];
631
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500632 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500633 {
634 if ($field != $index)
635 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500636 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500637 }
638 }
639 }
640
641 $sql = "UPDATE ".$table." SET ";
642 $cases = '';
643
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500644 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500645 {
646 $cases .= $k.' = CASE '."\n";
647 foreach ($v as $row)
648 {
649 $cases .= $row."\n";
650 }
651
652 $cases .= 'ELSE '.$k.' END, ';
653 }
654
655 $sql .= substr($cases, 0, -2);
656
657 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
658
659 return $sql;
660 }
Barry Mienydd671972010-10-04 16:33:58 +0200661
Derek Allard2067d1a2008-11-13 22:59:24 +0000662 // --------------------------------------------------------------------
663
664 /**
665 * Truncate statement
666 *
667 * Generates a platform-specific truncate string from the supplied data
668 * If the database does not support the truncate() command
669 * This function maps to "DELETE FROM table"
670 *
671 * @access public
672 * @param string the table name
673 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200674 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000675 function _truncate($table)
676 {
677 return "TRUNCATE ".$table;
678 }
Barry Mienydd671972010-10-04 16:33:58 +0200679
Derek Allard2067d1a2008-11-13 22:59:24 +0000680 // --------------------------------------------------------------------
681
682 /**
683 * Delete statement
684 *
685 * Generates a platform-specific delete string from the supplied data
686 *
687 * @access public
688 * @param string the table name
689 * @param array the where clause
690 * @param string the limit clause
691 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200692 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
694 {
695 $conditions = '';
696
697 if (count($where) > 0 OR count($like) > 0)
698 {
699 $conditions = "\nWHERE ";
700 $conditions .= implode("\n", $this->ar_where);
701
702 if (count($where) > 0 && count($like) > 0)
703 {
704 $conditions .= " AND ";
705 }
706 $conditions .= implode("\n", $like);
707 }
708
709 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 return "DELETE FROM ".$table.$conditions.$limit;
712 }
713
714 // --------------------------------------------------------------------
715
716 /**
717 * Limit string
718 *
719 * Generates a platform-specific LIMIT clause
720 *
721 * @access public
722 * @param string the sql query string
723 * @param integer the number of rows to limit the query to
724 * @param integer the offset value
725 * @return string
726 */
727 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200728 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 if ($offset > 0)
732 {
733 $sql .= " OFFSET ".$offset;
734 }
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 return $sql;
737 }
738
739 // --------------------------------------------------------------------
740
741 /**
742 * Close DB Connection
743 *
744 * @access public
745 * @param resource
746 * @return void
747 */
748 function _close($conn_id)
749 {
750 @mysqli_close($conn_id);
751 }
752
753
754}
755
756
757/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000758/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */