1
0
mirror of synced 2025-01-18 22:41:43 +03:00

[2.0] DDC-169 - Further refactorings, schema max identifier length is now used for asset generation. Added platform-wide test for unique index generation with create table.

This commit is contained in:
beberlei 2009-12-10 23:55:47 +00:00
parent 1546663743
commit fade63a29c
15 changed files with 226 additions and 46 deletions

View File

@ -936,6 +936,20 @@ abstract class AbstractSchemaManager
}
$tables = $this->listTables();
return new Schema($tables, $sequences, $this->_platform->createsExplicitIndexForForeignKeys());
return new Schema($tables, $sequences, $this->createSchemaConfig());
}
/**
* Create the configuration for this schema.
*
* @return SchemaConfig
*/
public function createSchemaConfig()
{
$schemaConfig = new SchemaConfig();
$schemaConfig->setExplicitForeignKeyIndexes($this->_platform->createsExplicitIndexForForeignKeys());
$schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
return $schemaConfig;
}
}

View File

@ -47,23 +47,28 @@ class Schema extends AbstractAsset
protected $_sequences = array();
/**
* @var bool
* @var SchemaConfig
*/
protected $_hasExplicitForeignKeyIndexes = false;
protected $_schemaConfig = false;
/**
* @param array $tables
* @param array $sequences
* @param SchemaConfig $schemaConfig
*/
public function __construct(array $tables=array(), array $sequences=array(), $hasExplicitForeignKeyIndexes=false)
public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
{
if ($schemaConfig == null) {
$schemaConfig = new SchemaConfig();
}
$this->_schemaConfig = $schemaConfig;
foreach ($tables AS $table) {
$this->_addTable($table);
}
foreach ($sequences AS $sequence) {
$this->_addSequence($sequence);
}
$this->_hasExplicitForeignKeyIndexes = $hasExplicitForeignKeyIndexes;
}
/**
@ -71,7 +76,7 @@ class Schema extends AbstractAsset
*/
public function hasExplicitForeignKeyIndexes()
{
return $this->_hasExplicitForeignKeyIndexes;
return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
}
/**
@ -85,6 +90,7 @@ class Schema extends AbstractAsset
}
$this->_tables[$tableName] = $table;
$table->setSchemaConfig($this->_schemaConfig);
}
/**

View File

@ -0,0 +1,76 @@
<?php
/*
* $Id: Schema.php 6876 2009-12-06 23:11:35Z beberlei $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Schema;
/**
* Configuration for a Schema
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class SchemaConfig
{
/**
* @var bool
*/
protected $_hasExplicitForeignKeyIndexes = false;
/**
* @var int
*/
protected $_maxIdentifierLength = 63;
/**
* @return bool
*/
public function hasExplicitForeignKeyIndexes()
{
return $this->_hasExplicitForeignKeyIndexes;
}
/**
* @param bool $flag
*/
public function setExplicitForeignKeyIndexes($flag)
{
$this->_hasExplicitForeignKeyIndexes = (bool)$flag;
}
/**
* @param int $length
*/
public function setMaxIdentifierLength($length)
{
$this->_maxIdentifierLength = (int)$length;
}
/**
* @return int
*/
public function getMaxIdentifierLength()
{
return $this->_maxIdentifierLength;
}
}

View File

@ -85,6 +85,11 @@ class Table extends AbstractAsset
*/
protected $_idGeneratorType = self::ID_NONE;
/**
* @var SchemaConfig
*/
protected $_schemaConfig = null;
/**
*
* @param string $tableName
@ -114,6 +119,26 @@ class Table extends AbstractAsset
$this->_options = $options;
}
/**
* @param SchemaConfig $schemaConfig
*/
public function setSchemaConfig(SchemaConfig $schemaConfig)
{
$this->_schemaConfig = $schemaConfig;
}
/**
* @return int
*/
protected function _getMaxIdentifierLength()
{
if ($this->_schemaConfig instanceof SchemaConfig) {
return $this->_schemaConfig->getMaxIdentifierLength();
} else {
return 63;
}
}
/**
* Set Primary Key
*
@ -145,7 +170,7 @@ class Table extends AbstractAsset
{
if($indexName == null) {
$indexName = $this->_generateIdentifierName(
array_merge(array($this->getName()), $columnNames), "idx"
array_merge(array($this->getName()), $columnNames), "idx", $this->_getMaxIdentifierLength()
);
}
@ -162,7 +187,7 @@ class Table extends AbstractAsset
{
if ($indexName == null) {
$indexName = $this->_generateIdentifierName(
array_merge(array($this->getName()), $columnNames), "uniq"
array_merge(array($this->getName()), $columnNames), "uniq", $this->_getMaxIdentifierLength()
);
}
@ -268,7 +293,7 @@ class Table extends AbstractAsset
*/
public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
{
$name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk");
$name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk", $this->_getMaxIdentifierLength());
return $this->addNamedForeignKeyConstraint($name, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
}
@ -394,7 +419,7 @@ class Table extends AbstractAsset
$name = $constraint->getName();
} else {
$name = $this->_generateIdentifierName(
array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk"
array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk", $this->_getMaxIdentifierLength()
);
}
$name = strtolower($name);

View File

