From ce2b69675075444c9e40b72bcdd42ab7edbbe633 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Fri, 28 Jan 2011 22:51:06 +0100 Subject: update to CI 2.0 Signed-off-by: Florian Pritz --- system/database/drivers/mysqli/index.html | 0 system/database/drivers/mysqli/mysqli_driver.php | 193 +++++++++++++++------- system/database/drivers/mysqli/mysqli_forge.php | 34 ++-- system/database/drivers/mysqli/mysqli_result.php | 28 ++-- system/database/drivers/mysqli/mysqli_utility.php | 44 +---- 5 files changed, 166 insertions(+), 133 deletions(-) mode change 100644 => 100755 system/database/drivers/mysqli/index.html mode change 100644 => 100755 system/database/drivers/mysqli/mysqli_driver.php mode change 100644 => 100755 system/database/drivers/mysqli/mysqli_forge.php mode change 100644 => 100755 system/database/drivers/mysqli/mysqli_result.php mode change 100644 => 100755 system/database/drivers/mysqli/mysqli_utility.php (limited to 'system/database/drivers/mysqli') diff --git a/system/database/drivers/mysqli/index.html b/system/database/drivers/mysqli/index.html old mode 100644 new mode 100755 diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php old mode 100644 new mode 100755 index 5d7200fbd..8942100d4 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -2,11 +2,11 @@ /** * CodeIgniter * - * An open source application development framework for PHP 4.3.2 or newer + * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 @@ -31,7 +31,7 @@ class CI_DB_mysqli_driver extends CI_DB { var $dbdriver = 'mysqli'; - + // The character used for escaping var $_escape_char = '`'; @@ -51,7 +51,7 @@ class CI_DB_mysqli_driver extends CI_DB { * Whether to use the MySQL "delete hack" which allows the number * of affected rows to be shown. Uses a preg_replace when enabled, * adding a bit more processing to all queries. - */ + */ var $delete_hack = TRUE; // -------------------------------------------------------------------- @@ -61,12 +61,12 @@ class CI_DB_mysqli_driver extends CI_DB { * * @access private called by the base class * @return resource - */ + */ function db_connect() { if ($this->port != '') { - return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port); + return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port); } else { @@ -82,12 +82,12 @@ class CI_DB_mysqli_driver extends CI_DB { * * @access private called by the base class * @return resource - */ + */ function db_pconnect() { return $this->db_connect(); } - + // -------------------------------------------------------------------- /** @@ -114,7 +114,7 @@ class CI_DB_mysqli_driver extends CI_DB { * * @access private called by the base class * @return resource - */ + */ function db_select() { return @mysqli_select_db($this->conn_id, $this->database); @@ -136,7 +136,7 @@ class CI_DB_mysqli_driver extends CI_DB { } // -------------------------------------------------------------------- - + /** * Version number query string * @@ -156,14 +156,14 @@ class CI_DB_mysqli_driver extends CI_DB { * @access private called by the base class * @param string an SQL query * @return resource - */ + */ function _execute($sql) { - $sql = $this->_prep_query($sql); + $sql = $this->_prep_query($sql); $result = @mysqli_query($this->conn_id, $sql); return $result; } - + // -------------------------------------------------------------------- /** @@ -174,7 +174,7 @@ class CI_DB_mysqli_driver extends CI_DB { * @access private called by execute() * @param string an SQL query * @return string - */ + */ function _prep_query($sql) { // "DELETE FROM TABLE" returns 0 affected rows This hack modifies @@ -186,7 +186,7 @@ class CI_DB_mysqli_driver extends CI_DB { $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql); } } - + return $sql; } @@ -196,15 +196,15 @@ class CI_DB_mysqli_driver extends CI_DB { * Begin Transaction * * @access public - * @return bool - */ + * @return bool + */ function trans_begin($test_mode = FALSE) { if ( ! $this->trans_enabled) { return TRUE; } - + // When transactions are nested we only begin/commit/rollback the outermost ones if ($this->_trans_depth > 0) { @@ -227,8 +227,8 @@ class CI_DB_mysqli_driver extends CI_DB { * Commit Transaction * * @access public - * @return bool - */ + * @return bool + */ function trans_commit() { if ( ! $this->trans_enabled) @@ -253,8 +253,8 @@ class CI_DB_mysqli_driver extends CI_DB { * Rollback Transaction * * @access public - * @return bool - */ + * @return bool + */ function trans_rollback() { if ( ! $this->trans_enabled) @@ -283,17 +283,17 @@ class CI_DB_mysqli_driver extends CI_DB { * @param bool whether or not the string will be used in a LIKE condition * @return string */ - function escape_str($str, $like = FALSE) + function escape_str($str, $like = FALSE) { if (is_array($str)) { foreach($str as $key => $val) - { + { $str[$key] = $this->escape_str($val, $like); - } - - return $str; - } + } + + return $str; + } if (function_exists('mysqli_real_escape_string') AND is_object($this->conn_id)) { @@ -307,16 +307,16 @@ class CI_DB_mysqli_driver extends CI_DB { { $str = addslashes($str); } - + // escape LIKE condition wildcards if ($like === TRUE) { $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); } - + return $str; } - + // -------------------------------------------------------------------- /** @@ -329,7 +329,7 @@ class CI_DB_mysqli_driver extends CI_DB { { return @mysqli_affected_rows($this->conn_id); } - + // -------------------------------------------------------------------- /** @@ -386,13 +386,13 @@ class CI_DB_mysqli_driver extends CI_DB { */ function _list_tables($prefix_limit = FALSE) { - $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char; - + $sql = "SHOW TABLES FROM ".$this->_escape_char.$this->database.$this->_escape_char; + if ($prefix_limit !== FALSE AND $this->dbprefix != '') { $sql .= " LIKE '".$this->escape_like_str($this->dbprefix)."%'"; } - + return $sql; } @@ -409,7 +409,7 @@ class CI_DB_mysqli_driver extends CI_DB { */ function _list_columns($table = '') { - return "SHOW COLUMNS FROM ".$table; + return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE); } // -------------------------------------------------------------------- @@ -440,7 +440,7 @@ class CI_DB_mysqli_driver extends CI_DB { { return mysqli_error($this->conn_id); } - + // -------------------------------------------------------------------- /** @@ -471,31 +471,31 @@ class CI_DB_mysqli_driver extends CI_DB { { return $item; } - + foreach ($this->_reserved_identifiers as $id) { if (strpos($item, '.'.$id) !== FALSE) { - $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); - + $str = $this->_escape_char. str_replace('.', $this->_escape_char.'.', $item); + // remove duplicates if the user already included the escape return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); - } + } } - + if (strpos($item, '.') !== FALSE) { - $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; + $str = $this->_escape_char.str_replace('.', $this->_escape_char.'.'.$this->_escape_char, $item).$this->_escape_char; } else { $str = $this->_escape_char.$item.$this->_escape_char; } - + // remove duplicates if the user already included the escape return preg_replace('/['.$this->_escape_char.']+/', $this->_escape_char, $str); } - + // -------------------------------------------------------------------- /** @@ -514,12 +514,12 @@ class CI_DB_mysqli_driver extends CI_DB { { $tables = array($tables); } - + return '('.implode(', ', $tables).')'; } // -------------------------------------------------------------------- - + /** * Insert statement * @@ -532,9 +532,27 @@ class CI_DB_mysqli_driver extends CI_DB { * @return string */ 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 + * + * @access public + * @param string the table name + * @param array the insert keys + * @param array the insert values + * @return string + */ + function _insert_batch($table, $keys, $values) + { + return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); + } // -------------------------------------------------------------------- @@ -557,21 +575,72 @@ class CI_DB_mysqli_driver extends CI_DB { { $valstr[] = $key." = ".$val; } - + $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - + $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):''; - + $sql = "UPDATE ".$table." SET ".implode(', ', $valstr); - + $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : ''; - + $sql .= $orderby.$limit; - + + return $sql; + } + + // -------------------------------------------------------------------- + + /** + * Update_Batch statement + * + * Generates a platform-specific batch update string from the supplied data + * + * @access public + * @param string the table name + * @param array the update data + * @param array the where clause + * @return string + */ + function _update_batch($table, $values, $index, $where = NULL) + { + $ids = array(); + $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; + + foreach($values as $key => $val) + { + $ids[] = $val[$index]; + + foreach(array_keys($val) as $field) + { + if ($field != $index) + { + $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; + } + } + } + + $sql = "UPDATE ".$table." SET "; + $cases = ''; + + foreach($final as $k => $v) + { + $cases .= $k.' = CASE '."\n"; + foreach ($v as $row) + { + $cases .= $row."\n"; + } + + $cases .= 'ELSE '.$k.' END, '; + } + + $sql .= substr($cases, 0, -2); + + $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; + return $sql; } - // -------------------------------------------------------------------- /** @@ -584,12 +653,12 @@ class CI_DB_mysqli_driver extends CI_DB { * @access public * @param string the table name * @return string - */ + */ function _truncate($table) { return "TRUNCATE ".$table; } - + // -------------------------------------------------------------------- /** @@ -602,7 +671,7 @@ class CI_DB_mysqli_driver extends CI_DB { * @param array the where clause * @param string the limit clause * @return string - */ + */ function _delete($table, $where = array(), $like = array(), $limit = FALSE) { $conditions = ''; @@ -620,7 +689,7 @@ class CI_DB_mysqli_driver extends CI_DB { } $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - + return "DELETE FROM ".$table.$conditions.$limit; } @@ -638,14 +707,14 @@ class CI_DB_mysqli_driver extends CI_DB { * @return string */ function _limit($sql, $limit, $offset) - { + { $sql .= "LIMIT ".$limit; - + if ($offset > 0) { $sql .= " OFFSET ".$offset; } - + return $sql; } diff --git a/system/database/drivers/mysqli/mysqli_forge.php b/system/database/drivers/mysqli/mysqli_forge.php old mode 100644 new mode 100755 index 262d491ed..d5097335e --- a/system/database/drivers/mysqli/mysqli_forge.php +++ b/system/database/drivers/mysqli/mysqli_forge.php @@ -2,11 +2,11 @@ /** * CodeIgniter * - * An open source application development framework for PHP 4.3.2 or newer + * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 @@ -23,7 +23,7 @@ * @link http://codeigniter.com/user_guide/database/ */ class CI_DB_mysqli_forge extends CI_DB_forge { - + /** * Create database * @@ -76,52 +76,52 @@ class CI_DB_mysqli_forge extends CI_DB_forge { else { $attributes = array_change_key_case($attributes, CASE_UPPER); - + $sql .= "\n\t".$this->db->_protect_identifiers($field); if (array_key_exists('NAME', $attributes)) { $sql .= ' '.$this->db->_protect_identifiers($attributes['NAME']).' '; } - + if (array_key_exists('TYPE', $attributes)) { $sql .= ' '.$attributes['TYPE']; } - + if (array_key_exists('CONSTRAINT', $attributes)) { $sql .= '('.$attributes['CONSTRAINT'].')'; } - + if (array_key_exists('UNSIGNED', $attributes) && $attributes['UNSIGNED'] === TRUE) { $sql .= ' UNSIGNED'; } - + if (array_key_exists('DEFAULT', $attributes)) { $sql .= ' DEFAULT \''.$attributes['DEFAULT'].'\''; } - + if (array_key_exists('NULL', $attributes)) { $sql .= ($attributes['NULL'] === TRUE) ? ' NULL' : ' NOT NULL'; } - + if (array_key_exists('AUTO_INCREMENT', $attributes) && $attributes['AUTO_INCREMENT'] === TRUE) { $sql .= ' AUTO_INCREMENT'; } } - + // don't add a comma on the end of the last field if (++$current_field_count < count($fields)) { $sql .= ','; } } - + return $sql; } @@ -141,12 +141,12 @@ class CI_DB_mysqli_forge extends CI_DB_forge { function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) { $sql = 'CREATE TABLE '; - + if ($if_not_exists === TRUE) { $sql .= 'IF NOT EXISTS '; } - + $sql .= $this->db->_escape_identifiers($table)." ("; $sql .= $this->_process_fields($fields); @@ -165,14 +165,14 @@ class CI_DB_mysqli_forge extends CI_DB_forge { if (is_array($key)) { $key_name = $this->db->_protect_identifiers(implode('_', $key)); - $key = $this->db->_protect_identifiers($key); + $key = $this->db->_protect_identifiers($key); } else { $key_name = $this->db->_protect_identifiers($key); $key = array($key_name); } - + $sql .= ",\n\tKEY {$key_name} (" . implode(', ', $key) . ")"; } } @@ -226,7 +226,7 @@ class CI_DB_mysqli_forge extends CI_DB_forge { { $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); } - + return $sql; } diff --git a/system/database/drivers/mysqli/mysqli_result.php b/system/database/drivers/mysqli/mysqli_result.php old mode 100644 new mode 100755 index 81d22cc9c..c4d8f5d58 --- a/system/database/drivers/mysqli/mysqli_result.php +++ b/system/database/drivers/mysqli/mysqli_result.php @@ -2,11 +2,11 @@ /** * CodeIgniter * - * An open source application development framework for PHP 4.3.2 or newer + * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 @@ -25,7 +25,7 @@ * @link http://codeigniter.com/user_guide/database/ */ class CI_DB_mysqli_result extends CI_DB_result { - + /** * Number of rows in the result set * @@ -36,7 +36,7 @@ class CI_DB_mysqli_result extends CI_DB_result { { return @mysqli_num_rows($this->result_id); } - + // -------------------------------------------------------------------- /** @@ -67,7 +67,7 @@ class CI_DB_mysqli_result extends CI_DB_result { { $field_names[] = $field->name; } - + return $field_names; } @@ -85,17 +85,17 @@ class CI_DB_mysqli_result extends CI_DB_result { { $retval = array(); while ($field = mysqli_fetch_field($this->result_id)) - { - $F = new stdClass(); - $F->name = $field->name; - $F->type = $field->type; + { + $F = new stdClass(); + $F->name = $field->name; + $F->type = $field->type; $F->default = $field->def; $F->max_length = $field->max_length; $F->primary_key = ($field->flags & MYSQLI_PRI_KEY_FLAG) ? 1 : 0; - + $retval[] = $F; } - + return $retval; } @@ -105,7 +105,7 @@ class CI_DB_mysqli_result extends CI_DB_result { * Free the result * * @return null - */ + */ function free_result() { if (is_object($this->result_id)) @@ -146,7 +146,7 @@ class CI_DB_mysqli_result extends CI_DB_result { { return mysqli_fetch_assoc($this->result_id); } - + // -------------------------------------------------------------------- /** @@ -161,7 +161,7 @@ class CI_DB_mysqli_result extends CI_DB_result { { return mysqli_fetch_object($this->result_id); } - + } diff --git a/system/database/drivers/mysqli/mysqli_utility.php b/system/database/drivers/mysqli/mysqli_utility.php old mode 100644 new mode 100755 index 44fd0f7aa..e17889b8c --- a/system/database/drivers/mysqli/mysqli_utility.php +++ b/system/database/drivers/mysqli/mysqli_utility.php @@ -2,11 +2,11 @@ /** * CodeIgniter * - * An open source application development framework for PHP 4.3.2 or newer + * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team - * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. + * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 @@ -23,7 +23,7 @@ * @link http://codeigniter.com/user_guide/database/ */ class CI_DB_mysqli_utility extends CI_DB_utility { - + /** * List databases * @@ -34,7 +34,7 @@ class CI_DB_mysqli_utility extends CI_DB_utility { { return "SHOW DATABASES"; } - + // -------------------------------------------------------------------- /** @@ -81,42 +81,6 @@ class CI_DB_mysqli_utility extends CI_DB_utility { // Currently unsupported return $this->db->display_error('db_unsuported_feature'); } - - - /** - * - * The functions below have been deprecated as of 1.6, and are only here for backwards - * compatibility. They now reside in dbforge(). The use of dbutils for database manipulation - * is STRONGLY discouraged in favour if using dbforge. - * - */ - - /** - * Create database - * - * @access private - * @param string the database name - * @return bool - */ - function _create_database($name) - { - return "CREATE DATABASE ".$name; - } - - // -------------------------------------------------------------------- - - /** - * Drop database - * - * @access private - * @param string the database name - * @return bool - */ - function _drop_database($name) - { - return "DROP DATABASE ".$name; - } - } /* End of file mysqli_utility.php */ -- cgit v1.2.3-24-g4f1b