blob: 35a7fc077812a03c9861ae700bfda1ebf8e98cb5 [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
Rick Ellis9ba2bf22008-09-12 23:34:39 +00009 * @copyright Copyright (c) 2008, 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 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000397
398 // --------------------------------------------------------------------
399
400 /**
401 * Escape Column Name
402 *
403 * This function adds backticks around supplied column name
404 *
405 * @access private
406 * @param string the column name
407 * @return string
408 */
409 function _escape_column($column)
410 {
411 return '`' .$column. '`';
412 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000413
414 // --------------------------------------------------------------------
415
416 /**
417 * Escape Table Name
418 *
419 * This function adds backticks if the table name has a period
420 * in it. Some DBs will get cranky unless periods are escaped
421 *
422 * @access private
423 * @param string the table name
424 * @return string
425 */
426 function _escape_table($table)
427 {
Derek Allardc0743382008-02-11 05:54:44 +0000428 if (strpos($table, '.') !== FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000429 {
Derek Allardc0743382008-02-11 05:54:44 +0000430 $table = '`' . str_replace('.', '`.`', $table) . '`';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000431 }
432
433 return $table;
434 }
435
436 // --------------------------------------------------------------------
437
438 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000439 * Protect Identifiers
440 *
441 * This function adds backticks if appropriate based on db type
442 *
443 * @access private
444 * @param mixed the item to escape
445 * @param boolean only affect the first word
446 * @return mixed the item with backticks
447 */
448 function _protect_identifiers($item, $first_word_only = FALSE)
449 {
450 if (is_array($item))
451 {
452 $escaped_array = array();
453
454 foreach($item as $k=>$v)
455 {
456 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
457 }
458
459 return $escaped_array;
460 }
461
462 // This function may get "item1 item2" as a string, and so
463 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000464 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000465 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000466 if (strpos($item, '.') !== FALSE)
467 {
468 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
469 $table_name = substr($item, 0, strpos($item, '.')+1);
470 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
471 }
472
Derek Allard39b622d2008-01-16 21:10:09 +0000473 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000474 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000475
Derek Allard61579382008-01-16 22:22:42 +0000476 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
477 }
478 else
479 {
480 return "`{$item}`";
Derek Allard39b622d2008-01-16 21:10:09 +0000481 }
482
Derek Allard9a4d1da2008-02-25 14:18:38 +0000483 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000484
485 foreach ($exceptions as $exception)
486 {
Derek Allard61579382008-01-16 22:22:42 +0000487
Derek Allard39b622d2008-01-16 21:10:09 +0000488 if (stristr($item, " `{$exception}` ") !== FALSE)
489 {
490 $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
491 }
492 }
Derek Allard39b622d2008-01-16 21:10:09 +0000493 return $item;
494 }
495
496 // --------------------------------------------------------------------
497
498 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000499 * From Tables
500 *
501 * This function implicitly groups FROM tables so there is no confusion
502 * about operator precedence in harmony with SQL standards
503 *
504 * @access public
505 * @param type
506 * @return type
507 */
508 function _from_tables($tables)
509 {
Derek Jones0b59f272008-05-13 04:22:33 +0000510 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000511 {
512 $tables = array($tables);
513 }
514
515 return '('.implode(', ', $tables).')';
516 }
517
518 // --------------------------------------------------------------------
519
520 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000521 * Insert statement
522 *
523 * Generates a platform-specific insert string from the supplied data
524 *
525 * @access public
526 * @param string the table name
527 * @param array the insert keys
528 * @param array the insert values
529 * @return string
530 */
531 function _insert($table, $keys, $values)
532 {
533 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
534 }
535
536 // --------------------------------------------------------------------
537
538 /**
539 * Update statement
540 *
541 * Generates a platform-specific update string from the supplied data
542 *
543 * @access public
544 * @param string the table name
545 * @param array the update data
546 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000547 * @param array the orderby clause
548 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000549 * @return string
550 */
Derek Allard39b622d2008-01-16 21:10:09 +0000551 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000552 {
553 foreach($values as $key => $val)
554 {
555 $valstr[] = $key." = ".$val;
556 }
Derek Allardda6d2402007-12-19 14:49:29 +0000557
Derek Jones0b59f272008-05-13 04:22:33 +0000558 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000559
560 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000561
Derek Allard32cf7eb2008-02-05 16:03:50 +0000562 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
563 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
564 $sql .= $orderby.$limit;
565
566 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000567 }
568
569
570 // --------------------------------------------------------------------
571
572 /**
573 * Truncate statement
574 *
575 * Generates a platform-specific truncate string from the supplied data
576 * If the database does not support the truncate() command
577 * This function maps to "DELETE FROM table"
578 *
579 * @access public
580 * @param string the table name
581 * @return string
582 */
583 function _truncate($table)
584 {
585 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000586 }
587
588 // --------------------------------------------------------------------
589
590 /**
591 * Delete statement
592 *
593 * Generates a platform-specific delete string from the supplied data
594 *
595 * @access public
596 * @param string the table name
597 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000598 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000599 * @return string
600 */
Derek Allard39b622d2008-01-16 21:10:09 +0000601 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000602 {
Derek Allard39b622d2008-01-16 21:10:09 +0000603 $conditions = '';
604
Derek Jones0b59f272008-05-13 04:22:33 +0000605 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000606 {
607 $conditions = "\nWHERE ";
608 $conditions .= implode("\n", $this->ar_where);
609
610 if (count($where) > 0 && count($like) > 0)
611 {
612 $conditions .= " AND ";
613 }
614 $conditions .= implode("\n", $like);
615 }
616
Derek Jones0b59f272008-05-13 04:22:33 +0000617 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000618
Derek Allard39b622d2008-01-16 21:10:09 +0000619 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000620 }
621
622 // --------------------------------------------------------------------
623
624 /**
625 * Limit string
626 *
627 * Generates a platform-specific LIMIT clause
628 *
629 * @access public
630 * @param string the sql query string
631 * @param integer the number of rows to limit the query to
632 * @param integer the offset value
633 * @return string
634 */
635 function _limit($sql, $limit, $offset)
636 {
637 $sql .= "LIMIT ".$limit;
638
639 if ($offset > 0)
640 {
641 $sql .= " OFFSET ".$offset;
642 }
643
644 return $sql;
645 }
646
647 // --------------------------------------------------------------------
648
649 /**
650 * Close DB Connection
651 *
652 * @access public
653 * @param resource
654 * @return void
655 */
656 function _close($conn_id)
657 {
658 @mysqli_close($conn_id);
659 }
660
661
662}
663
Derek Jones0b59f272008-05-13 04:22:33 +0000664
665/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000666/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */