From 3c0846b019ed533852a148eb68c62a02c03d27a8 Mon Sep 17 00:00:00 2001 From: bigCat Date: Tue, 21 Aug 2012 00:20:20 +0800 Subject: China's biggest ICP China Telecom will hijack user and leave a cookie contains | . such as "1345466626|7601294|43373|0|0|0" it's impossible to fix this shit... --- system/core/Input.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/core/Input.php b/system/core/Input.php index 968a42a9a..d7bfed3f8 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -620,7 +620,7 @@ class CI_Input { */ protected function _clean_input_keys($str) { - if ( ! preg_match('/^[a-z0-9:_\/-]+$/i', $str)) + if ( ! preg_match('/^[a-z0-9:_\/|-]+$/i', $str)) { set_status_header(503); exit('Disallowed Key Characters.'); -- 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(-) 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 fc2fbc55fbb234b842d9e9462e766211588d4880 Mon Sep 17 00:00:00 2001 From: Razican Date: Sat, 3 Nov 2012 13:52:34 +0100 Subject: Fixed typo in logs. --- system/libraries/Log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/libraries/Log.php b/system/libraries/Log.php index a4dd47f86..e66270840 100644 --- a/system/libraries/Log.php +++ b/system/libraries/Log.php @@ -152,7 +152,7 @@ class CI_Log { if ( ! file_exists($filepath)) { $newfile = TRUE; - $message .= '<'."?php if defined('BASEPATH') OR exit('No direct script access allowed'); ?".">\n\n"; + $message .= '<'."?php defined('BASEPATH') OR exit('No direct script access allowed'); ?".">\n\n"; } if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE)) -- cgit v1.2.3-24-g4f1b From cdac248e9cf7a8ea3ed426f189bb52254800bc2a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 3 Nov 2012 18:09:01 +0200 Subject: Revert 679525d0237ac2e0a94d7b05377eb31eb3398f19 It appears to break get_instance()->*_package_path*() usage which is very common. Need to figure out how to resolve this. --- system/core/Controller.php | 1 + system/core/Loader.php | 25 +++++++++++++++++++------ tests/codeigniter/core/Loader_test.php | 2 +- tests/mocks/ci_testcase.php | 2 -- user_guide_src/source/changelog.rst | 3 +-- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/system/core/Controller.php b/system/core/Controller.php index cbdf0515f..ee6fec8d5 100644 --- a/system/core/Controller.php +++ b/system/core/Controller.php @@ -65,6 +65,7 @@ class CI_Controller { } $this->load =& load_class('Loader', 'core'); + $this->load->initialize(); log_message('debug', 'Controller Class Initialized'); } diff --git a/system/core/Loader.php b/system/core/Loader.php index 2a78f4153..88fbdb6e1 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -130,10 +130,8 @@ class CI_Loader { /** * Class constructor * - * Sets component load paths, gets the initial output buffering level - * and calls the autoloader. + * Sets component load paths, gets the initial output buffering level. * - * @uses CI_Loader::_ci_autoloader() * @return void */ public function __construct() @@ -143,14 +141,29 @@ class CI_Loader { $this->_ci_helper_paths = array(APPPATH, BASEPATH); $this->_ci_model_paths = array(APPPATH); $this->_ci_view_paths = array(VIEWPATH => TRUE); - $this->_base_classes =& is_loaded(); - $this->_ci_autoloader(); log_message('debug', 'Loader Class Initialized'); } // -------------------------------------------------------------------- + /** + * Initializer + * + * @todo Figure out a way to move this to the constructor + * without breaking *package_path*() methods. + * @uses CI_Loader::_ci_autoloader() + * @used-by CI_Controller::__construct() + * @return void + */ + public function initialize() + { + $this->_base_classes =& is_loaded(); + $this->_ci_autoloader(); + } + + // -------------------------------------------------------------------- + /** * Is Loaded * @@ -1134,7 +1147,7 @@ class CI_Loader { * * Loads component listed in the config/autoload.php file. * - * @used-by CI_Loader::__construct() + * @used-by CI_Loader::initialize() * @return void */ protected function _ci_autoloader() diff --git a/tests/codeigniter/core/Loader_test.php b/tests/codeigniter/core/Loader_test.php index be223f97d..ecc5ca933 100644 --- a/tests/codeigniter/core/Loader_test.php +++ b/tests/codeigniter/core/Loader_test.php @@ -493,7 +493,7 @@ class Loader_test extends CI_TestCase { ); $this->ci_vfs_create('autoload', 'ci_app_root, 'config'); - $this->load->__construct(); + $this->load->initialize(); // Verify path $this->assertContains($path, $this->load->get_package_paths()); diff --git a/tests/mocks/ci_testcase.php b/tests/mocks/ci_testcase.php index 2d0a26830..f16492945 100644 --- a/tests/mocks/ci_testcase.php +++ b/tests/mocks/ci_testcase.php @@ -39,8 +39,6 @@ class CI_TestCase extends PHPUnit_Framework_TestCase { $this->ci_app_root = vfsStream::newDirectory('application')->at($this->ci_vfs_root); $this->ci_base_root = vfsStream::newDirectory('system')->at($this->ci_vfs_root); $this->ci_view_root = vfsStream::newDirectory('views')->at($this->ci_app_root); - vfsStream::newDirectory('config')->at($this->ci_app_root); - $this->ci_vfs_clone('application/config/autoload.php'); if (method_exists($this, 'set_up')) { diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index abb75f579..cbc6295c8 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -182,7 +182,7 @@ Release Date: Not Released - Added method ``remove()`` to remove a cart item, updating with quantity of 0 seemed like a hack but has remained to retain compatibility. - Added method ``get_item()`` to enable retrieving data for a single cart item. - :doc:`Image Manipulation library ` changes include: - - The initialize() method now only sets existing class properties. + - The ``initialize()`` method now only sets existing class properties. - Added support for 3-length hex color values for *wm_font_color* and *wm_shadow_color* properties, as well as validation for them. - Class properties *wm_font_color*, *wm_shadow_color* and *wm_use_drop_shadow* are now protected, to avoid breaking the ``text_watermark()`` method if they are set manually after initialization. - If property *maintain_ratio* is set to TRUE, ``image_reproportion()`` now doesn't need both width and height to be specified. @@ -245,7 +245,6 @@ Release Date: Not Released - ``library()`` method will now load drivers as well, for backward compatibility of converted libraries (like :doc:`Session `). - ``$config['rewrite_short_tags']`` now has no effect when using PHP 5.4 as ``` changes include: - Added ``method()`` to retrieve ``$_SERVER['REQUEST_METHOD']``. - Added support for arrays and network addresses (e.g. 192.168.1.1/24) for use with the *proxy_ips* setting. -- cgit v1.2.3-24-g4f1b From 751f2479460c6f5cfc5333f6561d9827a92089bd Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sat, 3 Nov 2012 18:26:27 +0200 Subject: Fix #1957 --- system/libraries/Form_validation.php | 11 ++++++++--- user_guide_src/source/libraries/form_validation.rst | 3 --- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 50708c7d8..e50eee4f2 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -440,11 +440,10 @@ class CI_Form_validation { // Load the language file containing error messages $this->CI->lang->load('form_validation'); - // Cycle through the rules for each field, match the - // corresponding $_POST item and test for errors + // Cycle through the rules for each field and match the corresponding $validation_data item foreach ($this->_field_data as $field => $row) { - // Fetch the data from the corresponding $_POST or validation array and cache it in the _field_data array. + // Fetch the data from the validation_data array item and cache it in the _field_data array. // Depending on whether the field name is an array or a string will determine where we get it from. if ($row['is_array'] === TRUE) { @@ -454,7 +453,13 @@ class CI_Form_validation { { $this->_field_data[$field]['postdata'] = $validation_array[$field]; } + } + // Execute validation rules + // Note: A second foreach (for now) is required in order to avoid false-positives + // for rules like 'matches', which correlate to other validation fields. + foreach ($this->_field_data as $field => $row) + { // Don't try to validate if we have no rules set if (empty($row['rules'])) { diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst index c010eb578..a3a35b499 100644 --- a/user_guide_src/source/libraries/form_validation.rst +++ b/user_guide_src/source/libraries/form_validation.rst @@ -910,9 +910,6 @@ Rule Parameter Description to two parameters, where at least one is required (to pass the field data). -.. note:: When using the **matches** rule, the form item specified - to compare against must already be defined. - ****************** Prepping Reference ****************** -- cgit v1.2.3-24-g4f1b From a3f5c5c78f6172565e3264764e1de7dcad894b0a Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 4 Nov 2012 20:08:42 +0200 Subject: Fix #98 --- system/core/Router.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/core/Router.php b/system/core/Router.php index 67e9b300d..30bf39657 100644 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -147,7 +147,7 @@ class CI_Router { // Set the default controller so we can display it in the event // the URI doesn't correlated to a valid controller. - $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']); + $this->default_controller = empty($this->routes['default_controller']) ? FALSE : $this->routes['default_controller']; // Were there any query string segments? If so, we'll validate them and bail out since we're done. if (count($segments) > 0) -- cgit v1.2.3-24-g4f1b From 7b18a3f268ba622cf938ac460d07bd58cb1eea06 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Sun, 4 Nov 2012 20:27:35 +0200 Subject: Fix #708 --- user_guide_src/source/general/common_functions.rst | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index f3d48ac91..7f327f00b 100644 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -46,10 +46,14 @@ recommended on platforms where this information may be unreliable. config_item('item_key') ======================= -The :doc:`Config library <../libraries/config>` is the preferred way of -accessing configuration information, however config_item() can be used -to retrieve single keys. See Config library documentation for more -information. +The :doc:`Config Library <../libraries/config>` is the preferred way of +accessing configuration information, however ``config_item()`` can be used +to retrieve single keys. See :doc:`Config Library <../libraries/config>` +documentation for more information. + +.. important:: This function only returns values set in your configuration + files. It does not take into account config values that are + dynamically set at runtime. show_error('message'), show_404('page'), log_message('level', 'message') ======================================================================== -- cgit v1.2.3-24-g4f1b From 522c73623b46afc9082ac7c8af5c34bf1b4f47f4 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 5 Nov 2012 16:40:32 +0200 Subject: Revert usage of is_callable() in system/core/CodeIgniter.php Seems to be causing issues (see #1970). Also updated the Controller docs, mainly to include an important note related to #1967. --- system/core/CodeIgniter.php | 6 +- user_guide_src/source/general/controllers.rst | 152 ++++++++++++++------------ 2 files changed, 86 insertions(+), 72 deletions(-) diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 1cd6403bf..89081b572 100644 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -295,7 +295,11 @@ defined('BASEPATH') OR exit('No direct script access allowed'); } else { - if ( ! is_callable(array($class, $method))) + // WARNING: It appears that there are issues with is_callable() even in PHP 5.2! + // Furthermore, there are bug reports and feature/change requests related to it + // that make it unreliable to use in this context. Please, DO NOT change this + // work-around until a better alternative is available. + if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($class)), TRUE)) { if (empty($RTR->routes['404_override'])) { diff --git a/user_guide_src/source/general/controllers.rst b/user_guide_src/source/general/controllers.rst index 6e5079419..150d2269c 100644 --- a/user_guide_src/source/general/controllers.rst +++ b/user_guide_src/source/general/controllers.rst @@ -67,21 +67,21 @@ This is **not** valid:: ?> Also, always make sure your controller extends the parent controller -class so that it can inherit all its functions. +class so that it can inherit all its methods. -Functions -========= +Methods +======= -In the above example the function name is index(). The "index" function +In the above example the method name is ``index()``. The "index" method is always loaded by default if the **second segment** of the URI is empty. Another way to show your "Hello World" message would be this:: example.com/index.php/blog/index/ -**The second segment of the URI determines which function in the +**The second segment of the URI determines which method in the controller gets called.** -Let's try it. Add a new function to your controller:: +Let's try it. Add a new method to your controller:: -Now load the following URL to see the comment function:: +Now load the following URL to see the comment method:: example.com/index.php/blog/comments/ You should see your new message. -Passing URI Segments to your Functions -====================================== +Passing URI Segments to your methods +==================================== If your URI contains more then two segments they will be passed to your -function as parameters. +method as parameters. For example, lets say you have a URI like this:: example.com/index.php/products/shoes/sandals/123 -Your function will be passed URI segments 3 and 4 ("sandals" and "123"):: +Your method will be passed URI segments 3 and 4 ("sandals" and "123"):: .. important:: If you are using the :doc:`URI Routing ` - feature, the segments passed to your function will be the re-routed + feature, the segments passed to your method will be the re-routed ones. Defining a Default Controller @@ -145,53 +145,53 @@ Where Blog is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you'll see your Hello World message by default. -Remapping Function Calls -======================== +Remapping Method Calls +====================== As noted above, the second segment of the URI typically determines which -function in the controller gets called. CodeIgniter permits you to -override this behavior through the use of the _remap() function:: +method in the controller gets called. CodeIgniter permits you to override +this behavior through the use of the ``_remap()`` method:: public function _remap() { - // Some code here... + // Some code here... } -.. important:: If your controller contains a function named _remap(), +.. important:: If your controller contains a method named _remap(), it will **always** get called regardless of what your URI contains. It - overrides the normal behavior in which the URI determines which function - is called, allowing you to define your own function routing rules. + overrides the normal behavior in which the URI determines which method + is called, allowing you to define your own method routing rules. -The overridden function call (typically the second segment of the URI) -will be passed as a parameter to the _remap() function:: +The overridden method call (typically the second segment of the URI) will +be passed as a parameter to the ``_remap()`` method:: public function _remap($method) { - if ($method == 'some_method') - { - $this->$method(); - } - else - { - $this->default_method(); - } + if ($method == 'some_method') + { + $this->$method(); + } + else + { + $this->default_method(); + } } -Any extra segments after the method name are passed into _remap() as an +Any extra segments after the method name are passed into ``_remap()`` as an optional second parameter. This array can be used in combination with -PHP's `call_user_func_array `_ +PHP's `call_user_func_array() `_ to emulate CodeIgniter's default behavior. :: public function _remap($method, $params = array()) { - $method = 'process_'.$method; - if (method_exists($this, $method)) - { - return call_user_func_array(array($this, $method), $params); - } - show_404(); + $method = 'process_'.$method; + if (method_exists($this, $method)) + { + return call_user_func_array(array($this, $method), $params); + } + show_404(); } Processing Output @@ -199,29 +199,29 @@ Processing Output CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically. More information on this -can be found in the :doc:`Views ` and :doc:`Output class <../libraries/output>` pages. In some cases, however, you -might want to post-process the finalized data in some way and send it to -the browser yourself. CodeIgniter permits you to add a function named -_output() to your controller that will receive the finalized output -data. +can be found in the :doc:`Views ` and :doc:`Output Class +<../libraries/output>` pages. In some cases, however, you might want to +post-process the finalized data in some way and send it to the browser +yourself. CodeIgniter permits you to add a method named ``_output()`` +to your controller that will receive the finalized output data. -.. important:: If your controller contains a function named _output(), - it will **always** be called by the output class instead of echoing the - finalized data directly. The first parameter of the function will - contain the finalized output. +.. important:: If your controller contains a method named _output(), it + will **always** be called by the output class instead of echoing + the finalized data directly. The first parameter of the method + will contain the finalized output. Here is an example:: public function _output($output) { - echo $output; + echo $output; } -.. note:: Please note that your _output() function will receive the data in its +.. note:: Please note that your _output() method will receive the data in its finalized state. Benchmark and memory usage data will be rendered, cache files written (if you have caching enabled), and headers will be sent (if you use that :doc:`feature <../libraries/output>`) before it is - handed off to the _output() function. + handed off to the _output() method. To have your controller's output cached properly, its _output() method can use:: @@ -236,23 +236,27 @@ Here is an example:: output *before* any of the final processing is done, please see the available methods in the :doc:`Output Class <../libraries/output>`. -Private Functions -================= +Private methods +=============== -In some cases you may want certain functions hidden from public access. -To make a function private, simply add an underscore as the name prefix -and it will not be served via a URL request. For example, if you were to -have a function like this:: +In some cases you may want certain methods hidden from public access. +In order to achieve this, simply declare the method as being private +or protected and it will not be served via a URL request. For example, +if you were to have a method like this:: private function _utility() { - // some code + // some code } Trying to access it via the URL, like this, will not work:: example.com/index.php/blog/_utility/ +.. note:: Prefixing method names with an underscore will also prevent + them from being called. This is a legacy feature that is left + for backwards-compatibility. + Organizing Your Controllers into Sub-folders ============================================ @@ -297,11 +301,11 @@ manually call it. @@ -309,16 +313,22 @@ Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. Constructors can't return a value, but they can do some default work. -Reserved Function Names -======================= +Reserved method names +===================== Since your controller classes will extend the main application -controller you must be careful not to name your functions identically to +controller you must be careful not to name your methods identically to the ones used by that class, otherwise your local functions will override them. See :doc:`Reserved Names ` for a full list. +.. important:: You should also never have a method named identically + to its class name. If you do, and there is no __construct() + method in the same class, then your e.g. Index::index() method + will be executed as a class constructor! This is a PHP4 + backwards-compatibility feature. + That's it! ========== -That, in a nutshell, is all there is to know about controllers. +That, in a nutshell, is all there is to know about controllers. \ No newline at end of file -- cgit v1.2.3-24-g4f1b From 2b73037e450859e85fb468ad7499a26882a67292 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 5 Nov 2012 17:01:11 +0200 Subject: Fix DB drivers version() implementations that don't execute a query Fails if called prior to the DB connection initialization. --- system/database/drivers/cubrid/cubrid_driver.php | 15 ++++++++++++--- system/database/drivers/mysql/mysql_driver.php | 18 +++++++++++++++--- system/database/drivers/mysqli/mysqli_driver.php | 13 ++++++++++--- system/database/drivers/oci8/oci8_driver.php | 18 +++++++++++++++--- system/database/drivers/pdo/pdo_driver.php | 4 ++++ system/database/drivers/postgre/postgre_driver.php | 6 +++++- system/database/drivers/sqlsrv/sqlsrv_driver.php | 6 +++++- 7 files changed, 66 insertions(+), 14 deletions(-) diff --git a/system/database/drivers/cubrid/cubrid_driver.php b/system/database/drivers/cubrid/cubrid_driver.php index 48b904286..c89a924d3 100644 --- a/system/database/drivers/cubrid/cubrid_driver.php +++ b/system/database/drivers/cubrid/cubrid_driver.php @@ -179,9 +179,18 @@ class CI_DB_cubrid_driver extends CI_DB { */ public function version() { - return isset($this->data_cache['version']) - ? $this->data_cache['version'] - : $this->data_cache['version'] = cubrid_get_server_info($this->conn_id); + if (isset($this->data_cache['version'])) + { + return $this->data_cache['version']; + } + elseif ( ! $this->conn_id) + { + $this->initialize(); + } + + return ( ! $this->conn_id OR ($version = cubrid_get_server_info($this->conn_id)) === FALSE) + ? FALSE + : $this->data_cache['version'] = $version; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index 2457e558c..b1edc2925 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -191,9 +191,21 @@ class CI_DB_mysql_driver extends CI_DB { */ public function version() { - return isset($this->data_cache['version']) - ? $this->data_cache['version'] - : $this->data_cache['version'] = @mysql_get_server_info($this->conn_id); + if (isset($this->data_cache['version'])) + { + return $this->data_cache['version']; + } + elseif ( ! $this->conn_id) + { + $this->initialize(); + } + + if ( ! $this->conn_id OR ($version = @mysql_get_server_info($this->conn_id)) === FALSE) + { + return FALSE; + } + + return $this->data_cache['version'] = $version; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index 7d2507b40..ae17703ac 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -173,9 +173,16 @@ class CI_DB_mysqli_driver extends CI_DB { */ public function version() { - return isset($this->data_cache['version']) - ? $this->data_cache['version'] - : $this->data_cache['version'] = $this->conn_id->server_info; + if (isset($this->data_cache['version'])) + { + return $this->data_cache['version']; + } + elseif ( ! $this->conn_id) + { + $this->initialize(); + } + + return $this->data_cache['version'] = $this->conn_id->server_info; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index 38d2395b6..b2663e2c5 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -240,9 +240,21 @@ class CI_DB_oci8_driver extends CI_DB { */ public function version() { - return isset($this->data_cache['version']) - ? $this->data_cache['version'] - : $this->data_cache['version'] = oci_server_version($this->conn_id); + if (isset($this->data_cache['version'])) + { + return $this->data_cache['version']; + } + elseif ( ! $this->conn_id) + { + $this->initialize(); + } + + if ( ! $this->conn_id OR ($version = oci_server_version($this->conn_id)) === FALSE) + { + return FALSE; + } + + return $this->data_cache['version'] = $version; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/pdo_driver.php b/system/database/drivers/pdo/pdo_driver.php index 57a3c5c98..923f0e125 100644 --- a/system/database/drivers/pdo/pdo_driver.php +++ b/system/database/drivers/pdo/pdo_driver.php @@ -165,6 +165,10 @@ class CI_DB_pdo_driver extends CI_DB { { return $this->data_cache['version']; } + elseif ( ! $this->conn_id) + { + $this->initialize(); + } // Not all subdrivers support the getAttribute() method try diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index 028dc1996..40f21b1e8 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -204,8 +204,12 @@ class CI_DB_postgre_driver extends CI_DB { { return $this->data_cache['version']; } + elseif ( ! $this->conn_id) + { + $this->initialize(); + } - if (($pg_version = pg_version($this->conn_id)) === FALSE) + if ( ! $this->conn_id OR ($pg_version = pg_version($this->conn_id)) === FALSE) { return FALSE; } diff --git a/system/database/drivers/sqlsrv/sqlsrv_driver.php b/system/database/drivers/sqlsrv/sqlsrv_driver.php index de3d87312..d3999bf75 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_driver.php +++ b/system/database/drivers/sqlsrv/sqlsrv_driver.php @@ -275,8 +275,12 @@ class CI_DB_sqlsrv_driver extends CI_DB { { return $this->data_cache['version']; } + elseif ( ! $this->conn_id) + { + $this->initialize(); + } - if (($info = sqlsrv_server_info($this->conn_id)) === FALSE) + if ( ! $this->conn_id OR ($info = sqlsrv_server_info($this->conn_id)) === FALSE) { return FALSE; } -- 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/core/Loader.php | 15 +- system/database/DB_forge.php | 631 +++++++++++++++++++-- system/database/drivers/cubrid/cubrid_forge.php | 214 +++---- system/database/drivers/ibase/ibase_forge.php | 204 ++++--- system/database/drivers/mssql/mssql_forge.php | 160 +++--- system/database/drivers/mysql/mysql_forge.php | 207 +++---- system/database/drivers/mysqli/mysqli_forge.php | 207 +++---- system/database/drivers/oci8/oci8_forge.php | 131 ++--- system/database/drivers/odbc/odbc_forge.php | 126 +--- system/database/drivers/pdo/pdo_forge.php | 131 +---- .../drivers/pdo/subdrivers/pdo_4d_forge.php | 209 +++++++ .../drivers/pdo/subdrivers/pdo_cubrid_forge.php | 173 ++++++ .../drivers/pdo/subdrivers/pdo_dblib_forge.php | 136 +++++ .../drivers/pdo/subdrivers/pdo_firebird_forge.php | 228 ++++++++ .../drivers/pdo/subdrivers/pdo_ibm_forge.php | 146 +++++ .../drivers/pdo/subdrivers/pdo_informix_forge.php | 155 +++++ .../drivers/pdo/subdrivers/pdo_mysql_forge.php | 198 +++++++ .../drivers/pdo/subdrivers/pdo_oci_forge.php | 133 +++++ .../drivers/pdo/subdrivers/pdo_odbc_forge.php | 62 ++ .../drivers/pdo/subdrivers/pdo_pgsql_forge.php | 193 +++++++ .../drivers/pdo/subdrivers/pdo_sqlite_forge.php | 228 ++++++++ .../drivers/pdo/subdrivers/pdo_sqlsrv_forge.php | 136 +++++ system/database/drivers/postgre/postgre_forge.php | 245 ++++---- system/database/drivers/sqlite/sqlite_forge.php | 192 +++---- system/database/drivers/sqlite3/sqlite3_forge.php | 201 +++---- system/database/drivers/sqlsrv/sqlsrv_forge.php | 160 +++--- tests/mocks/database/db/driver.php | 1 + tests/mocks/database/schema/skeleton.php | 6 +- user_guide_src/source/changelog.rst | 2 + user_guide_src/source/database/forge.rst | 14 +- user_guide_src/source/installation/upgrade_300.rst | 30 +- 31 files changed, 3487 insertions(+), 1387 deletions(-) create mode 100644 system/database/drivers/pdo/subdrivers/pdo_4d_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_cubrid_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_dblib_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_firebird_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_ibm_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_informix_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_oci_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_odbc_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_pgsql_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php create mode 100644 system/database/drivers/pdo/subdrivers/pdo_sqlsrv_forge.php diff --git a/system/core/Loader.php b/system/core/Loader.php index 88fbdb6e1..808fa80df 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -392,7 +392,20 @@ class CI_Loader { require_once(BASEPATH.'database/DB_forge.php'); require_once(BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/'.$CI->db->dbdriver.'_forge.php'); - $class = 'CI_DB_'.$CI->db->dbdriver.'_forge'; + + if ( ! empty($CI->db->subdriver)) + { + $driver_path = BASEPATH.'database/drivers/'.$CI->db->dbdriver.'/subdrivers/'.$CI->db->dbdriver.'_'.$CI->db->subdriver.'_forge.php'; + if (file_exists($driver_path)) + { + require_once($driver_path); + $class = 'CI_DB_'.$CI->db->dbdriver.'_'.$CI->db->subdriver.'_forge'; + } + } + else + { + $class = 'CI_DB_'.$CI->db->dbdriver.'_forge'; + } $CI->dbforge = new $class(); } 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; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/cubrid/cubrid_forge.php b/system/database/drivers/cubrid/cubrid_forge.php index 33d502137..e3f8bc334 100644 --- a/system/database/drivers/cubrid/cubrid_forge.php +++ b/system/database/drivers/cubrid/cubrid_forge.php @@ -42,8 +42,6 @@ class CI_DB_cubrid_forge extends CI_DB_forge { */ protected $_create_database = FALSE; - // -------------------------------------------------------------------- - /** * DROP DATABASE statement * @@ -52,177 +50,121 @@ class CI_DB_cubrid_forge extends CI_DB_forge { protected $_drop_database = FALSE; /** - * Process Fields + * CREATE TABLE IF statement * - * @param mixed $fields - * @return string + * @var string + */ + protected $_create_table_if = FALSE; + + /** + * UNSIGNED support + * + * @var array */ - protected function _process_fields($fields) + protected $_unsigned = array( + 'SHORT' => 'INTEGER', + 'SMALLINT' => 'INTEGER', + 'INT' => 'BIGINT', + 'INTEGER' => 'BIGINT', + 'BIGINT' => 'NUMERIC', + 'FLOAT' => 'DOUBLE', + 'REAL' => 'DOUBLE' + ); + + // -------------------------------------------------------------------- + + /** + * 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) { - $current_field_count = 0; - $sql = ''; + if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } - foreach ($fields as $field => $attributes) + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) + if ($field[$i]['_literal'] !== FALSE) { - $sql .= "\n\t".$attributes; + $sqls[] = $sql.' CHANGE '.$field[$i]['_literal']; } else { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field); - - empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' '; - - if ( ! empty($attributes['TYPE'])) - { - $sql .= ' '.$attributes['TYPE']; - - if ( ! empty($attributes['CONSTRAINT'])) - { - switch (strtolower($attributes['TYPE'])) - { - case 'decimal': - case 'float': - case 'numeric': - $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; - break; - case 'enum': - // Will be supported in the future as part a part of - // MySQL compatibility features. - break; - case 'set': - $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; - break; - default: - $sql .= '('.$attributes['CONSTRAINT'].')'; - } - } - } - - /* As of version 8.4.1 CUBRID does not support UNSIGNED INTEGER data type. - * Will be supported in the next release as a part of MySQL Compatibility. - * - if (isset($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - */ - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; - - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - - if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) + $sqls[] = $sql.' CHANGE '.$this->_process_column($field[$i]); + if ( ! empty($field[$i]['new_name'])) { - $sql .= ' UNIQUE'; + $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name']) + .' AS '.$this->db->escape_identifiers($field[$i]['name']); } } - - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } } - return $sql; + return $sqls; } // -------------------------------------------------------------------- /** - * Create Table + * Field attribute TYPE * - * @param string the table name - * @param mixed the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + protected function _attr_type(&$attributes) { - $sql = 'CREATE TABLE '; - - /* As of version 8.4.1 CUBRID does not support this SQL syntax. - if ($if_not_exists === TRUE) + switch (strtoupper($attributes['TYPE'])) { - $sql .= 'IF NOT EXISTS '; + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + default: return; } - */ - - $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields); - - // If there is a PK defined - if (count($primary_keys) > 0) - { - $key_name = $this->db->escape_identifiers('pk_'.$table.'_'.implode('_', $primary_keys)); - $sql .= ",\n\tCONSTRAINT ".$key_name.' PRIMARY KEY('.implode(', ', $this->db->escape_identifiers($primary_keys)).')'; - } - - if (is_array($keys) && count($keys) > 0) - { - foreach ($keys as $key) - { - if (is_array($key)) - { - $key_name = $this->db->escape_identifiers('idx_'.$table.implode('_', $key)); - $key = $this->db->escape_identifiers($key); - } - else - { - $key_name = $this->db->escape_identifiers('idx_'.$table.$key); - $key = array($key_name); - } - - $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; - } - } - - return $sql."\n);"; } // -------------------------------------------------------------------- /** - * Alter table query - * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * Process indexes * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param array fields - * @param string the field after which we should add the new field + * @param string $table (ignored) * @return string */ - protected function _alter_table($alter_type, $table, $fields, $after_field = '') + protected function _process_indexes($table = NULL) { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '; + $sql = ''; - // DROP has everything it needs now. - if ($alter_type === 'DROP') + for ($i = 0, $c = count($this->keys); $i < $c; $i++) { - return $sql.$this->db->escape_identifiers($fields); + 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]); + + $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i])) + .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')'; } - return $sql.$this->_process_fields($fields) - .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); + $this->keys = array(); + + return $sql; } } diff --git a/system/database/drivers/ibase/ibase_forge.php b/system/database/drivers/ibase/ibase_forge.php index 7c4d00a23..a0c4e6563 100644 --- a/system/database/drivers/ibase/ibase_forge.php +++ b/system/database/drivers/ibase/ibase_forge.php @@ -36,11 +36,43 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_DB_ibase_forge extends CI_DB_forge { /** - * DROP TABLE statement + * CREATE TABLE IF statement * * @var string */ - protected $_drop_table = 'DROP TABLE %s'; + protected $_create_table_if = FALSE; + + /** + * RENAME TABLE statement + * + * @var string + */ + protected $_rename_table = FALSE; + + /** + * DROP TABLE IF statement + * + * @var string + */ + protected $_drop_table_if = FALSE; + + /** + * UNSIGNED support + * + * @var array + */ + protected $_unsigned = array( + 'SMALLINT' => 'INTEGER', + 'INTEGER' => 'INT64', + 'FLOAT' => 'DOUBLE PRECISION' + ); + + /** + * NULL value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = 'NULL'; // -------------------------------------------------------------------- @@ -65,8 +97,7 @@ class CI_DB_ibase_forge extends CI_DB_forge { /** * Drop database * - * @param string the database name - * - not used in this driver, the current db is dropped + * @param string $db_name (ignored) * @return bool */ public function drop_database($db_name = '') @@ -90,110 +121,119 @@ class CI_DB_ibase_forge extends CI_DB_forge { // -------------------------------------------------------------------- /** - * Create Table + * ALTER TABLE * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return string + * @param string $alter_type ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * @return string|string[] */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) - { - $sql = 'CREATE TABLE '; - - $sql .= $this->db->escape_identifiers($table).'('; - $current_field_count = 0; + protected function _alter_table($alter_type, $table, $field) + { + if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } - foreach ($fields as $field => $attributes) + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++) { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) + if ($field[$i]['_literal'] !== FALSE) { - $sql .= "\n\t".$attributes; + return FALSE; } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; - - empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')'; - - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; - - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } + if (isset($field[$i]['type'])) + { + $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length']; } - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) + if ( ! empty($field[$i]['default'])) { - $sql .= ','; + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .' SET '.$field[$i]['default']; } - } - if (count($primary_keys) > 0) - { - $primary_keys = $this->db->escape_identifiers($primary_keys); - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $primary_keys).')'; - } - - if (is_array($keys) && count($keys) > 0) - { - foreach ($keys as $key) + if (isset($field[$i]['null'])) { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); + $sqls[] = 'UPDATE "RDB$RELATION_FIELDS" SET "RDB$NULL_FLAG" = ' + .($field[$i]['null'] === TRUE ? 'NULL' : '1') + .' WHERE "RDB$FIELD_NAME" = '.$this->db->escape($field[$i]['name']) + .' AND "RDB$RELATION_NAME" = '.$this->db->escape($table); + } - $sql .= ",\n\tUNIQUE (".implode(', ', $key).')'; + if ( ! empty($field[$i]['new_name'])) + { + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .' TO '.$this->db->escape_identifiers($field[$i]['new_name']); } } - return $sql."\n)"; + return $sqls; + } + + // -------------------------------------------------------------------- + + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .' '.$field['type'].$field['length'] + .$field['null'] + .$field['unique'] + .$field['default']; } // -------------------------------------------------------------------- /** - * Alter table query + * Field attribute TYPE * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * Performs a data type mapping between different databases. * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'INT': + $attributes['TYPE'] = 'INTEGER'; + return; + case 'BIGINT': + $attributes['TYPE'] = 'INT64'; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * Field attribute AUTO_INCREMENT + * + * @param array &$attributes + * @param array &$field + * @return void */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') + protected function _attr_auto_increment(&$attributes, &$field) { - return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name) - .' '.$column_definition - .($default_value !== '' ? ' DEFAULT "'.$default_value.'"' : '') - .($null === NULL ? ' NULL' : ' NOT NULL') - .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); + // Not supported } } diff --git a/system/database/drivers/mssql/mssql_forge.php b/system/database/drivers/mssql/mssql_forge.php index 1eef648b0..5eebedbf7 100644 --- a/system/database/drivers/mssql/mssql_forge.php +++ b/system/database/drivers/mssql/mssql_forge.php @@ -36,130 +36,98 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_DB_mssql_forge extends CI_DB_forge { /** - * DROP TABLE statement + * CREATE TABLE IF statement * * @var string */ - protected $_drop_table = 'DROP TABLE %s'; - - // -------------------------------------------------------------------- + protected $_create_table_if = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'%s') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\nCREATE TABLE"; /** - * Create Table + * DROP TABLE IF statement * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return string + * @var string */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) - { - $sql = ($if_not_exists === TRUE) - ? "IF NOT EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'".$table."') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\n" - : ''; - - $sql .= 'CREATE TABLE '.$this->db->escape_identifiers($table).' ('; - - $current_field_count = 0; - foreach ($fields as $field => $attributes) - { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; + protected $_drop_table_if = "IF EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'%s') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\nDROP TABLE"; - if (stripos($attributes['TYPE'], 'INT') === FALSE && ! empty($attributes['CONSTRAINT'])) - { - $sql .= '('.$attributes['CONSTRAINT'].')'; - } - - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; - - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } + /** + * UNSIGNED support + * + * @var array + */ + protected $_unsigned = array( + 'TINYINT' => 'SMALLINT', + 'SMALLINT' => 'INT', + 'INT' => 'BIGINT', + 'REAL' => 'FLOAT' + ); - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } + // -------------------------------------------------------------------- - if (count($primary_keys) > 0) + /** + * 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) + { + if (in_array($alter_type, array('ADD', 'DROP'), TRUE)) { - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; + return parent::_alter_table($alter_type, $table, $field); } - if (is_array($keys) && count($keys) > 0) + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' ALTER COLUMN '; + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); - - $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')'; - } + $sqls[] = $sql.$this->_process_column($field[$i]); } - return $sql."\n)"; + return $sqls; } // -------------------------------------------------------------------- /** - * Alter table query + * Field attribute TYPE * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * Performs a data type mapping between different databases. * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @param array &$attributes + * @return void */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') + protected function _attr_type(&$attributes) { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name); - - // DROP has everything it needs now. - if ($alter_type === 'DROP') + switch (strtoupper($attributes['TYPE'])) { - return $sql; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'INTEGER': + $attributes['TYPE'] = 'INT'; + return; + default: return; } + } - return $sql.' '.$column_definition - .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '') - .($null === NULL ? ' NULL' : ' NOT NULL') - .($after_field != '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); + // -------------------------------------------------------------------- + + /** + * 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'] = ' IDENTITY(1,1)'; + } } } diff --git a/system/database/drivers/mysql/mysql_forge.php b/system/database/drivers/mysql/mysql_forge.php index 7ce4e8b00..ea3207760 100644 --- a/system/database/drivers/mysql/mysql_forge.php +++ b/system/database/drivers/mysql/mysql_forge.php @@ -42,165 +42,140 @@ class CI_DB_mysql_forge extends CI_DB_forge { */ protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s'; - // -------------------------------------------------------------------- - /** - * Process Fields + * UNSIGNED support * - * @param mixed $fields - * @return string + * @var array */ - protected function _process_fields($fields) - { - $current_field_count = 0; - $sql = ''; - - foreach ($fields as $field => $attributes) - { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field); - - empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' '; - - if ( ! empty($attributes['TYPE'])) - { - $sql .= ' '.$attributes['TYPE']; - - if ( ! empty($attributes['CONSTRAINT'])) - { - switch (strtolower($attributes['TYPE'])) - { - case 'decimal': - case 'float': - case 'numeric': - $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; - break; - case 'enum': - case 'set': - $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; - break; - default: - $sql .= '('.$attributes['CONSTRAINT'].')'; - } - } - } + protected $_unsigned = array( + 'TINYINT', + 'SMALLINT', + 'MEDIUMINT', + 'INT', + 'INTEGER', + 'BIGINT', + 'REAL', + 'DOUBLE', + 'DOUBLE PRECISION', + 'FLOAT', + 'DECIMAL', + 'NUMERIC' + ); - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; + /** + * NULL value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = 'NULL'; - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } + // -------------------------------------------------------------------- - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } + /** + * Class constructor + * + * @return void + */ + public function __construct() + { + parent::__construct(); - return $sql; + $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat; } // -------------------------------------------------------------------- /** - * Create Table + * ALTER TABLE * - * @param string the table name - * @param mixed the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool + * @param string $alter_type ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * @return string|string[] */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + protected function _alter_table($alter_type, $table, $field) { - $sql = 'CREATE TABLE '; - - if ($if_not_exists === TRUE) - { - $sql .= 'IF NOT EXISTS '; - } - - $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields); - - if (count($primary_keys) > 0) + if ($alter_type === 'DROP') { - $key_name = $this->db->escape_identifiers(implode('_', $primary_keys)); - $sql .= ",\n\tPRIMARY KEY ".$key_name.' ('.implode(', ', $this->db->escape_identifiers($primary_keys)).')'; + return parent::_alter_table($alter_type, $table, $field); } - if (is_array($keys) && count($keys) > 0) + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + for ($i = 0, $c = count($field); $i < $c; $i++) { - foreach ($keys as $key) + if ($field[$i]['_literal'] !== FALSE) { - if (is_array($key)) + $field[$i] = ($alter_type === 'ADD') + ? "\n\tADD ".$field[$i]['_literal'] + : "\n\tMODIFY ".$field[$i]['_literal']; + } + else + { + if ($alter_type === 'ADD') { - $key_name = $this->db->escape_identifiers(implode('_', $key)); - $key = $this->db->escape_identifiers($key); + $field[$i]['_literal'] = "\n\tADD "; } else { - $key_name = $this->db->escape_identifiers($key); - $key = array($key_name); + $field[$i]['_literal'] = empty($field['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE "; } - $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; + $field[$i] = $field['_literal'].$this->_process_column($field[$i]); } } - return $sql."\n) DEFAULT CHARACTER SET ".$this->db->char_set.' COLLATE '.$this->db->dbcollat.';'; + return array($sql.implode(',', $field)); } // -------------------------------------------------------------------- /** - * Alter table query + * Process column * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column() and column_alter() + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name'])) + .' '.$field['type'].$field['length'] + .$field['unsigned'] + .$field['null'] + .$field['default'] + .$field['auto_increment'] + .$field['unique']; + } + + // -------------------------------------------------------------------- + + /** + * Process indexes * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param array fields - * @param string the field after which we should add the new field + * @param string $table (ignored) * @return string */ - protected function _alter_table($alter_type, $table, $fields, $after_field = '') + protected function _process_indexes($table = NULL) { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '; + $sql = ''; - // DROP has everything it needs now. - if ($alter_type === 'DROP') + for ($i = 0, $c = count($this->keys); $i < $c; $i++) { - return $sql.$this->db->escape_identifiers($fields); + 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]); + + $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i])) + .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')'; } - return $sql.$this->_process_fields($fields) - .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); + $this->keys = array(); + + return $sql; } } diff --git a/system/database/drivers/mysqli/mysqli_forge.php b/system/database/drivers/mysqli/mysqli_forge.php index 0732dfea5..914d6a268 100644 --- a/system/database/drivers/mysqli/mysqli_forge.php +++ b/system/database/drivers/mysqli/mysqli_forge.php @@ -42,165 +42,140 @@ class CI_DB_mysqli_forge extends CI_DB_forge { */ protected $_create_database = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s'; - // -------------------------------------------------------------------- - /** - * Process Fields + * UNSIGNED support * - * @param mixed $fields - * @return string + * @var array */ - protected function _process_fields($fields) - { - $current_field_count = 0; - $sql = ''; - - foreach ($fields as $field => $attributes) - { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field); - - empty($attributes['NAME']) OR $sql .= ' '.$this->db->escape_identifiers($attributes['NAME']).' '; - - if ( ! empty($attributes['TYPE'])) - { - $sql .= ' '.$attributes['TYPE']; - - if ( ! empty($attributes['CONSTRAINT'])) - { - switch (strtolower($attributes['TYPE'])) - { - case 'decimal': - case 'float': - case 'numeric': - $sql .= '('.implode(',', $attributes['CONSTRAINT']).')'; - break; - case 'enum': - case 'set': - $sql .= '("'.implode('","', $attributes['CONSTRAINT']).'")'; - break; - default: - $sql .= '('.$attributes['CONSTRAINT'].')'; - } - } - } + protected $_unsigned = array( + 'TINYINT', + 'SMALLINT', + 'MEDIUMINT', + 'INT', + 'INTEGER', + 'BIGINT', + 'REAL', + 'DOUBLE', + 'DOUBLE PRECISION', + 'FLOAT', + 'DECIMAL', + 'NUMERIC' + ); - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; + /** + * NULL value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = 'NULL'; - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } + // -------------------------------------------------------------------- - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } + /** + * Class constructor + * + * @return void + */ + public function __construct() + { + parent::__construct(); - return $sql; + $this->_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat; } // -------------------------------------------------------------------- /** - * Create Table + * ALTER TABLE * - * @param string the table name - * @param mixed the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool + * @param string $alter_type ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * @return string|string[] */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + protected function _alter_table($alter_type, $table, $field) { - $sql = 'CREATE TABLE '; - - if ($if_not_exists === TRUE) - { - $sql .= 'IF NOT EXISTS '; - } - - $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields); - - if (count($primary_keys) > 0) + if ($alter_type === 'DROP') { - $key_name = $this->db->escape_identifiers(implode('_', $primary_keys)); - $sql .= ",\n\tPRIMARY KEY ".$key_name.' ('.implode(', ', $this->db->escape_identifiers($primary_keys)).')'; + return parent::_alter_table($alter_type, $table, $field); } - if (is_array($keys) && count($keys) > 0) + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + for ($i = 0, $c = count($field); $i < $c; $i++) { - foreach ($keys as $key) + if ($field[$i]['_literal'] !== FALSE) { - if (is_array($key)) + $field[$i] = ($alter_type === 'ADD') + ? "\n\tADD ".$field[$i]['_literal'] + : "\n\tMODIFY ".$field[$i]['_literal']; + } + else + { + if ($alter_type === 'ADD') { - $key_name = $this->db->escape_identifiers(implode('_', $key)); - $key = $this->db->escape_identifiers($key); + $field[$i]['_literal'] = "\n\tADD "; } else { - $key_name = $this->db->escape_identifiers($key); - $key = array($key_name); + $field[$i]['_literal'] = empty($field['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE "; } - $sql .= ",\n\tKEY ".$key_name.' ('.implode(', ', $key).')'; + $field[$i] = $field['_literal'].$this->_process_column($field[$i]); } } - return $sql."\n) DEFAULT CHARACTER SET ".$this->db->char_set.' COLLATE '.$this->db->dbcollat.';'; + return array($sql.implode(',', $field)); } // -------------------------------------------------------------------- /** - * Alter table query + * Process column * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name'])) + .' '.$field['type'].$field['length'] + .$field['unsigned'] + .$field['null'] + .$field['default'] + .$field['auto_increment'] + .$field['unique']; + } + + // -------------------------------------------------------------------- + + /** + * Process indexes * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param array fields - * @param string the field after which we should add the new field + * @param string $table (ignored) * @return string */ - protected function _alter_table($alter_type, $table, $fields, $after_field = '') + protected function _process_indexes($table = NULL) { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '; + $sql = ''; - // DROP has everything it needs now. - if ($alter_type === 'DROP') + for ($i = 0, $c = count($this->keys); $i < $c; $i++) { - return $sql.$this->db->escape_identifiers($fields); + 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]); + + $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i])) + .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')'; } - return $sql.$this->_process_fields($fields) - .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); + $this->keys = array(); + + return $sql; } } diff --git a/system/database/drivers/oci8/oci8_forge.php b/system/database/drivers/oci8/oci8_forge.php index ad578f830..bd9d2df3c 100644 --- a/system/database/drivers/oci8/oci8_forge.php +++ b/system/database/drivers/oci8/oci8_forge.php @@ -50,126 +50,81 @@ class CI_DB_oci8_forge extends CI_DB_forge { protected $_drop_database = FALSE; /** - * DROP TABLE statement + * DROP TABLE IF statement * * @var string */ - protected $_drop_table = 'DROP TABLE %s'; + protected $_drop_table_if = FALSE; + + /** + * UNSIGNED support + * + * @var bool|array + */ + protected $_unsigned = FALSE; // -------------------------------------------------------------------- /** - * Create Table + * ALTER TABLE * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return string + * @param string $alter_type ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * @return string|string[] */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + protected function _alter_table($alter_type, $table, $field) { - $sql = 'CREATE TABLE '; - - if ($if_not_exists === TRUE) + if ($alter_type === 'DROP') { - $sql .= 'IF NOT EXISTS '; + return parent::_alter_table($alter_type, $table, $field); + } + elseif ($alter_type === 'CHANGE') + { + $alter_type = 'MODIFY'; } - $sql .= $this->db->escape_identifiers($table).' ('; - $current_field_count = 0; - - foreach ($fields as $field => $attributes) + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) + if ($field[$i]['_literal'] !== FALSE) { - $sql .= "\n\t".$attributes; + $field[$i] = "\n\t".$field[$i]['_literal']; } else { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; - - if (isset($attributes['UNSINGED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) + $field[$i]['_literal'] = "\n\t".$this->_process_column($field[$i]); + if ($alter_type === 'MODIFY' && ! empty($field[$i]['new_name'])) { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; + $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name']) + .' '.$this->db->escape_identifiers($field[$i]['new_name']); } - - $sql .= (isset($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? '' : ' NOT NULL'; - - empty($attributes['CONSTRAINT']) OR ' CONSTRAINT '.$attributes['CONSTRAINT']; - } - - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; } } - if (count($primary_keys) > 0) - { - $sql .= ",\n\tCONSTRAINT ".$table.' PRIMARY KEY ('.implode(', ', $this->db->escape_identifiers($primary_keys)).')'; - } + $sql .= ' '.$alter_type.' '; + $sql .= (count($field) === 1) + ? $fields[0] + : '('.implode(',', $field).')'; - if (is_array($keys) && count($keys) > 0) - { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); - - $sql .= ",\n\tUNIQUE COLUMNS (".implode(', ', $key).')'; - } - } - - return $sql."\n)"; + // RENAME COLUMN must be executed after MODIFY + array_unshift($sqls, $sql); + return $sql; } // -------------------------------------------------------------------- /** - * Alter table query - * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * Field attribute AUTO_INCREMENT * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @param array &$attributes + * @param array &$field + * @return void */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') + protected function _attr_auto_increment(&$attributes, &$field) { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name); - - // DROP has everything it needs now. - if ($alter_type === 'DROP') - { - return $sql; - } - - return $sql.' '.$column_definition - .($default_value !== '' ? ' DEFAULT "'.$default_value.'"' : '') - .($null === NULL ? ' NULL' : ' NOT NULL') - .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); - + // Not supported - sequences and triggers must be used instead } } diff --git a/system/database/drivers/odbc/odbc_forge.php b/system/database/drivers/odbc/odbc_forge.php index 699ce996c..fb16ca5cf 100644 --- a/system/database/drivers/odbc/odbc_forge.php +++ b/system/database/drivers/odbc/odbc_forge.php @@ -36,130 +36,38 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_DB_odbc_forge extends CI_DB_forge { /** - * DROP TABLE statement + * CREATE TABLE IF statement * * @var string */ - protected $_drop_table = 'DROP TABLE %s'; - - // -------------------------------------------------------------------- + protected $_create_table_if = FALSE; /** - * Create Table + * DROP TABLE IF statement * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool + * @var string */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) - { - $sql = 'CREATE TABLE '; - - if ($if_not_exists === TRUE) - { - $sql .= 'IF NOT EXISTS '; - } - - $sql .= $this->db->escape_identifiers($table).' ('; - $current_field_count = 0; - - foreach ($fields as $field => $attributes) - { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; - - empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')'; - - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; + protected $_drop_table_if = FALSE; - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } - - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } - - if (count($primary_keys) > 0) - { - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->escape_identifiers($primary_keys)).')'; - } - - if (is_array($keys) && count($keys) > 0) - { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); - - $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')'; - } - } - - return $sql."\n)"; - } + /** + * UNSIGNED support + * + * @var bool|array + */ + protected $_unsigned = FALSE; // -------------------------------------------------------------------- /** - * Alter table query + * Field attribute AUTO_INCREMENT * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), - * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @param array &$attributes + * @param array &$field + * @return void */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') + protected function _attr_auto_increment(&$attributes, &$field) { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name); - - // DROP has everything it needs now. - if ($alter_type === 'DROP') - { - return $sql; - } - - return $sql.' '.$column_definition - .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '') - .($null === NULL ? ' NULL' : ' NOT NULL') - .($after_field != '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); + // Not supported (in most databases at least) } } diff --git a/system/database/drivers/pdo/pdo_forge.php b/system/database/drivers/pdo/pdo_forge.php index e77970cf2..130d3667e 100644 --- a/system/database/drivers/pdo/pdo_forge.php +++ b/system/database/drivers/pdo/pdo_forge.php @@ -36,139 +36,18 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_DB_pdo_forge extends CI_DB_forge { /** - * DROP TABLE statement + * CREATE TABLE IF statement * * @var string */ - protected $_drop_table = 'DROP TABLE %s'; - - // -------------------------------------------------------------------- - - /** - * Create Table - * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool - */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) - { - $sql = 'CREATE TABLE '; - - if ($if_not_exists === TRUE) - { - $sql .= 'IF NOT EXISTS '; - } - - $sql .= $this->db->escape_identifiers($table).' ('; - $current_field_count = 0; - - foreach ($fields as $field => $attributes) - { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - $numeric = array('SERIAL', 'INTEGER'); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; - - if ( ! empty($attributes['CONSTRAINT'])) - { - // Exception for Postgre numeric which not too happy with constraint within those type - if ( ! ($this->db->subdriver === 'pgsql' && in_array($attributes['TYPE'], $numeric))) - { - $sql .= '('.$attributes['CONSTRAINT'].')'; - } - } - - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; - - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } - - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } - - if (count($primary_keys) > 0) - { - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; - } - - if (is_array($keys) && count($keys) > 0) - { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); - - $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')'; - } - } - - return $sql."\n)"; - } - - // -------------------------------------------------------------------- + protected $_create_table_if = FALSE; /** - * Alter table query - * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * DROP TABLE IF statement * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @var string */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') - { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name); - - // DROP has everything it needs now. - if ($alter_type === 'DROP') - { - return $sql; - } - - return $sql .' '.$column_definition - .($default_value !== '' ? " DEFAULT '".$default_value."'" : '') - .($null === NULL ? ' NULL' : ' NOT NULL') - .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); - } + protected $_drop_table_if = FALSE; } diff --git a/system/database/drivers/pdo/subdrivers/pdo_4d_forge.php b/system/database/drivers/pdo/subdrivers/pdo_4d_forge.php new file mode 100644 index 000000000..231ad53a4 --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_4d_forge.php @@ -0,0 +1,209 @@ + 'INT', + 'SMALLINT' => 'INT', + 'INT' => 'INT64', + 'INT32' => 'INT64' + ); + + /** + * DEFAULT value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_default = FALSE; + + // -------------------------------------------------------------------- + + /** + * 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) + { + if (in_array($alter_type, array('ADD', 'DROP'), TRUE)) + { + return parent::_alter_table($alter_table, $table, $field); + } + + // No method of modifying columns is supported + return FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .' '.$field['type'].$field['length'] + .$field['null'] + .$field['unique'] + .$field['auto_increment']; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'INTEGER': + $attributes['TYPE'] = 'INT'; + return; + case 'BIGINT': + $attribites['TYPE'] = 'INT64'; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * 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'; + + // UNIQUE must be used with NOT NULL + $field['null'] = ' NOT NULL'; + } + } + + // -------------------------------------------------------------------- + + /** + * 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) + { + if (stripos($field['type'], 'int') !== FALSE) + { + $field['auto_increment'] = ' AUTO_INCREMENT'; + } + elseif (strcasecmp($field['type'], 'UUID') === 0) + { + $field['auto_increment'] = ' AUTO_GENERATE'; + } + } + } + +} + +/* End of file pdo_4d_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_4d_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_cubrid_forge.php b/system/database/drivers/pdo/subdrivers/pdo_cubrid_forge.php new file mode 100644 index 000000000..c01a0fb81 --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_cubrid_forge.php @@ -0,0 +1,173 @@ + 'INTEGER', + 'SMALLINT' => 'INTEGER', + 'INT' => 'BIGINT', + 'INTEGER' => 'BIGINT', + 'BIGINT' => 'NUMERIC', + 'FLOAT' => 'DOUBLE', + 'REAL' => 'DOUBLE' + ); + + // -------------------------------------------------------------------- + + /** + * 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) + { + if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } + + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) + { + if ($field[$i]['_literal'] !== FALSE) + { + $sqls[] = $sql.' CHANGE '.$field[$i]['_literal']; + } + else + { + $sqls[] = $sql.' CHANGE '.$this->_process_column($field[$i]); + if ( ! empty($field[$i]['new_name'])) + { + $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name']) + .' AS '.$this->db->escape_identifiers($field[$i]['name']); + } + } + } + + return $sqls; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * Process indexes + * + * @param string $table (ignored) + * @return string + */ + protected function _process_indexes($table = NULL) + { + $sql = ''; + + 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]); + + $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i])) + .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')'; + } + + $this->keys = array(); + + return $sql; + } + +} + +/* End of file pdo_cubrid_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_cubrid_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_dblib_forge.php b/system/database/drivers/pdo/subdrivers/pdo_dblib_forge.php new file mode 100644 index 000000000..fb33127fd --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_dblib_forge.php @@ -0,0 +1,136 @@ + 'SMALLINT', + 'SMALLINT' => 'INT', + 'INT' => 'BIGINT', + 'REAL' => 'FLOAT' + ); + + // -------------------------------------------------------------------- + + /** + * 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) + { + if (in_array($alter_type, array('ADD', 'DROP'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } + + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' ALTER COLUMN '; + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) + { + $sqls[] = $sql.$this->_process_column($field[$i]); + } + + return $sqls; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'INTEGER': + $attributes['TYPE'] = 'INT'; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * 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'] = ' IDENTITY(1,1)'; + } + } + +} + +/* End of file pdo_dblib_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_dblib_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_firebird_forge.php b/system/database/drivers/pdo/subdrivers/pdo_firebird_forge.php new file mode 100644 index 000000000..88c53510a --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_firebird_forge.php @@ -0,0 +1,228 @@ + 'INTEGER', + 'INTEGER' => 'INT64', + 'FLOAT' => 'DOUBLE PRECISION' + ); + + /** + * NULL value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = 'NULL'; + + // -------------------------------------------------------------------- + + /** + * Create database + * + * @param string $db_name + * @return string + */ + public function create_database($db_name) + { + // Firebird databases are flat files, so a path is required + + // Hostname is needed for remote access + empty($this->db->hostname) OR $db_name = $this->hostname.':'.$db_name; + + return parent::create_database('"'.$db_name.'"'); + } + + // -------------------------------------------------------------------- + + /** + * Drop database + * + * @param string $db_name (ignored) + * @return bool + */ + public function drop_database($db_name = '') + { + if ( ! ibase_drop_db($this->conn_id)) + { + return ($this->db->db_debug) ? $this->db->display_error('db_unable_to_drop') : FALSE; + } + elseif ( ! empty($this->db->data_cache['db_names'])) + { + $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); + if ($key !== FALSE) + { + unset($this->db->data_cache['db_names'][$key]); + } + } + + 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) + { + if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } + + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++) + { + if ($field[$i]['_literal'] !== FALSE) + { + return FALSE; + } + + if (isset($field[$i]['type'])) + { + $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length']; + } + + if ( ! empty($field[$i]['default'])) + { + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .' SET '.$field[$i]['default']; + } + + if (isset($field[$i]['null'])) + { + $sqls[] = 'UPDATE "RDB$RELATION_FIELDS" SET "RDB$NULL_FLAG" = ' + .($field[$i]['null'] === TRUE ? 'NULL' : '1') + .' WHERE "RDB$FIELD_NAME" = '.$this->db->escape($field[$i]['name']) + .' AND "RDB$RELATION_NAME" = '.$this->db->escape($table); + } + + if ( ! empty($field[$i]['new_name'])) + { + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .' TO '.$this->db->escape_identifiers($field[$i]['new_name']); + } + } + + return $sqls; + } + + // -------------------------------------------------------------------- + + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .' '.$field['type'].$field['length'] + .$field['null'] + .$field['unique'] + .$field['default']; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'INT': + $attributes['TYPE'] = 'INTEGER'; + return; + case 'BIGINT': + $attributes['TYPE'] = 'INT64'; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * Field attribute AUTO_INCREMENT + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_auto_increment(&$attributes, &$field) + { + // Not supported + } + +} + +/* End of file pdo_firebird_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_firebird_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_ibm_forge.php b/system/database/drivers/pdo/subdrivers/pdo_ibm_forge.php new file mode 100644 index 000000000..6a2c9b87e --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_ibm_forge.php @@ -0,0 +1,146 @@ + 'INTEGER', + 'INT' => 'BIGINT', + 'INTEGER' => 'BIGINT' + ); + + /** + * DEFAULT value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_default = FALSE; + + // -------------------------------------------------------------------- + + /** + * 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) + { + if ($alter_type === 'CHANGE') + { + $alter_type = 'MODIFY'; + } + + return parent::_alter_table($alter_type, $table, $field); + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * 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'; + + // UNIQUE must be used with NOT NULL + $field['null'] = ' NOT NULL'; + } + } + + // -------------------------------------------------------------------- + + /** + * Field attribute AUTO_INCREMENT + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_auto_increment(&$attributes, &$field) + { + // Not supported + } + +} + +/* End of file pdo_ibm_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_ibm_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_informix_forge.php b/system/database/drivers/pdo/subdrivers/pdo_informix_forge.php new file mode 100644 index 000000000..715a7cbba --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_informix_forge.php @@ -0,0 +1,155 @@ + 'INTEGER', + 'INT' => 'BIGINT', + 'INTEGER' => 'BIGINT', + 'REAL' => 'DOUBLE PRECISION', + 'SMALLFLOAT' => 'DOUBLE PRECISION' + ); + + /** + * DEFAULT value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_default = ', '; + + // -------------------------------------------------------------------- + + /** + * 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) + { + if ($alter_type === 'CHANGE') + { + $alter_type = 'MODIFY'; + } + + return parent::_alter_table($alter_type, $table, $field); + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'BYTE': + case 'TEXT': + case 'BLOB': + case 'CLOB': + $attributes['UNIQUE'] = FALSE; + if (isset($attributes['DEFAULT'])) + { + unset($attributes['DEFAULT']); + } + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * 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 CONSTRAINT '.$this->db->escape_identifiers($field['name']); + } + } + + // -------------------------------------------------------------------- + + /** + * Field attribute AUTO_INCREMENT + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_auto_increment(&$attributes, &$field) + { + // Not supported + } + +} + +/* End of file pdo_informix_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_informix_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php b/system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php new file mode 100644 index 000000000..dc856ed2c --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php @@ -0,0 +1,198 @@ +_create_table .= ' DEFAULT CHARSET '.$this->db->char_set.' COLLATE '.$this->db->dbcollat; + } + + // -------------------------------------------------------------------- + + /** + * 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) + { + if ($alter_type === 'DROP') + { + return parent::_alter_table($alter_type, $table, $field); + } + + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + for ($i = 0, $c = count($field); $i < $c; $i++) + { + if ($field[$i]['_literal'] !== FALSE) + { + $field[$i] = ($alter_type === 'ADD') + ? "\n\tADD ".$field[$i]['_literal'] + : "\n\tMODIFY ".$field[$i]['_literal']; + } + else + { + if ($alter_type === 'ADD') + { + $field[$i]['_literal'] = "\n\tADD "; + } + else + { + $field[$i]['_literal'] = empty($field['new_name']) ? "\n\tMODIFY " : "\n\tCHANGE "; + } + + $field[$i] = $field['_literal'].$this->_process_column($field[$i]); + } + } + + return array($sql.implode(',', $field)); + } + + // -------------------------------------------------------------------- + + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .(empty($field['new_name']) ? '' : $this->db->escape_identifiers($field['new_name'])) + .' '.$field['type'].$field['length'] + .$field['unsigned'] + .$field['null'] + .$field['default'] + .$field['auto_increment'] + .$field['unique']; + } + + // -------------------------------------------------------------------- + + /** + * Process indexes + * + * @param string $table (ignored) + * @return string + */ + protected function _process_indexes($table = NULL) + { + $sql = ''; + + 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]); + + $sql .= ",\n\tKEY ".$this->db->escape_identifiers(implode('_', $this->keys[$i])) + .' ('.implode(', ', $this->db->escape_identifiers($this->keys[$i])).')'; + } + + $this->keys = array(); + + return $sql; + } + +} + +/* End of file pdo_mysql_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_mysql_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_oci_forge.php b/system/database/drivers/pdo/subdrivers/pdo_oci_forge.php new file mode 100644 index 000000000..dbcb64d31 --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_oci_forge.php @@ -0,0 +1,133 @@ +db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) + { + if ($field[$i]['_literal'] !== FALSE) + { + $field[$i] = "\n\t".$field[$i]['_literal']; + } + else + { + $field[$i]['_literal'] = "\n\t".$this->_process_column($field[$i]); + if ($alter_type === 'MODIFY' && ! empty($field[$i]['new_name'])) + { + $sqls[] = $sql.' RENAME COLUMN '.$this->db->escape_identifiers($field[$i]['name']) + .' '.$this->db->escape_identifiers($field[$i]['new_name']); + } + } + } + + $sql .= ' '.$alter_type.' '; + $sql .= (count($field) === 1) + ? $fields[0] + : '('.implode(',', $field).')'; + + // RENAME COLUMN must be executed after MODIFY + array_unshift($sqls, $sql); + return $sql; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute AUTO_INCREMENT + * + * @param array &$attributes + * @param array &$field + * @return void + */ + protected function _attr_auto_increment(&$attributes, &$field) + { + // Not supported - sequences and triggers must be used instead + } + +} + +/* End of file pdo_oci_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_oci_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_odbc_forge.php b/system/database/drivers/pdo/subdrivers/pdo_odbc_forge.php new file mode 100644 index 000000000..cd6b19c68 --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_odbc_forge.php @@ -0,0 +1,62 @@ + 'INTEGER', + 'SMALLINT' => 'INTEGER', + 'INT' => 'BIGINT', + 'INT4' => 'BIGINT', + 'INTEGER' => 'BIGINT', + 'INT8' => 'NUMERIC', + 'BIGINT' => 'NUMERIC', + 'REAL' => 'DOUBLE PRECISION', + 'FLOAT' => 'DOUBLE PRECISION' + ); + + /** + * NULL value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = 'NULL'; + + // -------------------------------------------------------------------- + + /** + * Class constructor + * + * @return void + */ + public function __construct() + { + parent::__construct(); + + if (version_compare($this->db->version(), '9.0', '>')) + { + $this->create_table_if = 'CREATE TABLE IF NOT EXISTS'; + } + } + + // -------------------------------------------------------------------- + + /** + * 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) + { + if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } + + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++) + { + if ($field[$i]['_literal'] !== FALSE) + { + return FALSE; + } + + if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type'])) + { + $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length']; + } + + if ( ! empty($field[$i]['default'])) + { + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .' SET '.$field[$i]['default']; + } + + if (isset($field[$i]['null'])) + { + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL'); + } + + if ( ! empty($field[$i]['new_name'])) + { + $sqls[] = $sql.' RENAME '.$this->db->escape_identifiers($field[$i]['name']) + .' TO '.$this->db->escape_identifiers($field[$i]['new_name']); + } + } + + return $sqls; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + // Reset field lenghts for data types that don't support it + if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE) + { + $attributes['CONSTRAINT'] = NULL; + } + + switch (strtoupper($attributes['TYPE'])) + { + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * 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) + { + $field['type'] = ($field['type'] === 'NUMERIC') + ? 'BIGSERIAL' + : 'SERIAL'; + } + } + +} + +/* End of file pdo_pgsql_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_pgsql_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php b/system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php new file mode 100644 index 000000000..414826212 --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php @@ -0,0 +1,228 @@ +db->version(), '3.3', '<')) + { + $this->_create_table_if = FALSE; + } + } + + // -------------------------------------------------------------------- + + /** + * Create database + * + * @param string $db_name (ignored) + * @return bool + */ + public function create_database($db_name = '') + { + // In SQLite, a database is created when you connect to the database. + // We'll return TRUE so that an error isn't generated + return TRUE; + } + + // -------------------------------------------------------------------- + + /** + * Drop database + * + * @param string $db_name (ignored) + * @return bool + */ + public function drop_database($db_name = '') + { + // In SQLite, a database is dropped when we delete a file + if (@file_exists($this->db->database)) + { + // We need to close the pseudo-connection first + $this->db->close(); + if ( ! @unlink($this->db->database)) + { + return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; + } + elseif ( ! empty($this->db->data_cache['db_names'])) + { + $key = array_search(strtolower($this->db->database), array_map('strtolower', $this->db->data_cache['db_names']), TRUE); + if ($key !== FALSE) + { + unset($this->db->data_cache['db_names'][$key]); + } + } + + return TRUE; + } + + return $this->db->db_debug ? $this->db->display_error('db_unable_to_drop') : FALSE; + } + + // -------------------------------------------------------------------- + + /** + * 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) + { + if ($alter_type === 'DROP' OR $alter_type === 'CHANGE') + { + // drop_column(): + // BEGIN TRANSACTION; + // CREATE TEMPORARY TABLE t1_backup(a,b); + // INSERT INTO t1_backup SELECT a,b FROM t1; + // DROP TABLE t1; + // CREATE TABLE t1(a,b); + // INSERT INTO t1 SELECT a,b FROM t1_backup; + // DROP TABLE t1_backup; + // COMMIT; + + return FALSE; + } + + return parent::_alter_table($alter_type, $table, $field); + } + + // -------------------------------------------------------------------- + + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .' '.$field['type'] + .$field['auto_increment'] + .$field['null'] + .$field['unique'] + .$field['default']; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'ENUM': + case 'SET': + $attributes['TYPE'] = 'TEXT'; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * 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['type'] = 'INTEGER PRIMARY KEY'; + $field['default'] = ''; + $field['null'] = ''; + $field['unique'] = ''; + $field['auto_increment'] = ' AUTOINCREMENT'; + + $this->primary_keys = array(); + } + } + +} + +/* End of file pdo_sqlite_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_sqlite_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_forge.php b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_forge.php new file mode 100644 index 000000000..c62720d48 --- /dev/null +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlsrv_forge.php @@ -0,0 +1,136 @@ + 'SMALLINT', + 'SMALLINT' => 'INT', + 'INT' => 'BIGINT', + 'REAL' => 'FLOAT' + ); + + // -------------------------------------------------------------------- + + /** + * 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) + { + if (in_array($alter_type, array('ADD', 'DROP'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } + + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' ALTER COLUMN '; + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) + { + $sqls[] = $sql.$this->_process_column($field[$i]); + } + + return $sqls; + } + + // -------------------------------------------------------------------- + + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) + { + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'INTEGER': + $attributes['TYPE'] = 'INT'; + return; + default: return; + } + } + + // -------------------------------------------------------------------- + + /** + * 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'] = ' IDENTITY(1,1)'; + } + } + +} + +/* End of file pdo_sqlsrv_forge.php */ +/* Location: ./system/database/drivers/pdo/subdrivers/pdo_sqlsrv_forge.php */ \ No newline at end of file diff --git a/system/database/drivers/postgre/postgre_forge.php b/system/database/drivers/postgre/postgre_forge.php index b08fa5177..ad26763a4 100644 --- a/system/database/drivers/postgre/postgre_forge.php +++ b/system/database/drivers/postgre/postgre_forge.php @@ -36,188 +36,149 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_DB_postgre_forge extends CI_DB_forge { /** - * DROP TABLE statement + * UNSIGNED support + * + * @var array + */ + protected $_unsigned = array( + 'INT2' => 'INTEGER', + 'SMALLINT' => 'INTEGER', + 'INT' => 'BIGINT', + 'INT4' => 'BIGINT', + 'INTEGER' => 'BIGINT', + 'INT8' => 'NUMERIC', + 'BIGINT' => 'NUMERIC', + 'REAL' => 'DOUBLE PRECISION', + 'FLOAT' => 'DOUBLE PRECISION' + ); + + /** + * NULL value representation in CREATE/ALTER TABLE statements * * @var string */ - protected $_drop_table = 'DROP TABLE IF EXISTS %s CASCADE'; + protected $_null = 'NULL'; // -------------------------------------------------------------------- /** - * Process Fields + * Class constructor * - * @param mixed $fields - * @param array $primary_keys - * @return string + * @return void */ - protected function _process_fields($fields, $primary_keys = array()) + public function __construct() { - $sql = ''; - $current_field_count = 0; + parent::__construct(); - foreach ($fields as $field => $attributes) + if (version_compare($this->db->version(), '9.0', '>')) { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) + $this->create_table_if = 'CREATE TABLE IF NOT EXISTS'; + } + } + + // -------------------------------------------------------------------- + + /** + * 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) + { + if (in_array($alter_type, array('DROP', 'ADD'), TRUE)) + { + return parent::_alter_table($alter_type, $table, $field); + } + + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table); + $sqls = array(); + for ($i = 0, $c = count($field), $sql .= $alter_type.' '; $i < $c; $i++) + { + if ($field[$i]['_literal'] !== FALSE) { - $sql .= "\n\t".$attributes; + return FALSE; } - else + + if (version_compare($this->db->version(), '8', '>=') && isset($field[$i]['type'])) { - $sql .= "\n\t".$this->db->escape_identifiers($field); - - $attributes = array_change_key_case($attributes, CASE_UPPER); - $is_unsigned = ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE); - - // Convert datatypes to be PostgreSQL-compatible - switch (strtoupper($attributes['TYPE'])) - { - case 'TINYINT': - $attributes['TYPE'] = 'SMALLINT'; - break; - case 'SMALLINT': - $attributes['TYPE'] = ($is_unsigned) ? 'INTEGER' : 'SMALLINT'; - break; - case 'MEDIUMINT': - $attributes['TYPE'] = 'INTEGER'; - break; - case 'INT': - $attributes['TYPE'] = ($is_unsigned) ? 'BIGINT' : 'INTEGER'; - break; - case 'BIGINT': - $attributes['TYPE'] = ($is_unsigned) ? 'NUMERIC' : 'BIGINT'; - break; - case 'DOUBLE': - $attributes['TYPE'] = 'DOUBLE PRECISION'; - break; - case 'DATETIME': - $attributes['TYPE'] = 'TIMESTAMP'; - break; - case 'LONGTEXT': - $attributes['TYPE'] = 'TEXT'; - break; - case 'BLOB': - $attributes['TYPE'] = 'BYTEA'; - break; - default: - break; - } - - // If this is an auto-incrementing primary key, use the serial data type instead - $sql .= (in_array($field, $primary_keys) && ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - ? ' SERIAL' : ' '.$attributes['TYPE']; - - // Modified to prevent constraints with integer data types - if ( ! empty($attributes['CONSTRAINT']) && strpos($attributes['TYPE'], 'INT') === FALSE) - { - $sql .= '('.$attributes['CONSTRAINT'].')'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; - - // Added new attribute to create unique fields. Also works with MySQL - if ( ! empty($attributes['UNIQUE']) && $attributes['UNIQUE'] === TRUE) - { - $sql .= ' UNIQUE'; - } + $sqls[] = $sql.' TYPE '.$field[$i]['type'].$field[$i]['length']; } - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) + if ( ! empty($field[$i]['default'])) { - $sql .= ','; + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .' SET '.$field[$i]['default']; + } + + if (isset($field[$i]['null'])) + { + $sqls[] = $sql.' ALTER '.$this->db->escape_identifiers($field[$i]['name']) + .($field[$i]['null'] === TRUE ? ' DROP NOT NULL' : ' SET NOT NULL'); + } + + if ( ! empty($field[$i]['new_name'])) + { + $sqls[] = $sql.' RENAME '.$this->db->escape_identifiers($field[$i]['name']) + .' TO '.$this->db->escape_identifiers($field[$i]['new_name']); } } - return $sql; - } + return $sqls; + } // -------------------------------------------------------------------- /** - * Create Table + * Field attribute TYPE * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + protected function _attr_type(&$attributes) { - $sql = 'CREATE TABLE '; - - // PostgreSQL doesn't support IF NOT EXISTS syntax so we check if table exists manually - if ($if_not_exists === TRUE && $this->db->table_exists($table)) + // Reset field lenghts for data types that don't support it + if (isset($attributes['CONSTRAINT']) && stripos($attributes['TYPE'], 'int') !== FALSE) { - return TRUE; + $attributes['CONSTRAINT'] = NULL; } - $sql .= $this->db->escape_identifiers($table).' ('.$this->_process_fields($fields, $primary_keys); - - if (count($primary_keys) > 0) + switch (strtoupper($attributes['TYPE'])) { - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; - } - - $sql .= "\n);"; - - if (is_array($keys) && count($keys) > 0) - { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); - - foreach ($key as $field) - { - $sql .= "\nCREATE INDEX ".$this->db->escape_identifiers($table.'_'.str_replace(array('"', "'"), '', $field).'_index') - .' ON '.$this->db->escape_identifiers($table).' ('.$this->db->escape_identifiers($field).');'; - } - } + case 'TINYINT': + $attributes['TYPE'] = 'SMALLINT'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + default: return; } - - return $sql; } // -------------------------------------------------------------------- /** - * Alter table query - * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * Field attribute AUTO_INCREMENT * - * @param string $alter_type the ALTER type (ADD, DROP, CHANGE) - * @param string $table the table name - * @param string $fields the column definition - * @param string $after_field - * @return string + * @param array &$attributes + * @param array &$field + * @return void */ - protected function _alter_table($alter_type, $table, $fields, $after_field = '') - { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '; - - // DROP has everything it needs now. - if ($alter_type === 'DROP') - { - return $sql.$this->db->escape_identifiers($fields); - } - - return $sql.$this->_process_fields($fields) - .($after_field !== '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); - } + protected function _attr_auto_increment(&$attributes, &$field) + { + if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) + { + $field['type'] = ($field['type'] === 'NUMERIC') + ? 'BIGSERIAL' + : 'SERIAL'; + } + } } diff --git a/system/database/drivers/sqlite/sqlite_forge.php b/system/database/drivers/sqlite/sqlite_forge.php index 5650af7be..6ad583560 100644 --- a/system/database/drivers/sqlite/sqlite_forge.php +++ b/system/database/drivers/sqlite/sqlite_forge.php @@ -35,10 +35,33 @@ defined('BASEPATH') OR exit('No direct script access allowed'); */ class CI_DB_sqlite_forge extends CI_DB_forge { + /** + * CREATE TABLE IF statement + * + * @var string + */ + protected $_create_table_if = FALSE; + + /** + * UNSIGNED support + * + * @var bool|array + */ + protected $_unsigned = FALSE; + + /** + * NULL value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = 'NULL'; + + // -------------------------------------------------------------------- + /** * Create database * - * @param string the database name + * @param string $db_name (ignored) * @return bool */ public function create_database($db_name = '') @@ -53,7 +76,7 @@ class CI_DB_sqlite_forge extends CI_DB_forge { /** * Drop database * - * @param string the database name (ignored) + * @param string $db_name (ignored) * @return bool */ public function drop_database($db_name = '') @@ -77,126 +100,95 @@ class CI_DB_sqlite_forge extends CI_DB_forge { // -------------------------------------------------------------------- /** - * Create Table + * ALTER TABLE * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool + * @todo implement drop_column(), modify_column() + * @param string $alter_type ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * @return string|string[] */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + protected function _alter_table($alter_type, $table, $field) { - $sql = 'CREATE TABLE '; - - // IF NOT EXISTS added to SQLite in 3.3.0 - if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE) + if ($alter_type === 'DROP' OR $alter_type === 'CHANGE') { - $sql .= 'IF NOT EXISTS '; - } - - $sql .= $this->db->escape_identifiers($table).' ('; - $current_field_count = 0; - - foreach ($fields as $field => $attributes) - { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; - - empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')'; - - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } + // drop_column(): + // BEGIN TRANSACTION; + // CREATE TEMPORARY TABLE t1_backup(a,b); + // INSERT INTO t1_backup SELECT a,b FROM t1; + // DROP TABLE t1; + // CREATE TABLE t1(a,b); + // INSERT INTO t1 SELECT a,b FROM t1_backup; + // DROP TABLE t1_backup; + // COMMIT; + return FALSE; + } - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; + return parent::_alter_table($alter_type, $table, $field); + } - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } + // -------------------------------------------------------------------- - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .' '.$field['type'] + .$field['auto_increment'] + .$field['null'] + .$field['unique'] + .$field['default']; + } - if (count($primary_keys) > 0) - { - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; - } + // -------------------------------------------------------------------- - if (is_array($keys) && count($keys) > 0) + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); - - $sql .= ",\n\tUNIQUE (".implode(', ', $key).')'; - } + case 'ENUM': + case 'SET': + $attributes['TYPE'] = 'TEXT'; + return; + default: return; } - - return $sql."\n)"; } // -------------------------------------------------------------------- /** - * Alter table query + * Field attribute AUTO_INCREMENT * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), - * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @param array &$attributes + * @param array &$field + * @return void */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') + protected function _attr_auto_increment(&$attributes, &$field) { - /* SQLite only supports adding new columns and it does - * NOT support the AFTER statement. Each new column will - * be added as the last one in the table. - */ - if ($alter_type !== 'ADD COLUMN') + if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE) { - // Not supported - return FALSE; - } + $field['type'] = 'INTEGER PRIMARY KEY'; + $field['default'] = ''; + $field['null'] = ''; + $field['unique'] = ''; + $field['auto_increment'] = ' AUTOINCREMENT'; - return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name) - .' '.$column_definition - .($default_value != '' ? " DEFAULT '".$default_value."'" : '') - // If NOT NULL is specified, the field must have a DEFAULT value other than NULL - .(($null !== NULL && $default_value !== 'NULL') ? ' NOT NULL' : ' NULL'); + $this->primary_keys = array(); + } } } diff --git a/system/database/drivers/sqlite3/sqlite3_forge.php b/system/database/drivers/sqlite3/sqlite3_forge.php index e1dd3fa91..7ba0f7bab 100644 --- a/system/database/drivers/sqlite3/sqlite3_forge.php +++ b/system/database/drivers/sqlite3/sqlite3_forge.php @@ -35,10 +35,43 @@ defined('BASEPATH') OR exit('No direct script access allowed'); */ class CI_DB_sqlite3_forge extends CI_DB_forge { + /** + * UNSIGNED support + * + * @var bool|array + */ + protected $_unsigned = FALSE; + + /** + * NULL value representation in CREATE/ALTER TABLE statements + * + * @var string + */ + protected $_null = 'NULL'; + + // -------------------------------------------------------------------- + + /** + * Class constructor + * + * @return void + */ + public function __construct() + { + parent::__construct(); + + if (version_compare($this->db->version(), '3.3', '<')) + { + $this->create_table_if = FALSE; + } + } + + // -------------------------------------------------------------------- + /** * Create database * - * @param string the database name + * @param string $db_name * @return bool */ public function create_database($db_name = '') @@ -53,7 +86,7 @@ class CI_DB_sqlite3_forge extends CI_DB_forge { /** * Drop database * - * @param string the database name (ignored) + * @param string $db_name (ignored) * @return bool */ public function drop_database($db_name = '') @@ -85,125 +118,95 @@ class CI_DB_sqlite3_forge extends CI_DB_forge { // -------------------------------------------------------------------- /** - * Create Table + * ALTER TABLE * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return bool + * @todo implement drop_column(), modify_column() + * @param string $alter_type ALTER type + * @param string $table Table name + * @param mixed $field Column definition + * @return string|string[] */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) + protected function _alter_table($alter_type, $table, $field) { - $sql = 'CREATE TABLE '; - - // IF NOT EXISTS added to SQLite in 3.3.0 - if ($if_not_exists === TRUE && version_compare($this->db->version(), '3.3.0', '>=') === TRUE) - { - $sql .= 'IF NOT EXISTS '; - } - - $sql .= $this->db->escape_identifiers($table).' ('; - $current_field_count = 0; - - foreach ($fields as $field => $attributes) + if ($alter_type === 'DROP' OR $alter_type === 'CHANGE') { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; - - empty($attributes['CONSTRAINT']) OR $sql .= '('.$attributes['CONSTRAINT'].')'; - - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } + // drop_column(): + // BEGIN TRANSACTION; + // CREATE TEMPORARY TABLE t1_backup(a,b); + // INSERT INTO t1_backup SELECT a,b FROM t1; + // DROP TABLE t1; + // CREATE TABLE t1(a,b); + // INSERT INTO t1 SELECT a,b FROM t1_backup; + // DROP TABLE t1_backup; + // COMMIT; - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } + return FALSE; + } - $sql .= ( ! empty($attributes['NULL']) && $attributes['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; + return parent::_alter_table($alter_type, $table, $field); + } - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } + // -------------------------------------------------------------------- - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } + /** + * Process column + * + * @param array $field + * @return string + */ + protected function _process_column($field) + { + return $this->db->escape_identifiers($field['name']) + .' '.$field['type'] + .$field['auto_increment'] + .$field['null'] + .$field['unique'] + .$field['default']; + } - if (count($primary_keys) > 0) - { - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; - } + // -------------------------------------------------------------------- - if (is_array($keys) && count($keys) > 0) + /** + * Field attribute TYPE + * + * Performs a data type mapping between different databases. + * + * @param array &$attributes + * @return void + */ + protected function _attr_type(&$attributes) + { + switch (strtoupper($attributes['TYPE'])) { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->db->escape_identifiers($key)); - - $sql .= ",\n\tUNIQUE (".implode(', ', $key).')'; - } + case 'ENUM': + case 'SET': + $attributes['TYPE'] = 'TEXT'; + return; + default: return; } - - return $sql."\n)"; } // -------------------------------------------------------------------- /** - * Alter table query - * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * Field attribute AUTO_INCREMENT * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @param array &$attributes + * @param array &$field + * @return void */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') + protected function _attr_auto_increment(&$attributes, &$field) { - /* SQLite only supports adding new columns and it does - * NOT support the AFTER statement. Each new column will - * be added as the last one in the table. - */ - if ($alter_type !== 'ADD COLUMN') + if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE && stripos($field['type'], 'int') !== FALSE) { - // Not supported - return FALSE; - } + $field['type'] = 'INTEGER PRIMARY KEY'; + $field['default'] = ''; + $field['null'] = ''; + $field['unique'] = ''; + $field['auto_increment'] = ' AUTOINCREMENT'; - return 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name) - .' '.$column_definition - .($default_value !== '' ? ' DEFAULT '.$default_value : '') - // If NOT NULL is specified, the field must have a DEFAULT value other than NULL - .(($null !== NULL && $default_value !== 'NULL') ? ' NOT NULL' : ' NULL'); + $this->primary_keys = array(); + } } } diff --git a/system/database/drivers/sqlsrv/sqlsrv_forge.php b/system/database/drivers/sqlsrv/sqlsrv_forge.php index 8155565c0..da8f68c56 100644 --- a/system/database/drivers/sqlsrv/sqlsrv_forge.php +++ b/system/database/drivers/sqlsrv/sqlsrv_forge.php @@ -36,130 +36,98 @@ defined('BASEPATH') OR exit('No direct script access allowed'); class CI_DB_sqlsrv_forge extends CI_DB_forge { /** - * DROP TABLE statement + * CREATE TABLE IF statement * * @var string */ - protected $_drop_table = 'DROP TABLE %s'; - - // -------------------------------------------------------------------- + protected $_create_table_if = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'%s') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\nCREATE TABLE"; /** - * Create Table + * DROP TABLE IF statement * - * @param string the table name - * @param array the fields - * @param mixed primary key(s) - * @param mixed key(s) - * @param bool should 'IF NOT EXISTS' be added to the SQL - * @return string + * @var string */ - protected function _create_table($table, $fields, $primary_keys, $keys, $if_not_exists) - { - $sql = ($if_not_exists === TRUE) - ? "IF NOT EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'".$table."') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\n" - : ''; - - $sql .= 'CREATE TABLE '.$this->db->escape_identifiers($table).' ('; - - $current_field_count = 0; - foreach ($fields as $field => $attributes) - { - // Numeric field names aren't allowed in databases, so if the key is - // numeric, we know it was assigned by PHP and the developer manually - // entered the field information, so we'll simply add it to the list - if (is_numeric($field)) - { - $sql .= "\n\t".$attributes; - } - else - { - $attributes = array_change_key_case($attributes, CASE_UPPER); - - $sql .= "\n\t".$this->db->escape_identifiers($field).' '.$attributes['TYPE']; + protected $_drop_table_if = "IF EXISTS (SELECT * FROM sysobjects WHERE ID = object_id(N'%s') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)\nDROP TABLE"; - if (stripos($attributes['TYPE'], 'INT') === FALSE && ! empty($attributes['CONSTRAINT'])) - { - $sql .= '('.$attributes['CONSTRAINT'].')'; - } - - if ( ! empty($attributes['UNSIGNED']) && $attributes['UNSIGNED'] === TRUE) - { - $sql .= ' UNSIGNED'; - } - - if (isset($attributes['DEFAULT'])) - { - $sql .= " DEFAULT '".$attributes['DEFAULT']."'"; - } - - $sql .= ( ! empty($attributes['NULL']) && $attribues['NULL'] === TRUE) - ? ' NULL' : ' NOT NULL'; - - if ( ! empty($attributes['AUTO_INCREMENT']) && $attributes['AUTO_INCREMENT'] === TRUE) - { - $sql .= ' AUTO_INCREMENT'; - } - } + /** + * UNSIGNED support + * + * @var array + */ + protected $_unsigned = array( + 'TINYINT' => 'SMALLINT', + 'SMALLINT' => 'INT', + 'INT' => 'BIGINT', + 'REAL' => 'FLOAT' + ); - // don't add a comma on the end of the last field - if (++$current_field_count < count($fields)) - { - $sql .= ','; - } - } + // -------------------------------------------------------------------- - if (count($primary_keys) > 0) + /** + * 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) + { + if (in_array($alter_type, array('ADD', 'DROP'), TRUE)) { - $sql .= ",\n\tPRIMARY KEY (".implode(', ', $this->db->escape_identifiers($primary_keys)).')'; + return parent::_alter_table($alter_type, $table, $field); } - if (is_array($keys) && count($keys) > 0) + $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' ALTER COLUMN '; + $sqls = array(); + for ($i = 0, $c = count($field); $i < $c; $i++) { - foreach ($keys as $key) - { - $key = is_array($key) - ? $this->db->escape_identifiers($key) - : array($this->escape_identifiers($key)); - - $sql .= ",\n\tFOREIGN KEY (".implode(', ', $key).')'; - } + $sqls[] = $sql.$this->_process_column($field[$i]); } - return $sql."\n)"; + return $sqls; } // -------------------------------------------------------------------- /** - * Alter table query + * Field attribute TYPE * - * Generates a platform-specific query so that a table can be altered - * Called by add_column(), drop_column(), and column_alter(), + * Performs a data type mapping between different databases. * - * @param string the ALTER type (ADD, DROP, CHANGE) - * @param string the column name - * @param string the table name - * @param string the column definition - * @param string the default value - * @param bool should 'NOT NULL' be added - * @param string the field after which we should add the new field - * @return string + * @param array &$attributes + * @return void */ - protected function _alter_table($alter_type, $table, $column_name, $column_definition = '', $default_value = '', $null = '', $after_field = '') + protected function _attr_type(&$attributes) { - $sql = 'ALTER TABLE '.$this->db->escape_identifiers($table).' '.$alter_type.' '.$this->db->escape_identifiers($column_name); - - // DROP has everything it needs now. - if ($alter_type === 'DROP') + switch (strtoupper($attributes['TYPE'])) { - return $sql; + case 'MEDIUMINT': + $attributes['TYPE'] = 'INTEGER'; + $attributes['UNSIGNED'] = FALSE; + return; + case 'INTEGER': + $attributes['TYPE'] = 'INT'; + return; + default: return; } + } - return $sql.' '.$column_definition - .($default_value != '' ? ' DEFAULT "'.$default_value.'"' : '') - .($null === NULL ? ' NULL' : ' NOT NULL') - .($after_field != '' ? ' AFTER '.$this->db->escape_identifiers($after_field) : ''); + // -------------------------------------------------------------------- + + /** + * 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'] = ' IDENTITY(1,1)'; + } } } diff --git a/tests/mocks/database/db/driver.php b/tests/mocks/database/db/driver.php index 65ac2c4cc..2cf54b97b 100644 --- a/tests/mocks/database/db/driver.php +++ b/tests/mocks/database/db/driver.php @@ -34,6 +34,7 @@ class Mock_Database_DB_Driver extends CI_DB_driver { return call_user_func_array(array($this->ci_db_driver, $method), $arguments); } + } class CI_DB extends Mock_Database_DB_QueryBuilder {} \ No newline at end of file diff --git a/tests/mocks/database/schema/skeleton.php b/tests/mocks/database/schema/skeleton.php index d72244528..e5c536090 100644 --- a/tests/mocks/database/schema/skeleton.php +++ b/tests/mocks/database/schema/skeleton.php @@ -69,7 +69,7 @@ class Mock_Database_Schema_Skeleton { ) )); static::$forge->add_key('id', TRUE); - static::$forge->create_table('user', (strpos(static::$driver, 'pgsql') === FALSE)); + static::$forge->create_table('user', TRUE); // Job Table static::$forge->add_field(array( @@ -86,7 +86,7 @@ class Mock_Database_Schema_Skeleton { ) )); static::$forge->add_key('id', TRUE); - static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE)); + static::$forge->create_table('job', TRUE); // Misc Table static::$forge->add_field(array( @@ -103,7 +103,7 @@ class Mock_Database_Schema_Skeleton { ) )); static::$forge->add_key('id', TRUE); - static::$forge->create_table('misc', (strpos(static::$driver, 'pgsql') === FALSE)); + static::$forge->create_table('misc', TRUE); } /** diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index cbc6295c8..717d7f6ba 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -154,6 +154,8 @@ Release Date: Not Released - Added MySQL client compression support. - Added encrypted connections support (for *mysql*, *sqlsrv* and PDO with *sqlsrv*). - Removed :doc:`Loader Class ` from Database error tracing to better find the likely culprit. + - Added an optional second parameter to ``drop_table()`` in :doc:`Database Forge ` that allows adding the IF EXISTS condition. + - Removed the optional AFTER clause from :doc:`Database Forge ` method ``add_column()``. - Libraries diff --git a/user_guide_src/source/database/forge.rst b/user_guide_src/source/database/forge.rst index bf17e2918..1d6b847b4 100644 --- a/user_guide_src/source/database/forge.rst +++ b/user_guide_src/source/database/forge.rst @@ -193,13 +193,15 @@ into the definition Dropping a table ================ -Executes a DROP TABLE sql +Execute a DROP TABLE statement and optionally add an IF EXISTS clause. :: + // Produces: DROP TABLE table_name $this->dbforge->drop_table('table_name'); - // gives DROP TABLE IF EXISTS table_name + // Produces: DROP TABLE IF EXISTS table_name + $this->dbforge->drop_table('table_name'); Renaming a table ================ @@ -231,14 +233,6 @@ number of additional fields. $this->dbforge->add_column('table_name', $fields); // gives ALTER TABLE table_name ADD preferences TEXT -An optional third parameter can be used to specify which existing column -to add the new column after. - -:: - - $this->dbforge->add_column('table_name', $fields, 'after_field'); - - $this->dbforge->drop_column() ============================== diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index fd5eea478..4e0b952d5 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -93,9 +93,31 @@ Step 8: Check the calls to Array Helper's element() and elements() functions The default return value of these functions, when the required elements don't exist, has been changed from FALSE to NULL. -********************************************************** -Step 9: Change usage of Email library with multiple emails -********************************************************** +************************************************************ +Step 9: Update usage of Database Forge's drop_table() method +************************************************************ + +Up until now, ``drop_table()`` added an IF EXISTS clause by default or it didn't work +at all with some drivers. In CodeIgniter 3.0, the IF EXISTS condition is no longer added +by deafault and has an optional second parameter that allows that instead and is set to +FALSE by default. + +If your application relies on IF EXISTS, you'll have to change its usage. + +:: + + // Now produces just DROP TABLE `table_name` + $this->dbforge->drop_table('table_name'); + + // Produces DROP TABLE IF EXISTS `table_name` + $this->dbforge->drop_table('table_name', TRUE); + +.. note:: The given example users MySQL-specific syntax, but it should work across + all drivers with the exception of ODBC. + +*********************************************************** +Step 10: Change usage of Email library with multiple emails +*********************************************************** The :doc:`Email library <../libraries/email>` will automatically clear the set parameters after successfully sending emails. To override this behaviour, @@ -110,7 +132,7 @@ pass FALSE as the first parameter in the ``send()`` method: **************************************************************** -Step 10: Remove usage of (previously) deprecated functionalities +Step 11: Remove usage of (previously) deprecated functionalities **************************************************************** In addition to the ``$autoload['core']`` configuration setting, there's a number of other functionalities -- 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(-) 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(-) 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