summaryrefslogtreecommitdiffstats
path: root/system/database/drivers/mysqli
diff options
context:
space:
mode:
authorJamie Rumbelow <jamie@jamierumbelow.net>2012-04-26 14:27:35 +0200
committerJamie Rumbelow <jamie@jamierumbelow.net>2012-04-26 14:27:35 +0200
commitd6ce1e936d60487ad8ddd099a040fba7d8f88367 (patch)
tree31dcdda9a24e2ed12135659a4e38cb4f35b0af31 /system/database/drivers/mysqli
parentbcee50ff3247dee71d83bf273e52bc10096cd48c (diff)
parent9e2d5d130eff40592b49337a8ba4d8c170934de1 (diff)
Merge branch 'develop' of git://github.com/EllisLab/CodeIgniter into develop
Conflicts: system/core/Loader.php system/database/DB_query_builder.php system/database/drivers/cubrid/cubrid_driver.php system/database/drivers/mssql/mssql_driver.php system/database/drivers/mysql/mysql_driver.php system/database/drivers/mysqli/mysqli_driver.php system/database/drivers/oci8/oci8_driver.php system/database/drivers/odbc/odbc_driver.php system/database/drivers/pdo/pdo_driver.php system/database/drivers/postgre/postgre_driver.php system/database/drivers/sqlite/sqlite_driver.php user_guide_src/source/changelog.rst user_guide_src/source/database/query_builder.rst
Diffstat (limited to 'system/database/drivers/mysqli')
-rw-r--r--system/database/drivers/mysqli/mysqli_driver.php210
-rw-r--r--system/database/drivers/mysqli/mysqli_forge.php95
-rw-r--r--system/database/drivers/mysqli/mysqli_result.php24
-rw-r--r--system/database/drivers/mysqli/mysqli_utility.php50
4 files changed, 68 insertions, 311 deletions
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php
index 677da1389..e2684e4f2 100644
--- a/system/database/drivers/mysqli/mysqli_driver.php
+++ b/system/database/drivers/mysqli/mysqli_driver.php
@@ -2,7 +2,7 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
*
@@ -72,8 +72,8 @@ class CI_DB_mysqli_driver extends CI_DB {
public function db_connect()
{
return ($this->port != '')
- ? @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port)
- : @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
+ ? @new mysqli($this->hostname, $this->username, $this->password, $this->database, $this->port)
+ : @new mysqli($this->hostname, $this->username, $this->password, $this->database);
}
// --------------------------------------------------------------------
@@ -92,8 +92,8 @@ class CI_DB_mysqli_driver extends CI_DB {
}
return ($this->port != '')
- ? @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port)
- : @mysqli_connect('p:'.$this->hostname, $this->username, $this->password, $this->database);
+ ? @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database, $this->port)
+ : @new mysqli('p:'.$this->hostname, $this->username, $this->password, $this->database);
}
// --------------------------------------------------------------------
@@ -108,7 +108,7 @@ class CI_DB_mysqli_driver extends CI_DB {
*/
public function reconnect()
{
- if (mysqli_ping($this->conn_id) === FALSE)
+ if ($this->conn_id !== FALSE && $this->conn_id->ping() === FALSE)
{
$this->conn_id = FALSE;
}
@@ -129,7 +129,7 @@ class CI_DB_mysqli_driver extends CI_DB {
$database = $this->database;
}
- if (@mysqli_select_db($this->conn_id, $database))
+ if (@$this->conn_id->select_db($database))
{
$this->database = $database;
return TRUE;
@@ -144,14 +144,11 @@ class CI_DB_mysqli_driver extends CI_DB {
* Set client character set
*
* @param string
- * @param string
* @return bool
*/
- protected function _db_set_charset($charset, $collation)
+ protected function _db_set_charset($charset)
{
- return function_exists('mysqli_set_charset')
- ? @mysqli_set_charset($this->conn_id, $charset)
- : @mysqli_query($this->conn_id, "SET NAMES '".$this->escape_str($charset)."' COLLATE '".$this->escape_str($collation)."'");
+ return @$this->conn_id->set_charset($charset);
}
// --------------------------------------------------------------------
@@ -165,7 +162,7 @@ class CI_DB_mysqli_driver extends CI_DB {
{
return isset($this->data_cache['version'])
? $this->data_cache['version']
- : $this->data_cache['version'] = @mysqli_get_server_info($this->conn_id);
+ : $this->data_cache['version'] = $this->conn_id->server_info;
}
// --------------------------------------------------------------------
@@ -178,7 +175,7 @@ class CI_DB_mysqli_driver extends CI_DB {
*/
protected function _execute($sql)
{
- return @mysqli_query($this->conn_id, $this->_prep_query($sql));
+ return @$this->conn_id->query($this->_prep_query($sql));
}
// --------------------------------------------------------------------
@@ -289,18 +286,7 @@ class CI_DB_mysqli_driver extends CI_DB {
return $str;
}
- if (function_exists('mysqli_real_escape_string') && is_object($this->conn_id))
- {
- $str = mysqli_real_escape_string($this->conn_id, $str);
- }
- elseif (function_exists('mysql_escape_string'))
- {
- $str = mysql_escape_string($str);
- }
- else
- {
- $str = addslashes($str);
- }
+ $str = is_object($this->conn_id) ? $this->conn_id->real_escape_string($str) : addslashes($str);
// escape LIKE condition wildcards
if ($like === TRUE)
@@ -320,7 +306,7 @@ class CI_DB_mysqli_driver extends CI_DB {
*/
public function affected_rows()
{
- return @mysqli_affected_rows($this->conn_id);
+ return $this->conn_id->affected_rows;
}
// --------------------------------------------------------------------
@@ -332,7 +318,7 @@ class CI_DB_mysqli_driver extends CI_DB {
*/
public function insert_id()
{
- return @mysqli_insert_id($this->conn_id);
+ return $this->conn_id->insert_id;
}
// --------------------------------------------------------------------
@@ -371,7 +357,6 @@ class CI_DB_mysqli_driver extends CI_DB {
*
* Generates a platform-specific query string so that the table names can be fetched
*
- * @access private
* @param bool
* @return string
*/
@@ -448,44 +433,7 @@ class CI_DB_mysqli_driver extends CI_DB {
*/
public function error()
{
- return array('code' => mysqli_errno($this->conn_id), 'message' => mysqli_error($this->conn_id));
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Escape the SQL Identifiers
- *
- * This function escapes column and table names
- *
- * @param string
- * @return string
- */
- public function _escape_identifiers($item)
- {
- if ($this->_escape_char == '')
- {
- return $item;
- }
-
- foreach ($this->_reserved_identifiers as $id)
- {
- if (strpos($item, '.'.$id) !== FALSE)
- {
- $item = str_replace('.', $this->_escape_char.'.', $item);
-
- // remove duplicates if the user already included the escape
- return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item);
- }
- }
-
- if (strpos($item, '.') !== FALSE)
- {
- $item = str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item);
- }
-
- // remove duplicates if the user already included the escape
- return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $this->_escape_char.$item.$this->_escape_char);
+ return array('code' => $this->conn_id->errno, 'message' => $this->conn_id->error);
}
// --------------------------------------------------------------------
@@ -512,85 +460,6 @@ class CI_DB_mysqli_driver extends CI_DB {
// --------------------------------------------------------------------
/**
- * Insert statement
- *
- * Generates a platform-specific insert string from the supplied data
- *
- * @param string the table name
- * @param array the insert keys
- * @param array the insert values
- * @return string
- */
- protected function _insert($table, $keys, $values)
- {
- return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Insert_batch statement
- *
- * Generates a platform-specific insert string from the supplied data
- *
- * @param string the table name
- * @param array the insert keys
- * @param array the insert values
- * @return string
- */
- protected function _insert_batch($table, $keys, $values)
- {
- return 'INSERT INTO '.$table.' ('.implode(', ', $keys).') VALUES '.implode(', ', $values);
- }
-
- // --------------------------------------------------------------------
-
-
- /**
- * Replace statement
- *
- * Generates a platform-specific replace string from the supplied data
- *
- * @param string the table name
- * @param array the insert keys
- * @param array the insert values
- * @return string
- */
- protected function _replace($table, $keys, $values)
- {
- return 'REPLACE INTO '.$table.' ('.implode(', ', $keys).') VALUES ('.implode(', ', $values).')';
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Update statement
- *
- * Generates a platform-specific update string from the supplied data
- *
- * @param string the table name
- * @param array the update data
- * @param array the where clause
- * @param array the orderby clause
- * @param array the limit clause
- * @return string
- */
- protected function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
- {
- foreach ($values as $key => $val)
- {
- $valstr[] = $key.' = '.$val;
- }
-
- return 'UPDATE '.$table.' SET '.implode(', ', $valstr)
- .(($where != '' && count($where) > 0) ? ' WHERE '.implode(' ', $where) : '')
- .(count($orderby) > 0 ? ' ORDER BY '.implode(', ', $orderby) : '')
- .( ! $limit ? '' : ' LIMIT '.$limit);
- }
-
- // --------------------------------------------------------------------
-
- /**
* Update_Batch statement
*
* Generates a platform-specific batch update string from the supplied data
@@ -634,52 +503,6 @@ class CI_DB_mysqli_driver extends CI_DB {
// --------------------------------------------------------------------
/**
- * Truncate statement
- *
- * Generates a platform-specific truncate string from the supplied data
- * If the database does not support the truncate() command
- * This function maps to "DELETE FROM table"
- *
- * @param string the table name
- * @return string
- */
- protected function _truncate($table)
- {
- return 'TRUNCATE '.$table;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Delete statement
- *
- * Generates a platform-specific delete string from the supplied data
- *
- * @param string the table name
- * @param array the where clause
- * @param string the limit clause
- * @return string
- */
- protected function _delete($table, $where = array(), $like = array(), $limit = FALSE)
- {
- $conditions = '';
- if (count($where) > 0 OR count($like) > 0)
- {
- $conditions = "\nWHERE ".implode("\n", $this->qb_where);
-
- if (count($where) > 0 && count($like) > 0)
- {
- $conditions .= ' AND ';
- }
- $conditions .= implode("\n", $like);
- }
-
- return 'DELETE FROM '.$table.$conditions.( ! $limit ? '' : ' LIMIT '.$limit);
- }
-
- // --------------------------------------------------------------------
-
- /**
* Limit string
*
* Generates a platform-specific LIMIT clause
@@ -705,7 +528,8 @@ class CI_DB_mysqli_driver extends CI_DB {
*/
protected function _close($conn_id)
{
- @mysqli_close($conn_id);
+ $this->conn_id->close();
+ $this->conn_id = FALSE;
}
}
diff --git a/system/database/drivers/mysqli/mysqli_forge.php b/system/database/drivers/mysqli/mysqli_forge.php
index 744525f02..503574dfc 100644
--- a/system/database/drivers/mysqli/mysqli_forge.php
+++ b/system/database/drivers/mysqli/mysqli_forge.php
@@ -2,7 +2,7 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
*
@@ -34,31 +34,7 @@
*/
class CI_DB_mysqli_forge extends CI_DB_forge {
- /**
- * Create database
- *
- * @param string the database name
- * @return string
- */
- public function _create_database($name)
- {
- return 'CREATE DATABASE '.$name;
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Drop database
- *
- * @param string the database name
- * @return string
- */
- public function _drop_database($name)
- {
- return 'DROP DATABASE '.$name;
- }
-
- // --------------------------------------------------------------------
+ protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s';
/**
* Process Fields
@@ -66,7 +42,7 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
* @param mixed the fields
* @return string
*/
- public function _process_fields($fields)
+ protected function _process_fields($fields)
{
$current_field_count = 0;
$sql = '';
@@ -86,9 +62,32 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
$sql .= "\n\t".$this->db->protect_identifiers($field)
.( ! empty($attributes['NAME']) ? ' '.$this->db->protect_identifiers($attributes['NAME']).' ' : '')
- .( ! empty($attributes['TYPE']) ? ' '.$attributes['TYPE'] : '')
- .( ! empty($attributes['CONSTRAINT']) ? '('.$attributes['CONSTRAINT'].')' : '')
- .(( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '')
+ ;
+
+ if ( ! empty($attributes['TYPE']))
+ {
+ $sql .= ' '.$attributes['TYPE'];
+
+ if ( ! empty($attributes['CONSTRAINT']))
+ {
+ switch (strtolower($attributes['TYPE']))
+ {
+ case 'decimal':
+ case 'float':
+ case 'numeric':
+ $sql .= '('.implode(',', $attributes['CONSTRAINT']).')';
+ break;
+ case 'enum':
+ case 'set':
+ $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")';
+ break;
+ default:
+ $sql .= '('.$attributes['CONSTRAINT'].')';
+ }
+ }
+ }
+
+ $sql .= (( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) ? ' UNSIGNED' : '')
.(isset($attributes['DEFAULT']) ? " DEFAULT '".$attributes['DEFAULT']."'" : '')
.(( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL')
.(( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) ? ' AUTO_INCREMENT' : '');
@@ -116,7 +115,7 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
* @param bool should 'IF NOT EXISTS' be added to the SQL
* @return bool
*/
- public function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
+ protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists)
{
$sql = 'CREATE TABLE ';
@@ -125,7 +124,7 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
$sql .= 'IF NOT EXISTS ';
}
- $sql .= $this->db->_escape_identifiers($table).' ('.$this->_process_fields($fields);
+ $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields);
if (count($primary_keys) > 0)
{
@@ -158,18 +157,6 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
// --------------------------------------------------------------------
/**
- * Drop Table
- *
- * @return string
- */
- public function _drop_table($table)
- {
- return 'DROP TABLE IF EXISTS '.$this->db->_escape_identifiers($table);
- }
-
- // --------------------------------------------------------------------
-
- /**
* Alter table query
*
* Generates a platform-specific query so that a table can be altered
@@ -181,7 +168,7 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
* @param string the field after which we should add the new field
* @return string
*/
- public function _alter_table($alter_type, $table, $fields, $after_field = '')
+ protected function _alter_table($alter_type, $table, $fields, $after_field = '')
{
$sql = 'ALTER TABLE '.$this->db->protect_identifiers($table).' '.$alter_type.' ';
@@ -195,23 +182,7 @@ class CI_DB_mysqli_forge extends CI_DB_forge {
.($after_field != '' ? ' AFTER '.$this->db->protect_identifiers($after_field) : '');
}
- // --------------------------------------------------------------------
-
- /**
- * Rename a table
- *
- * Generates a platform-specific query so that a table can be renamed
- *
- * @param string the old table name
- * @param string the new table name
- * @return string
- */
- public function _rename_table($table_name, $new_table_name)
- {
- return 'ALTER TABLE '.$this->db->protect_identifiers($table_name).' RENAME TO '.$this->db->protect_identifiers($new_table_name);
- }
-
}
/* End of file mysqli_forge.php */
-/* Location: ./system/database/drivers/mysqli/mysqli_forge.php */
+/* Location: ./system/database/drivers/mysqli/mysqli_forge.php */ \ No newline at end of file
diff --git a/system/database/drivers/mysqli/mysqli_result.php b/system/database/drivers/mysqli/mysqli_result.php
index 8b909cc56..9b4d494d4 100644
--- a/system/database/drivers/mysqli/mysqli_result.php
+++ b/system/database/drivers/mysqli/mysqli_result.php
@@ -2,7 +2,7 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
*
@@ -43,7 +43,7 @@ class CI_DB_mysqli_result extends CI_DB_result {
*/
public function num_rows()
{
- return @mysqli_num_rows($this->result_id);
+ return $this->result_id->num_rows;
}
// --------------------------------------------------------------------
@@ -55,7 +55,7 @@ class CI_DB_mysqli_result extends CI_DB_result {
*/
public function num_fields()
{
- return @mysqli_num_fields($this->result_id);
+ return $this->result_id->field_count;
}
// --------------------------------------------------------------------
@@ -70,7 +70,7 @@ class CI_DB_mysqli_result extends CI_DB_result {
public function list_fields()
{
$field_names = array();
- while ($field = mysqli_fetch_field($this->result_id))
+ while ($field = $this->result_id->fetch_field())
{
$field_names[] = $field->name;
}
@@ -90,7 +90,7 @@ class CI_DB_mysqli_result extends CI_DB_result {
public function field_data()
{
$retval = array();
- $field_data = mysqli_fetch_fields($this->result_id);
+ $field_data = $this->result_id->fetch_fields();
for ($i = 0, $c = count($field_data); $i < $c; $i++)
{
$retval[$i] = new stdClass();
@@ -115,7 +115,7 @@ class CI_DB_mysqli_result extends CI_DB_result {
{
if (is_object($this->result_id))
{
- mysqli_free_result($this->result_id);
+ $this->result_id->free();
$this->result_id = FALSE;
}
}
@@ -125,15 +125,15 @@ class CI_DB_mysqli_result extends CI_DB_result {
/**
* Data Seek
*
- * Moves the internal pointer to the desired offset. We call
+ * Moves the internal pointer to the desired offset. We call
* this internally before fetching results to make sure the
* result set starts at zero
*
- * @return array
+ * @return bool
*/
protected function _data_seek($n = 0)
{
- return mysqli_data_seek($this->result_id, $n);
+ return $this->result_id->data_seek($n);
}
// --------------------------------------------------------------------
@@ -147,7 +147,7 @@ class CI_DB_mysqli_result extends CI_DB_result {
*/
protected function _fetch_assoc()
{
- return mysqli_fetch_assoc($this->result_id);
+ return $this->result_id->fetch_assoc();
}
// --------------------------------------------------------------------
@@ -161,10 +161,10 @@ class CI_DB_mysqli_result extends CI_DB_result {
*/
protected function _fetch_object()
{
- return mysqli_fetch_object($this->result_id);
+ return $this->result_id->fetch_object();
}
}
/* End of file mysqli_result.php */
-/* Location: ./system/database/drivers/mysqli/mysqli_result.php */
+/* Location: ./system/database/drivers/mysqli/mysqli_result.php */ \ No newline at end of file
diff --git a/system/database/drivers/mysqli/mysqli_utility.php b/system/database/drivers/mysqli/mysqli_utility.php
index 3fdc5c723..27d4ef817 100644
--- a/system/database/drivers/mysqli/mysqli_utility.php
+++ b/system/database/drivers/mysqli/mysqli_utility.php
@@ -2,7 +2,7 @@
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP 5.2.4 or newer
*
* NOTICE OF LICENSE
*
@@ -34,47 +34,9 @@
*/
class CI_DB_mysqli_utility extends CI_DB_utility {
- /**
- * List databases
- *
- * @return string
- */
- public function _list_databases()
- {
- return 'SHOW DATABASES';
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Optimize table query
- *
- * Generates a platform-specific query so that a table can be optimized
- *
- * @param string the table name
- * @return string
- */
- public function _optimize_table($table)
- {
- return 'OPTIMIZE TABLE '.$this->db->_escape_identifiers($table);
- }
-
- // --------------------------------------------------------------------
-
- /**
- * Repair table query
- *
- * Generates a platform-specific query so that a table can be repaired
- *
- * @param string the table name
- * @return string
- */
- public function _repair_table($table)
- {
- return 'REPAIR TABLE '.$this->db->_escape_identifiers($table);
- }
-
- // --------------------------------------------------------------------
+ protected $_list_databases = 'SHOW DATABASES';
+ protected $_optimize_table = 'OPTIMIZE TABLE %s';
+ protected $_repair_table = 'REPAIR TABLE %s';
/**
* MySQLi Export
@@ -82,7 +44,7 @@ class CI_DB_mysqli_utility extends CI_DB_utility {
* @param array Preferences
* @return mixed
*/
- public function _backup($params = array())
+ protected function _backup($params = array())
{
// Currently unsupported
return $this->db->display_error('db_unsuported_feature');
@@ -90,4 +52,4 @@ class CI_DB_mysqli_utility extends CI_DB_utility {
}
/* End of file mysqli_utility.php */
-/* Location: ./system/database/drivers/mysqli/mysqli_utility.php */
+/* Location: ./system/database/drivers/mysqli/mysqli_utility.php */ \ No newline at end of file