blob: b1796c9df3687412cbbe40a859eccc342f1a198e [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();
Derek Allarde37ab382009-02-03 16:13:57 +0000389 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
391
392 // --------------------------------------------------------------------
393
394 /**
395 * List table query
396 *
397 * Generates a platform-specific query string so that the table names can be fetched
398 *
399 * @access private
400 * @param boolean
401 * @return string
402 */
403 function _list_tables($prefix_limit = FALSE)
404 {
Barry Mienydd671972010-10-04 16:33:58 +0200405 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
408 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000409 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 return $sql;
413 }
414
415 // --------------------------------------------------------------------
416
417 /**
418 * Show column query
419 *
420 * Generates a platform-specific query string so that the column names can be fetched
421 *
422 * @access public
423 * @param string the table name
424 * @return string
425 */
426 function _list_columns($table = '')
427 {
Greg Aker1edde302010-01-26 00:17:01 +0000428 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
430
431 // --------------------------------------------------------------------
432
433 /**
434 * Field data query
435 *
436 * Generates a platform-specific query so that the column data can be retrieved
437 *
438 * @access public
439 * @param string the table name
440 * @return object
441 */
442 function _field_data($table)
443 {
444 return "SELECT * FROM ".$table." LIMIT 1";
445 }
446
447 // --------------------------------------------------------------------
448
449 /**
450 * The error message string
451 *
452 * @access private
453 * @return string
454 */
455 function _error_message()
456 {
457 return mysqli_error($this->conn_id);
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 // --------------------------------------------------------------------
461
462 /**
463 * The error message number
464 *
465 * @access private
466 * @return integer
467 */
468 function _error_number()
469 {
470 return mysqli_errno($this->conn_id);
471 }
472
473 // --------------------------------------------------------------------
474
475 /**
476 * Escape the SQL Identifiers
477 *
478 * This function escapes column and table names
479 *
480 * @access private
481 * @param string
482 * @return string
483 */
484 function _escape_identifiers($item)
485 {
486 if ($this->_escape_char == '')
487 {
488 return $item;
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 foreach ($this->_reserved_identifiers as $id)
492 {
493 if (strpos($item, '.'.$id) !== FALSE)
494 {
Barry Mienydd671972010-10-04 16:33:58 +0200495 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
496
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 // remove duplicates if the user already included the escape
498 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200499 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Barry Mienydd671972010-10-04 16:33:58 +0200501
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 if (strpos($item, '.') !== FALSE)
503 {
Barry Mienydd671972010-10-04 16:33:58 +0200504 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 }
506 else
507 {
508 $str = $this->_escape_char.$item.$this->_escape_char;
509 }
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 // remove duplicates if the user already included the escape
512 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
513 }
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 // --------------------------------------------------------------------
516
517 /**
518 * From Tables
519 *
520 * This function implicitly groups FROM tables so there is no confusion
521 * about operator precedence in harmony with SQL standards
522 *
523 * @access public
524 * @param type
525 * @return type
526 */
527 function _from_tables($tables)
528 {
529 if ( ! is_array($tables))
530 {
531 $tables = array($tables);
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 return '('.implode(', ', $tables).')';
535 }
536
537 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 /**
540 * Insert statement
541 *
542 * Generates a platform-specific insert string from the supplied data
543 *
544 * @access public
545 * @param string the table name
546 * @param array the insert keys
547 * @param array the insert values
548 * @return string
549 */
550 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200551 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
553 }
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 // --------------------------------------------------------------------
556
557 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500558 * Insert_batch statement
559 *
560 * Generates a platform-specific insert string from the supplied data
561 *
562 * @access public
563 * @param string the table name
564 * @param array the insert keys
565 * @param array the insert values
566 * @return string
567 */
568 function _insert_batch($table, $keys, $values)
569 {
570 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
571 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500572
Pascal Kriete43ded232011-01-07 15:05:40 -0500573 // --------------------------------------------------------------------
574
575 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 * Update statement
577 *
578 * Generates a platform-specific update string from the supplied data
579 *
580 * @access public
581 * @param string the table name
582 * @param array the update data
583 * @param array the where clause
584 * @param array the orderby clause
585 * @param array the limit clause
586 * @return string
587 */
588 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
589 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500590 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 {
592 $valstr[] = $key." = ".$val;
593 }
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200598
Derek Allard2067d1a2008-11-13 22:59:24 +0000599 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
Barry Mienydd671972010-10-04 16:33:58 +0200600
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200602
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200604
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 return $sql;
606 }
607
Pascal Kriete43ded232011-01-07 15:05:40 -0500608 // --------------------------------------------------------------------
609
610 /**
611 * Update_Batch statement
612 *
613 * Generates a platform-specific batch update string from the supplied data
614 *
615 * @access public
616 * @param string the table name
617 * @param array the update data
618 * @param array the where clause
619 * @return string
620 */
621 function _update_batch($table, $values, $index, $where = NULL)
622 {
623 $ids = array();
624 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
625
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500626 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500627 {
628 $ids[] = $val[$index];
629
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500630 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500631 {
632 if ($field != $index)
633 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500634 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500635 }
636 }
637 }
638
639 $sql = "UPDATE ".$table." SET ";
640 $cases = '';
641
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500642 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500643 {
644 $cases .= $k.' = CASE '."\n";
645 foreach ($v as $row)
646 {
647 $cases .= $row."\n";
648 }
649
650 $cases .= 'ELSE '.$k.' END, ';
651 }
652
653 $sql .= substr($cases, 0, -2);
654
655 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
656
657 return $sql;
658 }
Barry Mienydd671972010-10-04 16:33:58 +0200659
Derek Allard2067d1a2008-11-13 22:59:24 +0000660 // --------------------------------------------------------------------
661
662 /**
663 * Truncate statement
664 *
665 * Generates a platform-specific truncate string from the supplied data
666 * If the database does not support the truncate() command
667 * This function maps to "DELETE FROM table"
668 *
669 * @access public
670 * @param string the table name
671 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200672 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 function _truncate($table)
674 {
675 return "TRUNCATE ".$table;
676 }
Barry Mienydd671972010-10-04 16:33:58 +0200677
Derek Allard2067d1a2008-11-13 22:59:24 +0000678 // --------------------------------------------------------------------
679
680 /**
681 * Delete statement
682 *
683 * Generates a platform-specific delete string from the supplied data
684 *
685 * @access public
686 * @param string the table name
687 * @param array the where clause
688 * @param string the limit clause
689 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200690 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000691 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
692 {
693 $conditions = '';
694
695 if (count($where) > 0 OR count($like) > 0)
696 {
697 $conditions = "\nWHERE ";
698 $conditions .= implode("\n", $this->ar_where);
699
700 if (count($where) > 0 && count($like) > 0)
701 {
702 $conditions .= " AND ";
703 }
704 $conditions .= implode("\n", $like);
705 }
706
707 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200708
Derek Allard2067d1a2008-11-13 22:59:24 +0000709 return "DELETE FROM ".$table.$conditions.$limit;
710 }
711
712 // --------------------------------------------------------------------
713
714 /**
715 * Limit string
716 *
717 * Generates a platform-specific LIMIT clause
718 *
719 * @access public
720 * @param string the sql query string
721 * @param integer the number of rows to limit the query to
722 * @param integer the offset value
723 * @return string
724 */
725 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200726 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000727 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200728
Derek Allard2067d1a2008-11-13 22:59:24 +0000729 if ($offset > 0)
730 {
731 $sql .= " OFFSET ".$offset;
732 }
Barry Mienydd671972010-10-04 16:33:58 +0200733
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 return $sql;
735 }
736
737 // --------------------------------------------------------------------
738
739 /**
740 * Close DB Connection
741 *
742 * @access public
743 * @param resource
744 * @return void
745 */
746 function _close($conn_id)
747 {
748 @mysqli_close($conn_id);
749 }
750
751
752}
753
754
755/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000756/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */