From a2097a077474eab1b9d35acc2c93f0e99a7f12d0 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 10 Oct 2011 10:10:46 -0400 Subject: Converted database constructors to PHP5 type --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 0dd29c238..6bc40411b 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -35,7 +35,7 @@ class CI_DB_forge { * Grabs the CI super object instance so we can access it. * */ - function CI_DB_forge() + function __construct() { // Assign the main database object to $this->db $CI =& get_instance(); -- cgit v1.2.3-24-g4f1b From f4a4bd8fac188ebc9cda822ffc811c218fd92b45 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Thu, 20 Oct 2011 12:18:42 -0500 Subject: adding new license file (OSL 3.0) and updating readme to ReST added notice of license to all source files. OSL to all except the few files we ship inside of the application folder, those are AFL. Updated license in user guide. incrementing next dev version to 3.0 due to licensing change --- system/database/DB_forge.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 6bc40411b..1aa2334ea 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -1,13 +1,25 @@ Date: Mon, 5 Dec 2011 15:28:33 +0100 Subject: changed create_table method to check whether a value returned from driver's forge is sql or bool (acts exactly as create_database) --- system/database/DB_forge.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 1aa2334ea..78bf77a9f 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -205,6 +205,12 @@ class CI_DB_forge { $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists); $this->_reset(); + + if (is_bool($sql)) + { + return $sql; + } + return $this->db->query($sql); } -- cgit v1.2.3-24-g4f1b From a7de97e74cbfe21cd2606c2253134a3bc8dda1f7 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Sat, 31 Dec 2011 18:41:08 +0000 Subject: Added method chaining to DBForge. --- system/database/DB_forge.php | 49 +++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 78bf77a9f..c74fa4068 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -102,12 +102,11 @@ class CI_DB_forge { /** * Add Key * - * @access public * @param string key * @param string type - * @return void + * @return object */ - function add_key($key = '', $primary = FALSE) + public function add_key($key = '', $primary = FALSE) { if (is_array($key)) { @@ -132,6 +131,8 @@ class CI_DB_forge { { $this->keys[] = $key; } + + return $this; } // -------------------------------------------------------------------- @@ -139,11 +140,10 @@ class CI_DB_forge { /** * Add Field * - * @access public * @param string collation - * @return void + * @return object */ - function add_field($field = '') + public function add_field($field = '') { if ($field == '') { @@ -155,12 +155,12 @@ class CI_DB_forge { if ($field == 'id') { $this->add_field(array( - 'id' => array( - 'type' => 'INT', - 'constraint' => 9, - 'auto_increment' => TRUE - ) - )); + 'id' => array( + 'type' => 'INT', + 'constraint' => 9, + 'auto_increment' => TRUE + ) + )); $this->add_key('id', TRUE); } else @@ -178,7 +178,8 @@ class CI_DB_forge { { $this->fields = array_merge($this->fields, $field); } - + + return $this; } // -------------------------------------------------------------------- @@ -186,11 +187,10 @@ class CI_DB_forge { /** * Create Table * - * @access public * @param string the table name * @return bool */ - function create_table($table = '', $if_not_exists = FALSE) + public function create_table($table = '', $if_not_exists = FALSE) { if ($table == '') { @@ -219,11 +219,10 @@ class CI_DB_forge { /** * Drop Table * - * @access public * @param string the table name * @return bool */ - function drop_table($table_name) + public function drop_table($table_name) { $sql = $this->_drop_table($this->db->dbprefix.$table_name); @@ -240,12 +239,11 @@ class CI_DB_forge { /** * Rename Table * - * @access public * @param string the old table name * @param string the new table name * @return bool */ - function rename_table($table_name, $new_table_name) + public function rename_table($table_name, $new_table_name) { if ($table_name == '' OR $new_table_name == '') { @@ -261,13 +259,12 @@ class CI_DB_forge { /** * Column Add * - * @access public * @param string the table name * @param string the column name * @param string the column definition * @return bool */ - function add_column($table = '', $field = array(), $after_field = '') + public function add_column($table = '', $field = array(), $after_field = '') { if ($table == '') { @@ -297,7 +294,6 @@ class CI_DB_forge { } return TRUE; - } // -------------------------------------------------------------------- @@ -305,12 +301,11 @@ class CI_DB_forge { /** * Column Drop * - * @access public * @param string the table name * @param string the column name * @return bool */ - function drop_column($table = '', $column_name = '') + public function drop_column($table = '', $column_name = '') { if ($table == '') @@ -333,13 +328,12 @@ class CI_DB_forge { /** * Column Modify * - * @access public * @param string the table name * @param string the column name * @param string the column definition * @return bool */ - function modify_column($table = '', $field = array()) + public function modify_column($table = '', $field = array()) { if ($table == '') { @@ -384,10 +378,9 @@ class CI_DB_forge { * * Resets table creation vars * - * @access private * @return void */ - function _reset() + protected function _reset() { $this->fields = array(); $this->keys = array(); -- cgit v1.2.3-24-g4f1b From 0defe5d33ee2633f377a109519ca818becc60f64 Mon Sep 17 00:00:00 2001 From: Greg Aker Date: Sun, 1 Jan 2012 18:46:41 -0600 Subject: Updating copyright date to 2012 --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index c74fa4068..762d18a46 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -18,7 +18,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 24276a3a204ddf5947c66bd74f183d8058c1171e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 8 Jan 2012 02:44:38 +0200 Subject: Improve database classes --- system/database/DB_forge.php | 94 ++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 69 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 762d18a46..336e9497d 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -1,13 +1,13 @@ -db $CI =& get_instance(); $this->db =& $CI->db; - log_message('debug', "Database Forge Class Initialized"); + log_message('debug', 'Database Forge Class Initialized'); } // -------------------------------------------------------------------- @@ -60,20 +54,13 @@ class CI_DB_forge { /** * Create database * - * @access public * @param string the database name * @return bool */ - function create_database($db_name) + public function create_database($db_name) { $sql = $this->_create_database($db_name); - - if (is_bool($sql)) - { - return $sql; - } - - return $this->db->query($sql); + return is_bool($sql) ? $sql : $this->db->query($sql); } // -------------------------------------------------------------------- @@ -81,20 +68,13 @@ class CI_DB_forge { /** * Drop database * - * @access public * @param string the database name * @return bool */ - function drop_database($db_name) + public function drop_database($db_name) { $sql = $this->_drop_database($db_name); - - if (is_bool($sql)) - { - return $sql; - } - - return $this->db->query($sql); + return is_bool($sql) ? $sql : $this->db->query($sql); } // -------------------------------------------------------------------- @@ -152,7 +132,7 @@ class CI_DB_forge { if (is_string($field)) { - if ($field == 'id') + if ($field === 'id') { $this->add_field(array( 'id' => array( @@ -178,7 +158,7 @@ class CI_DB_forge { { $this->fields = array_merge($this->fields, $field); } - + return $this; } @@ -197,21 +177,14 @@ class CI_DB_forge { show_error('A table name is required for that operation.'); } - if (count($this->fields) == 0) + if (count($this->fields) === 0) { show_error('Field information is required.'); } $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists); - $this->_reset(); - - if (is_bool($sql)) - { - return $sql; - } - - return $this->db->query($sql); + return is_bool($sql) ? $sql : $this->db->query($sql); } // -------------------------------------------------------------------- @@ -225,13 +198,7 @@ class CI_DB_forge { public function drop_table($table_name) { $sql = $this->_drop_table($this->db->dbprefix.$table_name); - - if (is_bool($sql)) - { - return $sql; - } - - return $this->db->query($sql); + return is_bool($sql) ? $sql : $this->db->query($sql); } // -------------------------------------------------------------------- @@ -250,8 +217,7 @@ class CI_DB_forge { show_error('A table name is required for that operation.'); } - $sql = $this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name); - return $this->db->query($sql); + return $this->db->query($this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name)); } // -------------------------------------------------------------------- @@ -273,8 +239,7 @@ class CI_DB_forge { // add field info into field array, but we can only do one at a time // so we cycle through - - foreach ($field as $k => $v) + foreach (array_keys($field) as $k) { $this->add_field(array($k => $field[$k])); @@ -284,7 +249,6 @@ class CI_DB_forge { } $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field); - $this->_reset(); if ($this->db->query($sql) === FALSE) @@ -307,7 +271,6 @@ class CI_DB_forge { */ public function drop_column($table = '', $column_name = '') { - if ($table == '') { show_error('A table name is required for that operation.'); @@ -318,9 +281,7 @@ class CI_DB_forge { show_error('A column name is required for that operation.'); } - $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name); - - return $this->db->query($sql); + return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name)); } // -------------------------------------------------------------------- @@ -342,8 +303,7 @@ class CI_DB_forge { // add field info into field array, but we can only do one at a time // so we cycle through - - foreach ($field as $k => $v) + foreach (array_keys($field) as $k) { // If no name provided, use the current name if ( ! isset($field[$k]['name'])) @@ -352,14 +312,12 @@ class CI_DB_forge { } $this->add_field(array($k => $field[$k])); - - if (count($this->fields) == 0) + if (count($this->fields) === 0) { show_error('Field information is required.'); } $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields); - $this->_reset(); if ($this->db->query($sql) === FALSE) @@ -382,12 +340,10 @@ class CI_DB_forge { */ protected function _reset() { - $this->fields = array(); - $this->keys = array(); - $this->primary_keys = array(); + $this->fields = $this->keys = $this->primary_keys = array(); } } /* End of file DB_forge.php */ -/* Location: ./system/database/DB_forge.php */ \ No newline at end of file +/* Location: ./system/database/DB_forge.php */ -- cgit v1.2.3-24-g4f1b From 07c1ac830b4e98aa40f48baef3dd05fb68c0a836 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Fri, 9 Mar 2012 17:03:37 +0000 Subject: Bumped CodeIgniter's PHP requirement to 5.2.4. Yes I know PHP 5.4 just came out, and yes I know PHP 5.3 has lovely features, but there are plenty of corporate systems running on CodeIgniter and PHP 5.3 still is not widely supported enough. CodeIgniter is great for distributed applications, and this is the highest we can reasonably go without breaking support. PHP 5.3 will most likely happen in another year or so. Fingers crossed on that one anyway... --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 336e9497d..fe2a67728 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_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 * -- cgit v1.2.3-24-g4f1b From 833d504a91f7c85fa2985bcff5016a052972bd7a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 19 Mar 2012 16:12:03 -0400 Subject: Made the rest of the db classes abstract \n except for the DB_cache class, because I'm not sure if it is directly called --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index fe2a67728..192b78fa6 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -34,7 +34,7 @@ * @author EllisLab Dev Team * @link http://codeigniter.com/user_guide/database/ */ -class CI_DB_forge { +abstract class CI_DB_forge { public $fields = array(); public $keys = array(); -- cgit v1.2.3-24-g4f1b From 215890b015d219f0d31e8ad678b0b655e6923f3b Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 20 Mar 2012 09:38:16 -0400 Subject: Remove extraneous newlines --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 192b78fa6..34c502a99 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -346,4 +346,4 @@ abstract class CI_DB_forge { } /* End of file DB_forge.php */ -/* Location: ./system/database/DB_forge.php */ +/* Location: ./system/database/DB_forge.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From d947eba0bdaf9d86401fdcba9e97706905cacf9d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 9 Apr 2012 14:58:28 +0300 Subject: Multiple DB Forge improvements - Replaced driver methods _create_database(), _drop_database(), _drop_table() and _rename_table() with properties - Added defaults for the above mentioned platform-specific queries, so that not all drivers need to define them - Improved support for the SQLite, ODBC and PDO drivers --- system/database/DB_forge.php | 61 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 10 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 34c502a99..a519575f0 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -25,10 +25,8 @@ * @filesource */ -// ------------------------------------------------------------------------ - /** - * Database Utility Class + * Database Forge Class * * @category Database * @author EllisLab Dev Team @@ -41,6 +39,12 @@ abstract class CI_DB_forge { public $primary_keys = array(); public $db_char_set = ''; + // Platform specific SQL strings + protected $_create_database = 'CREATE DATABASE %s'; + protected $_drop_database = 'DROP DATABASE %s'; + protected $_drop_table = 'DROP TABLE IF EXISTS %s'; + protected $_rename_table = 'ALTER TABLE %s RENAME TO %s'; + public function __construct() { // Assign the main database object to $this->db @@ -59,8 +63,16 @@ abstract class CI_DB_forge { */ public function create_database($db_name) { - $sql = $this->_create_database($db_name); - return is_bool($sql) ? $sql : $this->db->query($sql); + if ($this->_create_database === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat))) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; + } + + return TRUE; } // -------------------------------------------------------------------- @@ -73,8 +85,21 @@ abstract class CI_DB_forge { */ public function drop_database($db_name) { - $sql = $this->_drop_database($db_name); - return is_bool($sql) ? $sql : $this->db->query($sql); + if ($db_name == '') + { + show_error('A table name is required for that operation.'); + return FALSE; + } + elseif ($this->_drop_database === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name))) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; + } + + return TRUE; } // -------------------------------------------------------------------- @@ -197,8 +222,16 @@ abstract class CI_DB_forge { */ public function drop_table($table_name) { - $sql = $this->_drop_table($this->db->dbprefix.$table_name); - return is_bool($sql) ? $sql : $this->db->query($sql); + if ($table_name == '') + { + return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE; + } + elseif ($this->_drop_table === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + + return $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name))); } // -------------------------------------------------------------------- @@ -215,9 +248,17 @@ abstract class CI_DB_forge { if ($table_name == '' OR $new_table_name == '') { show_error('A table name is required for that operation.'); + return FALSE; + } + elseif ($this->_rename_table === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; } - return $this->db->query($this->_rename_table($this->db->dbprefix.$table_name, $this->db->dbprefix.$new_table_name)); + return $this->db->query(sprintf($this->_rename_table, + $this->db->escape_identifiers($this->db->dbprefix.$table_name), + $this->db->escape_identifiers($this->db->dbprefix.$new_table_name)) + ); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 48a2baf0e288accd206f5da5031d29076e130792 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Sat, 2 Jun 2012 11:09:54 +0100 Subject: Replaced `==` with `===` and `!=` with `!==` in /system/database --- system/database/DB_forge.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index a519575f0..ff5eb3fe6 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -85,7 +85,7 @@ abstract class CI_DB_forge { */ public function drop_database($db_name) { - if ($db_name == '') + if ($db_name === '') { show_error('A table name is required for that operation.'); return FALSE; @@ -123,7 +123,7 @@ abstract class CI_DB_forge { return; } - if ($key == '') + if ($key === '') { show_error('Key information is required for that operation.'); } @@ -150,7 +150,7 @@ abstract class CI_DB_forge { */ public function add_field($field = '') { - if ($field == '') + if ($field === '') { show_error('Field information is required.'); } @@ -197,7 +197,7 @@ abstract class CI_DB_forge { */ public function create_table($table = '', $if_not_exists = FALSE) { - if ($table == '') + if ($table === '') { show_error('A table name is required for that operation.'); } @@ -222,7 +222,7 @@ abstract class CI_DB_forge { */ public function drop_table($table_name) { - if ($table_name == '') + if ($table_name === '') { return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE; } @@ -245,7 +245,7 @@ abstract class CI_DB_forge { */ public function rename_table($table_name, $new_table_name) { - if ($table_name == '' OR $new_table_name == '') + if ($table_name === '' OR $new_table_name === '') { show_error('A table name is required for that operation.'); return FALSE; @@ -273,7 +273,7 @@ abstract class CI_DB_forge { */ public function add_column($table = '', $field = array(), $after_field = '') { - if ($table == '') + if ($table === '') { show_error('A table name is required for that operation.'); } @@ -284,7 +284,7 @@ abstract class CI_DB_forge { { $this->add_field(array($k => $field[$k])); - if (count($this->fields) == 0) + if (count($this->fields) === 0) { show_error('Field information is required.'); } @@ -312,12 +312,12 @@ abstract class CI_DB_forge { */ public function drop_column($table = '', $column_name = '') { - if ($table == '') + if ($table === '') { show_error('A table name is required for that operation.'); } - if ($column_name == '') + if ($column_name === '') { show_error('A column name is required for that operation.'); } @@ -337,7 +337,7 @@ abstract class CI_DB_forge { */ public function modify_column($table = '', $field = array()) { - if ($table == '') + if ($table === '') { show_error('A table name is required for that operation.'); } -- cgit v1.2.3-24-g4f1b From 5d28176a76355b230f1c4e1858475def4e34fa4c Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 11 Jun 2012 22:05:40 +0300 Subject: Fix issue #1264 --- system/database/DB_forge.php | 54 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index ff5eb3fe6..9b7639289 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -72,6 +72,11 @@ abstract class CI_DB_forge { return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; } + if ( ! empty($this->db->data_cache['db_names'])) + { + $this->db->data_cache['db_names'][] = $db_name; + } + return TRUE; } @@ -99,6 +104,15 @@ abstract class CI_DB_forge { return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; } + if ( ! empty($this->db->data_cache['db_names'])) + { + $key = array_search(strtolower($db_name), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); + if ($key !== FALSE) + { + unset($this->db->data_cache['db_names'][$key]); + } + } + return TRUE; } @@ -209,7 +223,18 @@ abstract class CI_DB_forge { $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists); $this->_reset(); - return is_bool($sql) ? $sql : $this->db->query($sql); + + if (is_bool($sql)) + { + return $sql; + } + + if (($result = $this->db->query($sql)) !== FALSE && ! empty($this->db->data_cache['table_names'])) + { + $this->db->data_cache['table_names'][] = $$this->db->dbprefix.$table; + } + + return $result; } // -------------------------------------------------------------------- @@ -231,7 +256,19 @@ abstract class CI_DB_forge { return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; } - return $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name))); + $result = $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name))); + + // Update table list cache + if ($result && ! empty($this->db->data_cache['table_names'])) + { + $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE); + if ($key !== FALSE) + { + unset($this->db->data_cache['table_names'][$key]); + } + } + + return $result; } // -------------------------------------------------------------------- @@ -255,10 +292,21 @@ abstract class CI_DB_forge { return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; } - return $this->db->query(sprintf($this->_rename_table, + $result = $this->db->query(sprintf($this->_rename_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name), $this->db->escape_identifiers($this->db->dbprefix.$new_table_name)) ); + + if ($result && ! empty($this->db->data_cache['table_names'])) + { + $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE); + if ($key !== FALSE) + { + $this->db->data_cache['table_names'][$key] = $this->db->dbprefix.$new_table_name; + } + } + + return $result; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From f68db392ea3861de9d80c41e3cd5b857468c53f9 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Thu, 14 Jun 2012 17:14:11 -0500 Subject: Somebody double `$$`ed, causing error Severity: 4096 Message: Object of class CI_DB_mysql_forge could not be converted to string Filename: database/DB_forge.php Line Number: 234 --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 9b7639289..91f9d560c 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -231,7 +231,7 @@ abstract class CI_DB_forge { if (($result = $this->db->query($sql)) !== FALSE && ! empty($this->db->data_cache['table_names'])) { - $this->db->data_cache['table_names'][] = $$this->db->dbprefix.$table; + $this->db->data_cache['table_names'][] = $this->db->dbprefix.$table; } return $result; -- cgit v1.2.3-24-g4f1b From 5fd3ae8d33a4f5d3159b86683b9a670e973a63f5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 24 Oct 2012 14:55:35 +0300 Subject: [ci skip] style and phpdoc-related changes (rel #1295) --- system/database/DB_forge.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 91f9d560c..119d78d38 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -37,7 +37,7 @@ abstract class CI_DB_forge { public $fields = array(); public $keys = array(); public $primary_keys = array(); - public $db_char_set = ''; + public $db_char_set = ''; // Platform specific SQL strings protected $_create_database = 'CREATE DATABASE %s'; @@ -45,6 +45,11 @@ abstract class CI_DB_forge { protected $_drop_table = 'DROP TABLE IF EXISTS %s'; protected $_rename_table = 'ALTER TABLE %s RENAME TO %s'; + /** + * Constructor + * + * @return void + */ public function __construct() { // Assign the main database object to $this->db @@ -206,7 +211,8 @@ abstract class CI_DB_forge { /** * Create Table * - * @param string the table name + * @param string $table = '' + * @param bool $if_not_exists = FALSE * @return bool */ public function create_table($table = '', $if_not_exists = FALSE) @@ -378,9 +384,8 @@ abstract class CI_DB_forge { /** * Column Modify * - * @param string the table name - * @param string the column name - * @param string the column definition + * @param string $table = '' + * @param string $field = array() column definition * @return bool */ public function modify_column($table = '', $field = array()) -- cgit v1.2.3-24-g4f1b From dc6fba5492a215b40c254ed6f704c580427cdfea Mon Sep 17 00:00:00 2001 From: GDmac Date: Wed, 31 Oct 2012 16:53:15 +0100 Subject: Fix #1946 dbforge add_key add_key not setting multiple-column keys when given array Signed-off-by: GDmac --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 119d78d38..2be2790dd 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -132,7 +132,7 @@ abstract class CI_DB_forge { */ public function add_key($key = '', $primary = FALSE) { - if (is_array($key)) + if ($primary && is_array($key)) { foreach ($key as $one) { -- cgit v1.2.3-24-g4f1b From c5536aac5752054f7f76e448d58b86407d8f574e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 1 Nov 2012 17:33:58 +0200 Subject: Manually apply PR #1594 (fixing phpdoc page-level generation/warnings) Also partially fixes issue #1295, fixes inconsistencies in some page-level docblocks and adds include checks in language files. --- system/database/DB_forge.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 119d78d38..50b55d60e 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -1,4 +1,4 @@ - Date: Fri, 2 Nov 2012 01:42:31 +0200 Subject: DocBlocks for base DB classes Partially fixes issue #1295. --- system/database/DB_forge.php | 51 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 50b55d60e..d2d99ccea 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -35,17 +35,66 @@ defined('BASEPATH') OR exit('No direct script access allowed'); */ abstract class CI_DB_forge { + /** + * Fields data + * + * @var array + */ public $fields = array(); + + /** + * Keys data + * + * @var array + */ public $keys = array(); + + /** + * Primary Keys data + * + * @var array + */ public $primary_keys = array(); + + /** + * Database character set + * + * @var string + */ public $db_char_set = ''; - // Platform specific SQL strings + // -------------------------------------------------------------------- + + /** + * CREATE DATABASE statement + * + * @var string + */ protected $_create_database = 'CREATE DATABASE %s'; + + /** + * DROP DATABASE statement + * + * @var string + */ protected $_drop_database = 'DROP DATABASE %s'; + + /** + * DROP TABLE statement + * + * @var string + */ protected $_drop_table = 'DROP TABLE IF EXISTS %s'; + + /** + * RENAME TABLE statement + * + * @var string + */ protected $_rename_table = 'ALTER TABLE %s RENAME TO %s'; + // -------------------------------------------------------------------- + /** * Constructor * -- cgit v1.2.3-24-g4f1b From a287a34c215903d3452023d74149eb5880125715 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 5 Nov 2012 23:19:59 +0200 Subject: Refactored DB Forge - PDO subdrivers are isolated from each other now. - Added compatibility for pretty much all of the features, for every DB platform. - Unified the way that stuff works in general. - Fixes issue #1005. --- system/database/DB_forge.php | 631 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 568 insertions(+), 63 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index f46237e25..d8ecefe77 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -35,6 +35,13 @@ defined('BASEPATH') OR exit('No direct script access allowed'); */ abstract class CI_DB_forge { + /** + * Database object + * + * @var object + */ + public $db; + /** * Fields data * @@ -80,23 +87,68 @@ abstract class CI_DB_forge { protected $_drop_database = 'DROP DATABASE %s'; /** - * DROP TABLE statement + * CREATE TABLE statement + * + * @var string + */ + protected $_create_table = "%s %s (%s\n)"; + + /** + * CREATE TABLE IF statement + * + * @var string + */ + protected $_create_table_if = 'CREATE TABLE IF NOT EXISTS'; + + /** + * CREATE TABLE keys flag + * + * Whether table keys are created from within the + * CREATE TABLE statement. + * + * @var bool + */ + protected $_create_table_keys = FALSE; + + /** + * DROP TABLE IF EXISTS statement * * @var string */ - protected $_drop_table = 'DROP TABLE IF EXISTS %s'; + protected $_drop_table_if = 'DROP TABLE IF EXISTS'; /** * RENAME TABLE statement * * @var string */ - protected $_rename_table = 'ALTER TABLE %s RENAME TO %s'; + protected $_rename_table = 'ALTER TABLE %s RENAME TO %s;'; + + /** + * UNSIGNED support + * + * @var bool|array + */ + protected $_unsigned = TRUE; + + /** + * NULL value representatin in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = ''; + + /** + * DEFAULT value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_default = ' DEFAULT '; // -------------------------------------------------------------------- /** - * Constructor + * Class constructor * * @return void */ @@ -113,7 +165,7 @@ abstract class CI_DB_forge { /** * Create database * - * @param string the database name + * @param string $db_name * @return bool */ public function create_database($db_name) @@ -140,7 +192,7 @@ abstract class CI_DB_forge { /** * Drop database * - * @param string the database name + * @param string $db_name * @return bool */ public function drop_database($db_name) @@ -176,25 +228,25 @@ abstract class CI_DB_forge { /** * Add Key * - * @param string key - * @param string type + * @param string $key + * @param bool $primary * @return object */ public function add_key($key = '', $primary = FALSE) { - if ($primary && is_array($key)) + if (empty($key)) + { + show_error('Key information is required for that operation.'); + } + + if (is_array($key)) { foreach ($key as $one) { $this->add_key($one, $primary); } - return; - } - - if ($key === '') - { - show_error('Key information is required for that operation.'); + return $this; } if ($primary === TRUE) @@ -214,12 +266,12 @@ abstract class CI_DB_forge { /** * Add Field * - * @param string collation + * @param array $field * @return object */ public function add_field($field = '') { - if ($field === '') + if (empty($field)) { show_error('Field information is required.'); } @@ -261,8 +313,8 @@ abstract class CI_DB_forge { /** * Create Table * - * @param string $table = '' - * @param bool $if_not_exists = FALSE + * @param string $table Table name + * @param bool $if_not_exists Whether to add IF NOT EXISTS condition * @return bool */ public function create_table($table = '', $if_not_exists = FALSE) @@ -271,51 +323,129 @@ abstract class CI_DB_forge { { show_error('A table name is required for that operation.'); } + else + { + $table = $this->db->dbprefix.$table; + } if (count($this->fields) === 0) { show_error('Field information is required.'); } - $sql = $this->_create_table($this->db->dbprefix.$table, $this->fields, $this->primary_keys, $this->keys, $if_not_exists); - $this->_reset(); + $sql = $this->_create_table($table, $if_not_exists); if (is_bool($sql)) { - return $sql; + $this->_reset(); + if ($sql === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } } - if (($result = $this->db->query($sql)) !== FALSE && ! empty($this->db->data_cache['table_names'])) + if (($result = $this->db->query($sql)) !== FALSE) { - $this->db->data_cache['table_names'][] = $this->db->dbprefix.$table; + empty($this->db->data_cache['table_names']) OR $this->db->data_cache['table_names'][] = $table; + + // Most databases don't support creating indexes from within the CREATE TABLE statement + if ( ! empty($this->keys)) + { + for ($i = 0, $sqls = $this->_process_indexes($table), $c = count($sqls); $i < $c; $i++) + { + $this->db->query($sqls[$i]); + } + } } + $this->_reset(); return $result; } // -------------------------------------------------------------------- + /** + * Create Table + * + * @param string $table Table name + * @param bool $if_not_exists Whether to add 'IF NOT EXISTS' condition + * @return mixed + */ + protected function _create_table($table, $if_not_exists) + { + if ($if_not_exists === TRUE && $this->_create_table_if === FALSE) + { + if ($this->db->table_exists($table)) + { + return TRUE; + } + else + { + $if_not_exists = FALSE; + } + } + + $sql = ($if_not_exists) + ? sprintf($this->_create_table_if, $this->db->escape_identifiers($table)) + : 'CREATE TABLE'; + + $columns = $this->_process_fields(TRUE); + for ($i = 0, $c = count($columns); $i < $c; $i++) + { + $columns[$i] = ($columns[$i]['_literal'] !== FALSE) + ? "\n\t".$columns[$i]['_literal'] + : "\n\t".$this->_process_column($columns[$i]); + } + + $columns = implode(',', $columns) + .$this->_process_primary_keys($table); + + // Are indexes created from within the CREATE TABLE statement? (e.g. in MySQL) + if ($this->_create_table_keys === TRUE) + { + $columns .= $this->_process_indexes(); + } + + // _create_table will usually have the following format: "%s %s (%s\n)" + $sql = sprintf($this->_create_table.';', + $sql, + $this->db->escape_identifiers($table), + $columns + ); + + return $sql; + } + + // -------------------------------------------------------------------- + /** * Drop Table * - * @param string the table name + * @param string $table_name Table name + * @param bool $if_exists Whether to add an IF EXISTS condition * @return bool */ - public function drop_table($table_name) + public function drop_table($table_name, $if_exists = FALSE) { if ($table_name === '') { return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE; } - elseif ($this->_drop_table === FALSE) + + $query = $this->_drop_table($this->db->dbprefix.$table_name, $if_exists); + if ($query === FALSE) { return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; } + elseif ($query === TRUE) + { + return TRUE; + } - $result = $this->db->query(sprintf($this->_drop_table, $this->db->escape_identifiers($this->db->dbprefix.$table_name))); + $query = $this->db->query($query); // Update table list cache - if ($result && ! empty($this->db->data_cache['table_names'])) + if ($query && ! empty($this->db->data_cache['table_names'])) { $key = array_search(strtolower($this->db->dbprefix.$table_name), array_map('strtolower', $this->db->data_cache['table_names']), TRUE); if ($key !== FALSE) @@ -324,7 +454,40 @@ abstract class CI_DB_forge { } } - return $result; + return $query; + } + + // -------------------------------------------------------------------- + + /** + * Drop Table + * + * Generates a platform-specific DROP TABLE string + * + * @param string $table Table name + * @param bool $if_exists Whether to add an IF EXISTS condition + * @return string + */ + protected function _drop_table($table, $if_exists) + { + $sql = 'DROP TABLE'; + + if ($if_exists) + { + if ($this->_drop_table_if === FALSE) + { + if ( ! $this->db->table_exists($table)) + { + return TRUE; + } + } + else + { + $sql = sprintf($this->_drop_table_if, $this->db->escape_identifiers($table)); + } + } + + return $sql.' '.$this->db->escape_identifiers($table); } // -------------------------------------------------------------------- @@ -332,8 +495,8 @@ abstract class CI_DB_forge { /** * Rename Table * - * @param string the old table name - * @param string the new table name + * @param string $table_name Old table name + * @param string $new_table_name New table name * @return bool */ public function rename_table($table_name, $new_table_name) @@ -370,32 +533,37 @@ abstract class CI_DB_forge { /** * Column Add * - * @param string the table name - * @param string the column name - * @param string the column definition + * @param string $table Table name + * @param array $field Column definition * @return bool */ - public function add_column($table = '', $field = array(), $after_field = '') + public function add_column($table = '', $field = array()) { if ($table === '') { show_error('A table name is required for that operation.'); } - // add field info into field array, but we can only do one at a time - // so we cycle through + // Work-around for literal column definitions + if ( ! is_array($field)) + { + $field = array($field); + } + foreach (array_keys($field) as $k) { $this->add_field(array($k => $field[$k])); + } - if (count($this->fields) === 0) - { - show_error('Field information is required.'); - } - - $sql = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->fields, $after_field); - $this->_reset(); + $sqls = $this->_alter_table('ADD', $this->db->dbprefix.$table, $this->_process_fields()); + $this->_reset(); + if ($sqls === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + for ($i = 0, $c = count($sqls); $i < $c; $i++) + { if ($this->db->query($sql) === FALSE) { return FALSE; @@ -410,8 +578,8 @@ abstract class CI_DB_forge { /** * Column Drop * - * @param string the table name - * @param string the column name + * @param string $table Table name + * @param string $column_name Column name * @return bool */ public function drop_column($table = '', $column_name = '') @@ -426,7 +594,13 @@ abstract class CI_DB_forge { show_error('A column name is required for that operation.'); } - return $this->db->query($this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name)); + $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name); + if ($sql === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + + return $this->db->query($sql); } // -------------------------------------------------------------------- @@ -434,8 +608,8 @@ abstract class CI_DB_forge { /** * Column Modify * - * @param string $table = '' - * @param string $field = array() column definition + * @param string $table Table name + * @param string $field Column definition * @return bool */ public function modify_column($table = '', $field = array()) @@ -445,32 +619,363 @@ abstract class CI_DB_forge { show_error('A table name is required for that operation.'); } - // add field info into field array, but we can only do one at a time - // so we cycle through + // Work-around for literal column definitions + if ( ! is_array($field)) + { + $field = array($field); + } + foreach (array_keys($field) as $k) { - // If no name provided, use the current name - if ( ! isset($field[$k]['name'])) + $this->add_field(array($k => $field[$k])); + } + + if (count($this->fields) === 0) + { + show_error('Field information is required.'); + } + + $sqls = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields); + $this->_reset(); + if ($sqls === FALSE) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + } + + for ($i = 0, $c = count($sqls); $i < $c; $i++) + { + if ($this->db->query($sql) === FALSE) { - $field[$k]['name'] = $k; + return FALSE; } + } - $this->add_field(array($k => $field[$k])); - if (count($this->fields) === 0) + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * ALTER TABLE + * + * @param string $alter_type ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * @return string|string[] + */ + protected function _alter_table($alter_type, $table, $field) + { + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '; + + // DROP has everything it needs now. + if ($alter_type === 'DROP') + { + return $sql.'DROP COLUMN '.$this->db->escape_identifiers($field); + } + + $sqls = array(); + for ($i = 0, $c = count($field), $sql .= $alter_type.' COLUMN '; $i < $c; $i++) + { + $sqls[] = $sql + .($field[$i]['_literal'] !== FALSE ? $field[$i]['_literal'] : $this->_process_column($field[$i])); + } + + return $sqls; + } + + // -------------------------------------------------------------------- + + /** + * Process fields + * + * @param bool $create_table + * @return array + */ + protected function _process_fields($create_table = FALSE) + { + $fields = array(); + + foreach ($this->fields as $key => $attributes) + { + if (is_int($key) && ! is_array($attributes)) { - show_error('Field information is required.'); + $fields[] = array('_literal' => $attributes); + continue; } - $sql = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields); - $this->_reset(); + $attributes = array_change_key_case($attributes, CASE_UPPER); - if ($this->db->query($sql) === FALSE) + if ($create_table === TRUE && empty($attributes['TYPE'])) { - return FALSE; + continue; + } + + if (isset($attributes['TYPE'])) + { + $this->_attr_type($attributes); + $this->_attr_unsigned($attributes, $field); } + + $field = array( + 'name' => $key, + 'new_name' => isset($attributes['NAME']) ? $attributes['NAME'] : NULL, + 'type' => isset($attributes['TYPE']) ? $attributes['TYPE'] : NULL, + 'length' => '', + 'unsigned' => '', + 'null' => '', + 'unique' => '', + 'default' => '', + 'auto_increment' => '', + '_literal' => FALSE + ); + + $this->_attr_default($attributes, $field); + + if (isset($attributes['NULL'])) + { + if ($attributes['NULL'] === TRUE) + { + $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; + } + elseif ($create_table === TRUE) + { + $field['null'] = ' NOT NULL'; + } + } + + $this->_attr_auto_increment($attributes, $field); + $this->_attr_unique($attributes, $field); + + if (isset($attributes['TYPE']) && ! empty($attributes['CONSTRAINT'])) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'ENUM': + case 'SET': + $attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']); + default: + $field['length'] = is_array($attributes['CONSTRAINT']) + ? '('.implode(',', $attributes['CONSTRAINT']).')' + : '('.$attributes['CONSTRAINT'].')'; + break; + } + } + + $fields[] = $field; } - return TRUE; + return $fields; + } + + // -------------------------------------------------------------------- + + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .' '.$field['type'].$field['length'] + .$field['unsigned'] + .$field['default'] + .$field['null'] + .$field['auto_increment'] + .$field['unique']; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + // Usually overriden by drivers + } + + // -------------------------------------------------------------------- + + /** + * Field attribute UNSIGNED + * + * Depending on the _unsigned property value: + * + * - TRUE will always set $field['unsigned'] to 'UNSIGNED' + * - FALSE will always set $field['unsigned'] to '' + * - array(TYPE) will set $field['unsigned'] to 'UNSIGNED', + * if $attributes['TYPE'] is found in the array + * - array(TYPE => UTYPE) will change $field['type'], + * from TYPE to UTYPE in case of a match + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_unsigned(&$attributes, &$field) + { + if (empty($attributes['UNSIGNED']) OR $attributes['UNSIGNED'] !== TRUE) + { + return; + } + + // Reset the attribute in order to avoid issues if we do type conversion + $attributes['UNSIGNED'] = FALSE; + + if (is_array($this->_unsigned)) + { + foreach (array_keys($this->_unsigned) as $key) + { + if (is_int($key) && strcasecmp($attributes['TYPE'], $this->_unsigned[$key]) === 0) + { + $field['unsigned'] = ' UNSIGNED'; + return; + } + elseif (is_string($key) && strcasecmp($attributes['TYPE'], $key) === 0) + { + $field['type'] = $key; + return; + } + } + + return; + } + + $field['unsigned'] = ($this->_unsigned === TRUE) ? ' UNSIGNED' : ''; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute DEFAULT + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_default(&$attributes, &$field) + { + if ($this->_default === FALSE) + { + return; + } + + if (array_key_exists('DEFAULT', $attributes)) + { + if ($attributes['DEFAULT'] === NULL) + { + $field['default'] = empty($this->_null) ? '' : $this->_default.$this->_null; + + // Override the NULL attribute if that's our default + $attributes['NULL'] = NULL; + $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; + } + else + { + $field['default'] = $this->_default.$this->db->escape($attributes['DEFAULT']); + } + } + } + + // -------------------------------------------------------------------- + + /** + * Field attribute UNIQUE + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_unique(&$attributes, &$field) + { + if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) + { + $field['unique'] = ' UNIQUE'; + } + } + + // -------------------------------------------------------------------- + + /** + * Field attribute AUTO_INCREMENT + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_auto_increment(&$attributes, &$field) + { + if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE) + { + $field['auto_increment'] = ' AUTO_INCREMENT'; + } + } + + // -------------------------------------------------------------------- + + /** + * Process primary keys + * + * @param string $table Table name + * @return string + */ + protected function _process_primary_keys($table) + { + $sql = ''; + + for ($i = 0, $c = count($this->primary_keys); $i < $c; $i++) + { + if ( ! isset($this->fields[$this->primary_keys[$i]])) + { + unset($this->primary_keys[$i]); + } + } + + if (count($this->primary_keys) > 0) + { + $sql .= ",\n\tCONSTRAINT ".$this->db->escape_identifiers('pk_'.$table) + .' PRIMARY KEY('.implode(', ', $this->db->escape_identifiers($this->primary_keys)).')'; + } + + return $sql; + } + + // -------------------------------------------------------------------- + + /** + * Process indexes + * + * @param string $table + * @return string + */ + protected function _process_indexes($table = NULL) + { + $table = $this->db->escape_identifiers($table); + $sqls = array(); + + for ($i = 0, $c = count($this->keys); $i < $c; $i++) + { + if ( ! isset($this->fields[$this->keys[$i]])) + { + unset($this->keys[$i]); + continue; + } + + is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]); + + $sqls[] = 'CREATE INDEX '.$this->db->escape_identifiers(implode('_', $this->keys[$i])) + .' ON '.$this->db->escape_identifiers($table) + .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).');'; + } + + return $sqls; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 137a742409b2a643e70f422efa17ca535b47d218 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 5 Nov 2012 23:46:44 +0200 Subject: Fix Forge add_column() and modify_column() --- system/database/DB_forge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index d8ecefe77..c284d7d82 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -564,7 +564,7 @@ abstract class CI_DB_forge { for ($i = 0, $c = count($sqls); $i < $c; $i++) { - if ($this->db->query($sql) === FALSE) + if ($this->db->query($sqls[$i]) === FALSE) { return FALSE; } @@ -644,7 +644,7 @@ abstract class CI_DB_forge { for ($i = 0, $c = count($sqls); $i < $c; $i++) { - if ($this->db->query($sql) === FALSE) + if ($this->db->query($sqls[$i]) === FALSE) { return FALSE; } -- cgit v1.2.3-24-g4f1b From d743cdbe448258cc7f02abf15e8dc797dc6403eb Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 6 Nov 2012 00:00:01 +0200 Subject: Re-fix multiple-column non-primary key indexes --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index c284d7d82..2b9fb169a 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -239,7 +239,7 @@ abstract class CI_DB_forge { show_error('Key information is required for that operation.'); } - if (is_array($key)) + if ($primary === TRUE && is_array($key)) { foreach ($key as $one) { -- cgit v1.2.3-24-g4f1b From eaa60c71082c1e49f8a48d633347c98b68a387c0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 6 Nov 2012 01:11:22 +0200 Subject: Added possibility to pass custom database objects to DB Forge and DB Utilities Also, their property is no longer public and the utility class no longer extends CI_DB_forge. --- system/database/DB_forge.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 2b9fb169a..34140c6e0 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -40,7 +40,7 @@ abstract class CI_DB_forge { * * @var object */ - public $db; + protected $db; /** * Fields data @@ -150,13 +150,12 @@ abstract class CI_DB_forge { /** * Class constructor * + * @param object &$db Database object * @return void */ - public function __construct() + public function __construct(&$db) { - // Assign the main database object to $this->db - $CI =& get_instance(); - $this->db =& $CI->db; + $this->db =& $db; log_message('debug', 'Database Forge Class Initialized'); } -- cgit v1.2.3-24-g4f1b From 8d3afde93f9ff7f461095d5b9147b662610c045b Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 6 Nov 2012 12:53:47 +0200 Subject: Fix a lang key typo --- system/database/DB_forge.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 34140c6e0..7c9c03e24 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -171,7 +171,7 @@ abstract class CI_DB_forge { { if ($this->_create_database === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat))) { @@ -203,7 +203,7 @@ abstract class CI_DB_forge { } elseif ($this->_drop_database === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name))) { @@ -339,7 +339,7 @@ abstract class CI_DB_forge { $this->_reset(); if ($sql === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } } @@ -434,7 +434,7 @@ abstract class CI_DB_forge { $query = $this->_drop_table($this->db->dbprefix.$table_name, $if_exists); if ($query === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } elseif ($query === TRUE) { @@ -507,7 +507,7 @@ abstract class CI_DB_forge { } elseif ($this->_rename_table === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } $result = $this->db->query(sprintf($this->_rename_table, @@ -558,7 +558,7 @@ abstract class CI_DB_forge { $this->_reset(); if ($sqls === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } for ($i = 0, $c = count($sqls); $i < $c; $i++) @@ -596,7 +596,7 @@ abstract class CI_DB_forge { $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name); if ($sql === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } return $this->db->query($sql); @@ -638,7 +638,7 @@ abstract class CI_DB_forge { $this->_reset(); if ($sqls === FALSE) { - return ($this->db->db_debug) ? $this->db->display_error('db_unsuported_feature') : FALSE; + return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } for ($i = 0, $c = count($sqls); $i < $c; $i++) -- cgit v1.2.3-24-g4f1b From b67277b8063b0e6aab051ce269194255ef83e808 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 12 Nov 2012 12:51:14 +0200 Subject: Bring back the AFTER clause for DB Forge add_column() (it was temporarily removed due to multiple inconsistencies with other drivers) This commit also fixes issue #1988. Also added support for the FIRST clause (again, MySQL and CUBRID only). --- system/database/DB_forge.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 7c9c03e24..21046012a 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -532,11 +532,13 @@ abstract class CI_DB_forge { /** * Column Add * + * @todo Remove deprecated $_after option in 3.1+ * @param string $table Table name * @param array $field Column definition + * @param string $_after Column for AFTER clause (deprecated) * @return bool */ - public function add_column($table = '', $field = array()) + public function add_column($table = '', $field = array(), $_after = NULL) { if ($table === '') { @@ -551,6 +553,12 @@ abstract class CI_DB_forge { foreach (array_keys($field) as $k) { + // Backwards-compatibility work-around for MySQL/CUBRID AFTER clause (remove in 3.1+) + if ($_after !== NULL && is_array($field[$k]) && ! isset($field[$k]['after'])) + { + $field[$k]['after'] = $_after; + } + $this->add_field(array($k => $field[$k])); } -- cgit v1.2.3-24-g4f1b From a00f90c2973111f0d95ac129eba967e8269ea0d6 Mon Sep 17 00:00:00 2001 From: Phil Sturgeon Date: Sun, 18 Nov 2012 20:52:14 -0500 Subject: PDO was not escaping strings for SET as array --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 21046012a..6a19096bf 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -762,7 +762,7 @@ abstract class CI_DB_forge { $attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']); default: $field['length'] = is_array($attributes['CONSTRAINT']) - ? '('.implode(',', $attributes['CONSTRAINT']).')' + ? '("'.implode('","', $attributes['CONSTRAINT']).'")' : '('.$attributes['CONSTRAINT'].')'; break; } -- cgit v1.2.3-24-g4f1b From 356d4f40d545fb08117ee09da3d3ad8f0746686d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 19 Nov 2012 20:13:11 +0200 Subject: DB forge to use single quotes for ENUM/SET string delimiters --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 6a19096bf..06e47a6b4 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -762,7 +762,7 @@ abstract class CI_DB_forge { $attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']); default: $field['length'] = is_array($attributes['CONSTRAINT']) - ? '("'.implode('","', $attributes['CONSTRAINT']).'")' + ? "('".implode("','", $attributes['CONSTRAINT'])."')" : '('.$attributes['CONSTRAINT'].')'; break; } -- cgit v1.2.3-24-g4f1b From 7ade8b7944d10f4fd1583789309cf003a3eac6f7 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 22 Nov 2012 13:12:22 +0200 Subject: Fix modify_column() issues (#2020) --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 06e47a6b4..bb1ee4809 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -642,7 +642,7 @@ abstract class CI_DB_forge { show_error('Field information is required.'); } - $sqls = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->fields); + $sqls = $this->_alter_table('CHANGE', $this->db->dbprefix.$table, $this->_process_fields()); $this->_reset(); if ($sqls === FALSE) { -- cgit v1.2.3-24-g4f1b From 3545102a83a70a42876148037bec05f4bb32913e Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 25 Nov 2012 17:20:04 +0200 Subject: Fix #2027 --- system/database/DB_forge.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index bb1ee4809..59c3baf8b 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -402,7 +402,7 @@ abstract class CI_DB_forge { // Are indexes created from within the CREATE TABLE statement? (e.g. in MySQL) if ($this->_create_table_keys === TRUE) { - $columns .= $this->_process_indexes(); + $columns .= $this->_process_indexes($table); } // _create_table will usually have the following format: "%s %s (%s\n)" @@ -962,14 +962,25 @@ abstract class CI_DB_forge { * @param string $table * @return string */ - protected function _process_indexes($table = NULL) + protected function _process_indexes($table) { $table = $this->db->escape_identifiers($table); $sqls = array(); for ($i = 0, $c = count($this->keys); $i < $c; $i++) { - if ( ! isset($this->fields[$this->keys[$i]])) + if (is_array($this->keys[$i])) + { + for ($i2 = 0, $c2 = count($this->keys[$i]); $i2 < $c2; $i2++) + { + if ( ! isset($this->fields[$this->keys[$i][$i2]])) + { + unset($this->keys[$i][$i2]); + continue; + } + } + } + elseif ( ! isset($this->fields[$this->keys[$i]])) { unset($this->keys[$i]); continue; -- cgit v1.2.3-24-g4f1b From 4296a65693504736b5e65bee5b163fa08cacb563 Mon Sep 17 00:00:00 2001 From: Andrew Podner Date: Mon, 17 Dec 2012 07:51:15 -0500 Subject: update for Issue #2064 (changed docblocks which return $this or only call a method that returns $this to @return CI_DB_class_name) --- system/database/DB_forge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 59c3baf8b..fd224c469 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -229,7 +229,7 @@ abstract class CI_DB_forge { * * @param string $key * @param bool $primary - * @return object + * @return CI_DB_forge */ public function add_key($key = '', $primary = FALSE) { @@ -266,7 +266,7 @@ abstract class CI_DB_forge { * Add Field * * @param array $field - * @return object + * @return CI_DB_forge */ public function add_field($field = '') { -- cgit v1.2.3-24-g4f1b From d8dba5d3ecbe1ff4502b04a9cf3086908db140d1 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 17 Dec 2012 15:42:01 +0200 Subject: [ci skip] Fix some spaces --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index fd224c469..5c782f875 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -229,7 +229,7 @@ abstract class CI_DB_forge { * * @param string $key * @param bool $primary - * @return CI_DB_forge + * @return CI_DB_forge */ public function add_key($key = '', $primary = FALSE) { -- cgit v1.2.3-24-g4f1b From 80500afbd188600212ca913a7bac073009feac73 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 1 Jan 2013 08:16:53 +0200 Subject: [ci skip] Happy new year --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 5c782f875..53cdd53b6 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -18,7 +18,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From 13f6eabafa655828a8c09b4ae0a58a2e3776c269 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 15 Mar 2013 10:56:55 +0200 Subject: Fix MSSQL ALTER TABLE ADD statement An improved version of PR #2329 --- system/database/DB_forge.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 53cdd53b6..d52029ecd 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -680,8 +680,12 @@ abstract class CI_DB_forge { return $sql.'DROP COLUMN '.$this->db->escape_identifiers($field); } + $sql .= ($alter_type === 'ADD') + ? 'ADD ' + : $alter_type.' COLUMN '; + $sqls = array(); - for ($i = 0, $c = count($field), $sql .= $alter_type.' COLUMN '; $i < $c; $i++) + for ($i = 0, $c = count($field); $i < $c; $i++) { $sqls[] = $sql .($field[$i]['_literal'] !== FALSE ? $field[$i]['_literal'] : $this->_process_column($field[$i])); -- cgit v1.2.3-24-g4f1b From 5d69a6e8e096faa99fb838dabd7fe548213b0f26 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 28 Oct 2013 15:34:47 +0200 Subject: Fix #2703 --- system/database/DB_forge.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index d52029ecd..f156d24eb 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -740,6 +740,18 @@ abstract class CI_DB_forge { '_literal' => FALSE ); + if ($create_table === FALSE) + { + if (isset($attributes['AFTER'])) + { + $field['after'] = $attributes['after']; + } + elseif (isset($attributes['FIRST'])) + { + $field['first'] = (bool) $attributes['FIRST']; + } + } + $this->_attr_default($attributes, $field); if (isset($attributes['NULL'])) @@ -748,11 +760,15 @@ abstract class CI_DB_forge { { $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; } - elseif ($create_table === TRUE) + else { $field['null'] = ' NOT NULL'; } } + elseif ($create_table === TRUE) + { + $field['null'] = ' NOT NULL'; + } $this->_attr_auto_increment($attributes, $field); $this->_attr_unique($attributes, $field); -- cgit v1.2.3-24-g4f1b From 96185a3a0edcc27fa0af6df761d6353a2208ea9d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 28 Oct 2013 16:04:02 +0200 Subject: Really fix #2703 --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index f156d24eb..92806d305 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -744,7 +744,7 @@ abstract class CI_DB_forge { { if (isset($attributes['AFTER'])) { - $field['after'] = $attributes['after']; + $field['after'] = $attributes['AFTER']; } elseif (isset($attributes['FIRST'])) { -- cgit v1.2.3-24-g4f1b From 225c37315bdaa05371cd3142dda25783f4cca8af Mon Sep 17 00:00:00 2001 From: mjnaderi Date: Thu, 19 Dec 2013 00:35:08 +0330 Subject: Cleanup DB_forge _process_indexes --- system/database/DB_forge.php | 1 - 1 file changed, 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 92806d305..bde4e2872 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -984,7 +984,6 @@ abstract class CI_DB_forge { */ protected function _process_indexes($table) { - $table = $this->db->escape_identifiers($table); $sqls = array(); for ($i = 0, $c = count($this->keys); $i < $c; $i++) -- cgit v1.2.3-24-g4f1b From 18c2e0c7d75ae91d0223bccf41d07e5a42626cd4 Mon Sep 17 00:00:00 2001 From: mjnaderi Date: Thu, 19 Dec 2013 01:16:17 +0330 Subject: Cleanup DB_forge _process_indexes --- system/database/DB_forge.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index bde4e2872..bec8d5554 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -984,6 +984,7 @@ abstract class CI_DB_forge { */ protected function _process_indexes($table) { + $table = $this->db->escape_identifiers($table); $sqls = array(); for ($i = 0, $c = count($this->keys); $i < $c; $i++) @@ -1008,7 +1009,7 @@ abstract class CI_DB_forge { is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]); $sqls[] = 'CREATE INDEX '.$this->db->escape_identifiers(implode('_', $this->keys[$i])) - .' ON '.$this->db->escape_identifiers($table) + .' ON '.$table .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).');'; } -- cgit v1.2.3-24-g4f1b From d3a6ca207f5851852a4d0de7c70c861434f70c5c Mon Sep 17 00:00:00 2001 From: mjnaderi Date: Thu, 19 Dec 2013 01:35:57 +0330 Subject: Use table name as a prefix for index names --- system/database/DB_forge.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index bec8d5554..1cebb189c 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -984,7 +984,6 @@ abstract class CI_DB_forge { */ protected function _process_indexes($table) { - $table = $this->db->escape_identifiers($table); $sqls = array(); for ($i = 0, $c = count($this->keys); $i < $c; $i++) @@ -1008,8 +1007,8 @@ abstract class CI_DB_forge { is_array($this->keys[$i]) OR $this->keys[$i] = array($this->keys[$i]); - $sqls[] = 'CREATE INDEX '.$this->db->escape_identifiers(implode('_', $this->keys[$i])) - .' ON '.$table + $sqls[] = 'CREATE INDEX '.$this->db->escape_identifiers($table.'_'.implode('_', $this->keys[$i])) + .' ON '.$this->db->escape_identifiers($table) .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).');'; } -- cgit v1.2.3-24-g4f1b From 27f798b9d64025fecaaecbd80a8cba41d455940f Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 20 Jan 2014 18:19:13 +0200 Subject: Add support for optional table attributes to CI_DB_forge::create_table() Supersedes PRs #989, #2776 Related issue: #41 --- system/database/DB_forge.php | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 1cebb189c..fc4a9230f 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -314,9 +314,10 @@ abstract class CI_DB_forge { * * @param string $table Table name * @param bool $if_not_exists Whether to add IF NOT EXISTS condition + * @param array $attributes Associative array of table attributes * @return bool */ - public function create_table($table = '', $if_not_exists = FALSE) + public function create_table($table = '', $if_not_exists = FALSE, array $attributes = array()) { if ($table === '') { @@ -332,7 +333,7 @@ abstract class CI_DB_forge { show_error('Field information is required.'); } - $sql = $this->_create_table($table, $if_not_exists); + $sql = $this->_create_table($table, $if_not_exists, $attributes); if (is_bool($sql)) { @@ -368,9 +369,10 @@ abstract class CI_DB_forge { * * @param string $table Table name * @param bool $if_not_exists Whether to add 'IF NOT EXISTS' condition + * @param array $attributes Associative array of table attributes * @return mixed */ - protected function _create_table($table, $if_not_exists) + protected function _create_table($table, $if_not_exists, $attributes) { if ($if_not_exists === TRUE && $this->_create_table_if === FALSE) { @@ -406,10 +408,11 @@ abstract class CI_DB_forge { } // _create_table will usually have the following format: "%s %s (%s\n)" - $sql = sprintf($this->_create_table.';', + $sql = sprintf($this->_create_table.'%s;', $sql, $this->db->escape_identifiers($table), - $columns + $columns, + $this->_create_table_attr($attributes) ); return $sql; @@ -417,6 +420,29 @@ abstract class CI_DB_forge { // -------------------------------------------------------------------- + /** + * CREATE TABLE attributes + * + * @param array $attributes Associative array of table attributes + * @return string + */ + protected function _create_table_attr($attributes) + { + $sql = ''; + + foreach (array_keys($attributes) as $key) + { + if (is_string($key)) + { + $sql .= ' '.strtoupper($key).' '.$attributes[$key]; + } + } + + return $sql; + } + + // -------------------------------------------------------------------- + /** * Drop Table * -- cgit v1.2.3-24-g4f1b From 871754af60251993d640981e107d2def5f2db396 Mon Sep 17 00:00:00 2001 From: darwinel Date: Tue, 11 Feb 2014 17:34:57 +0100 Subject: 2013 > 2014 Update copyright notices from 2013 to 2014. And update one calendar example in user_guide from year 2013/2014 to 2014/2015. --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index fc4a9230f..4eeb74a9c 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -18,7 +18,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * @link http://codeigniter.com * @since Version 1.0 -- cgit v1.2.3-24-g4f1b From c5a0af287d3aba7ba4186406ca750bb2f986f947 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 10 Mar 2014 10:29:43 +0200 Subject: Fix #2928 --- system/database/DB_forge.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 4eeb74a9c..bcf7a8d71 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -806,10 +806,13 @@ abstract class CI_DB_forge { case 'ENUM': case 'SET': $attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']); + $field['length'] = is_array($attributes['CONSTRAINT']) + ? "('".implode("','", $attributes['CONSTRAINT'])."')" + : '('.$attributes['CONSTRAINT'].')'; default: $field['length'] = is_array($attributes['CONSTRAINT']) - ? "('".implode("','", $attributes['CONSTRAINT'])."')" - : '('.$attributes['CONSTRAINT'].')'; + ? '('.implode(',', $attributes['CONSTRAINT']).')' + : '('.$attributes['CONSTRAINT'].')'; break; } } -- cgit v1.2.3-24-g4f1b From fde170c0640a04e0d9d686bfa368e1f368cc1ba1 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 10 Mar 2014 19:55:11 +0200 Subject: Fix #2928, #2929 --- system/database/DB_forge.php | 1 + 1 file changed, 1 insertion(+) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index bcf7a8d71..38e4ccf2c 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -809,6 +809,7 @@ abstract class CI_DB_forge { $field['length'] = is_array($attributes['CONSTRAINT']) ? "('".implode("','", $attributes['CONSTRAINT'])."')" : '('.$attributes['CONSTRAINT'].')'; + break; default: $field['length'] = is_array($attributes['CONSTRAINT']) ? '('.implode(',', $attributes['CONSTRAINT']).')' -- cgit v1.2.3-24-g4f1b From 1394304472f1c917b8f6680c57bf50c780744f2d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 17 Mar 2014 11:50:45 +0200 Subject: Fix DB forge unsigned attribute (PR #2949) --- system/database/DB_forge.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 38e4ccf2c..21ef40119 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -747,25 +747,23 @@ abstract class CI_DB_forge { continue; } - if (isset($attributes['TYPE'])) - { - $this->_attr_type($attributes); - $this->_attr_unsigned($attributes, $field); - } + isset($attributes['TYPE']) && $this->_attr_type($attributes); $field = array( - 'name' => $key, - 'new_name' => isset($attributes['NAME']) ? $attributes['NAME'] : NULL, - 'type' => isset($attributes['TYPE']) ? $attributes['TYPE'] : NULL, - 'length' => '', - 'unsigned' => '', - 'null' => '', - 'unique' => '', - 'default' => '', - 'auto_increment' => '', - '_literal' => FALSE + 'name' => $key, + 'new_name' => isset($attributes['NAME']) ? $attributes['NAME'] : NULL, + 'type' => isset($attributes['TYPE']) ? $attributes['TYPE'] : NULL, + 'length' => '', + 'unsigned' => '', + 'null' => '', + 'unique' => '', + 'default' => '', + 'auto_increment' => '', + '_literal' => FALSE ); + isset($attributes['TYPE']) && $this->_attr_unsigned($attributes, $field); + if ($create_table === FALSE) { if (isset($attributes['AFTER'])) -- cgit v1.2.3-24-g4f1b From aaca5cb7a3df2cfb8e18e87dca43be6ab112747d Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 31 Mar 2014 17:20:55 +0300 Subject: [ci skip] Fix #2972 --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 21ef40119..111546ecc 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -408,7 +408,7 @@ abstract class CI_DB_forge { } // _create_table will usually have the following format: "%s %s (%s\n)" - $sql = sprintf($this->_create_table.'%s;', + $sql = sprintf($this->_create_table.'%s', $sql, $this->db->escape_identifiers($table), $columns, -- cgit v1.2.3-24-g4f1b From 22ce276f4f696d69c11ee1d7c8b8acee67a97b09 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 15 Aug 2014 11:59:16 +0300 Subject: Fix #3187 --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 111546ecc..2dd243cae 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -929,7 +929,7 @@ abstract class CI_DB_forge { $field['default'] = empty($this->_null) ? '' : $this->_default.$this->_null; // Override the NULL attribute if that's our default - $attributes['NULL'] = NULL; + $attributes['NULL'] = TRUE; $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; } else -- cgit v1.2.3-24-g4f1b From bdb96ca1b1dbfc1791172fd169d7751cbc4d7d55 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 28 Oct 2014 00:13:31 +0200 Subject: [ci skip] Switch to MIT license; close #3293 --- system/database/DB_forge.php | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 2dd243cae..aa8bbbe3f 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -4,24 +4,35 @@ * * An open source application development framework for PHP 5.2.4 or newer * - * NOTICE OF LICENSE + * This content is released under the MIT License (MIT) * - * Licensed under the Open Software License version 3.0 + * Copyright (c) 2014, British Columbia Institute of Technology * - * This source file is subject to the Open Software License (OSL 3.0) that is - * bundled with this package in the files license.txt / license.rst. It is - * also available through the world wide web at this URL: - * http://opensource.org/licenses/OSL-3.0 - * If you did not receive a copy of the license and are unable to obtain it - * through the world wide web, please send an email to - * licensing@ellislab.com so we can send you a copy immediately. + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: * - * @package CodeIgniter - * @author EllisLab Dev Team + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @package CodeIgniter + * @author EllisLab Dev Team * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) - * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) - * @link http://codeigniter.com - * @since Version 1.0 + * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) + * @license http://opensource.org/licenses/MIT MIT License + * @link http://codeigniter.com + * @since Version 1.0.0 * @filesource */ defined('BASEPATH') OR exit('No direct script access allowed'); -- cgit v1.2.3-24-g4f1b From 24fbc61e4a46c1e9f185c36629960dce0bad71e2 Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Sun, 2 Nov 2014 21:50:15 -0700 Subject: Add support for the COMMENT field in DBForge and MySQL Forge classes (pdo, mysql, and mysqli) Signed-off-by: Zachary Flower --- system/database/DB_forge.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index aa8bbbe3f..df3b90be6 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -156,6 +156,13 @@ abstract class CI_DB_forge { */ protected $_default = ' DEFAULT '; + /** + * COMMENT value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_comment = ' COMMENT '; + // -------------------------------------------------------------------- /** @@ -849,6 +856,7 @@ abstract class CI_DB_forge { .$field['default'] .$field['null'] .$field['auto_increment'] + .$field['comment'] .$field['unique']; } @@ -986,6 +994,28 @@ abstract class CI_DB_forge { // -------------------------------------------------------------------- + /** + * Field attribute COMMENT + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_comment(&$attributes, &$field) + { + if ($this->_comment === FALSE) + { + return; + } + + if (!empty($attributes['COMMENT'])) + { + $field['comment'] = $this->_default.$this->db->escape($attributes['COMMENT']); + } + } + + // -------------------------------------------------------------------- + /** * Process primary keys * -- cgit v1.2.3-24-g4f1b From e59347dd1665078beb41a1e6ca1b12932b489336 Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Mon, 3 Nov 2014 08:34:12 -0700 Subject: Ensure forge comments are actually getting parsed Signed-off-by: Zachary Flower --- system/database/DB_forge.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index df3b90be6..6dc2a4514 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -777,6 +777,7 @@ abstract class CI_DB_forge { 'unique' => '', 'default' => '', 'auto_increment' => '', + 'comment' => '', '_literal' => FALSE ); @@ -813,6 +814,7 @@ abstract class CI_DB_forge { } $this->_attr_auto_increment($attributes, $field); + $this->_attr_comment($attributes, $field); $this->_attr_unique($attributes, $field); if (isset($attributes['TYPE']) && ! empty($attributes['CONSTRAINT'])) -- cgit v1.2.3-24-g4f1b From 12ee7a1a74e45b1369876ea945ae3347da046087 Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Mon, 3 Nov 2014 08:38:41 -0700 Subject: Move comment field after unique field in db forge --- system/database/DB_forge.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 6dc2a4514..0317489f6 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -814,8 +814,8 @@ abstract class CI_DB_forge { } $this->_attr_auto_increment($attributes, $field); - $this->_attr_comment($attributes, $field); $this->_attr_unique($attributes, $field); + $this->_attr_comment($attributes, $field); if (isset($attributes['TYPE']) && ! empty($attributes['CONSTRAINT'])) { @@ -858,8 +858,8 @@ abstract class CI_DB_forge { .$field['default'] .$field['null'] .$field['auto_increment'] - .$field['comment'] - .$field['unique']; + .$field['unique'] + .$field['comment']; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From e4b10bf0ad59049ed78ed9cfc5f708188f3cd442 Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Mon, 3 Nov 2014 10:42:57 -0700 Subject: Move MySQL comments to MySQL forge classes only --- system/database/DB_forge.php | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 0317489f6..85505ce41 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -156,13 +156,6 @@ abstract class CI_DB_forge { */ protected $_default = ' DEFAULT '; - /** - * COMMENT value representation in CREATE/ALTER TABLE statements - * - * @var string - */ - protected $_comment = ' COMMENT '; - // -------------------------------------------------------------------- /** @@ -777,7 +770,6 @@ abstract class CI_DB_forge { 'unique' => '', 'default' => '', 'auto_increment' => '', - 'comment' => '', '_literal' => FALSE ); @@ -815,7 +807,11 @@ abstract class CI_DB_forge { $this->_attr_auto_increment($attributes, $field); $this->_attr_unique($attributes, $field); - $this->_attr_comment($attributes, $field); + + if (isset($attributes['COMMENT'])) + { + $field['comment'] = $this->db->escape($attributes['COMMENT']); + } if (isset($attributes['TYPE']) && ! empty($attributes['CONSTRAINT'])) { @@ -858,8 +854,7 @@ abstract class CI_DB_forge { .$field['default'] .$field['null'] .$field['auto_increment'] - .$field['unique'] - .$field['comment']; + .$field['unique']; } // -------------------------------------------------------------------- @@ -996,28 +991,6 @@ abstract class CI_DB_forge { // -------------------------------------------------------------------- - /** - * Field attribute COMMENT - * - * @param array &$attributes - * @param array &$field - * @return void - */ - protected function _attr_comment(&$attributes, &$field) - { - if ($this->_comment === FALSE) - { - return; - } - - if (!empty($attributes['COMMENT'])) - { - $field['comment'] = $this->_default.$this->db->escape($attributes['COMMENT']); - } - } - - // -------------------------------------------------------------------- - /** * Process primary keys * -- cgit v1.2.3-24-g4f1b From b906149a2e6b4d607294b12d4a690ed3af84eed0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 4 Dec 2014 16:33:24 +0200 Subject: DB forge/utilities polishing (docs) following #3375, #3378 --- system/database/DB_forge.php | 59 +++++++------------------------------------- 1 file changed, 9 insertions(+), 50 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 85505ce41..4238e37ee 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -207,12 +207,7 @@ abstract class CI_DB_forge { */ public function drop_database($db_name) { - if ($db_name === '') - { - show_error('A table name is required for that operation.'); - return FALSE; - } - elseif ($this->_drop_database === FALSE) + if ($this->_drop_database === FALSE) { return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } @@ -242,13 +237,8 @@ abstract class CI_DB_forge { * @param bool $primary * @return CI_DB_forge */ - public function add_key($key = '', $primary = FALSE) + public function add_key($key, $primary = FALSE) { - if (empty($key)) - { - show_error('Key information is required for that operation.'); - } - if ($primary === TRUE && is_array($key)) { foreach ($key as $one) @@ -279,13 +269,8 @@ abstract class CI_DB_forge { * @param array $field * @return CI_DB_forge */ - public function add_field($field = '') + public function add_field($field) { - if (empty($field)) - { - show_error('Field information is required.'); - } - if (is_string($field)) { if ($field === 'id') @@ -328,7 +313,7 @@ abstract class CI_DB_forge { * @param array $attributes Associative array of table attributes * @return bool */ - public function create_table($table = '', $if_not_exists = FALSE, array $attributes = array()) + public function create_table($table, $if_not_exists = FALSE, array $attributes = array()) { if ($table === '') { @@ -575,18 +560,10 @@ abstract class CI_DB_forge { * @param string $_after Column for AFTER clause (deprecated) * @return bool */ - public function add_column($table = '', $field = array(), $_after = NULL) + public function add_column($table, $field, $_after = NULL) { - if ($table === '') - { - show_error('A table name is required for that operation.'); - } - // Work-around for literal column definitions - if ( ! is_array($field)) - { - $field = array($field); - } + is_array($field) OR $field = array($field); foreach (array_keys($field) as $k) { @@ -626,18 +603,8 @@ abstract class CI_DB_forge { * @param string $column_name Column name * @return bool */ - public function drop_column($table = '', $column_name = '') + public function drop_column($table, $column_name) { - if ($table === '') - { - show_error('A table name is required for that operation.'); - } - - if ($column_name === '') - { - show_error('A column name is required for that operation.'); - } - $sql = $this->_alter_table('DROP', $this->db->dbprefix.$table, $column_name); if ($sql === FALSE) { @@ -656,18 +623,10 @@ abstract class CI_DB_forge { * @param string $field Column definition * @return bool */ - public function modify_column($table = '', $field = array()) + public function modify_column($table, $field) { - if ($table === '') - { - show_error('A table name is required for that operation.'); - } - // Work-around for literal column definitions - if ( ! is_array($field)) - { - $field = array($field); - } + is_array($field) OR $field = array($field); foreach (array_keys($field) as $k) { -- cgit v1.2.3-24-g4f1b From fe9309d22c1b088f5363954d6dac013c8c955894 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 9 Jan 2015 17:48:58 +0200 Subject: Bulk (mostly documentation) update - Remove PHP version from license notices - Bump year number in copyright notices - Recommend PHP 5.4 or newer to be used - Tell Travis-CI to test on PHP 5.3.0 instead of the latest 5.3 version Related: #3450 --- system/database/DB_forge.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 4238e37ee..4ca980810 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -2,11 +2,11 @@ /** * CodeIgniter * - * An open source application development framework for PHP 5.2.4 or newer + * An open source application development framework for PHP * * This content is released under the MIT License (MIT) * - * Copyright (c) 2014, British Columbia Institute of Technology + * Copyright (c) 2014 - 2015, British Columbia Institute of Technology * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ * @package CodeIgniter * @author EllisLab Dev Team * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) - * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) + * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) * @license http://opensource.org/licenses/MIT MIT License * @link http://codeigniter.com * @since Version 1.0.0 -- cgit v1.2.3-24-g4f1b From 90726b8c769ea75aec34814ddfa91655d488e6c3 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 20 Jan 2015 12:39:22 +0200 Subject: [ci skip] Change some log messages' level 'Class Loaded' type of messages flood log files when log_threshold is set to 2 (debug). They're now logged as 'info' level. This is manually applying PR #1528, which was created to do the same thing, but became outdated. --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 4ca980810..41f9bf5e2 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -167,7 +167,7 @@ abstract class CI_DB_forge { public function __construct(&$db) { $this->db =& $db; - log_message('debug', 'Database Forge Class Initialized'); + log_message('info', 'Database Forge Class Initialized'); } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 4cbe463b4c442e0e2dae2f43565e77f7ac5ecb86 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Wed, 21 Jan 2015 22:56:22 +0100 Subject: Remove closing blocks at end of PHP files --- system/database/DB_forge.php | 3 --- 1 file changed, 3 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 41f9bf5e2..c8d773c98 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -1033,6 +1033,3 @@ abstract class CI_DB_forge { } } - -/* End of file DB_forge.php */ -/* Location: ./system/database/DB_forge.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b From ba6bd220d9ff5976caf979cb70ffb0a005020eba Mon Sep 17 00:00:00 2001 From: Claudio Galdiolo Date: Thu, 29 Jan 2015 11:43:25 -0500 Subject: fix typo in comments --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index c8d773c98..f6ee2a63a 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -828,7 +828,7 @@ abstract class CI_DB_forge { */ protected function _attr_type(&$attributes) { - // Usually overriden by drivers + // Usually overridden by drivers } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b From 441d3536b79bc53c80dd710a803ac5d0baba6c18 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 2 Jul 2015 11:34:20 +0300 Subject: Close #3941 --- system/database/DB_forge.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index f6ee2a63a..d99fd0024 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -453,12 +453,7 @@ abstract class CI_DB_forge { return ($this->db->db_debug) ? $this->db->display_error('db_table_name_required') : FALSE; } - $query = $this->_drop_table($this->db->dbprefix.$table_name, $if_exists); - if ($query === FALSE) - { - return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; - } - elseif ($query === TRUE) + if (($query = $this->_drop_table($this->db->dbprefix.$table_name, $if_exists)) === TRUE) { return TRUE; } -- cgit v1.2.3-24-g4f1b From 7cc6cea2d421862726081a39e932dbceeefcc775 Mon Sep 17 00:00:00 2001 From: Adrian Voicu Date: Fri, 10 Jul 2015 14:41:25 +0300 Subject: allow add of keys with array This will allow adding multiple keys using array (http://www.codeigniter.com/user_guide/database/forge.html#adding-keys). Only if user wants, he can use the table columns to set a primary key by setting second parameter as TRUE. --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index d99fd0024..865498fb5 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -239,7 +239,7 @@ abstract class CI_DB_forge { */ public function add_key($key, $primary = FALSE) { - if ($primary === TRUE && is_array($key)) + if (is_array($key)) { foreach ($key as $one) { -- cgit v1.2.3-24-g4f1b From 55bc50578b9f1aa3fd71cb427848b21748655690 Mon Sep 17 00:00:00 2001 From: Calvin Tam Date: Fri, 24 Jul 2015 02:27:24 -0700 Subject: Fixed typos --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 865498fb5..dde285598 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -143,7 +143,7 @@ abstract class CI_DB_forge { protected $_unsigned = TRUE; /** - * NULL value representatin in CREATE/ALTER TABLE statements + * NULL value representation in CREATE/ALTER TABLE statements * * @var string */ -- cgit v1.2.3-24-g4f1b From 95f815745855a5ac365595eb44abbda11766cfe5 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 19 Oct 2015 13:16:19 +0300 Subject: Fix #4173 This reverts commit 7cc6cea2d421862726081a39e932dbceeefcc775 from PR #3968. At the time this seemed logical, but turns out it breaks the ability to create non-PRIMARY composite keys, so ... --- system/database/DB_forge.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index dde285598..f9cf76a14 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -239,7 +239,13 @@ abstract class CI_DB_forge { */ public function add_key($key, $primary = FALSE) { - if (is_array($key)) + // DO NOT change this! This condition is only applicable + // for PRIMARY keys because you can only have one such, + // and therefore all fields you add to it will be included + // in the same, composite PRIMARY KEY. + // + // It's not the same for regular indexes. + if ($primary === TRUE && is_array($key)) { foreach ($key as $one) { -- cgit v1.2.3-24-g4f1b From 9e2c7b9dd7e87a94c5a3696bc326cbbec5da7bb8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 16 Nov 2015 15:44:24 +0200 Subject: [ci skip] Fix #4245 --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index f9cf76a14..d2302b2f2 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -781,7 +781,7 @@ abstract class CI_DB_forge { case 'SET': $attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']); $field['length'] = is_array($attributes['CONSTRAINT']) - ? "('".implode("','", $attributes['CONSTRAINT'])."')" + ? "(".implode(",", $attributes['CONSTRAINT']).")" : '('.$attributes['CONSTRAINT'].')'; break; default: -- cgit v1.2.3-24-g4f1b From 422fd592428d6048e9a75868fa3e75527506dbb7 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 24 Nov 2015 11:36:29 +0200 Subject: [ci skip] Remove some redundant code from DB_forge --- system/database/DB_forge.php | 4 ---- 1 file changed, 4 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index d2302b2f2..1546e40c0 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -780,10 +780,6 @@ abstract class CI_DB_forge { case 'ENUM': case 'SET': $attributes['CONSTRAINT'] = $this->db->escape($attributes['CONSTRAINT']); - $field['length'] = is_array($attributes['CONSTRAINT']) - ? "(".implode(",", $attributes['CONSTRAINT']).")" - : '('.$attributes['CONSTRAINT'].')'; - break; default: $field['length'] = is_array($attributes['CONSTRAINT']) ? '('.implode(',', $attributes['CONSTRAINT']).')' -- cgit v1.2.3-24-g4f1b From 125ef4751080a2118cb203357d77687699e3eb25 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 11 Jan 2016 12:33:00 +0200 Subject: [ci skip] Bump year to 2016 --- system/database/DB_forge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 1546e40c0..6fc1e1f2b 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -6,7 +6,7 @@ * * This content is released under the MIT License (MIT) * - * Copyright (c) 2014 - 2015, British Columbia Institute of Technology + * Copyright (c) 2014 - 2016, British Columbia Institute of Technology * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ * @package CodeIgniter * @author EllisLab Dev Team * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) - * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) + * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) * @license http://opensource.org/licenses/MIT MIT License * @link http://codeigniter.com * @since Version 1.0.0 -- cgit v1.2.3-24-g4f1b From bd202c91b0e9cf0a8c93bcaa71df9574f5909346 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 11 Jan 2016 12:50:18 +0200 Subject: [ci skip] Update codeigniter.com links to https --- system/database/DB_forge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 6fc1e1f2b..4392eb25f 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -31,7 +31,7 @@ * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) * @license http://opensource.org/licenses/MIT MIT License - * @link http://codeigniter.com + * @link https://codeigniter.com * @since Version 1.0.0 * @filesource */ @@ -42,7 +42,7 @@ defined('BASEPATH') OR exit('No direct script access allowed'); * * @category Database * @author EllisLab Dev Team - * @link http://codeigniter.com/user_guide/database/ + * @link https://codeigniter.com/user_guide/database/ */ abstract class CI_DB_forge { -- cgit v1.2.3-24-g4f1b From 1924e879b165fb119847a49a7a5eab2f28295fa2 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 11 Jan 2016 12:55:34 +0200 Subject: [ci skip] Update ellislab.com links to https too --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 4392eb25f..826aa1ebf 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -28,7 +28,7 @@ * * @package CodeIgniter * @author EllisLab Dev Team - * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) * @license http://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com -- cgit v1.2.3-24-g4f1b From da270b26d7cb9c55385150659ecfb7d2d97b4c63 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 17 Oct 2016 18:22:43 +0300 Subject: Fix #4851 --- system/database/DB_forge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 826aa1ebf..ed6f4b672 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -184,7 +184,7 @@ abstract class CI_DB_forge { { return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } - elseif ( ! $this->db->query(sprintf($this->_create_database, $db_name, $this->db->char_set, $this->db->dbcollat))) + elseif ( ! $this->db->query(sprintf($this->_create_database, $this->db->escape_identifiers($db_name), $this->db->char_set, $this->db->dbcollat))) { return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; } @@ -211,7 +211,7 @@ abstract class CI_DB_forge { { return ($this->db->db_debug) ? $this->db->display_error('db_unsupported_feature') : FALSE; } - elseif ( ! $this->db->query(sprintf($this->_drop_database, $db_name))) + elseif ( ! $this->db->query(sprintf($this->_drop_database, $this->db->escape_identifiers($db_name)))) { return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; } -- cgit v1.2.3-24-g4f1b From da60e9bc66ec90970fbd2dfd08b0a6e66b9f5f5f Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Sat, 31 Dec 2016 08:46:18 -0800 Subject: Update copyright data to 2017 --- system/database/DB_forge.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index ed6f4b672..2b2ff1c24 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -6,7 +6,7 @@ * * This content is released under the MIT License (MIT) * - * Copyright (c) 2014 - 2016, British Columbia Institute of Technology + * Copyright (c) 2014 - 2017, British Columbia Institute of Technology * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -29,7 +29,7 @@ * @package CodeIgniter * @author EllisLab Dev Team * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) - * @copyright Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/) + * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/) * @license http://opensource.org/licenses/MIT MIT License * @link https://codeigniter.com * @since Version 1.0.0 -- cgit v1.2.3-24-g4f1b From 593ce680b87fadb05af6ba13c857ef8b16303bcf Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 3 Jan 2017 12:40:32 +0200 Subject: [ci skip] Fix 4953 --- system/database/DB_forge.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 2b2ff1c24..7289235c8 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -348,7 +348,7 @@ abstract class CI_DB_forge { if (($result = $this->db->query($sql)) !== FALSE) { - empty($this->db->data_cache['table_names']) OR $this->db->data_cache['table_names'][] = $table; + isset($this->db->data_cache['table_names']) && $this->db->data_cache['table_names'][] = $table; // Most databases don't support creating indexes from within the CREATE TABLE statement if ( ! empty($this->keys)) -- cgit v1.2.3-24-g4f1b From ee9d428171dc201f51eaffdb62616312915681ff Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 5 Jun 2017 10:44:37 +0300 Subject: [ci skip] Merge pull request #5143 from TysonAndre/misc-phpdoc-nits Fix misc inconsistencies between code and doc comments --- system/database/DB_forge.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'system/database/DB_forge.php') diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index 7289235c8..3cb02ca4e 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -488,7 +488,7 @@ abstract class CI_DB_forge { * * @param string $table Table name * @param bool $if_exists Whether to add an IF EXISTS condition - * @return string + * @return mixed (Returns a platform-specific DROP table string, or TRUE to indicate there's nothing to do) */ protected function _drop_table($table, $if_exists) { @@ -979,8 +979,8 @@ abstract class CI_DB_forge { /** * Process indexes * - * @param string $table - * @return string + * @param string $table Table name + * @return string[] list of SQL statements */ protected function _process_indexes($table) { -- cgit v1.2.3-24-g4f1b