@ -88,7 +88,7 @@ class DatabaseDriver implements Driver
}
$fieldMapping = array();
if (in_array($column->getName(), $indexes['primary']->getColumns())) {
if (isset($indexes['primary']) && in_array($column->getName(), $indexes['primary']->getColumns())) {
$fieldMapping['id'] = true;
}

View File

@ -109,7 +109,8 @@ class SchemaTool
{
$processedClasses = array(); // Reminder for processed classes, used for hierarchies
$schema = new \Doctrine\DBAL\Schema\Schema();
$sm = $this->_em->getConnection()->getSchemaManager();
$schema = new \Doctrine\DBAL\Schema\Schema(array(), array(), $sm->createSchemaConfig());
foreach ($classes as $class) {
if (isset($processedClasses[$class->name]) || $class->isMappedSuperclass) {

View File

@ -390,6 +390,7 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
protected function getTestTable($name, $options=array())
{
$table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), \Doctrine\DBAL\Schema\Table::ID_NONE, $options);
$table->setSchemaConfig($this->_sm->createSchemaConfig());
$table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);
$table->createColumn('id', 'integer', array('notnull' => true));
$table->setPrimaryKey(array('id'));

View File

@ -27,6 +27,19 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
abstract public function getGenerateTableSql();
public function testGenerateTableWithMultiColumnUniqueIndex()
{
$table = new \Doctrine\DBAL\Schema\Table('test');
$table->createColumn('foo', 'string', array('notnull' => false, 'length' => 255));
$table->createColumn('bar', 'string', array('notnull' => false, 'length' => 255));
$table->addUniqueIndex(array("foo", "bar"));
$sql = $this->_platform->getCreateTableSql($table);
$this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
}
abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();
public function testGeneratesIndexCreationSql()
{
$indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));

View File

@ -19,6 +19,13 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
}
public function getGenerateTableWithMultiColumnUniqueIndexSql()
{
return array(
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX test_foo_bar_uniq (foo, bar))'
);
}
public function getGenerateAlterTableSql()
{
return array(

View File

@ -28,6 +28,13 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB';
}
public function getGenerateTableWithMultiColumnUniqueIndexSql()
{
return array(
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX test_foo_bar_uniq (foo, bar)) ENGINE = InnoDB'
);
}
public function getGenerateAlterTableSql()
{
return array(

View File

@ -19,6 +19,14 @@ class OraclePlatformTest extends AbstractPlatformTestCase
return 'CREATE TABLE test (id NUMBER(10) NOT NULL, test VARCHAR2(255) DEFAULT NULL, PRIMARY KEY(id))';
}
public function getGenerateTableWithMultiColumnUniqueIndexSql()
{
return array(
'CREATE TABLE test (foo VARCHAR2(255) DEFAULT NULL, bar VARCHAR2(255) DEFAULT NULL)',
'CREATE UNIQUE INDEX test_foo_bar_uniq ON test (foo, bar)',
);
}
public function getGenerateAlterTableSql()
{
return array(

View File

@ -20,6 +20,14 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
}
public function getGenerateTableWithMultiColumnUniqueIndexSql()
{
return array(
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)',
'CREATE UNIQUE INDEX test_foo_bar_uniq ON test (foo, bar)'
);
}
public function getGenerateAlterTableSql()
{
return array(

View File

@ -19,6 +19,14 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
return 'CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL)';
}
public function getGenerateTableWithMultiColumnUniqueIndexSql()
{
return array(
'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL)',
'CREATE UNIQUE INDEX test_foo_bar_uniq ON test (foo, bar)',
);
}
public function testGeneratesSqlSnippets()
{
$this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');

View File

@ -87,47 +87,28 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
public function testCompareMissingTable()
{
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
)
),
) );
$schema2 = new Schema( array(
) );
$schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
$table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
$table->setSchemaConfig($schemaConfig);
$schema1 = new Schema( array($table), array(), $schemaConfig );
$schema2 = new Schema( array(), array(), $schemaConfig );
$expected = new SchemaDiff( array(), array(),
array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
)
),
)
);
$expected = new SchemaDiff( array(), array(), array('bugdb' => $table) );
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareNewTable()
{
$schema1 = new Schema( array(
) );
$schema2 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
)
),
) );
$schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
$table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
$table->setSchemaConfig($schemaConfig);
$expected = new SchemaDiff( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
)
),
) );
$schema1 = new Schema( array(), array(), $schemaConfig );
$schema2 = new Schema( array($table), array(), $schemaConfig );
$expected = new SchemaDiff( array('bugdb' => $table), array(), array() );
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}

View File

@ -187,4 +187,29 @@ class SchemaTest extends \PHPUnit_Framework_TestCase
$index = current($indexes);
$this->assertTrue($index->hasColumnAtPosition('foo_id', 0));
}
public function testConfigHasExplicitForeignKeyIndex()
{
$schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
$schemaConfig->setExplicitForeignKeyIndexes(false);
$schema = new Schema(array(), array(), $schemaConfig);
$this->assertFalse($schema->hasExplicitForeignKeyIndexes());
$schemaConfig->setExplicitForeignKeyIndexes(true);
$this->assertTrue($schema->hasExplicitForeignKeyIndexes());
}
public function testConfigMaxIdentifierLength()
{
$schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
$schemaConfig->setMaxIdentifierLength(10);
$schema = new Schema(array(), array(), $schemaConfig);
$table = $schema->createTable("smalltable");
$table->createColumn('long_id', 'integer');
$table->addIndex(array('long_id'));
$this->assertTrue($table->hasIndex('le_id_idx'));
}
}