blob: abef80fbd55191d736321e8cc48f81bc7a189fd7 [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;
RH Beckercfdb2322011-10-03 17:28:32 -070059
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 {
RH Beckercfdb2322011-10-03 17:28:32 -0700138 return function_exists('mysqli_set_charset')
139 ? @mysqli_set_charset($this->conn_id, $charset)
140 : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
142
143 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 /**
146 * Version number query string
147 *
148 * @access public
149 * @return string
150 */
151 function _version()
152 {
153 return "SELECT version() AS ver";
154 }
155
156 // --------------------------------------------------------------------
157
158 /**
159 * Execute the query
160 *
161 * @access private called by the base class
162 * @param string an SQL query
163 * @return resource
Barry Mienydd671972010-10-04 16:33:58 +0200164 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 function _execute($sql)
166 {
Barry Mienydd671972010-10-04 16:33:58 +0200167 $sql = $this->_prep_query($sql);
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $result = @mysqli_query($this->conn_id, $sql);
169 return $result;
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 // --------------------------------------------------------------------
173
174 /**
175 * Prep the query
176 *
177 * If needed, each database adapter can prep the query string
178 *
179 * @access private called by execute()
180 * @param string an SQL query
181 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200182 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 function _prep_query($sql)
184 {
185 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
186 // the query so that it returns the number of affected rows
187 if ($this->delete_hack === TRUE)
188 {
189 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
190 {
191 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
192 }
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 return $sql;
196 }
197
198 // --------------------------------------------------------------------
199
200 /**
201 * Begin Transaction
202 *
203 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200204 * @return bool
205 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 function trans_begin($test_mode = FALSE)
207 {
208 if ( ! $this->trans_enabled)
209 {
210 return TRUE;
211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // When transactions are nested we only begin/commit/rollback the outermost ones
214 if ($this->_trans_depth > 0)
215 {
216 return TRUE;
217 }
218
219 // Reset the transaction failure flag.
220 // If the $test_mode flag is set to TRUE transactions will be rolled back
221 // even if the queries produce a successful result.
222 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
223
224 $this->simple_query('SET AUTOCOMMIT=0');
225 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
226 return TRUE;
227 }
228
229 // --------------------------------------------------------------------
230
231 /**
232 * Commit Transaction
233 *
234 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200235 * @return bool
236 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 function trans_commit()
238 {
239 if ( ! $this->trans_enabled)
240 {
241 return TRUE;
242 }
243
244 // When transactions are nested we only begin/commit/rollback the outermost ones
245 if ($this->_trans_depth > 0)
246 {
247 return TRUE;
248 }
249
250 $this->simple_query('COMMIT');
251 $this->simple_query('SET AUTOCOMMIT=1');
252 return TRUE;
253 }
254
255 // --------------------------------------------------------------------
256
257 /**
258 * Rollback Transaction
259 *
260 * @access public
Barry Mienydd671972010-10-04 16:33:58 +0200261 * @return bool
262 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 function trans_rollback()
264 {
265 if ( ! $this->trans_enabled)
266 {
267 return TRUE;
268 }
269
270 // When transactions are nested we only begin/commit/rollback the outermost ones
271 if ($this->_trans_depth > 0)
272 {
273 return TRUE;
274 }
275
276 $this->simple_query('ROLLBACK');
277 $this->simple_query('SET AUTOCOMMIT=1');
278 return TRUE;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Escape String
285 *
286 * @access public
287 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000288 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 * @return string
290 */
Barry Mienydd671972010-10-04 16:33:58 +0200291 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000293 if (is_array($str))
294 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500295 foreach ($str as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000297 $str[$key] = $this->escape_str($val, $like);
Barry Mienydd671972010-10-04 16:33:58 +0200298 }
299
300 return $str;
301 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
304 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000305 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 }
307 elseif (function_exists('mysql_escape_string'))
308 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000309 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 else
312 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000313 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Jonese4ed5832009-02-20 21:44:59 +0000316 // escape LIKE condition wildcards
317 if ($like === TRUE)
318 {
319 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Jonese4ed5832009-02-20 21:44:59 +0000322 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
326
327 /**
328 * Affected Rows
329 *
330 * @access public
331 * @return integer
332 */
333 function affected_rows()
334 {
335 return @mysqli_affected_rows($this->conn_id);
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // --------------------------------------------------------------------
339
340 /**
341 * Insert ID
342 *
343 * @access public
344 * @return integer
345 */
346 function insert_id()
347 {
348 return @mysqli_insert_id($this->conn_id);
349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * "Count All" query
355 *
356 * Generates a platform-specific query string that counts all records in
357 * the specified database
358 *
359 * @access public
360 * @param string
361 * @return string
362 */
363 function count_all($table = '')
364 {
365 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000366 {
367 return 0;
368 }
369
370 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000373 {
374 return 0;
375 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000376
377 $row = $query->row();
Greg Aker90248ab2011-08-20 14:23:14 -0500378 $this->_reset_select();
Derek Allarde37ab382009-02-03 16:13:57 +0000379 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 }
381
382 // --------------------------------------------------------------------
383
384 /**
385 * List table query
386 *
387 * Generates a platform-specific query string so that the table names can be fetched
388 *
389 * @access private
390 * @param boolean
391 * @return string
392 */
393 function _list_tables($prefix_limit = FALSE)
394 {
Barry Mienydd671972010-10-04 16:33:58 +0200395 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
398 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000399 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 return $sql;
403 }
404
405 // --------------------------------------------------------------------
406
407 /**
408 * Show column query
409 *
410 * Generates a platform-specific query string so that the column names can be fetched
411 *
412 * @access public
413 * @param string the table name
414 * @return string
415 */
416 function _list_columns($table = '')
417 {
Greg Aker1edde302010-01-26 00:17:01 +0000418 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 }
420
421 // --------------------------------------------------------------------
422
423 /**
424 * Field data query
425 *
426 * Generates a platform-specific query so that the column data can be retrieved
427 *
428 * @access public
429 * @param string the table name
430 * @return object
431 */
432 function _field_data($table)
433 {
danmontgomeryfc756452011-08-21 15:31:22 -0400434 return "DESCRIBE ".$table;
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
436
437 // --------------------------------------------------------------------
438
439 /**
440 * The error message string
441 *
442 * @access private
443 * @return string
444 */
445 function _error_message()
446 {
447 return mysqli_error($this->conn_id);
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // --------------------------------------------------------------------
451
452 /**
453 * The error message number
454 *
455 * @access private
456 * @return integer
457 */
458 function _error_number()
459 {
460 return mysqli_errno($this->conn_id);
461 }
462
463 // --------------------------------------------------------------------
464
465 /**
466 * Escape the SQL Identifiers
467 *
468 * This function escapes column and table names
469 *
470 * @access private
471 * @param string
472 * @return string
473 */
474 function _escape_identifiers($item)
475 {
476 if ($this->_escape_char == '')
477 {
478 return $item;
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 foreach ($this->_reserved_identifiers as $id)
482 {
483 if (strpos($item, '.'.$id) !== FALSE)
484 {
Barry Mienydd671972010-10-04 16:33:58 +0200485 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 // remove duplicates if the user already included the escape
488 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
Barry Mienydd671972010-10-04 16:33:58 +0200489 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 if (strpos($item, '.') !== FALSE)
493 {
Barry Mienydd671972010-10-04 16:33:58 +0200494 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496 else
497 {
498 $str = $this->_escape_char.$item.$this->_escape_char;
499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 // remove duplicates if the user already included the escape
502 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
503 }
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 // --------------------------------------------------------------------
506
507 /**
508 * From Tables
509 *
510 * This function implicitly groups FROM tables so there is no confusion
511 * about operator precedence in harmony with SQL standards
512 *
513 * @access public
514 * @param type
515 * @return type
516 */
517 function _from_tables($tables)
518 {
519 if ( ! is_array($tables))
520 {
521 $tables = array($tables);
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 return '('.implode(', ', $tables).')';
525 }
526
527 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 /**
530 * Insert statement
531 *
532 * Generates a platform-specific insert string from the supplied data
533 *
534 * @access public
535 * @param string the table name
536 * @param array the insert keys
537 * @param array the insert values
538 * @return string
539 */
540 function _insert($table, $keys, $values)
Barry Mienydd671972010-10-04 16:33:58 +0200541 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
543 }
Barry Mienydd671972010-10-04 16:33:58 +0200544
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 // --------------------------------------------------------------------
546
547 /**
Pascal Kriete43ded232011-01-07 15:05:40 -0500548 * Insert_batch statement
549 *
550 * Generates a platform-specific insert string from the supplied data
551 *
552 * @access public
553 * @param string the table name
554 * @param array the insert keys
555 * @param array the insert values
556 * @return string
557 */
558 function _insert_batch($table, $keys, $values)
559 {
560 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
561 }
RH Beckercfdb2322011-10-03 17:28:32 -0700562
Pascal Kriete43ded232011-01-07 15:05:40 -0500563 // --------------------------------------------------------------------
564
565 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 * Update statement
567 *
568 * Generates a platform-specific update string from the supplied data
569 *
570 * @access public
571 * @param string the table name
572 * @param array the update data
573 * @param array the where clause
574 * @param array the orderby clause
575 * @param array the limit clause
576 * @return string
577 */
578 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
579 {
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500580 foreach ($values as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000581 {
582 $valstr[] = $key." = ".$val;
583 }
Barry Mienydd671972010-10-04 16:33:58 +0200584
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200586
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Barry Mienydd671972010-10-04 16:33:58 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
Barry Mienydd671972010-10-04 16:33:58 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 $sql .= $orderby.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 return $sql;
596 }
597
Pascal Kriete43ded232011-01-07 15:05:40 -0500598 // --------------------------------------------------------------------
599
600 /**
601 * Update_Batch statement
602 *
603 * Generates a platform-specific batch update string from the supplied data
604 *
605 * @access public
606 * @param string the table name
607 * @param array the update data
608 * @param array the where clause
609 * @return string
610 */
611 function _update_batch($table, $values, $index, $where = NULL)
612 {
613 $ids = array();
614 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
615
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500616 foreach ($values as $key => $val)
Pascal Kriete43ded232011-01-07 15:05:40 -0500617 {
618 $ids[] = $val[$index];
619
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500620 foreach (array_keys($val) as $field)
Pascal Kriete43ded232011-01-07 15:05:40 -0500621 {
622 if ($field != $index)
623 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500624 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
Pascal Kriete43ded232011-01-07 15:05:40 -0500625 }
626 }
627 }
628
629 $sql = "UPDATE ".$table." SET ";
630 $cases = '';
631
Pascal Krietec3a4a8d2011-02-14 13:40:08 -0500632 foreach ($final as $k => $v)
Pascal Kriete43ded232011-01-07 15:05:40 -0500633 {
634 $cases .= $k.' = CASE '."\n";
635 foreach ($v as $row)
636 {
637 $cases .= $row."\n";
638 }
639
640 $cases .= 'ELSE '.$k.' END, ';
641 }
642
643 $sql .= substr($cases, 0, -2);
644
645 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
646
647 return $sql;
648 }
Barry Mienydd671972010-10-04 16:33:58 +0200649
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 // --------------------------------------------------------------------
651
652 /**
653 * Truncate statement
654 *
655 * Generates a platform-specific truncate string from the supplied data
656 * If the database does not support the truncate() command
657 * This function maps to "DELETE FROM table"
658 *
659 * @access public
660 * @param string the table name
661 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200662 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 function _truncate($table)
664 {
665 return "TRUNCATE ".$table;
666 }
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 // --------------------------------------------------------------------
669
670 /**
671 * Delete statement
672 *
673 * Generates a platform-specific delete string from the supplied data
674 *
675 * @access public
676 * @param string the table name
677 * @param array the where clause
678 * @param string the limit clause
679 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200680 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
682 {
683 $conditions = '';
684
685 if (count($where) > 0 OR count($like) > 0)
686 {
687 $conditions = "\nWHERE ";
688 $conditions .= implode("\n", $this->ar_where);
689
690 if (count($where) > 0 && count($like) > 0)
691 {
692 $conditions .= " AND ";
693 }
694 $conditions .= implode("\n", $like);
695 }
696
697 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 return "DELETE FROM ".$table.$conditions.$limit;
700 }
701
702 // --------------------------------------------------------------------
703
704 /**
705 * Limit string
706 *
707 * Generates a platform-specific LIMIT clause
708 *
709 * @access public
710 * @param string the sql query string
711 * @param integer the number of rows to limit the query to
712 * @param integer the offset value
713 * @return string
714 */
715 function _limit($sql, $limit, $offset)
Barry Mienydd671972010-10-04 16:33:58 +0200716 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000717 $sql .= "LIMIT ".$limit;
Barry Mienydd671972010-10-04 16:33:58 +0200718
Derek Allard2067d1a2008-11-13 22:59:24 +0000719 if ($offset > 0)
720 {
721 $sql .= " OFFSET ".$offset;
722 }
Barry Mienydd671972010-10-04 16:33:58 +0200723
Derek Allard2067d1a2008-11-13 22:59:24 +0000724 return $sql;
725 }
726
727 // --------------------------------------------------------------------
728
729 /**
730 * Close DB Connection
731 *
732 * @access public
733 * @param resource
734 * @return void
735 */
736 function _close($conn_id)
737 {
738 @mysqli_close($conn_id);
739 }
740
741
742}
743
744
745/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000746/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */