blob: 6558112cd8713ba731e0d02f455da3ac101a5990 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
10 * @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';
34
35 // 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.
54 */
55 var $delete_hack = TRUE;
56
57 // --------------------------------------------------------------------
58
59 /**
60 * Non-persistent database connection
61 *
62 * @access private called by the base class
63 * @return resource
64 */
65 function db_connect()
66 {
67 if ($this->port != '')
68 {
69 return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
70 }
71 else
72 {
73 return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
74 }
75
76 }
77
78 // --------------------------------------------------------------------
79
80 /**
81 * Persistent database connection
82 *
83 * @access private called by the base class
84 * @return resource
85 */
86 function db_pconnect()
87 {
88 return $this->db_connect();
89 }
90
91 // --------------------------------------------------------------------
92
93 /**
94 * Select the database
95 *
96 * @access private called by the base class
97 * @return resource
98 */
99 function db_select()
100 {
101 return @mysqli_select_db($this->conn_id, $this->database);
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Set client character set
108 *
109 * @access private
110 * @param string
111 * @param string
112 * @return resource
113 */
114 function _db_set_charset($charset, $collation)
115 {
116 return @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
117 }
118
119 // --------------------------------------------------------------------
120
121 /**
122 * Version number query string
123 *
124 * @access public
125 * @return string
126 */
127 function _version()
128 {
129 return "SELECT version() AS ver";
130 }
131
132 // --------------------------------------------------------------------
133
134 /**
135 * Execute the query
136 *
137 * @access private called by the base class
138 * @param string an SQL query
139 * @return resource
140 */
141 function _execute($sql)
142 {
143 $sql = $this->_prep_query($sql);
144 $result = @mysqli_query($this->conn_id, $sql);
145 return $result;
146 }
147
148 // --------------------------------------------------------------------
149
150 /**
151 * Prep the query
152 *
153 * If needed, each database adapter can prep the query string
154 *
155 * @access private called by execute()
156 * @param string an SQL query
157 * @return string
158 */
159 function _prep_query($sql)
160 {
161 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
162 // the query so that it returns the number of affected rows
163 if ($this->delete_hack === TRUE)
164 {
165 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
166 {
167 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
168 }
169 }
170
171 return $sql;
172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Begin Transaction
178 *
179 * @access public
180 * @return bool
181 */
182 function trans_begin($test_mode = FALSE)
183 {
184 if ( ! $this->trans_enabled)
185 {
186 return TRUE;
187 }
188
189 // When transactions are nested we only begin/commit/rollback the outermost ones
190 if ($this->_trans_depth > 0)
191 {
192 return TRUE;
193 }
194
195 // Reset the transaction failure flag.
196 // If the $test_mode flag is set to TRUE transactions will be rolled back
197 // even if the queries produce a successful result.
198 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
199
200 $this->simple_query('SET AUTOCOMMIT=0');
201 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
202 return TRUE;
203 }
204
205 // --------------------------------------------------------------------
206
207 /**
208 * Commit Transaction
209 *
210 * @access public
211 * @return bool
212 */
213 function trans_commit()
214 {
215 if ( ! $this->trans_enabled)
216 {
217 return TRUE;
218 }
219
220 // When transactions are nested we only begin/commit/rollback the outermost ones
221 if ($this->_trans_depth > 0)
222 {
223 return TRUE;
224 }
225
226 $this->simple_query('COMMIT');
227 $this->simple_query('SET AUTOCOMMIT=1');
228 return TRUE;
229 }
230
231 // --------------------------------------------------------------------
232
233 /**
234 * Rollback Transaction
235 *
236 * @access public
237 * @return bool
238 */
239 function trans_rollback()
240 {
241 if ( ! $this->trans_enabled)
242 {
243 return TRUE;
244 }
245
246 // When transactions are nested we only begin/commit/rollback the outermost ones
247 if ($this->_trans_depth > 0)
248 {
249 return TRUE;
250 }
251
252 $this->simple_query('ROLLBACK');
253 $this->simple_query('SET AUTOCOMMIT=1');
254 return TRUE;
255 }
256
257 // --------------------------------------------------------------------
258
259 /**
260 * Escape String
261 *
262 * @access public
263 * @param string
Derek Jonese4ed5832009-02-20 21:44:59 +0000264 * @param bool whether or not the string will be used in a LIKE condition
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 * @return string
266 */
Derek Jonese4ed5832009-02-20 21:44:59 +0000267 function escape_str($str, $like = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000269 if (is_array($str))
270 {
271 foreach($str as $key => $val)
272 {
273 $str[$key] = $this->escape_str($val, $like);
274 }
275
276 return $str;
277 }
278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id))
280 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000281 $str = mysqli_real_escape_string($this->conn_id, $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
283 elseif (function_exists('mysql_escape_string'))
284 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000285 $str = mysql_escape_string($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
287 else
288 {
Derek Jonese4ed5832009-02-20 21:44:59 +0000289 $str = addslashes($str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Derek Jonese4ed5832009-02-20 21:44:59 +0000291
292 // escape LIKE condition wildcards
293 if ($like === TRUE)
294 {
295 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
296 }
297
298 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 }
300
301 // --------------------------------------------------------------------
302
303 /**
304 * Affected Rows
305 *
306 * @access public
307 * @return integer
308 */
309 function affected_rows()
310 {
311 return @mysqli_affected_rows($this->conn_id);
312 }
313
314 // --------------------------------------------------------------------
315
316 /**
317 * Insert ID
318 *
319 * @access public
320 * @return integer
321 */
322 function insert_id()
323 {
324 return @mysqli_insert_id($this->conn_id);
325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * "Count All" query
331 *
332 * Generates a platform-specific query string that counts all records in
333 * the specified database
334 *
335 * @access public
336 * @param string
337 * @return string
338 */
339 function count_all($table = '')
340 {
341 if ($table == '')
Derek Allarde37ab382009-02-03 16:13:57 +0000342 {
343 return 0;
344 }
345
346 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 if ($query->num_rows() == 0)
Derek Allarde37ab382009-02-03 16:13:57 +0000349 {
350 return 0;
351 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000352
353 $row = $query->row();
Derek Allarde37ab382009-02-03 16:13:57 +0000354 return (int) $row->numrows;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
356
357 // --------------------------------------------------------------------
358
359 /**
360 * List table query
361 *
362 * Generates a platform-specific query string so that the table names can be fetched
363 *
364 * @access private
365 * @param boolean
366 * @return string
367 */
368 function _list_tables($prefix_limit = FALSE)
369 {
370 $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char;
371
372 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
373 {
Derek Jones3c11b6f2009-02-20 22:36:27 +0000374 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'";
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 }
376
377 return $sql;
378 }
379
380 // --------------------------------------------------------------------
381
382 /**
383 * Show column query
384 *
385 * Generates a platform-specific query string so that the column names can be fetched
386 *
387 * @access public
388 * @param string the table name
389 * @return string
390 */
391 function _list_columns($table = '')
392 {
393 return "SHOW COLUMNS FROM ".$table;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Field data query
400 *
401 * Generates a platform-specific query so that the column data can be retrieved
402 *
403 * @access public
404 * @param string the table name
405 * @return object
406 */
407 function _field_data($table)
408 {
409 return "SELECT * FROM ".$table." LIMIT 1";
410 }
411
412 // --------------------------------------------------------------------
413
414 /**
415 * The error message string
416 *
417 * @access private
418 * @return string
419 */
420 function _error_message()
421 {
422 return mysqli_error($this->conn_id);
423 }
424
425 // --------------------------------------------------------------------
426
427 /**
428 * The error message number
429 *
430 * @access private
431 * @return integer
432 */
433 function _error_number()
434 {
435 return mysqli_errno($this->conn_id);
436 }
437
438 // --------------------------------------------------------------------
439
440 /**
441 * Escape the SQL Identifiers
442 *
443 * This function escapes column and table names
444 *
445 * @access private
446 * @param string
447 * @return string
448 */
449 function _escape_identifiers($item)
450 {
451 if ($this->_escape_char == '')
452 {
453 return $item;
454 }
455
456 foreach ($this->_reserved_identifiers as $id)
457 {
458 if (strpos($item, '.'.$id) !== FALSE)
459 {
460 $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item);
461
462 // remove duplicates if the user already included the escape
463 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
464 }
465 }
466
467 if (strpos($item, '.') !== FALSE)
468 {
469 $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char;
470 }
471 else
472 {
473 $str = $this->_escape_char.$item.$this->_escape_char;
474 }
475
476 // remove duplicates if the user already included the escape
477 return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str);
478 }
479
480 // --------------------------------------------------------------------
481
482 /**
483 * 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 {
494 if ( ! is_array($tables))
495 {
496 $tables = array($tables);
497 }
498
499 return '('.implode(', ', $tables).')';
500 }
501
502 // --------------------------------------------------------------------
503
504 /**
505 * 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 ".$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
531 * @param array the orderby clause
532 * @param array the limit clause
533 * @return string
534 */
535 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
536 {
537 foreach($values as $key => $val)
538 {
539 $valstr[] = $key." = ".$val;
540 }
541
542 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
543
544 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
545
546 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
547
548 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
549
550 $sql .= $orderby.$limit;
551
552 return $sql;
553 }
554
555
556 // --------------------------------------------------------------------
557
558 /**
559 * Truncate statement
560 *
561 * Generates a platform-specific truncate string from the supplied data
562 * If the database does not support the truncate() command
563 * This function maps to "DELETE FROM table"
564 *
565 * @access public
566 * @param string the table name
567 * @return string
568 */
569 function _truncate($table)
570 {
571 return "TRUNCATE ".$table;
572 }
573
574 // --------------------------------------------------------------------
575
576 /**
577 * Delete statement
578 *
579 * Generates a platform-specific delete string from the supplied data
580 *
581 * @access public
582 * @param string the table name
583 * @param array the where clause
584 * @param string the limit clause
585 * @return string
586 */
587 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
588 {
589 $conditions = '';
590
591 if (count($where) > 0 OR count($like) > 0)
592 {
593 $conditions = "\nWHERE ";
594 $conditions .= implode("\n", $this->ar_where);
595
596 if (count($where) > 0 && count($like) > 0)
597 {
598 $conditions .= " AND ";
599 }
600 $conditions .= implode("\n", $like);
601 }
602
603 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
604
605 return "DELETE FROM ".$table.$conditions.$limit;
606 }
607
608 // --------------------------------------------------------------------
609
610 /**
611 * Limit string
612 *
613 * Generates a platform-specific LIMIT clause
614 *
615 * @access public
616 * @param string the sql query string
617 * @param integer the number of rows to limit the query to
618 * @param integer the offset value
619 * @return string
620 */
621 function _limit($sql, $limit, $offset)
622 {
623 $sql .= "LIMIT ".$limit;
624
625 if ($offset > 0)
626 {
627 $sql .= " OFFSET ".$offset;
628 }
629
630 return $sql;
631 }
632
633 // --------------------------------------------------------------------
634
635 /**
636 * Close DB Connection
637 *
638 * @access public
639 * @param resource
640 * @return void
641 */
642 function _close($conn_id)
643 {
644 @mysqli_close($conn_id);
645 }
646
647
648}
649
650
651/* End of file mysqli_driver.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000652/* Location: ./system/database/drivers/mysqli/mysqli_driver.php */