From 21b7a2a2d00bd5645b2ca1afcfa4098e207292a4 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 22 Nov 2016 13:23:33 +0200 Subject: Add support for CURRENT_TIMESTAMP (and similar) defaults to date/time fields in DB Forge As requested in #4852 --- system/database/DB_forge.php | 36 ++++++++++++++++++++++++------------ user_guide_src/source/changelog.rst | 6 +++++- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/system/database/DB_forge.php b/system/database/DB_forge.php index ed6f4b672..fe81d0510 100644 --- a/system/database/DB_forge.php +++ b/system/database/DB_forge.php @@ -894,21 +894,33 @@ abstract class CI_DB_forge { return; } - if (array_key_exists('DEFAULT', $attributes)) + if ( ! array_key_exists('DEFAULT', $attributes)) { - if ($attributes['DEFAULT'] === NULL) - { - $field['default'] = empty($this->_null) ? '' : $this->_default.$this->_null; + return; + } - // Override the NULL attribute if that's our default - $attributes['NULL'] = TRUE; - $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; - } - else - { - $field['default'] = $this->_default.$this->db->escape($attributes['DEFAULT']); - } + if ($attributes['DEFAULT'] === NULL) + { + $field['default'] = empty($this->_null) ? '' : $this->_default.$this->_null; + + // Override the NULL attribute if that's our default + $attributes['NULL'] = TRUE; + $field['null'] = empty($this->_null) ? '' : ' '.$this->_null; + return; } + + // White-list CURRENT_TIMESTAMP & similar (e.g. Oracle has stuff like SYSTIMESTAMP) defaults for date/time fields + if ( + isset($attributes['TYPE']) + && (stripos($attributes['TYPE'], 'time') !== FALSE || stripos($attributes['TYPE'], 'date') !== FALSE) + && (stripos($attributes['DEFAULT'], 'time') !== FALSE || stripos($attributes['DEFAULT'], 'date') !== FALSE) + ) + { + $field['default'] = $this->_default.$attributes['DEFAULT']; + return; + } + + $field['default'] = $this->_default.$this->db->escape($attributes['DEFAULT']); } // -------------------------------------------------------------------- diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 9c3058ce5..692d06833 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -31,12 +31,16 @@ Release Date: Not Released - Changed method ``clear()`` to also reset captions. -- Database +- :doc:`Database ` - Changed method ``initialize()`` to return void and instead throw a ``RuntimeException`` in case of failure. - Changed method ``db_connect()`` to always set the connection character set (if supported by the driver) and to fail if it can't. - Removed method ``db_set_charset()`` and the ability to change a connection character set at runtime. + - :doc:`Database Forge `: + + - Added support for declaring date/time type fields default values as ``CURRENT_TIMESTAMP`` and similar. + - Helpers - Added new function :php:func:`ordinal_format()` to :doc:`Inflector Helper `. -- cgit v1.2.3-24-g4f1b