1
0
mirror of synced 2024-12-13 06:46:03 +03:00

[2.0] DDC-169 - Changed AbstractPlatform and AbstractSchemaManager and children to accept Table, Index and ForeignKey instances for the respective create*() methods.

This commit is contained in:
beberlei 2009-12-02 18:52:21 +00:00
parent 4d5a6ac7bc
commit 8562c80890
25 changed files with 461 additions and 711 deletions

View File

@ -23,7 +23,10 @@ namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException,
Doctrine\DBAL\Connection,
Doctrine\DBAL\Types;
Doctrine\DBAL\Types,
Doctrine\DBAL\Schema\Table,
Doctrine\DBAL\Schema\Index,
Doctrine\DBAL\Schema\ForeignKeyConstraint;
/**
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central
@ -456,9 +459,22 @@ abstract class AbstractPlatform
return 'DROP TABLE ' . $table;
}
public function getDropIndexSql($table, $name)
/**
* Drop index from a table
*
* @param Index|string $name
* @param string|Table $table
* @return string
*/
public function getDropIndexSql($index, $table=null)
{
return 'DROP INDEX ' . $name;
if($index instanceof \Doctrine\DBAL\Schema\Index) {
$index = $index->getName();
} else if(!is_string($index)) {
throw new \InvalidArgumentException('AbstractPlatform::getDropIndexSql() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
}
return 'DROP INDEX ' . $index;
}
public function getDropConstraintSql($table, $name, $primary = false)
@ -480,7 +496,62 @@ abstract class AbstractPlatform
* @param array $options The table constraints.
* @return array The sequence of SQL statements.
*/
public function getCreateTableSql($table, array $columns, array $options = array())
public function getCreateTableSql(Table $table)
{
$tableName = $table->getName();
$options = $table->getOptions();
$options['uniqueConstraints'] = array();
$options['indexes'] = array();
$options['primary'] = array();
foreach($table->getIndexes() AS $index) {
/* @var $index Index */
if($index->isPrimary()) {
$options['primary'] = $index->getColumns();
} else {
$options['indexes'][$index->getName()] = $index;
}
}
$columns = array();
foreach($table->getColumns() AS $column) {
/* @var \Doctrine\DBAL\Schema\Column $column */
$columnData = array();
$columnData['name'] = $column->getName();
$columnData['type'] = $column->getType();
$columnData['length'] = $column->getLength();
$columnData['notnull'] = $column->getNotNull();
$columnData['unique'] = ($column->hasPlatformOption("unique"))?$column->getPlatformOption('unique'):false;
$columnData['version'] = ($column->hasPlatformOption("version"))?$column->getPlatformOption('version'):false;
if(strtolower($columnData['type']) == "string" && $columnData['length'] === null) {
$columnData['length'] = 255;
}
$columnData['precision'] = $column->getPrecision();
$columnData['scale'] = $column->getScale();
$columnData['default'] = $column->getDefault();
// TODO: Fixed? Unsigned?
if(in_array($column->getName(), $options['primary'])) {
$columnData['primary'] = true;
if($table->isIdGeneratorIdentity()) {
$columnData['autoincrement'] = true;
}
}
$columns[$columnData['name']] = $columnData;
}
return $this->_getCreateTableSql($tableName, $columns, $options);
}
/**
* @param string $table
* @param array $columns
* @param array $options
* @return array
*/
protected function _getCreateTableSql($table, array $columns, array $options = array())
{
$columnListSql = $this->getColumnDeclarationListSql($columns);
@ -511,10 +582,8 @@ abstract class AbstractPlatform
$sql[] = $query;
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $k => $definition) {
if (is_array($definition)) {
$sql[] = $this->getCreateForeignKeySql($name, $definition);
}
foreach ((array) $options['foreignKeys'] AS $definition) {
$sql[] = $this->getCreateForeignKeySql($definition, $name);
}
}
@ -583,31 +652,30 @@ abstract class AbstractPlatform
/**
* Gets the SQL to create an index on a table on this platform.
*
* @param string $table name of the table on which the index is to be created
* @param string $name name of the index to be created
* @param array $definition associative array that defines properties of the index to be created.
* @param Index $index
* @param string|Table $table name of the table on which the index is to be created
* @return string
*/
public function getCreateIndexSql($table, $name, array $definition)
public function getCreateIndexSql(Index $index, $table)
{
if ( ! isset($definition['columns'])) {
throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
if ($table instanceof Table) {
$table = $table->getName();
}
$name = $index->getName();
$columns = $index->getColumns();
if (count($columns) == 0) {
throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
$type = '';
if (isset($definition['type'])) {
switch (strtolower($definition['type'])) {
case 'unique':
$type = strtoupper($definition['type']) . ' ';
break;
default:
throw \InvalidArgumentException('Unknown type: ' . $definition['type']);
}
if ($index->isUnique()) {
$type = 'UNIQUE ';
}
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['columns']) . ')';
$query .= ' (' . $this->getIndexFieldDeclarationListSql($columns) . ')';
return $query;
}
@ -631,15 +699,19 @@ abstract class AbstractPlatform
}
/**
* createForeignKeySql
* Create a new foreign key
*
* @param string $table name of the table on which the foreign key is to be created
* @param array $definition associative array that defines properties of the foreign key to be created.
* @param ForeignKeyConstraint $foreignKey ForeignKey instance
* @param string|Table $table name of the table on which the foreign key is to be created
* @return string
*/
public function getCreateForeignKeySql($table, array $definition)
public function getCreateForeignKeySql(ForeignKeyConstraint $foreignKey, $table)
{
$query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSql($definition);
if ($table instanceof \Doctrine\DBAL\Schema\Table) {
$table = $table->getName();
}
$query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSql($foreignKey);
return $query;
}
@ -859,28 +931,24 @@ abstract class AbstractPlatform
* declaration to be used in statements like CREATE TABLE.
*
* @param string $name name of the index
* @param array $definition index definition
* @param Index $index index definition
* @return string DBMS specific SQL code portion needed to set an index
*/
public function getIndexDeclarationSql($name, array $definition)
public function getIndexDeclarationSql($name, Index $index)
{
$type = '';
if (isset($definition['type'])) {
if (strtolower($definition['type']) == 'unique') {
$type = strtoupper($definition['type']) . ' ';
} else {
throw \InvalidArgumentException('Invalid type: ' . $definition['type']);
}
if($index->isUnique()) {
$type = "UNIQUE";
}
if ( ! isset($definition['columns']) || ! is_array($definition['columns'])) {
if (count($index->getColumns()) == 0) {
throw \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
$query = $type . 'INDEX ' . $name;
$query = $type . ' INDEX ' . $name;
$query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['columns']) . ')';
$query .= ' (' . $this->getIndexFieldDeclarationListSql($index->getColumns()) . ')';
return $query;
}
@ -976,10 +1044,10 @@ abstract class AbstractPlatform
* @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
* of a field declaration.
*/
public function getForeignKeyDeclarationSql(array $definition)
public function getForeignKeyDeclarationSql(ForeignKeyConstraint $foreignKey)
{
$sql = $this->getForeignKeyBaseDeclarationSql($definition);
$sql .= $this->getAdvancedForeignKeyOptionsSql($definition);
$sql = $this->getForeignKeyBaseDeclarationSql($foreignKey);
$sql .= $this->getAdvancedForeignKeyOptionsSql($foreignKey);
return $sql;
}
@ -988,17 +1056,17 @@ abstract class AbstractPlatform
* Return the FOREIGN KEY query section dealing with non-standard options
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
*
* @param array $definition foreign key definition
* @param ForeignKeyConstraint $foreignKey foreign key definition
* @return string
*/
public function getAdvancedForeignKeyOptionsSql(array $definition)
public function getAdvancedForeignKeyOptionsSql(ForeignKeyConstraint $foreignKey)
{
$query = '';
if ( ! empty($definition['onUpdate'])) {
$query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSql($definition['onUpdate']);
if ($this->supportsForeignKeyOnUpdate() && $foreignKey->hasOption('onUpdate')) {
$query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSql($foreignKey->getOption('onUpdate'));
}
if ( ! empty($definition['onDelete'])) {
$query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSql($definition['onDelete']);
if ($foreignKey->hasOption('onDelete')) {
$query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSql($foreignKey->getOption('onDelete'));
}
return $query;
}
@ -1031,38 +1099,31 @@ abstract class AbstractPlatform
* Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
* of a field declaration to be used in statements like CREATE TABLE.
*
* @param array $definition
* @param ForeignKeyConstraint $foreignKey
* @return string
*/
public function getForeignKeyBaseDeclarationSql(array $definition)
public function getForeignKeyBaseDeclarationSql(ForeignKeyConstraint $foreignKey)
{
$sql = '';
if (isset($definition['name'])) {
$sql .= ' CONSTRAINT ' . $definition['name'] . ' ';
if (strlen($foreignKey->getName())) {
$sql .= 'CONSTRAINT ' . $foreignKey->getName() . ' ';
}
$sql .= 'FOREIGN KEY (';
if ( ! isset($definition['local'])) {
if (count($foreignKey->getLocalColumns()) == 0) {
throw new \InvalidArgumentException("Incomplete definition. 'local' required.");
}
if ( ! isset($definition['foreign'])) {
if (count($foreignKey->getForeignColumns()) == 0) {
throw new \InvalidArgumentException("Incomplete definition. 'foreign' required.");
}
if ( ! isset($definition['foreignTable'])) {
if (strlen($foreignKey->getForeignTableName()) == 0) {
throw new \InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
}
if ( ! is_array($definition['local'])) {
$definition['local'] = array($definition['local']);
}
if ( ! is_array($definition['foreign'])) {
$definition['foreign'] = array($definition['foreign']);
}
$sql .= implode(', ', $definition['local'])
$sql .= implode(', ', $foreignKey->getLocalColumns())
. ') REFERENCES '
. $definition['foreignTable'] . '('
. implode(', ', $definition['foreign']) . ')';
. $foreignKey->getForeignTableName() . '('
. implode(', ', $foreignKey->getForeignColumns()) . ')';
return $sql;
}
@ -1450,7 +1511,7 @@ abstract class AbstractPlatform
}
/**
* Whether the platform supports foreign key constraints.
* Does the platform supports foreign key constraints?
*
* @return boolean
*/
@ -1459,6 +1520,16 @@ abstract class AbstractPlatform
return true;
}
/**
* Does this platform supports onUpdate in foreign key constraints?
*
* @return bool
*/
public function supportsForeignKeyOnUpdate()
{
return ($this->supportsForeignKeyConstraints() && true);
}
/**
* Whether the platform supports database schemas.
*

View File

@ -402,8 +402,8 @@ class MySqlPlatform extends AbstractPlatform
/**
* create a new table
*
* @param string $name Name of the database that should be created
* @param array $fields Associative array that contains the definition of each field of the new table
* @param string $tableName Name of the database that should be created
* @param array $columns Associative array that contains the definition of each field of the new table
* The indexes of the array entries are the names of the fields of the table an
* the array entry values are associative arrays like those that are meant to be
* passed with the field definitions to get[Type]Declaration() functions.
@ -434,15 +434,15 @@ class MySqlPlatform extends AbstractPlatform
* @return void
* @override
*/
public function getCreateTableSql($name, array $fields, array $options = array())
protected function _getCreateTableSql($tableName, array $columns, array $options = array())
{
if ( ! $name) {
if ( ! $tableName) {
throw DoctrineException::missingTableName();
}
if (empty($fields)) {
throw DoctrineException::missingFieldsArrayForTable($name);
if (empty($columns)) {
throw DoctrineException::missingFieldsArrayForTable($tableName);
}
$queryFields = $this->getColumnDeclarationListSql($fields);
$queryFields = $this->getColumnDeclarationListSql($columns);
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $uniqueConstraint) {
@ -467,7 +467,7 @@ class MySqlPlatform extends AbstractPlatform
if (!empty($options['temporary'])) {
$query .= 'TEMPORARY ';
}
$query.= 'TABLE ' . $name . ' (' . $queryFields . ')';
$query.= 'TABLE ' . $tableName . ' (' . $queryFields . ')';
$optionStrings = array();
@ -495,10 +495,8 @@ class MySqlPlatform extends AbstractPlatform
$sql[] = $query;
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $k => $definition) {
if (is_array($definition)) {
$sql[] = $this->getCreateForeignKeySql($name, $definition);
}
foreach ((array) $options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySql($definition, $tableName);
}
}
@ -736,41 +734,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.
*
* @param string $charset name of the index
* @param array $definition index definition
* @return string DBMS specific SQL code portion needed to set an index
* @override
*/
public function getIndexDeclarationSql($name, array $definition)
{
$type = '';
if (isset($definition['type'])) {
switch (strtolower($definition['type'])) {
case 'fulltext':
case 'unique':
$type = strtoupper($definition['type']) . ' ';
break;
default:
throw DoctrineException::invalidIndexType($definition['type']);
}
}
if ( ! isset($definition['columns'])) {
throw DoctrineException::indexFieldsArrayRequired();
}
$definition['columns'] = (array) $definition['columns'];
$query = $type . 'INDEX ' . $name;
$query .= ' (' . $this->getIndexFieldDeclarationListSql($definition['columns']) . ')';
return $query;
}
/**
* Obtain DBMS specific SQL code portion needed to set an index
* declaration to be used in statements like CREATE TABLE.
@ -813,35 +776,42 @@ class MySqlPlatform extends AbstractPlatform
* Return the FOREIGN KEY query section dealing with non-standard options
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
*
* @param array $definition
* @param ForeignKeyConstraint $foreignKey
* @return string
* @override
*/
public function getAdvancedForeignKeyOptionsSql(array $definition)
public function getAdvancedForeignKeyOptionsSql(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
{
$query = '';
if ( ! empty($definition['match'])) {
$query .= ' MATCH ' . $definition['match'];
}
if ( ! empty($definition['onUpdate'])) {
$query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSql($definition['onUpdate']);
}
if ( ! empty($definition['onDelete'])) {
$query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSql($definition['onDelete']);
if ($foreignKey->hasOption('match')) {
$query .= ' MATCH ' . $foreignKey->getOption('match');
}
$query .= parent::getAdvancedForeignKeyOptionsSql($foreignKey);
return $query;
}
/**
* Gets the SQL to drop an index of a table.
*
* @param string $table name of table that should be used in method
* @param string $name name of the index to be dropped
* @param Index $index name of the index to be dropped
* @param string|Table $table name of table that should be used in method
* @override
*/
public function getDropIndexSql($table, $name)
public function getDropIndexSql($index, $table=null)
{
return 'DROP INDEX ' . $name . ' ON ' . $table;
if($index instanceof \Doctrine\DBAL\Schema\Index) {
$index = $index->getName();
} else if(!is_string($index)) {
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSql() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
}
if($table instanceof \Doctrine\DBAL\Schema\Table) {
$table = $table->getName();
} else if(!is_string($table)) {
throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSql() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
}
return 'DROP INDEX ' . $index . ' ON ' . $table;
}
/**

View File

@ -267,11 +267,18 @@ class OraclePlatform extends AbstractPlatform
"WHERE SEQUENCE_OWNER = '".strtoupper($database)."'";
}
public function getCreateTableSql($table, array $columns, array $options = array())
/**
*
* @param string $table
* @param array $columns
* @param array $options
* @return array
*/
protected function _getCreateTableSql($table, array $columns, array $options = array())
{
$indexes = isset($options['indexes']) ? $options['indexes'] : array();
$options['indexes'] = array();
$sql = parent::getCreateTableSql($table, $columns, $options);
$sql = parent::_getCreateTableSql($table, $columns, $options);
foreach ($columns as $name => $column) {
if (isset($column['sequence'])) {
@ -285,8 +292,8 @@ class OraclePlatform extends AbstractPlatform
}
if (isset($indexes) && ! empty($indexes)) {
foreach ($indexes as $indexName => $definition) {
$sql[] = $this->getCreateIndexSql($table, $indexName, $definition);
foreach ($indexes as $indexName => $index) {
$sql[] = $this->getCreateIndexSql($index, $table);
}
}
@ -610,4 +617,9 @@ END;';
{
return true;
}
public function supportsForeignKeyOnUpdate()
{
return false;
}
}

View File

@ -453,28 +453,23 @@ class PostgreSqlPlatform extends AbstractPlatform
* Return the FOREIGN KEY query section dealing with non-standard options
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
*
* @param array $definition foreign key definition
* @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey foreign key definition
* @return string
* @override
*/
public function getAdvancedForeignKeyOptionsSql(array $definition)
public function getAdvancedForeignKeyOptionsSql(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
{
$query = '';
if (isset($definition['match'])) {
$query .= ' MATCH ' . $definition['match'];
if ($foreignKey->hasOption('match')) {
$query .= ' MATCH ' . $foreignKey->getOption('match');
}
if (isset($definition['onUpdate'])) {
$query .= ' ON UPDATE ' . $definition['onUpdate'];
}
if (isset($definition['onDelete'])) {
$query .= ' ON DELETE ' . $definition['onDelete'];
}
if (isset($definition['deferrable'])) {
$query .= parent::getAdvancedForeignKeyOptionsSql($foreignKey);
if ($foreignKey->hasOption('deferrable') && $foreignKey->getOption('deferrable') !== false) {
$query .= ' DEFERRABLE';
} else {
$query .= ' NOT DEFERRABLE';
}
if (isset($definition['feferred'])) {
if ($foreignKey->hasOption('feferred') && $foreignKey->getOption('feferred') !== false) {
$query .= ' INITIALLY DEFERRED';
} else {
$query .= ' INITIALLY IMMEDIATE';
@ -589,33 +584,33 @@ class PostgreSqlPlatform extends AbstractPlatform
/**
* Gets the SQL used to create a table.
*
* @param unknown_type $name
* @param array $fields
* @param unknown_type $tableName
* @param array $columns
* @param array $options
* @return unknown
*/
public function getCreateTableSql($name, array $fields, array $options = array())
protected function _getCreateTableSql($tableName, array $columns, array $options = array())
{
$queryFields = $this->getColumnDeclarationListSql($fields);
$queryFields = $this->getColumnDeclarationListSql($columns);
if (isset($options['primary']) && ! empty($options['primary'])) {
$keyColumns = array_unique(array_values($options['primary']));
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}
$query = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
$query = 'CREATE TABLE ' . $tableName . ' (' . $queryFields . ')';
$sql[] = $query;
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach($options['indexes'] as $index => $definition) {
$sql[] = $this->getCreateIndexSql($name, $index, $definition);
foreach ($options['indexes'] AS $index) {
$sql[] = $this->getCreateIndexSql($index, $tableName);
}
}
if (isset($options['foreignKeys'])) {
foreach ((array) $options['foreignKeys'] as $k => $definition) {
$sql[] = $this->getCreateForeignKeySql($name, $definition);
foreach ((array) $options['foreignKeys'] as $definition) {
$sql[] = $this->getCreateForeignKeySql($definition, $tableName);
}
}

View File

@ -313,19 +313,19 @@ class SqlitePlatform extends AbstractPlatform
* @return void
* @override
*/
public function getCreateTableSql($name, array $fields, array $options = array())
protected function _getCreateTableSql($name, array $columns, array $options = array())
{
if ( ! $name) {
throw DoctrineException::invalidTableName($name);
}
if (empty($fields)) {
if (empty($columns)) {
throw DoctrineException::noFieldsSpecifiedForTable($name);
}
$queryFields = $this->getColumnDeclarationListSql($fields);
$queryFields = $this->getColumnDeclarationListSql($columns);
$autoinc = false;
foreach($fields as $field) {
foreach($columns as $field) {
if (isset($field['autoincrement']) && $field['autoincrement']) {
$autoinc = true;
break;
@ -338,23 +338,16 @@ class SqlitePlatform extends AbstractPlatform
$queryFields.= ', PRIMARY KEY('.implode(', ', $keyColumns).')';
}
$sql = 'CREATE TABLE ' . $name . ' (' . $queryFields;
/*if ($check = $this->getCheckDeclarationSql($fields)) {
$sql .= ', ' . $check;
}
if (isset($options['checks']) && $check = $this->getCheckDeclarationSql($options['checks'])) {
$sql .= ', ' . $check;
}*/
$sql .= ')';
$query[] = $sql;
$query[] = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
if (isset($options['indexes']) && ! empty($options['indexes'])) {
foreach ($options['indexes'] as $index => $definition) {
$query[] = $this->getCreateIndexSql($name, $index, $definition);
foreach ($options['indexes'] as $index => $indexDef) {
$query[] = $this->getCreateIndexSql($indexDef, $name);
}
}
if (isset($options['unique']) && ! empty($options['unique'])) {
foreach ($options['unique'] as $index => $indexDef) {
$query[] = $this->getCreateIndexSql($indexDef, $name);
}
}
return $query;

View File

@ -77,9 +77,9 @@ abstract class AbstractAsset
$columnCount = count($columnNames);
$postfixLen = strlen($postfix);
$parts = array_map(function($columnName) use($columnCount, $postfixLen, $maxSize) {
return substr($columnName, 0, floor(($maxSize-$postfixLen)/$columnCount - 1));
return substr($columnName, -floor(($maxSize-$postfixLen)/$columnCount - 1));
}, $columnNames);
$parts[] = $postfix;
return implode("_", $parts);
return trim(implode("_", $parts), '_');
}
}

View File

@ -336,12 +336,16 @@ abstract class AbstractSchemaManager
/**
* Drop the index from the given table
*
* @param string $table The name of the table
* @param string $name The name of the index
* @param Index|string $index The name of the index
* @param string|Table $table The name of the table
*/
public function dropIndex($table, $name)
public function dropIndex($index, $table)
{
$this->_execSql($this->_platform->getDropIndexSql($table, $name));
if($index instanceof Index) {
$index = $index->getName();
}
$this->_execSql($this->_platform->getDropIndexSql($index, $table));
}
/**
@ -404,25 +408,11 @@ abstract class AbstractSchemaManager
/**
* Create a new table.
*
* @param string $name Name of the database that should be created
* @param array $columns Associative array that contains the definition of each field of the new table
* @param array $options An associative array of table options.
* @param Table $table
*/
public function createTable($name, array $columns, array $options = array())
public function createTable(Table $table)
{
// Build array of the primary keys if any of the individual field definitions
// specify primary => true
$count = 0;
foreach ($columns as $columnName => $definition) {
if (isset($definition['primary']) && $definition['primary']) {
if ($count == 0) {
$options['primary'] = array();
}
++$count;
$options['primary'][] = $columnName;
}
}
$this->_execSql($this->_platform->getCreateTableSql($name, $columns, $options));
$this->_execSql($this->_platform->getCreateTableSql($table));
}
/**
@ -466,47 +456,23 @@ abstract class AbstractSchemaManager
/**
* Create a new index on a table
*
* @param Index $index
* @param string $table name of the table on which the index is to be created
* @param string $name name of the index to be created
* @param array $definition associative array that defines properties of the index to be created.
* Currently, only one property named FIELDS is supported. This property
* is also an associative with the names of the index fields as array
* indexes. Each entry of this array is set to another type of associative
* array that specifies properties of the index that are specific to
* each field.
*
* Currently, only the sorting property is supported. It should be used
* to define the sorting direction of the index. It may be set to either
* ascending or descending.
*
* Not all DBMS support index sorting direction configuration. The DBMS
* drivers of those that do not support it ignore this property. Use the
* function supports() to determine whether the DBMS driver can manage indexes.
*
* Example
* array(
* 'columns' => array(
* 'user_name' => array(
* 'sorting' => 'ascending'
* ),
* 'last_login' => array()
* )
* )
*/
public function createIndex($table, $name, array $definition)
public function createIndex(Index $index, $table)
{
$this->_execSql($this->_platform->getCreateIndexSql($table, $name, $definition));
$this->_execSql($this->_platform->getCreateIndexSql($index, $table));
}
/**
* Create a new foreign key
*
* @param string $table name of the table on which the foreign key is to be created
* @param array $definition associative array that defines properties of the foreign key to be created.
* @param ForeignKeyConstraint $foreignKey ForeignKey instance
* @param string|Table $table name of the table on which the foreign key is to be created
*/
public function createForeignKey($table, array $definition)
public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
{
$this->_execSql($this->_platform->getCreateForeignKeySql($table, $definition));
$this->_execSql($this->_platform->getCreateForeignKeySql($foreignKey, $table));
}
/**
@ -554,37 +520,13 @@ abstract class AbstractSchemaManager
/**
* Drop and create a new index on a table
*
* @param string $table name of the table on which the index is to be created
* @param string $name name of the index to be created
* @param array $definition associative array that defines properties of the index to be created.
* Currently, only one property named FIELDS is supported. This property
* is also an associative with the names of the index fields as array
* indexes. Each entry of this array is set to another type of associative
* array that specifies properties of the index that are specific to
* each field.
*
* Currently, only the sorting property is supported. It should be used
* to define the sorting direction of the index. It may be set to either
* ascending or descending.
*
* Not all DBMS support index sorting direction configuration. The DBMS
* drivers of those that do not support it ignore this property. Use the
* function supports() to determine whether the DBMS driver can manage indexes.
*
* Example
* array(
* 'columns' => array(
* 'user_name' => array(
* 'sorting' => 'ascending'
* ),
* 'last_login' => array()
* )
* )
* @param string|Table $table name of the table on which the index is to be created
* @param Index $index
*/
public function dropAndCreateIndex($table, $name, array $definition)
public function dropAndCreateIndex(Index $index, $table)
{
$this->tryMethod('dropIndex', $table, $name);
$this->createIndex($table, $name, $definition);
$this->tryMethod('dropIndex', $index->getName(), $table);
$this->createIndex($index, $table);
}
/**
@ -616,14 +558,12 @@ abstract class AbstractSchemaManager
/**
* Drop and create a new table.
*
* @param string $name Name of the database that should be created
* @param array $columns Associative array that contains the definition of each field of the new table
* @param array $options An associative array of table options.
* @param Table $table
*/
public function dropAndCreateTable($name, array $columns, array $options = array())
public function dropAndCreateTable(Table $table)
{
$this->tryMethod('dropTable', $name);
$this->createTable($name, $columns, $options);
$this->tryMethod('dropTable', $table->getName());
$this->createTable($table);
}
/**

View File

@ -353,4 +353,17 @@ class Comparator
return false;
}
/**
* @param Schema $fromSchema
* @param Schema $toSchema
* @param AbstractSchemaManager $sm
* @return array
*/
public function toSql(Schema $fromSchema, Schema $toSchema, AbstractSchemaManager $sm)
{
$diffSchema = $this->compare($fromSchema, $toSchema);
}
}

View File

@ -64,10 +64,10 @@ class MySqlSchemaManager extends AbstractSchemaManager
} else {
$v['primary'] = false;
}
$tablesIndexes[$k] = $v;
$tableIndexes[$k] = $v;
}
return parent::_getPortableTableIndexesList($tablesIndexes, $tableName);
return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
}
protected function _getPortableTableConstraintDefinition($tableConstraint)

View File

@ -192,16 +192,6 @@ class OracleSchemaManager extends AbstractSchemaManager
return new Column($tableColumn['column_name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
}
public function createForeignKey($table, array $definition)
{
if(isset($definition['onUpdate'])) {
// Oracle does not support onUpdate
unset($definition['onUpdate']);
}
return parent::createForeignKey($table, $definition);
}
protected function _getPortableTableForeignKeysList($tableForeignKeys)
{
$list = array();
@ -285,34 +275,6 @@ class OracleSchemaManager extends AbstractSchemaManager
return true;
}
/**
* getAdvancedForeignKeyOptions
* Return the FOREIGN KEY query section dealing with non-standard options
* as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
*
* @param array $definition foreign key definition
* @return string
* @access protected
*/
public function getAdvancedForeignKeyOptions(array $definition)
{
$query = '';
if (isset($definition['onDelete'])) {
$query .= ' ON DELETE ' . $definition['onDelete'];
}
if (isset($definition['deferrable'])) {
$query .= ' DEFERRABLE';
} else {
$query .= ' NOT DEFERRABLE';
}
if (isset($definition['feferred'])) {
$query .= ' INITIALLY DEFERRED';
} else {
$query .= ' INITIALLY IMMEDIATE';
}
return $query;
}
public function dropTable($name)
{
$this->dropAutoincrement($name);

View File

@ -266,10 +266,26 @@ class Table extends AbstractAsset
*/
public function addForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
{
$name = $this->_generateIdentifierName($localColumnNames, "fk");
$name = $this->_generateIdentifierName(array_merge((array)$this->getName(), $localColumnNames), "fk");
return $this->addNamedForeignKeyConstraint($name, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
}
/**
* Add a foreign key constraint
*
* Name is to be generated by the database itsself.
*
* @param Table $foreignTable
* @param array $localColumns
* @param array $foreignColumns
* @param array $options
* @return Table
*/
public function addUnnamedForeignKeyConstraint($foreignTable, array $localColumnNames, array $foreignColumnNames, array $options=array())
{
return $this->addNamedForeignKeyConstraint(null, $foreignTable, $localColumnNames, $foreignColumnNames, $options);
}
/**
* Add a foreign key constraint with a given name
*
@ -354,11 +370,19 @@ class Table extends AbstractAsset
}
/**
* @param Constraint $constraint
* @param ForeignKeyConstraint $constraint
*/
protected function _addForeignKeyConstraint(Constraint $constraint)
protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
{
$this->_fkConstraints[$constraint->getName()] = $constraint;
if(strlen($constraint->getName())) {
$name = $constraint->getName();
} else {
$name = $this->_generateIdentifierName(
array_merge((array)$this->getName(), $constraint->getLocalColumns()), "fk"
);
}
$this->_fkConstraints[$name] = $constraint;
}
/**

View File

@ -76,55 +76,8 @@ class CreateSchemaSqlCollector implements Visitor
*/
public function acceptTable(Table $table)
{
$options = $table->getOptions();
$options['uniqueConstraints'] = array();
$options['indexes'] = array();
$options['primary'] = array();
foreach($table->getIndexes() AS $index) {
/* @var $index Index */
if(!$index->isPrimary() && !$index->isUnique()) {
$options['indexes'][$index->getName()] = $index->getColumns();
} else {
if($index->isPrimary()) {
$options['primary'] = $index->getColumns();
} else {
$options['uniqueConstraints'][$index->getName()] = $index->getColumns();
}
}
}
$columns = array();
foreach($table->getColumns() AS $column) {
/* @var \Doctrine\DBAL\Schema\Column $column */
$columnData = array();
$columnData['name'] = $column->getName();
$columnData['type'] = $column->getType();
$columnData['length'] = $column->getLength();
$columnData['notnull'] = $column->getNotNull();
$columnData['unique'] = ($column->hasPlatformOption("unique"))?$column->getPlatformOption('unique'):false;
$columnData['version'] = ($column->hasPlatformOption("version"))?$column->getPlatformOption('version'):false;
if(strtolower($columnData['type']) == "string" && $columnData['length'] === null) {
$columnData['length'] = 255;
}
$columnData['precision'] = $column->getPrecision();
$columnData['scale'] = $column->getScale();
$columnData['default'] = $column->getDefault();
// TODO: Fixed? Unsigned?
if(in_array($column->getName(), $options['primary'])) {
$columnData['primary'] = true;
if($table->isIdGeneratorIdentity()) {
$columnData['autoincrement'] = true;
}
}
$columns[$columnData['name']] = $columnData;
}
$this->_createTableQueries = array_merge($this->_createTableQueries,
$this->_platform->getCreateTableSql($table->getName(), $columns, $options)
$this->_platform->getCreateTableSql($table)
);
}
@ -139,19 +92,10 @@ class CreateSchemaSqlCollector implements Visitor
*/
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
{
$fkConstraintArray = array(
'tableName' => $fkConstraint->getName(),
'foreignTable' => $fkConstraint->getForeignTableName(),
'local' => $fkConstraint->getLocalColumns(),
'foreign' => $fkConstraint->getForeignColumns(),
'onUpdate' => ($fkConstraint->hasOption('onUpdate')?$fkConstraint->getOption('onUpdate'):null),
'onDelete' => ($fkConstraint->hasOption('onDelete')?$fkConstraint->getOption('onDelete'):null),
);
// Append the foreign key constraints SQL
if ($this->_platform->supportsForeignKeyConstraints()) {
$this->_createFkConstraintQueries = array_merge($this->_createFkConstraintQueries,
(array) $this->_platform->getCreateForeignKeySql($localTable->getName(), $fkConstraintArray)
(array) $this->_platform->getCreateForeignKeySql($fkConstraint, $localTable->getName())
);
}
}

View File

@ -177,7 +177,7 @@ class SchemaTool
}
// Add a FK constraint on the ID column
$table->addForeignKeyConstraint(
$table->addUnnamedForeignKeyConstraint(
$this->_em->getClassMetadata($class->rootEntityName)->getQuotedTableName($this->_platform),
array($columnName), array($columnName), array('onDelete' => 'CASCADE')
);
@ -414,7 +414,7 @@ class SchemaTool
}
}
$theJoinTable->addForeignKeyConstraint(
$theJoinTable->addUnnamedForeignKeyConstraint(
$class->getQuotedTableName($this->_platform), $localColumns, $foreignColumns, $fkOptions
);
}

View File

@ -8,13 +8,6 @@ require_once __DIR__ . '/../../../TestInit.php';
class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
public function testListSequences()
{
$this->createTestTable('list_sequences_test');
$sequences = $this->_sm->listSequences();
$this->assertEquals(true, in_array('list_sequences_test', $sequences));
}
public function testListTableConstraints()
{
$this->createTestTable('list_table_constraints_test');

View File

@ -36,7 +36,7 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
$this->createTestTable('test_constraints');
$tableConstraints = $this->_sm->listTableConstraints('test_constraints');
$this->assertEquals(2, count($tableConstraints));
$this->assertTrue(count($tableConstraints)>=2);
}
public function testListUsers()

View File

@ -80,39 +80,16 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
public function testListTableColumns()
{
$data = array();
$data['columns'] = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255,
'notnull' => false,
),
'foo' => array(
'type' => Type::getType('text'),
'notnull' => true,
),
'bar' => array(
'type' => Type::getType('decimal'),
'precision' => 10,
'scale' => 4,
),
'baz1' => array(
'type' => Type::getType('datetime'),
),
'baz2' => array(
'type' => Type::getType('time'),
),
'baz3' => array(
'type' => Type::getType('date'),
),
);
$this->createTestTable('list_table_columns', $data);
$table = new \Doctrine\DBAL\Schema\Table('list_table_columns');
$table->createColumn('id', 'integer', array('notnull' => true));
$table->createColumn('test', 'string', array('length' => 255, 'notnull' => false));
$table->createColumn('foo', 'text', array('notnull' => true));
$table->createColumn('bar', 'decimal', array('precision' => 10, 'scale' => 4, 'notnull' => false));
$table->createColumn('baz1', 'datetime');
$table->createColumn('baz2', 'time');
$table->createColumn('baz3', 'date');
$this->_sm->dropAndCreateTable($table);
$columns = $this->_sm->listTableColumns('list_table_columns');
@ -157,43 +134,30 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->assertEquals('baz1', strtolower($columns['baz1']->getname()));
$this->assertType('Doctrine\DBAL\Types\DateTimeType', $columns['baz1']->gettype());
$this->assertEquals(false, $columns['baz1']->getnotnull());
$this->assertEquals(true, $columns['baz1']->getnotnull());
$this->assertEquals(null, $columns['baz1']->getdefault());
$this->assertType('array', $columns['baz1']->getPlatformOptions());
$this->assertEquals('baz2', strtolower($columns['baz2']->getname()));
$this->assertContains($columns['baz2']->gettype()->getName(), array('Time', 'Date', 'DateTime'));
$this->assertEquals(false, $columns['baz2']->getnotnull());
$this->assertEquals(true, $columns['baz2']->getnotnull());
$this->assertEquals(null, $columns['baz2']->getdefault());
$this->assertType('array', $columns['baz2']->getPlatformOptions());
$this->assertEquals('baz3', strtolower($columns['baz3']->getname()));
$this->assertContains($columns['baz2']->gettype()->getName(), array('Time', 'Date', 'DateTime'));
$this->assertEquals(false, $columns['baz3']->getnotnull());
$this->assertEquals(true, $columns['baz3']->getnotnull());
$this->assertEquals(null, $columns['baz3']->getdefault());
$this->assertType('array', $columns['baz3']->getPlatformOptions());
}
public function testListTableIndexes()
{
$data = array();
$data['options'] = array(
'indexes' => array(
'test_index_name' => array(
'columns' => array(
'test' => array()
),
'type' => 'unique'
),
'test_composite_idx' => array(
'columns' => array(
'id' => array(), 'test' => array(),
)
),
)
);
$table = $this->getTestTable('list_table_indexes_test');
$table->addUniqueIndex(array('test'), 'test_index_name');
$table->addIndex(array('id', 'test'), 'test_composite_idx');
$this->createTestTable('list_table_indexes_test', $data);
$this->_sm->createTable($table);
$tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
@ -216,16 +180,11 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
public function testDropAndCreateIndex()
{
$this->createTestTable('test_create_index');
$table = $this->getTestTable('test_create_index');
$table->addUniqueIndex(array('test'), 'test');
$this->_sm->dropAndCreateTable($table);
$index = array(
'columns' => array(
'test' => array()
),
'type' => 'unique'
);
$this->_sm->dropAndCreateIndex('test_create_index', 'test', $index);
$this->_sm->dropAndCreateIndex($table->getIndex('test'), $table);
$tableIndexes = $this->_sm->listTableIndexes('test_create_index');
$this->assertType('array', $tableIndexes);
@ -244,16 +203,11 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->createTestTable('test_create_fk1');
$this->createTestTable('test_create_fk2');
$definition = array(
'name' => 'foreign_key_test_fk',
'local' => array('foreign_key_test'),
'foreign' => array('id'),
'foreignTable' => 'test_create_fk2',
'onUpdate' => 'CASCADE',
'onDelete' => 'CASCADE',
$foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
array('foreign_key_test'), 'test_create_fk2', array('id'), 'foreign_key_test_fk', array('onUpdate' => 'CASCADE', 'onDelete' => 'CASCADE')
);
$this->_sm->createForeignKey('test_create_fk1', $definition);
$this->_sm->createForeignKey($foreignKey, 'test_create_fk1');
$fkeys = $this->_sm->listTableForeignKeys('test_create_fk1');
@ -331,32 +285,25 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
protected function createTestTable($name = 'test_table', $data = array())
{
if ( ! isset($data['columns'])) {
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
),
'foreign_key_test' => array(
'type' => Type::getType('integer')
)
);
} else {
$columns = $data['columns'];
}
$options = array();
if (isset($data['options'])) {
$options = $data['options'];
}
$this->_sm->dropAndCreateTable($name, $columns, $options);
$table = $this->getTestTable($name, $options);
$this->_sm->dropAndCreateTable($table);
}
protected function getTestTable($name, $options=array())
{
$table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), \Doctrine\DBAL\Schema\Table::ID_NONE, $options);
$table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);
$table->createColumn('id', 'integer', array('notnull' => true));
$table->setPrimaryKey(array('id'));
$table->createColumn('test', 'string', array('length' => 255));
$table->createColumn('foreign_key_test', 'integer');
return $table;
}
protected function assertHasTable($tables, $tableName)

View File

@ -45,31 +45,6 @@ class SqliteSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertEquals(array(), $tableConstraints);
}
/*public function testListTableColumns()
{
$this->createTestTable('list_table_columns_test');
$tableColumns = $this->_sm->listTableColumns('list_table_columns_test');
$this->assertEquals('id', $tableColumns[0]['name']);
$this->assertEquals(true, $tableColumns[0]['primary']);
$this->assertEquals('Doctrine\DBAL\Types\IntegerType', get_class($tableColumns[0]['type']));
$this->assertEquals(null, $tableColumns[0]['length']);
$this->assertEquals(false, $tableColumns[0]['unsigned']);
$this->assertEquals(false, $tableColumns[0]['fixed']);
$this->assertEquals(true, $tableColumns[0]['notnull']);
$this->assertEquals(null, $tableColumns[0]['default']);
$this->assertEquals('test', $tableColumns[1]['name']);
$this->assertEquals(false, $tableColumns[1]['primary']);
$this->assertEquals('Doctrine\DBAL\Types\StringType', get_class($tableColumns[1]['type']));
$this->assertEquals(255, $tableColumns[1]['length']);
$this->assertEquals(false, $tableColumns[1]['unsigned']);
$this->assertEquals(false, $tableColumns[1]['fixed']);
$this->assertEquals(false, $tableColumns[1]['notnull']);
$this->assertEquals(null, $tableColumns[1]['default']);
}*/
/**
* @expectedException \Exception
*/

View File

@ -0,0 +1,61 @@
<?php
namespace Doctrine\Tests\DBAL\Platforms;
abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
{
protected $_platform;
abstract public function createPlatform();
public function setUp()
{
$this->_platform = $this->createPlatform();
}
public function testGeneratesTableCreationSql()
{
$table = new \Doctrine\DBAL\Schema\Table('test');
$table->createColumn('id', 'integer', array('notnull' => true));
$table->createColumn('test', 'string', array('notnull' => false, 'length' => 255));
$table->setPrimaryKey(array('id'));
$table->setIdGeneratorType(\Doctrine\DBAL\Schema\Table::ID_IDENTITY);
$sql = $this->_platform->getCreateTableSql($table);
$this->assertEquals($this->getGenerateTableSql(), $sql[0]);
}
abstract public function getGenerateTableSql();
public function testGeneratesIndexCreationSql()
{
$indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));
$this->assertEquals(
$this->getGenerateIndexSql(),
$this->_platform->getCreateIndexSql($indexDef, 'mytable')
);
}
abstract public function getGenerateIndexSql();
public function testGeneratesUniqueIndexCreationSql()
{
$indexDef = new \Doctrine\DBAL\Schema\Index('index_name', array('test', 'test2'), true);
$sql = $this->_platform->getCreateIndexSql($indexDef, 'test');
$this->assertEquals($this->getGenerateUniqueIndexSql(), $sql);
}
abstract public function getGenerateUniqueIndexSql();
public function testGeneratesForeignKeyCreationSql()
{
$fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');
$sql = $this->_platform->getCreateForeignKeySql($fk, 'test');
$this->assertEquals($sql, $this->getGenerateForeignKeySql());
}
abstract public function getGenerateForeignKeySql();
}

View File

@ -7,37 +7,16 @@ use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php';
class MsSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
class MsSqlPlatformTest extends AbstractPlatformTestCase
{
private $_platform;
public function setUp()
public function createPlatform()
{
$this->_platform = new MsSqlPlatform;
return new MsSqlPlatform;
}
public function testGeneratesTableCreationSql()
public function getGenerateTableSql()
{
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255,
'notnull' => true
)
);
$options = array(
'primary' => array('id')
);
$sql = $this->_platform->getCreateTableSql('test', $columns, $options);
$this->assertEquals('CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) NOT NULL, PRIMARY KEY(id))', $sql[0]);
return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
}
public function testGeneratesTableAlterationSql()
@ -152,34 +131,19 @@ class MsSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals($sql, 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)');
}
public function testGeneratesIndexCreationSql()
public function getGenerateIndexSql()
{
$indexDef = array(
'columns' => array(
'user_name' => array(
'sorting' => 'ASC',
'length' => 10
),
'last_login' => array()
)
);
$this->assertEquals(
'CREATE INDEX my_idx ON mytable (user_name, last_login)',
$this->_platform->getCreateIndexSql('mytable', 'my_idx', $indexDef)
);
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
}
public function testGeneratesUniqueIndexCreationSql()
public function getGenerateUniqueIndexSql()
{
$sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2')));
$this->assertEquals($sql, 'CREATE UNIQUE INDEX index_name ON test (test, test2)');
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
}
public function testGeneratesForeignKeyCreationSql()
public function getGenerateForeignKeySql()
{
$sql = $this->_platform->getCreateForeignKeySql('test', array('foreignTable' => 'other_table', 'local' => 'fk_name_id', 'foreign' => 'id'));
$this->assertEquals($sql, 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)');
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
}
public function testModifyLimitQuery()

View File

@ -7,37 +7,16 @@ use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php';
class MySqlPlatformTest extends \Doctrine\Tests\DbalTestCase
class MySqlPlatformTest extends AbstractPlatformTestCase
{
private $_platform;
public function setUp()
public function createPlatform()
{
$this->_platform = new MySqlPlatform;
return new MysqlPlatform;
}
public function testGeneratesTableCreationSql()
public function getGenerateTableSql()
{
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255,
'notnull' => true
)
);
$options = array(
'primary' => array('id')
);
$sql = $this->_platform->getCreateTableSql('test', $columns, $options);
$this->assertEquals('CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB', $sql[0]);
return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB';
}
public function testGeneratesTableAlterationSql()
@ -154,34 +133,19 @@ class MySqlPlatformTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals($sql, 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)');
}
public function testGeneratesIndexCreationSql()
public function getGenerateIndexSql()
{
$indexDef = array(
'columns' => array(
'user_name' => array(
'sorting' => 'ASC',
'length' => 10
),
'last_login' => array()
)
);
$this->assertEquals(
'CREATE INDEX my_idx ON mytable (user_name(10) ASC, last_login)',
$this->_platform->getCreateIndexSql('mytable', 'my_idx', $indexDef)
);
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
}
public function testGeneratesUniqueIndexCreationSql()
public function getGenerateUniqueIndexSql()
{
$sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2')));
$this->assertEquals($sql, 'CREATE UNIQUE INDEX index_name ON test (test, test2)');
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
}
public function testGeneratesForeignKeyCreationSql()
public function getGenerateForeignKeySql()
{
$sql = $this->_platform->getCreateForeignKeySql('test', array('foreignTable' => 'other_table', 'local' => 'fk_name_id', 'foreign' => 'id'));
$this->assertEquals($sql, 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)');
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
}
public function testModifyLimitQuery()

View File

@ -7,37 +7,16 @@ use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php';
class OraclePlatformTest extends \Doctrine\Tests\DbalTestCase
class OraclePlatformTest extends AbstractPlatformTestCase
{
private $_platform;
public function setUp()
public function createPlatform()
{
$this->_platform = new OraclePlatform;
return new OraclePlatform;
}
public function testGeneratesTableCreationSql()
public function getGenerateTableSql()
{
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255,
'notnull' => true
)
);
$options = array(
'primary' => array('id')
);
$sql = $this->_platform->getCreateTableSql('test', $columns, $options);
$this->assertEquals('CREATE TABLE test (id NUMBER(10) NOT NULL, test VARCHAR2(255) NOT NULL, PRIMARY KEY(id))', $sql[0]);
return 'CREATE TABLE test (id NUMBER(10) NOT NULL, test VARCHAR2(255) DEFAULT NULL, PRIMARY KEY(id))';
}
public function testGeneratesTableAlterationSql()
@ -190,34 +169,19 @@ class OraclePlatformTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals($sql, 'ALTER TABLE test ADD CONSTRAINT constraint_name (test)');
}
public function testGeneratesIndexCreationSql()
public function getGenerateIndexSql()
{
$indexDef = array(
'columns' => array(
'user_name' => array(
'sorting' => 'ASC',
'length' => 10
),
'last_login' => array()
)
);
$this->assertEquals(
'CREATE INDEX my_idx ON mytable (user_name, last_login)',
$this->_platform->getCreateIndexSql('mytable', 'my_idx', $indexDef)
);
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
}
public function testGeneratesUniqueIndexCreationSql()
public function getGenerateUniqueIndexSql()
{
$sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2')));
$this->assertEquals($sql, 'CREATE UNIQUE INDEX index_name ON test (test, test2)');
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
}
public function testGeneratesForeignKeyCreationSql()
public function getGenerateForeignKeySql()
{
$sql = $this->_platform->getCreateForeignKeySql('test', array('foreignTable' => 'other_table', 'local' => 'fk_name_id', 'foreign' => 'id'));
$this->assertEquals($sql, 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)');
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
}
public function testModifyLimitQuery()

View File

@ -8,37 +8,16 @@ use Doctrine\DBAL\Connection;
require_once __DIR__ . '/../../TestInit.php';
class PostgreSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
class PostgreSqlPlatformTest extends AbstractPlatformTestCase
{
private $_platform;
public function setUp()
public function createPlatform()
{
$this->_platform = new PostgreSqlPlatform;
return new PostgreSqlPlatform;
}
public function testGeneratesTableCreationSql()
public function getGenerateTableSql()
{
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255,
'notnull' => true
)
);
$options = array(
'primary' => array('id')
);
$sql = $this->_platform->getCreateTableSql('test', $columns, $options);
$this->assertEquals('CREATE TABLE test (id INT NOT NULL, test VARCHAR(255) NOT NULL, PRIMARY KEY(id))', $sql[0]);
return 'CREATE TABLE test (id SERIAL NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))';
}
public function testGeneratesTableAlterationSqlForAddingAndRenaming()
@ -63,35 +42,24 @@ class PostgreSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
);
}
public function testGeneratesIndexCreationSql()
public function getGenerateIndexSql()
{
$indexDef = array(
'columns' => array(
'user_name',
'last_login'
)
);
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
}
$sql = $this->_platform->getCreateIndexSql('mytable', 'my_idx', $indexDef);
$this->assertEquals(
'CREATE INDEX my_idx ON mytable (user_name, last_login)',
$sql
);
public function getGenerateForeignKeySql()
{
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id) NOT DEFERRABLE INITIALLY IMMEDIATE';
}
public function testGeneratesForeignKeySqlForNonStandardOptions()
{
$definition = array(
'name' => 'my_fk',
'local' => 'foreign_id',
'foreign' => 'id',
'foreignTable' => 'my_table',
'onDelete' => 'CASCADE'
$foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
array('foreign_id'), 'my_table', array('id'), 'my_fk', array('onDelete' => 'CASCADE')
);
$this->assertEquals(
" CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table(id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE",
$this->_platform->getForeignKeyDeclarationSql($definition)
"CONSTRAINT my_fk FOREIGN KEY (foreign_id) REFERENCES my_table(id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE",
$this->_platform->getForeignKeyDeclarationSql($foreignKey)
);
}
@ -168,6 +136,11 @@ class PostgreSqlPlatformTest extends \Doctrine\Tests\DbalTestCase
);
}
public function getGenerateUniqueIndexSql()
{
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
}
public function testGeneratesSequenceSqlCommands()
{
$this->assertEquals(

View File

@ -7,34 +7,16 @@ use Doctrine\DBAL\Types\Type;
require_once __DIR__ . '/../../TestInit.php';
class SqlitePlatformTest extends \Doctrine\Tests\DbalTestCase
class SqlitePlatformTest extends AbstractPlatformTestCase
{
private $_platform;
public function setUp()
public function createPlatform()
{
$this->_platform = new SqlitePlatform;
return new SqlitePlatform;
}
public function testGeneratesTableCreationSql()
public function getGenerateTableSql()
{
$columns = array(
'id' => array(
'type' => Type::getType('integer'),
'autoincrement' => true,
'primary' => true,
'notnull' => true
),
'test' => array(
'type' => Type::getType('string'),
'length' => 255
)
);
$options = array();
$sql = $this->_platform->getCreateTableSql('test', $columns, $options);
$this->assertEquals('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL)', $sql[0]);
return 'CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL)';
}
public function testGeneratesSqlSnippets()
@ -99,16 +81,19 @@ class SqlitePlatformTest extends \Doctrine\Tests\DbalTestCase
$this->assertEquals('ALTER TABLE test ADD CONSTRAINT constraint_name (test)', $sql);
}
public function testGeneratesIndexCreationSql()
public function getGenerateIndexSql()
{
$sql = $this->_platform->getCreateIndexSql('test', 'index_name', array('type' => 'unique', 'columns' => array('test', 'test2')));
$this->assertEquals('CREATE UNIQUE INDEX index_name ON test (test, test2)', $sql);
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
}
public function testGeneratesForeignKeyCreationSql()
public function getGenerateUniqueIndexSql()
{
$sql = $this->_platform->getCreateForeignKeySql('test', array('foreignTable' => 'other_table', 'local' => 'fk_name_id', 'foreign' => 'id'));
$this->assertEquals('ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)', $sql);
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
}
public function getGenerateForeignKeySql()
{
$this->markTestSkipped('SQLite does not support ForeignKeys.');
}
public function testModifyLimitQuery()

View File

@ -296,11 +296,11 @@ class TableTest extends \PHPUnit_Framework_TestCase
$constraints = $table->getForeignKeys();
$this->assertEquals(1, count($constraints));
$this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraints["id_fk"]);
$this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraints["foo_id_fk"]);
$this->assertEquals("id_fk", $constraints["id_fk"]->getName());
$this->assertTrue($constraints["id_fk"]->hasOption("foo"));
$this->assertEquals("bar", $constraints["id_fk"]->getOption("foo"));
$this->assertEquals("foo_id_fk", $constraints["foo_id_fk"]->getName());
$this->assertTrue($constraints["foo_id_fk"]->hasOption("foo"));
$this->assertEquals("bar", $constraints["foo_id_fk"]->getOption("foo"));
}
}

View File

@ -26,7 +26,7 @@ class MySqlSchemaToolTest extends \Doctrine\Tests\OrmFunctionalTestCase
$tool = new SchemaTool($this->_em);
$sql = $tool->getCreateSchemaSql($classes);
$this->assertEquals(count($sql), 8);
$this->assertEquals(8, count($sql));
$this->assertEquals("CREATE TABLE cms_addresses (id INT AUTO_INCREMENT NOT NULL, country VARCHAR(50) NOT NULL, zip VARCHAR(50) NOT NULL, city VARCHAR(50) NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[0]);
$this->assertEquals("CREATE TABLE cms_users (id INT AUTO_INCREMENT NOT NULL, status VARCHAR(50) NOT NULL, username VARCHAR(255) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB", $sql[1]);
$this->assertEquals("CREATE TABLE cms_users_groups (user_id INT DEFAULT NULL, group_id INT DEFAULT NULL, PRIMARY KEY(user_id, group_id)) ENGINE = InnoDB", $sql[2]);