blob: 4e840e7499d872f26cd1eaeac0fa02aacabfaeb6 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000028 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000029 * @link http://codeigniter.com/user_guide/database/
Derek Allardd2df9bc2007-04-15 17:41:17 +000030 */
31class CI_DB_mysqli_driver extends CI_DB {
32
33 /**
Derek Allard694b5b82007-12-18 15:58:03 +000034 * The syntax to count rows is slightly different across different
35 * database engines, so this string appears in each driver and is
36 * used for the count_all() and count_all_results() functions.
37 */
Derek Allard39b622d2008-01-16 21:10:09 +000038 var $_count_string = "SELECT COUNT(*) AS ";
Derek Allard6ddb5a12007-12-18 17:22:50 +000039 var $_random_keyword = ' RAND()'; // database specific random keyword
40
Derek Allard694b5b82007-12-18 15:58:03 +000041 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000042 * Whether to use the MySQL "delete hack" which allows the number
43 * of affected rows to be shown. Uses a preg_replace when enabled,
44 * adding a bit more processing to all queries.
45 */
46 var $delete_hack = TRUE;
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Non-persistent database connection
52 *
53 * @access private called by the base class
54 * @return resource
55 */
56 function db_connect()
57 {
58 return @mysqli_connect($this->hostname, $this->username, $this->password);
59 }
60
61 // --------------------------------------------------------------------
62
63 /**
64 * Persistent database connection
65 *
66 * @access private called by the base class
67 * @return resource
68 */
69 function db_pconnect()
70 {
71 return $this->db_connect();
72 }
73
74 // --------------------------------------------------------------------
75
76 /**
77 * Select the database
78 *
79 * @access private called by the base class
80 * @return resource
81 */
82 function db_select()
83 {
84 return @mysqli_select_db($this->conn_id, $this->database);
85 }
86
87 // --------------------------------------------------------------------
88
89 /**
Derek Allard39b622d2008-01-16 21:10:09 +000090 * Set client character set
91 *
92 * @access public
93 * @param string
94 * @param string
95 * @return resource
96 */
97 function db_set_charset($charset, $collation)
98 {
Derek Jonesb1f90db2008-02-13 05:22:38 +000099 return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard39b622d2008-01-16 21:10:09 +0000100 }
101
102 // --------------------------------------------------------------------
103
104 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000105 * Version number query string
106 *
107 * @access public
108 * @return string
109 */
110 function _version()
111 {
112 return "SELECT version() AS ver";
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Execute the query
119 *
120 * @access private called by the base class
121 * @param string an SQL query
122 * @return resource
123 */
124 function _execute($sql)
125 {
126 $sql = $this->_prep_query($sql);
127 $result = @mysqli_query($this->conn_id, $sql);
128 return $result;
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * Prep the query
135 *
136 * If needed, each database adapter can prep the query string
137 *
138 * @access private called by execute()
139 * @param string an SQL query
140 * @return string
141 */
142 function _prep_query($sql)
143 {
144 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
145 // the query so that it returns the number of affected rows
146 if ($this->delete_hack === TRUE)
147 {
148 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
149 {
150 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
151 }
152 }
153
154 return $sql;
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
160 * Begin Transaction
161 *
162 * @access public
163 * @return bool
164 */
165 function trans_begin($test_mode = FALSE)
166 {
Derek Jones0b59f272008-05-13 04:22:33 +0000167 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000168 {
169 return TRUE;
170 }
171
172 // When transactions are nested we only begin/commit/rollback the outermost ones
173 if ($this->_trans_depth > 0)
174 {
175 return TRUE;
176 }
177
178 // Reset the transaction failure flag.
179 // If the $test_mode flag is set to TRUE transactions will be rolled back
180 // even if the queries produce a successful result.
181 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
182
183 $this->simple_query('SET AUTOCOMMIT=0');
184 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
185 return TRUE;
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
191 * Commit Transaction
192 *
193 * @access public
194 * @return bool
195 */
196 function trans_commit()
197 {
Derek Jones0b59f272008-05-13 04:22:33 +0000198 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000199 {
200 return TRUE;
201 }
202
203 // When transactions are nested we only begin/commit/rollback the outermost ones
204 if ($this->_trans_depth > 0)
205 {
206 return TRUE;
207 }
208
209 $this->simple_query('COMMIT');
210 $this->simple_query('SET AUTOCOMMIT=1');
211 return TRUE;
212 }
213
214 // --------------------------------------------------------------------
215
216 /**
217 * Rollback Transaction
218 *
219 * @access public
220 * @return bool
221 */
222 function trans_rollback()
223 {
Derek Jones0b59f272008-05-13 04:22:33 +0000224 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000225 {
226 return TRUE;
227 }
228
229 // When transactions are nested we only begin/commit/rollback the outermost ones
230 if ($this->_trans_depth > 0)
231 {
232 return TRUE;
233 }
234
235 $this->simple_query('ROLLBACK');
236 $this->simple_query('SET AUTOCOMMIT=1');
237 return TRUE;
238 }
239
240 // --------------------------------------------------------------------
241
242 /**
243 * Escape String
244 *
245 * @access public
246 * @param string
247 * @return string
248 */
249 function escape_str($str)
Derek Allard694b5b82007-12-18 15:58:03 +0000250 {
Derek Jonesd33972b2008-02-13 04:16:27 +0000251 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
Derek Allard694b5b82007-12-18 15:58:03 +0000252 {
253 return mysqli_real_escape_string($this->conn_id, $str);
254 }
255 elseif (function_exists('mysql_escape_string'))
256 {
257 return mysql_escape_string($str);
258 }
259 else
260 {
261 return addslashes($str);
262 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000263 }
264
265 // --------------------------------------------------------------------
266
267 /**
268 * Affected Rows
269 *
270 * @access public
271 * @return integer
272 */
273 function affected_rows()
274 {
275 return @mysqli_affected_rows($this->conn_id);
276 }
277
278 // --------------------------------------------------------------------
279
280 /**
281 * Insert ID
282 *
283 * @access public
284 * @return integer
285 */
286 function insert_id()
287 {
288 return @mysqli_insert_id($this->conn_id);
289 }
290
291 // --------------------------------------------------------------------
292
293 /**
294 * "Count All" query
295 *
296 * Generates a platform-specific query string that counts all records in
297 * the specified database
298 *
299 * @access public
300 * @param string
301 * @return string
302 */
303 function count_all($table = '')
304 {
305 if ($table == '')
306 return '0';
307
Derek Allardf6cd45c2008-01-18 14:31:51 +0000308 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000309
310 if ($query->num_rows() == 0)
311 return '0';
312
313 $row = $query->row();
314 return $row->numrows;
315 }
316
317 // --------------------------------------------------------------------
318
319 /**
320 * List table query
321 *
322 * Generates a platform-specific query string so that the table names can be fetched
323 *
324 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000325 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000326 * @return string
327 */
Derek Allard694b5b82007-12-18 15:58:03 +0000328 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000329 {
Derek Allard694b5b82007-12-18 15:58:03 +0000330 $sql = "SHOW TABLES FROM `".$this->database."`";
331
Derek Allard39b622d2008-01-16 21:10:09 +0000332 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
Derek Allard694b5b82007-12-18 15:58:03 +0000333 {
Derek Allard39b622d2008-01-16 21:10:09 +0000334 $sql .= " LIKE '".$this->dbprefix."%'";
Derek Allard694b5b82007-12-18 15:58:03 +0000335 }
336
337 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000338 }
339
340 // --------------------------------------------------------------------
341
342 /**
343 * Show column query
344 *
345 * Generates a platform-specific query string so that the column names can be fetched
346 *
347 * @access public
348 * @param string the table name
349 * @return string
350 */
351 function _list_columns($table = '')
352 {
353 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
354 }
355
356 // --------------------------------------------------------------------
357
358 /**
359 * Field data query
360 *
361 * Generates a platform-specific query so that the column data can be retrieved
362 *
363 * @access public
364 * @param string the table name
365 * @return object
366 */
367 function _field_data($table)
368 {
369 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
370 }
371
372 // --------------------------------------------------------------------
373
374 /**
375 * The error message string
376 *
377 * @access private
378 * @return string
379 */
380 function _error_message()
381 {
382 return mysqli_error($this->conn_id);
383 }
384
385 // --------------------------------------------------------------------
386
387 /**
388 * The error message number
389 *
390 * @access private
391 * @return integer
392 */
393 function _error_number()
394 {
395 return mysqli_errno($this->conn_id);
396 }
397
398 // --------------------------------------------------------------------
399
400 /**
401 * Escape Table Name
402 *
403 * This function adds backticks if the table name has a period
404 * in it. Some DBs will get cranky unless periods are escaped
405 *
406 * @access private
407 * @param string the table name
408 * @return string
409 */
410 function _escape_table($table)
411 {
Derek Allardc0743382008-02-11 05:54:44 +0000412 if (strpos($table, '.') !== FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000413 {
Derek Allardc0743382008-02-11 05:54:44 +0000414 $table = '`' . str_replace('.', '`.`', $table) . '`';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000415 }
416
417 return $table;
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000423 * Protect Identifiers
424 *
425 * This function adds backticks if appropriate based on db type
426 *
427 * @access private
428 * @param mixed the item to escape
429 * @param boolean only affect the first word
430 * @return mixed the item with backticks
431 */
432 function _protect_identifiers($item, $first_word_only = FALSE)
433 {
434 if (is_array($item))
435 {
436 $escaped_array = array();
437
438 foreach($item as $k=>$v)
439 {
440 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
441 }
442
443 return $escaped_array;
444 }
445
446 // This function may get "item1 item2" as a string, and so
447 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000448 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000449 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000450 if (strpos($item, '.') !== FALSE)
451 {
452 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
453 $table_name = substr($item, 0, strpos($item, '.')+1);
454 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
455 }
456
Derek Allard39b622d2008-01-16 21:10:09 +0000457 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000458 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000459
Derek Allard61579382008-01-16 22:22:42 +0000460 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
461 }
462 else
463 {
464 return "`{$item}`";
Derek Allard39b622d2008-01-16 21:10:09 +0000465 }
466
Derek Allard9a4d1da2008-02-25 14:18:38 +0000467 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000468
469 foreach ($exceptions as $exception)
470 {
Derek Allard61579382008-01-16 22:22:42 +0000471
Derek Allard39b622d2008-01-16 21:10:09 +0000472 if (stristr($item, " `{$exception}` ") !== FALSE)
473 {
474 $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
475 }
476 }
Derek Allard39b622d2008-01-16 21:10:09 +0000477 return $item;
478 }
479
480 // --------------------------------------------------------------------
481
482 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000483 * From Tables
484 *
485 * This function implicitly groups FROM tables so there is no confusion
486 * about operator precedence in harmony with SQL standards
487 *
488 * @access public
489 * @param type
490 * @return type
491 */
492 function _from_tables($tables)
493 {
Derek Jones0b59f272008-05-13 04:22:33 +0000494 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000495 {
496 $tables = array($tables);
497 }
498
499 return '('.implode(', ', $tables).')';
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000505 * Insert statement
506 *
507 * Generates a platform-specific insert string from the supplied data
508 *
509 * @access public
510 * @param string the table name
511 * @param array the insert keys
512 * @param array the insert values
513 * @return string
514 */
515 function _insert($table, $keys, $values)
516 {
517 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
518 }
519
520 // --------------------------------------------------------------------
521
522 /**
523 * Update statement
524 *
525 * Generates a platform-specific update string from the supplied data
526 *
527 * @access public
528 * @param string the table name
529 * @param array the update data
530 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000531 * @param array the orderby clause
532 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000533 * @return string
534 */
Derek Allard39b622d2008-01-16 21:10:09 +0000535 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000536 {
537 foreach($values as $key => $val)
538 {
539 $valstr[] = $key." = ".$val;
540 }
Derek Allardda6d2402007-12-19 14:49:29 +0000541
Derek Jones0b59f272008-05-13 04:22:33 +0000542 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000543
544 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000545
Derek Allard32cf7eb2008-02-05 16:03:50 +0000546 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
547 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
548 $sql .= $orderby.$limit;
549
550 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000551 }
552
553
554 // --------------------------------------------------------------------
555
556 /**
557 * Truncate statement
558 *
559 * Generates a platform-specific truncate string from the supplied data
560 * If the database does not support the truncate() command
561 * This function maps to "DELETE FROM table"
562 *
563 * @access public
564 * @param string the table name
565 * @return string
566 */
567 function _truncate($table)
568 {
569 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000570 }
571
572 // --------------------------------------------------------------------
573
574 /**
575 * Delete statement
576 *
577 * Generates a platform-specific delete string from the supplied data
578 *
579 * @access public
580 * @param string the table name
581 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000582 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000583 * @return string
584 */
Derek Allard39b622d2008-01-16 21:10:09 +0000585 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000586 {
Derek Allard39b622d2008-01-16 21:10:09 +0000587 $conditions = '';
588
Derek Jones0b59f272008-05-13 04:22:33 +0000589 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000590 {
591 $conditions = "\nWHERE ";
592 $conditions .= implode("\n", $this->ar_where);
593
594 if (count($where) > 0 && count($like) > 0)
595 {
596 $conditions .= " AND ";
597 }
598 $conditions .= implode("\n", $like);
599 }
600
Derek Jones0b59f272008-05-13 04:22:33 +0000601 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000602
Derek Allard39b622d2008-01-16 21:10:09 +0000603 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000604 }
605
606 // --------------------------------------------------------------------
607
608 /**
609 * Limit string
610 *
611 * Generates a platform-specific LIMIT clause
612 *
613 * @access public
614 * @param string the sql query string
615 * @param integer the number of rows to limit the query to
616 * @param integer the offset value
617 * @return string
618 */
619 function _limit($sql, $limit, $offset)
620 {
621 $sql .= "LIMIT ".$limit;
622
623 if ($offset > 0)
624 {
625 $sql .= " OFFSET ".$offset;
626 }
627
628 return $sql;
629 }
630
631 // --------------------------------------------------------------------
632
633 /**
634 * Close DB Connection
635 *
636 * @access public
637 * @param resource
638 * @return void
639 */
640 function _close($conn_id)
641 {
642 @mysqli_close($conn_id);
643 }
644
645
646}
647
Derek Jones0b59f272008-05-13 04:22:33 +0000648
649/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000650/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */