diff --git a/lib/Doctrine/DBAL/Connection.php b/lib/Doctrine/DBAL/Connection.php index e6196280c..8e26cdf96 100644 --- a/lib/Doctrine/DBAL/Connection.php +++ b/lib/Doctrine/DBAL/Connection.php @@ -180,6 +180,16 @@ class Connection $this->_platform = $driver->getDatabasePlatform(); $this->_transactionIsolationLevel = $this->_platform->getDefaultTransactionIsolationLevel(); } + + /** + * Gets the DBAL driver instance. + * + * @return Doctrine\DBAL\Driver + */ + public function getDriver() + { + return $this->_driver; + } /** * Gets the Configuration used by the Connection. diff --git a/lib/Doctrine/DBAL/DriverManager.php b/lib/Doctrine/DBAL/DriverManager.php index 8ca285f5c..ad7d988f4 100644 --- a/lib/Doctrine/DBAL/DriverManager.php +++ b/lib/Doctrine/DBAL/DriverManager.php @@ -16,7 +16,7 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ namespace Doctrine\DBAL; diff --git a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php index 161d743b6..e53c57580 100644 --- a/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php @@ -1013,13 +1013,13 @@ abstract class AbstractPlatform } } - $query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name, true) . ' (' . $queryFields; + $query = 'CREATE TABLE ' . $this->quoteIdentifier($table, true) . ' (' . $queryFields; - $check = $this->getCheckDeclaration($columns); + /*$check = $this->getCheckDeclaration($columns); if ( ! empty($check)) { $query .= ', ' . $check; - } + }*/ $query .= ')'; diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 2d0097fb5..abec3c083 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -114,7 +114,6 @@ class MySqlPlatform extends AbstractPlatform );*/ /** - * Constructor. * Creates a new MySqlPlatform instance. */ public function __construct() @@ -1041,56 +1040,43 @@ class MySqlPlatform extends AbstractPlatform */ public function getIntegerTypeDeclarationSql(array $field) { - return 'INT ' . $this->_getCommonIntegerTypeDeclarationSql($field); + return 'INT' . $this->_getCommonIntegerTypeDeclarationSql($field); } /** @override */ public function getBigIntTypeDeclarationSql(array $field) { - return 'BIGINT ' . $this->_getCommonIntegerTypeDeclarationSql($field); + return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSql($field); } /** @override */ public function getTinyIntTypeDeclarationSql(array $field) { - return 'TINYINT ' . $this->_getCommonIntegerTypeDeclarationSql($field); + return 'TINYINT' . $this->_getCommonIntegerTypeDeclarationSql($field); } /** @override */ public function getSmallIntTypeDeclarationSql(array $field) { - return 'SMALLINT ' . $this->_getCommonIntegerTypeDeclarationSql($field); + return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSql($field); } /** @override */ public function getMediumIntTypeDeclarationSql(array $field) { - return 'MEDIUMINT ' . $this->_getCommonIntegerTypeDeclarationSql($field); + return 'MEDIUMINT' . $this->_getCommonIntegerTypeDeclarationSql($field); } /** @override */ protected function _getCommonIntegerTypeDeclarationSql(array $columnDef) { - $default = $autoinc = ''; + $autoinc = ''; if ( ! empty($columnDef['autoincrement'])) { $autoinc = ' AUTO_INCREMENT'; - } elseif (array_key_exists('default', $columnDef)) { - if ($columnDef['default'] === '') { - $columnDef['default'] = empty($columnDef['notnull']) ? null : 0; - } - if (is_null($columnDef['default'])) { - $default = ' DEFAULT NULL'; - } else { - $default = ' DEFAULT '.$this->quote($columnDef['default']); - } - } elseif (empty($columnDef['notnull'])) { - $default = ' DEFAULT NULL'; } - - $notnull = (isset($columnDef['notnull']) && $columnDef['notnull']) ? ' NOT NULL' : ''; $unsigned = (isset($columnDef['unsigned']) && $columnDef['unsigned']) ? ' UNSIGNED' : ''; - return $unsigned . $default . $notnull . $autoinc; + return $unsigned . $autoinc; } /** @@ -1104,33 +1090,31 @@ class MySqlPlatform extends AbstractPlatform */ public function getDefaultFieldDeclarationSql($field) { - $default = empty($field['notnull']) && !in_array($field['type'], array('clob', 'blob')) - ? ' DEFAULT NULL' : ''; + $default = empty($field['notnull']) ? ' DEFAULT NULL' : ''; if (isset($field['default']) && ( ! isset($field['length']) || $field['length'] <= 255)) { if ($field['default'] === '') { $field['default'] = null; - if (! empty($field['notnull']) && array_key_exists($field['type'], $this->valid_default_values)) { + /*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'] = ' '; - } + }*/ } - if ($field['type'] == 'enum' && $this->_conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { + /*if ($field['type'] == 'enum' && $this->_conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) { $fieldType = 'varchar'; } else { if ($field['type'] === 'boolean') { $fields['default'] = $this->convertBooleans($field['default']); } $fieldType = $field['type']; - } + }*/ - $default = ' DEFAULT ' . $this->quote($field['default'], $fieldType); + $default = ' DEFAULT ' . $this->quote($field['default'], $field['type']); } return $default; } diff --git a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php index c8ca3b318..2b7565e9d 100644 --- a/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php @@ -450,10 +450,10 @@ class SqlitePlatform extends AbstractPlatform /** @override */ protected function _getCommonIntegerTypeDeclarationSql(array $columnDef) { - $autoinc = ! empty($columnDef['autoincrement']) ? 'AUTOINCREMENT' : ''; - $pk = ! empty($columnDef['primary']) && ! empty($autoinc) ? 'PRIMARY KEY' : ''; + $autoinc = ! empty($columnDef['autoincrement']) ? ' AUTOINCREMENT' : ''; + $pk = ! empty($columnDef['primary']) && ! empty($autoinc) ? ' PRIMARY KEY' : ''; - return "INTEGER $pk $autoinc"; + return "INTEGER" . $pk . $autoinc; } /** @@ -513,13 +513,13 @@ class SqlitePlatform extends AbstractPlatform $name = $this->quoteIdentifier($name, true); $sql = 'CREATE TABLE ' . $name . ' (' . $queryFields; - if ($check = $this->getCheckDeclarationSql($fields)) { + /*if ($check = $this->getCheckDeclarationSql($fields)) { $sql .= ', ' . $check; } if (isset($options['checks']) && $check = $this->getCheckDeclarationSql($options['checks'])) { $sql .= ', ' . $check; - } + }*/ $sql .= ')'; @@ -527,7 +527,7 @@ class SqlitePlatform extends AbstractPlatform if (isset($options['indexes']) && ! empty($options['indexes'])) { foreach ($options['indexes'] as $index => $definition) { - $query[] = $this->createIndexSql($name, $index, $definition); + $query[] = $this->getCreateIndexSql($name, $index, $definition); } } return $query; diff --git a/lib/Doctrine/ORM/Export/ClassExporter.php b/lib/Doctrine/ORM/Export/ClassExporter.php index ffa2116e7..d1ac2edbc 100644 --- a/lib/Doctrine/ORM/Export/ClassExporter.php +++ b/lib/Doctrine/ORM/Export/ClassExporter.php @@ -94,6 +94,7 @@ class ClassExporter $column['notnull'] = ! $mapping['nullable']; if ($class->isIdentifier($fieldName)) { $column['primary'] = true; + $options['primary'][] = $mapping['columnName']; if ($class->isIdGeneratorIdentity()) { $column['autoincrement'] = true; } @@ -124,6 +125,7 @@ class ClassExporter } else if ($mapping->isManyToMany() && $mapping->isOwningSide()) { //... create join table $joinTableColumns = array(); + $joinTableOptions = array(); $joinTable = $mapping->getJoinTable(); $constraint1 = array(); $constraint1['tableName'] = $joinTable['name']; @@ -133,6 +135,7 @@ class ClassExporter foreach ($joinTable['joinColumns'] as $joinColumn) { $column = array(); $column['primary'] = true; + $joinTableOptions['primary'][] = $joinColumn['name']; $column['name'] = $joinColumn['name']; $column['type'] = $class->getTypeOfColumn($joinColumn['referencedColumnName']); $joinTableColumns[$joinColumn['name']] = $column; @@ -149,6 +152,7 @@ class ClassExporter foreach ($joinTable['inverseJoinColumns'] as $inverseJoinColumn) { $column = array(); $column['primary'] = true; + $joinTableOptions['primary'][] = $inverseJoinColumn['name']; $column['name'] = $inverseJoinColumn['name']; $column['type'] = $this->_em->getClassMetadata($mapping->getTargetEntityName()) ->getTypeOfColumn($inverseJoinColumn['referencedColumnName']); @@ -158,7 +162,8 @@ class ClassExporter } $foreignKeyConstraints[] = $constraint2; - $sql = array_merge($sql, $this->_platform->getCreateTableSql($joinTable['name'], $joinTableColumns, array())); + $sql = array_merge($sql, $this->_platform->getCreateTableSql( + $joinTable['name'], $joinTableColumns, $joinTableOptions)); } } @@ -168,7 +173,7 @@ class ClassExporter // Now create the foreign key constraints if ($this->_platform->supportsForeignKeyConstraints()) { foreach ($foreignKeyConstraints as $fkConstraint) { - $sql = array_merge($sql, $this->_platform->getCreateForeignKeySql($fkConstraint['tableName'], $fkConstraint)); + $sql = array_merge($sql, (array)$this->_platform->getCreateForeignKeySql($fkConstraint['tableName'], $fkConstraint)); } } diff --git a/tests/Doctrine/Tests/Mocks/ConnectionMock.php b/tests/Doctrine/Tests/Mocks/ConnectionMock.php index ee60aa2cf..67c5f4b26 100644 --- a/tests/Doctrine/Tests/Mocks/ConnectionMock.php +++ b/tests/Doctrine/Tests/Mocks/ConnectionMock.php @@ -8,7 +8,8 @@ class ConnectionMock extends \Doctrine\DBAL\Connection private $_lastInsertId = 0; private $_inserts = array(); - public function __construct() { + public function __construct(array $params, $driver, $config = null, $eventManager = null) { + parent::__construct($params, $driver, $config, $eventManager); $this->_platformMock = new DatabasePlatformMock(); $this->_platform = $this->_platformMock; } diff --git a/tests/Doctrine/Tests/Mocks/DriverMock.php b/tests/Doctrine/Tests/Mocks/DriverMock.php index 8fc4eeda0..77df38fc7 100644 --- a/tests/Doctrine/Tests/Mocks/DriverMock.php +++ b/tests/Doctrine/Tests/Mocks/DriverMock.php @@ -3,11 +3,10 @@ namespace Doctrine\Tests\Mocks; -// THIS FILE DOES NOT EXIST YET!!!! -//require_once 'lib/mocks/Doctrine_SchemaManagerMock.php'; - class DriverMock implements \Doctrine\DBAL\Driver { + private $_platformMock; + public function connect(array $params, $username = null, $password = null, array $driverOptions = array()) { return new DriverConnectionMock(); @@ -23,15 +22,31 @@ class DriverMock implements \Doctrine\DBAL\Driver { return ""; } - + + /** + * @override + */ public function getDatabasePlatform() { - return new DatabasePlatformMock(); + if ( ! $this->_platformMock) { + $this->_platformMock = new DatabasePlatformMock; + } + return $this->_platformMock; } - + + /** + * @override + */ public function getSchemaManager(\Doctrine\DBAL\Connection $conn) { return new SchemaManagerMock($conn); } + + /* MOCK API */ + + public function setDatabasePlatform(\Doctrine\DBAL\Platforms\AbstractPlatform $platform) + { + $this->_platformMock = $platform; + } } diff --git a/tests/Doctrine/Tests/Mocks/SchemaManagerMock.php b/tests/Doctrine/Tests/Mocks/SchemaManagerMock.php new file mode 100644 index 000000000..de88224f3 --- /dev/null +++ b/tests/Doctrine/Tests/Mocks/SchemaManagerMock.php @@ -0,0 +1,20 @@ +_connMock = new ConnectionMock(array()); + $this->_connMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock()); $this->_emMock = EntityManagerMock::create($this->_connMock); $this->_uowMock = new UnitOfWorkMock($this->_emMock); $this->_emMock->setUnitOfWork($this->_uowMock); diff --git a/tests/Doctrine/Tests/ORM/Export/ClassExporterTest.php b/tests/Doctrine/Tests/ORM/Export/ClassExporterTest.php index 5186275e6..8a9d7a48f 100644 --- a/tests/Doctrine/Tests/ORM/Export/ClassExporterTest.php +++ b/tests/Doctrine/Tests/ORM/Export/ClassExporterTest.php @@ -16,7 +16,7 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ namespace Doctrine\Tests\ORM\Export; @@ -40,15 +40,22 @@ class ClassExporterTest extends \Doctrine\Tests\OrmTestCase { public function testTest() { - /* - $em = $this->_getTestEntityManager(); + // DDL is platform dependant. We can inject the platform to test into the driver mock. + $driver = new \Doctrine\Tests\Mocks\DriverMock; + $conn = new \Doctrine\Tests\Mocks\ConnectionMock(array(), $driver); + //$conn->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SqlitePlatform()); + $conn->setDatabasePlatform(new \Doctrine\DBAL\Platforms\MySqlPlatform()); - $classes = array($em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber')); + $em = $this->_getTestEntityManager($conn); + + $classes = array( + $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'), + $em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber') + ); $exporter = new ClassExporter($em); $sql = $exporter->getExportClassesSql($classes); print_r($sql); - exit('test'); - */ + } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php index 6ef8357d2..95f5de772 100644 --- a/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php +++ b/tests/Doctrine/Tests/ORM/UnitOfWorkTest.php @@ -27,7 +27,7 @@ class UnitOfWorkTest extends \Doctrine\Tests\OrmTestCase protected function setUp() { parent::setUp(); - $this->_connectionMock = new ConnectionMock(array()); + $this->_connectionMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock()); $this->_emMock = EntityManagerMock::create($this->_connectionMock); // SUT $this->_unitOfWork = new UnitOfWorkMock($this->_emMock); diff --git a/tests/Doctrine/Tests/OrmTestCase.php b/tests/Doctrine/Tests/OrmTestCase.php index 3ccc97312..c1db50657 100644 --- a/tests/Doctrine/Tests/OrmTestCase.php +++ b/tests/Doctrine/Tests/OrmTestCase.php @@ -15,18 +15,20 @@ class OrmTestCase extends DoctrineTestCase * * @return Doctrine\ORM\EntityManager */ - protected function _getTestEntityManager($conf = null, $eventManager = null) + protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null) { $config = new \Doctrine\ORM\Configuration(); $config->setMetadataCacheImpl(self::getSharedMetadataCacheImpl()); $eventManager = new \Doctrine\Common\EventManager(); - $connectionOptions = array( + if (is_null($conn)) { + $conn = array( 'driverClass' => 'Doctrine\Tests\Mocks\DriverMock', 'wrapperClass' => 'Doctrine\Tests\Mocks\ConnectionMock', 'user' => 'john', 'password' => 'wayne' - ); - return \Doctrine\ORM\EntityManager::create($connectionOptions, $config, $eventManager); + ); + } + return \Doctrine\ORM\EntityManager::create($conn, $config, $eventManager); } private static function getSharedMetadataCacheImpl()