]>
jfr.im git - z_archive/KronOS.git/blob - system/database/drivers/mysqli/mysqli_driver.php
d3200f3280eee6f973b5db3d32b55dbf73849790
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * MySQLi Database Adapter Class - MySQLi only works with PHP 5
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.
25 * @package CodeIgniter
28 * @author ExpressionEngine Dev Team
29 * @link http://codeigniter.com/user_guide/database/
31 class CI_DB_mysqli_driver
extends CI_DB
{
33 var $dbdriver = 'mysqli';
35 // The character used for escaping
36 var $_escape_char = '`';
38 // clause and character used for LIKE escape sequences - not used in MySQL
39 var $_like_escape_str = '';
40 var $_like_escape_chr = '';
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.
47 var $_count_string = "SELECT COUNT(*) AS ";
48 var $_random_keyword = ' RAND()'; // database specific random keyword
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.
55 var $delete_hack = TRUE;
57 // whether SET NAMES must be used to set the character set
60 // --------------------------------------------------------------------
63 * Non-persistent database connection
65 * @access private called by the base class
70 if ($this->port
!= '')
72 return @mysqli_connect($this->hostname
, $this->username
, $this->password
, $this->database
, $this->port
);
76 return @mysqli_connect($this->hostname
, $this->username
, $this->password
, $this->database
);
81 // --------------------------------------------------------------------
84 * Persistent database connection
86 * @access private called by the base class
89 function db_pconnect()
91 return $this->db_connect();
94 // --------------------------------------------------------------------
99 * Keep / reestablish the db connection if no queries have been
100 * sent for a length of time exceeding the server's idle timeout
107 if (mysqli_ping($this->conn_id
) === FALSE)
109 $this->conn_id
= FALSE;
113 // --------------------------------------------------------------------
116 * Select the database
118 * @access private called by the base class
123 return @mysqli_select_db($this->conn_id
, $this->database
);
126 // --------------------------------------------------------------------
129 * Set client character set
136 function _db_set_charset($charset, $collation)
138 if ( ! isset($this->use_set_names
))
140 // mysqli_set_charset() requires MySQL >= 5.0.7, use SET NAMES as fallback
141 $this->use_set_names
= (version_compare(mysqli_get_server_info($this->conn_id
), '5.0.7', '>=')) ? FALSE : TRUE;
144 if ($this->use_set_names
=== TRUE)
146 return @mysqli_query($this->conn_id
, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
150 return @mysqli_set_charset($this->conn_id
, $charset);
154 // --------------------------------------------------------------------
157 * Version number query string
164 return "SELECT version() AS ver";
167 // --------------------------------------------------------------------
172 * @access private called by the base class
173 * @param string an SQL query
176 function _execute($sql)
178 $sql = $this->_prep_query($sql);
179 $result = @mysqli_query($this->conn_id
, $sql);
183 // --------------------------------------------------------------------
188 * If needed, each database adapter can prep the query string
190 * @access private called by execute()
191 * @param string an SQL query
194 function _prep_query($sql)
196 // "DELETE FROM TABLE" returns 0 affected rows This hack modifies
197 // the query so that it returns the number of affected rows
198 if ($this->delete_hack
=== TRUE)
200 if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $sql))
202 $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql);
209 // --------------------------------------------------------------------
217 function trans_begin($test_mode = FALSE)
219 if ( ! $this->trans_enabled
)
224 // When transactions are nested we only begin/commit/rollback the outermost ones
225 if ($this->_trans_depth
> 0)
230 // Reset the transaction failure flag.
231 // If the $test_mode flag is set to TRUE transactions will be rolled back
232 // even if the queries produce a successful result.
233 $this->_trans_failure
= ($test_mode === TRUE) ? TRUE : FALSE;
235 $this->simple_query('SET AUTOCOMMIT=0');
236 $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK
240 // --------------------------------------------------------------------
248 function trans_commit()
250 if ( ! $this->trans_enabled
)
255 // When transactions are nested we only begin/commit/rollback the outermost ones
256 if ($this->_trans_depth
> 0)
261 $this->simple_query('COMMIT');
262 $this->simple_query('SET AUTOCOMMIT=1');
266 // --------------------------------------------------------------------
269 * Rollback Transaction
274 function trans_rollback()
276 if ( ! $this->trans_enabled
)
281 // When transactions are nested we only begin/commit/rollback the outermost ones
282 if ($this->_trans_depth
> 0)
287 $this->simple_query('ROLLBACK');
288 $this->simple_query('SET AUTOCOMMIT=1');
292 // --------------------------------------------------------------------
299 * @param bool whether or not the string will be used in a LIKE condition
302 function escape_str($str, $like = FALSE)
306 foreach ($str as $key => $val)
308 $str[$key] = $this->escape_str($val, $like);
314 if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id
))
316 $str = mysqli_real_escape_string($this->conn_id
, $str);
318 elseif (function_exists('mysql_escape_string'))
320 $str = mysql_escape_string($str);
324 $str = addslashes($str);
327 // escape LIKE condition wildcards
330 $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
336 // --------------------------------------------------------------------
344 function affected_rows()
346 return @mysqli_affected_rows($this->conn_id
);
349 // --------------------------------------------------------------------
359 return @mysqli_insert_id($this->conn_id
);
362 // --------------------------------------------------------------------
367 * Generates a platform-specific query string that counts all records in
368 * the specified database
374 function count_all($table = '')
381 $query = $this->query($this->_count_string
. $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE));
383 if ($query->num_rows() == 0)
388 $row = $query->row();
389 $this->_reset_select();
390 return (int) $row->numrows
;
393 // --------------------------------------------------------------------
398 * Generates a platform-specific query string so that the table names can be fetched
404 function _list_tables($prefix_limit = FALSE)
406 $sql = "SHOW TABLES FROM ".$this->_escape_char
.$this->database
.$this->_escape_char
;
408 if ($prefix_limit !== FALSE AND $this->dbprefix
!= '')
410 $sql .= " LIKE '".$this->escape_like_str($this->dbprefix
)."%'";
416 // --------------------------------------------------------------------
421 * Generates a platform-specific query string so that the column names can be fetched
424 * @param string the table name
427 function _list_columns($table = '')
429 return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE);
432 // --------------------------------------------------------------------
437 * Generates a platform-specific query so that the column data can be retrieved
440 * @param string the table name
443 function _field_data($table)
445 return "DESCRIBE ".$table;
448 // --------------------------------------------------------------------
451 * The error message string
456 function _error_message()
458 return mysqli_error($this->conn_id
);
461 // --------------------------------------------------------------------
464 * The error message number
469 function _error_number()
471 return mysqli_errno($this->conn_id
);
474 // --------------------------------------------------------------------
477 * Escape the SQL Identifiers
479 * This function escapes column and table names
485 function _escape_identifiers($item)
487 if ($this->_escape_char
== '')
492 foreach ($this->_reserved_identifiers
as $id)
494 if (strpos($item, '.'.$id) !== FALSE)
496 $str = $this->_escape_char
. str_replace('.', $this->_escape_char
.'.', $item);
498 // remove duplicates if the user already included the escape
499 return preg_replace('/['.$this->_escape_char
.']+/', $this->_escape_char
, $str);
503 if (strpos($item, '.') !== FALSE)
505 $str = $this->_escape_char
.str_replace('.', $this->_escape_char
.'.'.$this->_escape_char
, $item).$this->_escape_char
;
509 $str = $this->_escape_char
.$item.$this->_escape_char
;
512 // remove duplicates if the user already included the escape
513 return preg_replace('/['.$this->_escape_char
.']+/', $this->_escape_char
, $str);
516 // --------------------------------------------------------------------
521 * This function implicitly groups FROM tables so there is no confusion
522 * about operator precedence in harmony with SQL standards
528 function _from_tables($tables)
530 if ( ! is_array($tables))
532 $tables = array($tables);
535 return '('.implode(', ', $tables).')';
538 // --------------------------------------------------------------------
543 * Generates a platform-specific insert string from the supplied data
546 * @param string the table name
547 * @param array the insert keys
548 * @param array the insert values
551 function _insert($table, $keys, $values)
553 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
556 // --------------------------------------------------------------------
559 * Insert_batch statement
561 * Generates a platform-specific insert string from the supplied data
564 * @param string the table name
565 * @param array the insert keys
566 * @param array the insert values
569 function _insert_batch($table, $keys, $values)
571 return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
574 // --------------------------------------------------------------------
580 * Generates a platform-specific replace string from the supplied data
583 * @param string the table name
584 * @param array the insert keys
585 * @param array the insert values
588 function _replace($table, $keys, $values)
590 return "REPLACE INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
593 // --------------------------------------------------------------------
598 * Generates a platform-specific update string from the supplied data
601 * @param string the table name
602 * @param array the update data
603 * @param array the where clause
604 * @param array the orderby clause
605 * @param array the limit clause
608 function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
610 foreach ($values as $key => $val)
612 $valstr[] = $key." = ".$val;
615 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
617 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
619 $sql = "UPDATE ".$table." SET ".implode(', ', $valstr);
621 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
623 $sql .= $orderby.$limit;
628 // --------------------------------------------------------------------
631 * Update_Batch statement
633 * Generates a platform-specific batch update string from the supplied data
636 * @param string the table name
637 * @param array the update data
638 * @param array the where clause
641 function _update_batch($table, $values, $index, $where = NULL)
644 $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : '';
646 foreach ($values as $key => $val)
648 $ids[] = $val[$index];
650 foreach (array_keys($val) as $field)
652 if ($field != $index)
654 $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field];
659 $sql = "UPDATE ".$table." SET ";
662 foreach ($final as $k => $v)
664 $cases .= $k.' = CASE '."\n";
670 $cases .= 'ELSE '.$k.' END, ';
673 $sql .= substr($cases, 0, -2);
675 $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')';
680 // --------------------------------------------------------------------
685 * Generates a platform-specific truncate string from the supplied data
686 * If the database does not support the truncate() command
687 * This function maps to "DELETE FROM table"
690 * @param string the table name
693 function _truncate($table)
695 return "TRUNCATE ".$table;
698 // --------------------------------------------------------------------
703 * Generates a platform-specific delete string from the supplied data
706 * @param string the table name
707 * @param array the where clause
708 * @param string the limit clause
711 function _delete($table, $where = array(), $like = array(), $limit = FALSE)
715 if (count($where) > 0 OR count($like) > 0)
717 $conditions = "\nWHERE ";
718 $conditions .= implode("\n", $this->ar_where
);
720 if (count($where) > 0 && count($like) > 0)
722 $conditions .= " AND ";
724 $conditions .= implode("\n", $like);
727 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
729 return "DELETE FROM ".$table.$conditions.$limit;
732 // --------------------------------------------------------------------
737 * Generates a platform-specific LIMIT clause
740 * @param string the sql query string
741 * @param integer the number of rows to limit the query to
742 * @param integer the offset value
745 function _limit($sql, $limit, $offset)
747 $sql .= "LIMIT ".$limit;
751 $sql .= " OFFSET ".$offset;
757 // --------------------------------------------------------------------
760 * Close DB Connection
766 function _close($conn_id)
768 @mysqli_close($conn_id);
775 /* End of file mysqli_driver.php */
776 /* Location: ./system/database/drivers/mysqli/mysqli_driver.php */