diff options
author | Florian Pritz <bluewind@server-speed.net> | 2011-01-28 22:51:06 +0100 |
---|---|---|
committer | Florian Pritz <bluewind@server-speed.net> | 2011-01-28 22:58:52 +0100 |
commit | ce2b69675075444c9e40b72bcdd42ab7edbbe633 (patch) | |
tree | 2932f13b0db14fe53dc0622d888318db638a017f /system/database/drivers/mysql | |
parent | b6b8a6587c399bfd89e13e92ce04ee8486688e6e (diff) |
update to CI 2.0
Signed-off-by: Florian Pritz <bluewind@server-speed.net>
Diffstat (limited to 'system/database/drivers/mysql')
-rwxr-xr-x[-rw-r--r--] | system/database/drivers/mysql/index.html | 0 | ||||
-rwxr-xr-x[-rw-r--r--] | system/database/drivers/mysql/mysql_driver.php | 218 | ||||
-rwxr-xr-x[-rw-r--r--] | system/database/drivers/mysql/mysql_forge.php | 57 | ||||
-rwxr-xr-x[-rw-r--r--] | system/database/drivers/mysql/mysql_result.php | 28 | ||||
-rwxr-xr-x[-rw-r--r--] | system/database/drivers/mysql/mysql_utility.php | 85 |
5 files changed, 230 insertions, 158 deletions
diff --git a/system/database/drivers/mysql/index.html b/system/database/drivers/mysql/index.html index c942a79ce..c942a79ce 100644..100755 --- a/system/database/drivers/mysql/index.html +++ b/system/database/drivers/mysql/index.html diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 85a6ef4a0..df18c912e 100644..100755 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_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 @@ -43,9 +43,9 @@ class CI_DB_mysql_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; - + /** * The syntax to count rows is slightly different across different * database engines, so this string appears in each driver and is @@ -59,17 +59,17 @@ class CI_DB_mysql_driver extends CI_DB { * * @access private called by the base class * @return resource - */ + */ function db_connect() { if ($this->port != '') { $this->hostname .= ':'.$this->port; } - + return @mysql_connect($this->hostname, $this->username, $this->password, TRUE); } - + // -------------------------------------------------------------------- /** @@ -77,7 +77,7 @@ class CI_DB_mysql_driver extends CI_DB { * * @access private called by the base class * @return resource - */ + */ function db_pconnect() { if ($this->port != '') @@ -87,7 +87,7 @@ class CI_DB_mysql_driver extends CI_DB { return @mysql_pconnect($this->hostname, $this->username, $this->password); } - + // -------------------------------------------------------------------- /** @@ -108,13 +108,13 @@ class CI_DB_mysql_driver extends CI_DB { } // -------------------------------------------------------------------- - + /** * Select the database * * @access private called by the base class * @return resource - */ + */ function db_select() { return @mysql_select_db($this->database, $this->conn_id); @@ -136,7 +136,7 @@ class CI_DB_mysql_driver extends CI_DB { } // -------------------------------------------------------------------- - + /** * Version number query string * @@ -156,13 +156,13 @@ class CI_DB_mysql_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); return @mysql_query($sql, $this->conn_id); } - + // -------------------------------------------------------------------- /** @@ -173,7 +173,7 @@ class CI_DB_mysql_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 @@ -185,7 +185,7 @@ class CI_DB_mysql_driver extends CI_DB { $sql = preg_replace("/^\s*DELETE\s+FROM\s+(\S+)\s*$/", "DELETE FROM \\1 WHERE 1=1", $sql); } } - + return $sql; } @@ -195,15 +195,15 @@ class CI_DB_mysql_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) { @@ -214,7 +214,7 @@ class CI_DB_mysql_driver extends CI_DB { // If the $test_mode flag is set to TRUE transactions will be rolled back // even if the queries produce a successful result. $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE; - + $this->simple_query('SET AUTOCOMMIT=0'); $this->simple_query('START TRANSACTION'); // can also be BEGIN or BEGIN WORK return TRUE; @@ -226,8 +226,8 @@ class CI_DB_mysql_driver extends CI_DB { * Commit Transaction * * @access public - * @return bool - */ + * @return bool + */ function trans_commit() { if ( ! $this->trans_enabled) @@ -252,8 +252,8 @@ class CI_DB_mysql_driver extends CI_DB { * Rollback Transaction * * @access public - * @return bool - */ + * @return bool + */ function trans_rollback() { if ( ! $this->trans_enabled) @@ -271,7 +271,7 @@ class CI_DB_mysql_driver extends CI_DB { $this->simple_query('SET AUTOCOMMIT=1'); return TRUE; } - + // -------------------------------------------------------------------- /** @@ -282,17 +282,17 @@ class CI_DB_mysql_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('mysql_real_escape_string') AND is_resource($this->conn_id)) { @@ -306,16 +306,16 @@ class CI_DB_mysql_driver extends CI_DB { { $str = addslashes($str); } - + // escape LIKE condition wildcards if ($like === TRUE) { $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str); } - + return $str; } - + // -------------------------------------------------------------------- /** @@ -328,7 +328,7 @@ class CI_DB_mysql_driver extends CI_DB { { return @mysql_affected_rows($this->conn_id); } - + // -------------------------------------------------------------------- /** @@ -360,7 +360,7 @@ class CI_DB_mysql_driver extends CI_DB { { return 0; } - + $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows') . " FROM " . $this->_protect_identifiers($table, TRUE, NULL, FALSE)); if ($query->num_rows() == 0) @@ -385,7 +385,7 @@ class CI_DB_mysql_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 != '') { @@ -394,7 +394,7 @@ class CI_DB_mysql_driver extends CI_DB { return $sql; } - + // -------------------------------------------------------------------- /** @@ -408,7 +408,7 @@ class CI_DB_mysql_driver extends CI_DB { */ function _list_columns($table = '') { - return "SHOW COLUMNS FROM ".$table; + return "SHOW COLUMNS FROM ".$this->_protect_identifiers($table, TRUE, NULL, FALSE); } // -------------------------------------------------------------------- @@ -439,7 +439,7 @@ class CI_DB_mysql_driver extends CI_DB { { return mysql_error($this->conn_id); } - + // -------------------------------------------------------------------- /** @@ -475,26 +475,26 @@ class CI_DB_mysql_driver extends CI_DB { { 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); } - + // -------------------------------------------------------------------- /** @@ -513,12 +513,12 @@ class CI_DB_mysql_driver extends CI_DB { { $tables = array($tables); } - + return '('.implode(', ', $tables).')'; } // -------------------------------------------------------------------- - + /** * Insert statement * @@ -531,12 +531,50 @@ class CI_DB_mysql_driver extends CI_DB { * @return string */ function _insert($table, $keys, $values) - { + { return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")"; } - + // -------------------------------------------------------------------- + + /** + * Replace statement + * + * Generates a platform-specific replace 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 _replace($table, $keys, $values) + { + return "REPLACE 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); + } + + // -------------------------------------------------------------------- + + /** * Update statement * @@ -556,22 +594,76 @@ class CI_DB_mysql_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; } // -------------------------------------------------------------------- + /** * Truncate statement * @@ -582,12 +674,12 @@ class CI_DB_mysql_driver extends CI_DB { * @access public * @param string the table name * @return string - */ + */ function _truncate($table) { return "TRUNCATE ".$table; } - + // -------------------------------------------------------------------- /** @@ -600,7 +692,7 @@ class CI_DB_mysql_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 = ''; @@ -618,7 +710,7 @@ class CI_DB_mysql_driver extends CI_DB { } $limit = ( ! $limit) ? '' : ' LIMIT '.$limit; - + return "DELETE FROM ".$table.$conditions.$limit; } @@ -636,7 +728,7 @@ class CI_DB_mysql_driver extends CI_DB { * @return string */ function _limit($sql, $limit, $offset) - { + { if ($offset == 0) { $offset = ''; @@ -645,7 +737,7 @@ class CI_DB_mysql_driver extends CI_DB { { $offset .= ", "; } - + return $sql."LIMIT ".$offset.$limit; } @@ -662,7 +754,7 @@ class CI_DB_mysql_driver extends CI_DB { { @mysql_close($conn_id); } - + } diff --git a/system/database/drivers/mysql/mysql_forge.php b/system/database/drivers/mysql/mysql_forge.php index ccacf99f8..529ec980d 100644..100755 --- a/system/database/drivers/mysql/mysql_forge.php +++ b/system/database/drivers/mysql/mysql_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_mysql_forge extends CI_DB_forge { - + /** * Create database * @@ -76,52 +76,67 @@ class CI_DB_mysql_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)) + { + switch ($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'].')'; + } + } } - - 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 +156,12 @@ class CI_DB_mysql_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 +180,14 @@ class CI_DB_mysql_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 +241,7 @@ class CI_DB_mysql_forge extends CI_DB_forge { { $sql .= ' AFTER ' . $this->db->_protect_identifiers($after_field); } - + return $sql; } diff --git a/system/database/drivers/mysql/mysql_result.php b/system/database/drivers/mysql/mysql_result.php index acc586626..507389603 100644..100755 --- a/system/database/drivers/mysql/mysql_result.php +++ b/system/database/drivers/mysql/mysql_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 @@ -36,7 +36,7 @@ class CI_DB_mysql_result extends CI_DB_result { { return @mysql_num_rows($this->result_id); } - + // -------------------------------------------------------------------- /** @@ -49,7 +49,7 @@ class CI_DB_mysql_result extends CI_DB_result { { return @mysql_num_fields($this->result_id); } - + // -------------------------------------------------------------------- /** @@ -67,7 +67,7 @@ class CI_DB_mysql_result extends CI_DB_result { { $field_names[] = $field->name; } - + return $field_names; } @@ -85,27 +85,27 @@ class CI_DB_mysql_result extends CI_DB_result { { $retval = array(); while ($field = mysql_fetch_field($this->result_id)) - { + { $F = new stdClass(); - $F->name = $field->name; - $F->type = $field->type; + $F->name = $field->name; + $F->type = $field->type; $F->default = $field->def; $F->max_length = $field->max_length; $F->primary_key = $field->primary_key; - + $retval[] = $F; } - + return $retval; } - + // -------------------------------------------------------------------- /** * Free the result * * @return null - */ + */ function free_result() { if (is_resource($this->result_id)) @@ -146,7 +146,7 @@ class CI_DB_mysql_result extends CI_DB_result { { return mysql_fetch_assoc($this->result_id); } - + // -------------------------------------------------------------------- /** @@ -161,7 +161,7 @@ class CI_DB_mysql_result extends CI_DB_result { { return mysql_fetch_object($this->result_id); } - + } diff --git a/system/database/drivers/mysql/mysql_utility.php b/system/database/drivers/mysql/mysql_utility.php index 3277b0ff7..e9747c540 100644..100755 --- a/system/database/drivers/mysql/mysql_utility.php +++ b/system/database/drivers/mysql/mysql_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 @@ -84,7 +84,7 @@ class CI_DB_mysql_utility extends CI_DB_utility { // Extract the prefs for simplicity extract($params); - + // Build the output $output = ''; foreach ((array)$tables as $table) @@ -97,31 +97,31 @@ class CI_DB_mysql_utility extends CI_DB_utility { // Get the table schema $query = $this->db->query("SHOW CREATE TABLE `".$this->db->database.'`.'.$table); - + // No result means the table name was invalid if ($query === FALSE) { continue; } - + // Write out the table schema $output .= '#'.$newline.'# TABLE STRUCTURE FOR: '.$table.$newline.'#'.$newline.$newline; - if ($add_drop == TRUE) - { + if ($add_drop == TRUE) + { $output .= 'DROP TABLE IF EXISTS '.$table.';'.$newline.$newline; } - + $i = 0; $result = $query->result_array(); foreach ($result[0] as $val) { if ($i++ % 2) - { + { $output .= $val.';'.$newline.$newline; } } - + // If inserts are not needed we're done... if ($add_insert == FALSE) { @@ -130,16 +130,16 @@ class CI_DB_mysql_utility extends CI_DB_utility { // Grab all the data from the current table $query = $this->db->query("SELECT * FROM $table"); - + if ($query->num_rows() == 0) { continue; } - + // Fetch the field names and determine if the field is an // integer type. We use this info to decide whether to // surround the data with quotes or not - + $i = 0; $field_str = ''; $is_int = array(); @@ -148,24 +148,24 @@ class CI_DB_mysql_utility extends CI_DB_utility { // Most versions of MySQL store timestamp as a string $is_int[$i] = (in_array( strtolower(mysql_field_type($query->result_id, $i)), - array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'), + array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'), //, 'timestamp'), TRUE) ) ? TRUE : FALSE; - + // Create a string of field names $field_str .= '`'.$field->name.'`, '; $i++; } - + // Trim off the end comma $field_str = preg_replace( "/, $/" , "" , $field_str); - - + + // Build the insert string foreach ($query->result_array() as $row) { $val_str = ''; - + $i = 0; foreach ($row as $v) { @@ -184,61 +184,26 @@ class CI_DB_mysql_utility extends CI_DB_utility { else { $val_str .= $v; - } - } - + } + } + // Append a comma $val_str .= ', '; $i++; } - + // Remove the comma at the end of the string $val_str = preg_replace( "/, $/" , "" , $val_str); - + // Build the INSERT string $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline; } - + $output .= $newline.$newline; } return $output; } - - /** - * - * 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 mysql_utility.php */ |