blob: a5d104cc25ff9286cbab3d8f058ceec4a7d06d98 [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
Rick Ellis5aa8c602008-10-07 01:24:07 +000033 var $dbdriver = 'mysqli';
34
Derek Allardd2df9bc2007-04-15 17:41:17 +000035 /**
Derek Allard694b5b82007-12-18 15:58:03 +000036 * The syntax to count rows is slightly different across different
37 * database engines, so this string appears in each driver and is
38 * used for the count_all() and count_all_results() functions.
39 */
Derek Allard39b622d2008-01-16 21:10:09 +000040 var $_count_string = "SELECT COUNT(*) AS ";
Derek Allard6ddb5a12007-12-18 17:22:50 +000041 var $_random_keyword = ' RAND()'; // database specific random keyword
42
Derek Allard694b5b82007-12-18 15:58:03 +000043 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +000044 * Whether to use the MySQL "delete hack" which allows the number
45 * of affected rows to be shown. Uses a preg_replace when enabled,
46 * adding a bit more processing to all queries.
47 */
48 var $delete_hack = TRUE;
49
50 // --------------------------------------------------------------------
51
52 /**
53 * Non-persistent database connection
54 *
55 * @access private called by the base class
56 * @return resource
57 */
58 function db_connect()
59 {
60 return @mysqli_connect($this->hostname, $this->username, $this->password);
61 }
62
63 // --------------------------------------------------------------------
64
65 /**
66 * Persistent database connection
67 *
68 * @access private called by the base class
69 * @return resource
70 */
71 function db_pconnect()
72 {
73 return $this->db_connect();
74 }
75
76 // --------------------------------------------------------------------
77
78 /**
79 * Select the database
80 *
81 * @access private called by the base class
82 * @return resource
83 */
84 function db_select()
85 {
86 return @mysqli_select_db($this->conn_id, $this->database);
87 }
88
89 // --------------------------------------------------------------------
90
91 /**
Derek Allard39b622d2008-01-16 21:10:09 +000092 * Set client character set
93 *
94 * @access public
95 * @param string
96 * @param string
97 * @return resource
98 */
99 function db_set_charset($charset, $collation)
100 {
Derek Jonesb1f90db2008-02-13 05:22:38 +0000101 return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
Derek Allard39b622d2008-01-16 21:10:09 +0000102 }
103
104 // --------------------------------------------------------------------
105
106 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000107 * Version number query string
108 *
109 * @access public
110 * @return string
111 */
112 function _version()
113 {
114 return "SELECT version() AS ver";
115 }
116
117 // --------------------------------------------------------------------
118
119 /**
120 * Execute the query
121 *
122 * @access private called by the base class
123 * @param string an SQL query
124 * @return resource
125 */
126 function _execute($sql)
127 {
128 $sql = $this->_prep_query($sql);
129 $result = @mysqli_query($this->conn_id, $sql);
130 return $result;
131 }
132
133 // --------------------------------------------------------------------
134
135 /**
136 * Prep the query
137 *
138 * If needed, each database adapter can prep the query string
139 *
140 * @access private called by execute()
141 * @param string an SQL query
142 * @return string
143 */
144 function _prep_query($sql)
145 {
146 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
147 // the query so that it returns the number of affected rows
148 if ($this->delete_hack === TRUE)
149 {
150 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
151 {
152 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
153 }
154 }
155
156 return $sql;
157 }
158
159 // --------------------------------------------------------------------
160
161 /**
162 * Begin Transaction
163 *
164 * @access public
165 * @return bool
166 */
167 function trans_begin($test_mode = FALSE)
168 {
Derek Jones0b59f272008-05-13 04:22:33 +0000169 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000170 {
171 return TRUE;
172 }
173
174 // When transactions are nested we only begin/commit/rollback the outermost ones
175 if ($this->_trans_depth > 0)
176 {
177 return TRUE;
178 }
179
180 // Reset the transaction failure flag.
181 // If the $test_mode flag is set to TRUE transactions will be rolled back
182 // even if the queries produce a successful result.
183 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
184
185 $this->simple_query('SET AUTOCOMMIT=0');
186 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
187 return TRUE;
188 }
189
190 // --------------------------------------------------------------------
191
192 /**
193 * Commit Transaction
194 *
195 * @access public
196 * @return bool
197 */
198 function trans_commit()
199 {
Derek Jones0b59f272008-05-13 04:22:33 +0000200 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000201 {
202 return TRUE;
203 }
204
205 // When transactions are nested we only begin/commit/rollback the outermost ones
206 if ($this->_trans_depth > 0)
207 {
208 return TRUE;
209 }
210
211 $this->simple_query('COMMIT');
212 $this->simple_query('SET AUTOCOMMIT=1');
213 return TRUE;
214 }
215
216 // --------------------------------------------------------------------
217
218 /**
219 * Rollback Transaction
220 *
221 * @access public
222 * @return bool
223 */
224 function trans_rollback()
225 {
Derek Jones0b59f272008-05-13 04:22:33 +0000226 if ( ! $this->trans_enabled)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000227 {
228 return TRUE;
229 }
230
231 // When transactions are nested we only begin/commit/rollback the outermost ones
232 if ($this->_trans_depth > 0)
233 {
234 return TRUE;
235 }
236
237 $this->simple_query('ROLLBACK');
238 $this->simple_query('SET AUTOCOMMIT=1');
239 return TRUE;
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Escape String
246 *
247 * @access public
248 * @param string
249 * @return string
250 */
251 function escape_str($str)
Derek Allard694b5b82007-12-18 15:58:03 +0000252 {
Derek Jonesd33972b2008-02-13 04:16:27 +0000253 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
Derek Allard694b5b82007-12-18 15:58:03 +0000254 {
255 return mysqli_real_escape_string($this->conn_id, $str);
256 }
257 elseif (function_exists('mysql_escape_string'))
258 {
259 return mysql_escape_string($str);
260 }
261 else
262 {
263 return addslashes($str);
264 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Affected Rows
271 *
272 * @access public
273 * @return integer
274 */
275 function affected_rows()
276 {
277 return @mysqli_affected_rows($this->conn_id);
278 }
279
280 // --------------------------------------------------------------------
281
282 /**
283 * Insert ID
284 *
285 * @access public
286 * @return integer
287 */
288 function insert_id()
289 {
290 return @mysqli_insert_id($this->conn_id);
291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * "Count All" query
297 *
298 * Generates a platform-specific query string that counts all records in
299 * the specified database
300 *
301 * @access public
302 * @param string
303 * @return string
304 */
305 function count_all($table = '')
306 {
307 if ($table == '')
308 return '0';
309
Derek Allardf6cd45c2008-01-18 14:31:51 +0000310 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
Derek Allardd2df9bc2007-04-15 17:41:17 +0000311
312 if ($query->num_rows() == 0)
313 return '0';
314
315 $row = $query->row();
316 return $row->numrows;
317 }
318
319 // --------------------------------------------------------------------
320
321 /**
322 * List table query
323 *
324 * Generates a platform-specific query string so that the table names can be fetched
325 *
326 * @access private
Derek Allard39b622d2008-01-16 21:10:09 +0000327 * @param boolean
Derek Allardd2df9bc2007-04-15 17:41:17 +0000328 * @return string
329 */
Derek Allard694b5b82007-12-18 15:58:03 +0000330 function _list_tables($prefix_limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000331 {
Derek Allard694b5b82007-12-18 15:58:03 +0000332 $sql = "SHOW TABLES FROM `".$this->database."`";
333
Derek Allard39b622d2008-01-16 21:10:09 +0000334 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
Derek Allard694b5b82007-12-18 15:58:03 +0000335 {
Derek Allard39b622d2008-01-16 21:10:09 +0000336 $sql .= " LIKE '".$this->dbprefix."%'";
Derek Allard694b5b82007-12-18 15:58:03 +0000337 }
338
339 return $sql;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000340 }
341
342 // --------------------------------------------------------------------
343
344 /**
345 * Show column query
346 *
347 * Generates a platform-specific query string so that the column names can be fetched
348 *
349 * @access public
350 * @param string the table name
351 * @return string
352 */
353 function _list_columns($table = '')
354 {
355 return "SHOW COLUMNS FROM ".$this->_escape_table($table);
356 }
357
358 // --------------------------------------------------------------------
359
360 /**
361 * Field data query
362 *
363 * Generates a platform-specific query so that the column data can be retrieved
364 *
365 * @access public
366 * @param string the table name
367 * @return object
368 */
369 function _field_data($table)
370 {
371 return "SELECT * FROM ".$this->_escape_table($table)." LIMIT 1";
372 }
373
374 // --------------------------------------------------------------------
375
376 /**
377 * The error message string
378 *
379 * @access private
380 * @return string
381 */
382 function _error_message()
383 {
384 return mysqli_error($this->conn_id);
385 }
386
387 // --------------------------------------------------------------------
388
389 /**
390 * The error message number
391 *
392 * @access private
393 * @return integer
394 */
395 function _error_number()
396 {
397 return mysqli_errno($this->conn_id);
398 }
Rick Ellis52dc8ca2008-09-30 19:53:52 +0000399
400 // --------------------------------------------------------------------
401
402 /**
403 * Escape Column Name
404 *
405 * This function adds backticks around supplied column name
406 *
407 * @access private
408 * @param string the column name
409 * @return string
410 */
411 function _escape_column($column)
412 {
413 return '`' .$column. '`';
414 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000415
416 // --------------------------------------------------------------------
417
418 /**
419 * Escape Table Name
420 *
421 * This function adds backticks if the table name has a period
422 * in it. Some DBs will get cranky unless periods are escaped
423 *
424 * @access private
425 * @param string the table name
426 * @return string
427 */
428 function _escape_table($table)
429 {
Derek Allardc0743382008-02-11 05:54:44 +0000430 if (strpos($table, '.') !== FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000431 {
Derek Allardc0743382008-02-11 05:54:44 +0000432 $table = '`' . str_replace('.', '`.`', $table) . '`';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000433 }
434
435 return $table;
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
Derek Allard39b622d2008-01-16 21:10:09 +0000441 * Protect Identifiers
442 *
443 * This function adds backticks if appropriate based on db type
444 *
445 * @access private
446 * @param mixed the item to escape
447 * @param boolean only affect the first word
448 * @return mixed the item with backticks
449 */
450 function _protect_identifiers($item, $first_word_only = FALSE)
451 {
452 if (is_array($item))
453 {
454 $escaped_array = array();
455
456 foreach($item as $k=>$v)
457 {
458 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
459 }
460
461 return $escaped_array;
462 }
463
464 // This function may get "item1 item2" as a string, and so
465 // we may need "`item1` `item2`" and not "`item1 item2`"
Derek Allard61579382008-01-16 22:22:42 +0000466 if (ctype_alnum($item) === FALSE)
Derek Allard39b622d2008-01-16 21:10:09 +0000467 {
Derek Allard9b3e7b52008-02-04 23:20:34 +0000468 if (strpos($item, '.') !== FALSE)
469 {
470 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
471 $table_name = substr($item, 0, strpos($item, '.')+1);
472 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
473 }
474
Derek Allard39b622d2008-01-16 21:10:09 +0000475 // This function may get "field >= 1", and need it to return "`field` >= 1"
Derek Allard61579382008-01-16 22:22:42 +0000476 $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
Derek Allard39b622d2008-01-16 21:10:09 +0000477
Derek Allard61579382008-01-16 22:22:42 +0000478 $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1`$2`$3', $item);
479 }
480 else
481 {
482 return "`{$item}`";
Derek Allard39b622d2008-01-16 21:10:09 +0000483 }
484
Derek Allard9a4d1da2008-02-25 14:18:38 +0000485 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
Derek Allard39b622d2008-01-16 21:10:09 +0000486
487 foreach ($exceptions as $exception)
488 {
Derek Allard61579382008-01-16 22:22:42 +0000489
Derek Allard39b622d2008-01-16 21:10:09 +0000490 if (stristr($item, " `{$exception}` ") !== FALSE)
491 {
492 $item = preg_replace('/ `('.preg_quote($exception).')` /i', ' $1 ', $item);
493 }
494 }
Derek Allard39b622d2008-01-16 21:10:09 +0000495 return $item;
496 }
497
498 // --------------------------------------------------------------------
499
500 /**
Derek Jonesc6ad0232008-01-29 18:44:54 +0000501 * From Tables
502 *
503 * This function implicitly groups FROM tables so there is no confusion
504 * about operator precedence in harmony with SQL standards
505 *
506 * @access public
507 * @param type
508 * @return type
509 */
510 function _from_tables($tables)
511 {
Derek Jones0b59f272008-05-13 04:22:33 +0000512 if ( ! is_array($tables))
Derek Jonesc6ad0232008-01-29 18:44:54 +0000513 {
514 $tables = array($tables);
515 }
516
517 return '('.implode(', ', $tables).')';
518 }
519
520 // --------------------------------------------------------------------
521
522 /**
Derek Allardd2df9bc2007-04-15 17:41:17 +0000523 * Insert statement
524 *
525 * Generates a platform-specific insert string from the supplied data
526 *
527 * @access public
528 * @param string the table name
529 * @param array the insert keys
530 * @param array the insert values
531 * @return string
532 */
533 function _insert($table, $keys, $values)
534 {
535 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
536 }
537
538 // --------------------------------------------------------------------
539
540 /**
541 * Update statement
542 *
543 * Generates a platform-specific update string from the supplied data
544 *
545 * @access public
546 * @param string the table name
547 * @param array the update data
548 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000549 * @param array the orderby clause
550 * @param array the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000551 * @return string
552 */
Derek Allard39b622d2008-01-16 21:10:09 +0000553 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000554 {
555 foreach($values as $key => $val)
556 {
557 $valstr[] = $key." = ".$val;
558 }
Derek Allardda6d2402007-12-19 14:49:29 +0000559
Derek Jones0b59f272008-05-13 04:22:33 +0000560 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allard39b622d2008-01-16 21:10:09 +0000561
562 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
Derek Allardd2df9bc2007-04-15 17:41:17 +0000563
Derek Allard32cf7eb2008-02-05 16:03:50 +0000564 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
565 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
566 $sql .= $orderby.$limit;
567
568 return $sql;
Derek Allard39b622d2008-01-16 21:10:09 +0000569 }
570
571
572 // --------------------------------------------------------------------
573
574 /**
575 * Truncate statement
576 *
577 * Generates a platform-specific truncate string from the supplied data
578 * If the database does not support the truncate() command
579 * This function maps to "DELETE FROM table"
580 *
581 * @access public
582 * @param string the table name
583 * @return string
584 */
585 function _truncate($table)
586 {
587 return "TRUNCATE ".$this->_escape_table($table);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000588 }
589
590 // --------------------------------------------------------------------
591
592 /**
593 * Delete statement
594 *
595 * Generates a platform-specific delete string from the supplied data
596 *
597 * @access public
598 * @param string the table name
599 * @param array the where clause
Derek Allard39b622d2008-01-16 21:10:09 +0000600 * @param string the limit clause
Derek Allardd2df9bc2007-04-15 17:41:17 +0000601 * @return string
602 */
Derek Allard39b622d2008-01-16 21:10:09 +0000603 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000604 {
Derek Allard39b622d2008-01-16 21:10:09 +0000605 $conditions = '';
606
Derek Jones0b59f272008-05-13 04:22:33 +0000607 if (count($where) > 0 OR count($like) > 0)
Derek Allard39b622d2008-01-16 21:10:09 +0000608 {
609 $conditions = "\nWHERE ";
610 $conditions .= implode("\n", $this->ar_where);
611
612 if (count($where) > 0 && count($like) > 0)
613 {
614 $conditions .= " AND ";
615 }
616 $conditions .= implode("\n", $like);
617 }
618
Derek Jones0b59f272008-05-13 04:22:33 +0000619 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
Derek Allarde77d77c2007-12-19 15:01:55 +0000620
Derek Allard39b622d2008-01-16 21:10:09 +0000621 return "DELETE FROM ".$table.$conditions.$limit;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000622 }
623
624 // --------------------------------------------------------------------
625
626 /**
627 * Limit string
628 *
629 * Generates a platform-specific LIMIT clause
630 *
631 * @access public
632 * @param string the sql query string
633 * @param integer the number of rows to limit the query to
634 * @param integer the offset value
635 * @return string
636 */
637 function _limit($sql, $limit, $offset)
638 {
639 $sql .= "LIMIT ".$limit;
640
641 if ($offset > 0)
642 {
643 $sql .= " OFFSET ".$offset;
644 }
645
646 return $sql;
647 }
648
649 // --------------------------------------------------------------------
650
651 /**
652 * Close DB Connection
653 *
654 * @access public
655 * @param resource
656 * @return void
657 */
658 function _close($conn_id)
659 {
660 @mysqli_close($conn_id);
661 }
662
663
664}
665
Derek Jones0b59f272008-05-13 04:22:33 +0000666
667/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000668/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */