From 07e73880f5d99fab180987ffc7758646fea96639 Mon Sep 17 00:00:00 2001
From: beberlei <beberlei@625475ce-881a-0410-a577-b389adb331d8>
Date: Fri, 19 Feb 2010 22:33:54 +0000
Subject: [PATCH] [2.0] DDC-92 - Completly removed DoctrineException in DBAL
 package

---
 lib/Doctrine/DBAL/DBALException.php           | 25 +++++++++-
 .../DBAL/Platforms/AbstractPlatform.php       |  8 +++-
 lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php |  7 ++-
 lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 46 +------------------
 .../DBAL/Platforms/OraclePlatform.php         |  2 +-
 .../DBAL/Platforms/PostgreSqlPlatform.php     |  2 +-
 .../DBAL/Platforms/SqlitePlatform.php         |  9 +---
 .../DBAL/Schema/AbstractSchemaManager.php     |  1 -
 lib/Doctrine/DBAL/Schema/Table.php            |  5 ++
 .../Platforms/AbstractPlatformTestCase.php    |  8 ++++
 .../Doctrine/Tests/DBAL/Schema/TableTest.php  |  6 +++
 11 files changed, 56 insertions(+), 63 deletions(-)

diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php
index 58728ac56..8d29c310f 100644
--- a/lib/Doctrine/DBAL/DBALException.php
+++ b/lib/Doctrine/DBAL/DBALException.php
@@ -6,7 +6,7 @@ class DBALException extends \Exception
 {
     public static function notSupported($method)
     {
-        return new self("Operation '$method' is not supported.");
+        return new self("Operation '$method' is not supported by platform.");
     }
 
     public static function invalidPlatformSpecified()
@@ -47,4 +47,27 @@ class DBALException extends \Exception
         return new self("The given 'driverClass' ".$driverClass." has to implement the ".
             "\Doctrine\DBAL\Driver interface.");
     }
+
+    /**
+     * @param string $tableName
+     * @return DBALException
+     */
+    public static function invalidTableName($tableName)
+    {
+        return new self("Invalid table name specified: ".$tableName);
+    }
+
+    /**
+     * @param string $tableName
+     * @return DBALException
+     */
+    public static function noColumnsSpecifiedForTable($tableName)
+    {
+        return new self("No columns specified for table ".$tableName);
+    }
+
+    public static function limitOffsetInvalid()
+    {
+        return new self("Invalid Offset in Limit Query, it has to be larger or equal to 0.");
+    }
 }
\ No newline at end of file
diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
index 63e421535..9706b2c20 100644
--- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -582,6 +582,10 @@ abstract class AbstractPlatform
             throw new \InvalidArgumentException("Second argument of AbstractPlatform::getCreateTableSql() has to be integer.");
         }
 
+        if (count($table->getColumns()) == 0) {
+            throw DBALException::noColumnsSpecifiedForTable($table->getName());
+        }
+
         $tableName = $table->getName();
         $options = $table->getOptions();
         $options['uniqueConstraints'] = array();
@@ -692,7 +696,7 @@ abstract class AbstractPlatform
      * Gets the SQL to create a sequence on this platform.
      *
      * @param \Doctrine\DBAL\Schema\Sequence $sequence
-     * @throws DoctrineException
+     * @throws DBALException
      */
     public function getCreateSequenceSql(\Doctrine\DBAL\Schema\Sequence $sequence)
     {
@@ -1573,7 +1577,7 @@ abstract class AbstractPlatform
      */
     public function getTimeTypeDeclarationSql(array $fieldDeclaration)
     {
-        throw DoctrineException::getTimeTypeDeclarationNotSupported($this);
+        throw DBALException::notSupported(__METHOD__);
     }
 
     /**
diff --git a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
index 4eb758b0f..04f970c7c 100644
--- a/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
+++ b/lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
@@ -21,9 +21,8 @@
 
 namespace Doctrine\DBAL\Platforms;
 
-use \Doctrine\DBAL\Schema\TableDiff;
-
-use Doctrine\Common\DoctrineException;
+use Doctrine\DBAL\Schema\TableDiff;
+use Doctrine\DBAL\DBALException;
 
 /**
  * The MsSqlPlatform provides the behavior, features and SQL dialect of the
@@ -54,7 +53,7 @@ class MsSqlPlatform extends AbstractPlatform
 
             $offset = intval($offset);
             if ($offset < 0) {
-                throw \Doctrine\Common\DoctrineException::limitOffsetInvalid($offset);
+                throw DBALException::limitOffsetInvalid($offset);
             }
     
             $orderby = stristr($query, 'ORDER BY');
diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
index a1f8f1b19..6e6349548 100644
--- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
+++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
@@ -21,7 +21,7 @@
 
 namespace Doctrine\DBAL\Platforms;
 
-use Doctrine\Common\DoctrineException,
+use Doctrine\DBAL\DBALException,
     Doctrine\DBAL\Schema\TableDiff;
 
 /**
@@ -347,12 +347,6 @@ class MySqlPlatform extends AbstractPlatform
      */
     protected function _getCreateTableSql($tableName, array $columns, array $options = array())
     {
-        if ( ! $tableName) {
-            throw DoctrineException::missingTableName();
-        }
-        if (empty($columns)) {
-            throw DoctrineException::missingFieldsArrayForTable($tableName);
-        }
         $queryFields = $this->getColumnDeclarationListSql($columns);
 
         if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
@@ -593,44 +587,6 @@ class MySqlPlatform extends AbstractPlatform
         return $unsigned . $autoinc;
     }
     
-    /**
-     * Obtain DBMS specific SQL code portion needed to set an index
-     * declaration to be used in statements like CREATE TABLE.
-     *
-     * @return string
-     * @override
-     */
-    public function getIndexFieldDeclarationListSql(array $fields)
-    {
-        $declFields = array();
-
-        foreach ($fields as $fieldName => $field) {
-            $fieldString = $fieldName;
-
-            if (is_array($field)) {
-                if (isset($field['length'])) {
-                    $fieldString .= '(' . $field['length'] . ')';
-                }
-
-                if (isset($field['sorting'])) {
-                    $sort = strtoupper($field['sorting']);
-                    switch ($sort) {
-                        case 'ASC':
-                        case 'DESC':
-                            $fieldString .= ' ' . $sort;
-                            break;
-                        default:
-                            throw DoctrineException::unknownIndexSortingOption($sort);
-                    }
-                }
-            } else {
-                $fieldString = $field;
-            }
-            $declFields[] = $fieldString;
-        }
-        return implode(', ', $declFields);
-    }
-    
     /**
      * Return the FOREIGN KEY query section dealing with non-standard options
      * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php
index d86489ca0..9c9285a16 100644
--- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php
+++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php
@@ -111,7 +111,7 @@ class OraclePlatform extends AbstractPlatform
      * in {@see listSequences()}
      *
      * @param \Doctrine\DBAL\Schema\Sequence $sequence
-     * @throws DoctrineException
+     * @return string
      */
     public function getCreateSequenceSql(\Doctrine\DBAL\Schema\Sequence $sequence)
     {
diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
index d1316625e..6b6d8279e 100644
--- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
+++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
@@ -387,7 +387,7 @@ class PostgreSqlPlatform extends AbstractPlatform
      * Gets the SQL to create a sequence on this platform.
      *
      * @param \Doctrine\DBAL\Schema\Sequence $sequence
-     * @throws DoctrineException
+     * @return string
      */
     public function getCreateSequenceSql(\Doctrine\DBAL\Schema\Sequence $sequence)
     {
diff --git a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
index 01b72c253..e9e46eac8 100644
--- a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
+++ b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
@@ -21,7 +21,7 @@
 
 namespace Doctrine\DBAL\Platforms;
 
-use Doctrine\Common\DoctrineException;
+use Doctrine\DBAL\DBALException;
 
 /**
  * The SqlitePlatform class describes the specifics and dialects of the SQLite
@@ -267,13 +267,6 @@ class SqlitePlatform extends AbstractPlatform
      */
     protected function _getCreateTableSql($name, array $columns, array $options = array())
     {
-        if ( ! $name) {
-            throw DoctrineException::invalidTableName($name);
-        }
-
-        if (empty($columns)) {
-            throw DoctrineException::noFieldsSpecifiedForTable($name);
-        }
         $queryFields = $this->getColumnDeclarationListSql($columns);
 
         $autoinc = false;
diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
index bf9134563..9df12fc1c 100644
--- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
+++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php
@@ -22,7 +22,6 @@
 namespace Doctrine\DBAL\Schema;
 
 use \Doctrine\DBAL\Types;
-use \Doctrine\Common\DoctrineException;
 use \Doctrine\DBAL\DBALException;
 use \Doctrine\DBAL\Platforms\AbstractPlatform;
 
diff --git a/lib/Doctrine/DBAL/Schema/Table.php b/lib/Doctrine/DBAL/Schema/Table.php
index 18ab773b2..4a91b0f38 100644
--- a/lib/Doctrine/DBAL/Schema/Table.php
+++ b/lib/Doctrine/DBAL/Schema/Table.php
@@ -23,6 +23,7 @@ namespace Doctrine\DBAL\Schema;
 
 use Doctrine\DBAL\Types\Type;
 use Doctrine\DBAL\Schema\Visitor\Visitor;
+use Doctrine\DBAL\DBALException;
 
 /**
  * Object Representation of a table
@@ -101,6 +102,10 @@ class Table extends AbstractAsset
      */
     public function __construct($tableName, array $columns=array(), array $indexes=array(), array $fkConstraints=array(), $idGeneratorType=self::ID_NONE, array $options=array())
     {
+        if (strlen($tableName) == 0) {
+            throw DBALException::invalidTableName($tableName);
+        }
+
         $this->_setName($tableName);
         $this->_idGeneratorType = $idGeneratorType;
         
diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
index a100b37f0..6918254c9 100644
--- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
+++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php
@@ -16,6 +16,14 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
         $this->_platform = $this->createPlatform();
     }
 
+    public function testCreateWithNoColumns()
+    {
+        $table = new \Doctrine\DBAL\Schema\Table('test');
+
+        $this->setExpectedException('Doctrine\DBAL\DBALException');
+        $sql = $this->_platform->getCreateTableSql($table);
+    }
+
     public function testGeneratesTableCreationSql()
     {
         $table = new \Doctrine\DBAL\Schema\Table('test');
diff --git a/tests/Doctrine/Tests/DBAL/Schema/TableTest.php b/tests/Doctrine/Tests/DBAL/Schema/TableTest.php
index 1b39d9d32..cb70499f0 100644
--- a/tests/Doctrine/Tests/DBAL/Schema/TableTest.php
+++ b/tests/Doctrine/Tests/DBAL/Schema/TableTest.php
@@ -14,6 +14,12 @@ use Doctrine\DBAL\Types\Type;
 
 class TableTest extends \PHPUnit_Framework_TestCase
 {
+    public function testCreateWithInvalidTableName()
+    {
+        $this->setExpectedException('Doctrine\DBAL\DBALException');
+        $table = new \Doctrine\DBAL\Schema\Table('');
+    }
+
     public function testGetName()
     {
         $table =  new Table("foo", array(), array(), array());