From 442fb47768d9d0f15ed0de7795fea66fb7e4e69a Mon Sep 17 00:00:00 2001
From: lsmith <lsmith@625475ce-881a-0410-a577-b389adb331d8>
Date: Thu, 10 Jan 2008 09:45:01 +0000
Subject: [PATCH] - unified getDefaultFieldDeclaration(), force DEFAULT NULL
 when no default is set and the field allows nulls

---
 lib/Doctrine/DataDict/Mysql.php | 19 ++++++++-----------
 lib/Doctrine/Export.php         | 28 ++++++++++++++++------------
 lib/Doctrine/Export/Mysql.php   |  5 ++++-
 3 files changed, 28 insertions(+), 24 deletions(-)

diff --git a/lib/Doctrine/DataDict/Mysql.php b/lib/Doctrine/DataDict/Mysql.php
index cfb7c849e..845de3942 100644
--- a/lib/Doctrine/DataDict/Mysql.php
+++ b/lib/Doctrine/DataDict/Mysql.php
@@ -230,7 +230,7 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict
     }
 
     /**
-     * Maps a native array description of a field to a MDB2 datatype and length
+     * Maps a native array description of a field to a Doctrine datatype and length
      *
      * @param array  $field native field description
      * @return array containing the various possible types, length, sign, fixed
@@ -440,25 +440,22 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict
      *                 declare the specified field.
      */
     public function getIntegerDeclaration($name, $field)
-    {
+    {
         $default = $autoinc = '';
         if ( ! empty($field['autoincrement'])) {
             $autoinc = ' AUTO_INCREMENT';
         } elseif (array_key_exists('default', $field)) {
             if ($field['default'] === '') {
                 $field['default'] = empty($field['notnull']) ? null : 0;
-            }
-            if (is_null($field['default'])) {
-                $default = ' DEFAULT NULL';
-            } else {
+            }
+            if (is_null($field['default'])) {
+                $default = ' DEFAULT NULL';
+            } else {
                 $default = ' DEFAULT '.$this->conn->quote($field['default']);
             }
-        }
-        /**
-        elseif (empty($field['notnull'])) {
+        } elseif (empty($field['notnull'])) {
             $default = ' DEFAULT NULL';
         }
-        */
 
         $notnull  = (isset($field['notnull'])  && $field['notnull'])  ? ' NOT NULL' : '';
         $unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : '';
@@ -467,4 +464,4 @@ class Doctrine_DataDict_Mysql extends Doctrine_DataDict
 
         return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned . $default . $notnull . $autoinc;
     }
-}
\ No newline at end of file
+}
diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php
index b84172d17..87de3850d 100644
--- a/lib/Doctrine/Export.php
+++ b/lib/Doctrine/Export.php
@@ -694,6 +694,12 @@ class Doctrine_Export extends Doctrine_Connection_Module
     public function getDeclaration($name, array $field)
     {
 
+        $method = 'get' . $field['type'] . 'Declaration';
+
+        if (method_exists($this->conn->dataDict, $method)) {
+            return $this->conn->dataDict->$method($name, $field);
+        }
+
         $default   = $this->getDefaultFieldDeclaration($field);
 
         $charset   = (isset($field['charset']) && $field['charset']) ?
@@ -710,13 +716,8 @@ class Doctrine_Export extends Doctrine_Connection_Module
         $check     = (isset($field['check']) && $field['check']) ?
                     ' ' . $field['check'] : '';
 
-        $method = 'get' . $field['type'] . 'Declaration';
+        $dec = $this->conn->dataDict->getNativeDeclaration($field);
 
-        if (method_exists($this->conn->dataDict, $method)) {
-            return $this->conn->dataDict->$method($name, $field);
-        } else {
-            $dec = $this->conn->dataDict->getNativeDeclaration($field);
-        }
         return $this->conn->quoteIdentifier($name, true) . ' ' . $dec . $charset . $default . $notnull . $unique . $check . $collation;
     }
 
@@ -730,15 +731,18 @@ class Doctrine_Export extends Doctrine_Connection_Module
      */
     public function getDefaultFieldDeclaration($field)
     {
-        $default = '';
+        $default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
         if (isset($field['default'])) {
             if ($field['default'] === '') {
-                $field['default'] = empty($field['notnull'])
-                    ? null : $this->valid_default_values[$field['type']];
+                $field['default'] = null;
+                if (! empty($field['notnull']) && array_key_exists($field['type'], $this->valid_default_values)) {
+                   $field['default'] = $this->valid_default_values[$field['type']];
+                }
 
-                if ($field['default'] === '' &&
-                   ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)) {
-                    $field['default'] = null;
+                if ($field['default'] === ''
+                    && ($this->conn->getAttribute(Doctrine::ATTR_PORTABILITY) & Doctrine::PORTABILITY_EMPTY_TO_NULL)
+                ) {
+                    $field['default'] = ' ';
                 }
             }
 
diff --git a/lib/Doctrine/Export/Mysql.php b/lib/Doctrine/Export/Mysql.php
index 8fcdc8677..fc8cdcd9b 100644
--- a/lib/Doctrine/Export/Mysql.php
+++ b/lib/Doctrine/Export/Mysql.php
@@ -512,7 +512,7 @@ class Doctrine_Export_Mysql extends Doctrine_Export
      */
     public function getDefaultFieldDeclaration($field)
     {
-        $default = '';
+        $default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
         if (isset($field['default']) && ( ! isset($field['length']) || $field['length'] <= 255)) {
             if ($field['default'] === '') {
                 $field['default'] = null;
@@ -530,6 +530,9 @@ class Doctrine_Export_Mysql extends Doctrine_Export
             if ($field['type'] == 'enum' && $this->conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
                 $fieldType = 'varchar';
             } else {
+               if ($field['type'] === 'boolean') {
+                   $fields['default'] = $this->conn->convertBooleans($field['default']);
+               }
                 $fieldType = $field['type'];
             }