Welcome Guest, Not a member yet? Register   Sign In
DB_Forge, default value
#1

Hi,

I hope I've put this discussion in the right forum since it's, in the end, a feature discussion.

I'm using DBForge to create my database. I would like the following SQL emitted from DBForge:
Code:
CREATE TABLE `UserEvents` (
 `id` int(9) NOT NULL AUTO_INCREMENT,
 `Ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

I do:
Code:
$this->dbforge->add_field(
           array(
                 'Ts' => array(
                     'type' => 'TIMESTAMP',
                     'default' => 'CURRENT_TIMESTAMP',
                 ),
             )
         );

However this emits (note the quotes around CURRENT_TIMESTAMP):
Code:
 `Ts` timestamp NOT NULL DEFAULT 'CURRENT_TIMESTAMP'

The reason is found in system/database/DB_forge.php, function _attr_default, where $this->db->escape() is used on the supplied default value. While this would be a reasonable way in most cases, in this case it's not since the default value should not be the string 'CURRENT_TIMESTAMP' but instead a MySQL function CURRENT_TIMESTAMP.

I would suggest adding another attribute (e.g. DEFAULT_LITTERAL) which is not run through $this->db->escape()
. These would be mutually exclusive (if both are present). In fact, I've already implemented this in my own project and would like to hear any comments and also see if there is any interest in adding this to the default codebase.

My humble patch:

Code:
--- DB_forge.php.orig   2014-12-17 22:01:42.337112070 +0100
+++ DB_forge.php        2014-12-17 22:07:55.365566945 +0100
@@ -940,18 +940,28 @@

               if (array_key_exists('DEFAULT', $attributes))
               {
-                       if ($attributes['DEFAULT'] === NULL)
-                       {
-                               $field['default'] = empty($this->_null) ? '' : $this->_default.$this->_null;
+                       $arraykey = 'DEFAULT';
+               }
+               elseif (array_key_exists('DEFAULT_LITTERAL', $attributes))
+               {
+                       $arraykey = 'DEFAULT_LITTERAL';
+               }
+               else
+               {
+                       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[$arraykey] === 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;
+               }
+               else
+               {
+                       $field['default'] = ($arraykey == 'DEFAULT' ? $this->_default.$this->db->escape($attributes[$arraykey]) : $this->_default.$attributes[$arraykey]);
               }
       }

Kind regards

Daniel Sahlberg
Reply


Messages In This Thread
DB_Forge, default value - by danielsan - 12-26-2014, 01:11 AM
RE: DB_Forge, default value - by jlp - 12-31-2014, 05:36 PM
RE: DB_Forge, default value - by mwhitney - 01-02-2015, 02:16 PM
RE: DB_Forge, default value - by kenjis - 12-09-2015, 07:37 PM
RE: DB_Forge, default value - by danielsan - 01-03-2015, 11:24 AM
RE: DB_Forge, default value - by mwhitney - 01-05-2015, 03:13 PM
RE: DB_Forge, default value - by harpreet - 03-31-2015, 06:23 AM
RE: DB_Forge, default value - by CroNiX - 03-31-2015, 07:04 AM
RE: DB_Forge, default value - by harpreet - 03-31-2015, 07:09 AM
RE: DB_Forge, default value - by harpreet - 03-31-2015, 07:12 AM
RE: DB_Forge, default value - by CroNiX - 03-31-2015, 08:41 AM
RE: DB_Forge, default value - by mwhitney - 04-01-2015, 10:25 AM
RE: DB_Forge, default value - by CroNiX - 04-01-2015, 10:43 AM



Theme © iAndrew 2016 - Forum software by © MyBB