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

[2.0] DDC-169 - Converted introspection queries of all platforms to return Column, Sequence, Index, ForeignKey Schema Instances instead of arrays, heavily pimped Oracle support thereby.

This commit is contained in:
beberlei 2009-11-30 19:02:05 +00:00
parent 709dfb52c4
commit af48974a75
19 changed files with 316 additions and 271 deletions

View File

@ -202,13 +202,14 @@ class MySqlPlatform extends AbstractPlatform
public function getListTableForeignKeysSql($table, $database = null) public function getListTableForeignKeysSql($table, $database = null)
{ {
$sql = "SELECT column_name, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM information_schema.key_column_usage WHERE table_name = '" . $table . "'"; $sql = "SELECT `CONSTRAINT_NAME`, `COLUMN_NAME`, `REFERENCED_TABLE_NAME`, ".
"`REFERENCED_COLUMN_NAME` FROM information_schema.key_column_usage WHERE table_name = '" . $table . "'";
if ( ! is_null($database)) { if ( ! is_null($database)) {
$sql .= " AND table_schema = '$database'"; $sql .= " AND table_schema = '$database'";
} }
$sql .= " AND REFERENCED_COLUMN_NAME is not NULL"; $sql .= " AND `REFERENCED_COLUMN_NAME` is not NULL";
return $sql; return $sql;
} }

View File

@ -106,6 +106,10 @@ class OraclePlatform extends AbstractPlatform
* Gets the SQL used to create a sequence that starts with a given value * Gets the SQL used to create a sequence that starts with a given value
* and increments by the given allocation size. * and increments by the given allocation size.
* *
* Need to specifiy minvalue, since start with is hidden in the system and MINVALUE <= START WITH.
* Therefore we can use MINVALUE to be able to get a hint what START WITH was for later introspection
* in {@see listSequences()}
*
* @param string $sequenceName * @param string $sequenceName
* @param integer $start * @param integer $start
* @param integer $allocationSize * @param integer $allocationSize
@ -114,7 +118,7 @@ class OraclePlatform extends AbstractPlatform
public function getCreateSequenceSql($sequenceName, $start = 1, $allocationSize = 1) public function getCreateSequenceSql($sequenceName, $start = 1, $allocationSize = 1)
{ {
return 'CREATE SEQUENCE ' . $sequenceName return 'CREATE SEQUENCE ' . $sequenceName
. ' START WITH ' . $start . ' INCREMENT BY ' . $allocationSize; . ' START WITH ' . $start . ' MINVALUE ' . $start . ' INCREMENT BY ' . $allocationSize;
} }
/** /**
@ -249,7 +253,7 @@ class OraclePlatform extends AbstractPlatform
public function getListDatabasesSql() public function getListDatabasesSql()
{ {
return 'SELECT username FROM sys.dba_users'; return 'SELECT username FROM all_users';
} }
public function getListFunctionsSql() public function getListFunctionsSql()
@ -259,7 +263,7 @@ class OraclePlatform extends AbstractPlatform
public function getListSequencesSql($database) public function getListSequencesSql($database)
{ {
return 'SELECT sequence_name FROM sys.user_sequences'; return 'SELECT sequence_name, min_value, increment_by FROM sys.user_sequences';
} }
public function getCreateTableSql($table, array $columns, array $options = array()) public function getCreateTableSql($table, array $columns, array $options = array())
@ -281,11 +285,7 @@ class OraclePlatform extends AbstractPlatform
if (isset($indexes) && ! empty($indexes)) { if (isset($indexes) && ! empty($indexes)) {
foreach ($indexes as $indexName => $definition) { foreach ($indexes as $indexName => $definition) {
// create nonunique indexes, as they are a part of CREATE TABLE DDL $sql[] = $this->getCreateIndexSql($table, $indexName, $definition);
if ( ! isset($definition['type']) ||
(isset($definition['type']) && strtolower($definition['type']) != 'unique')) {
$sql[] = $this->getCreateIndexSql($table, $indexName, $definition);
}
} }
} }
@ -293,7 +293,6 @@ class OraclePlatform extends AbstractPlatform
} }
/** /**
*
* @license New BSD License * @license New BSD License
* @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html
* @param string $table * @param string $table
@ -301,11 +300,14 @@ class OraclePlatform extends AbstractPlatform
*/ */
public function getListTableIndexesSql($table) public function getListTableIndexesSql($table)
{ {
$table = strtoupper($table);
return "SELECT uind.index_name AS name, " . return "SELECT uind.index_name AS name, " .
" uind.index_type AS type, " . " uind.index_type AS type, " .
" decode( uind.uniqueness, 'NONUNIQUE', 0, 'UNIQUE', 1 ) AS is_unique, " . " decode( uind.uniqueness, 'NONUNIQUE', 0, 'UNIQUE', 1 ) AS is_unique, " .
" uind_col.column_name AS column_name, " . " uind_col.column_name AS column_name, " .
" uind_col.column_position AS column_pos " . " uind_col.column_position AS column_pos, " .
" (SELECT ucon.constraint_type FROM user_constraints ucon WHERE ucon.constraint_name = uind.index_name) AS is_primary ".
"FROM user_indexes uind, user_ind_columns uind_col " . "FROM user_indexes uind, user_ind_columns uind_col " .
"WHERE uind.index_name = uind_col.index_name AND uind_col.table_name = '$table'"; "WHERE uind.index_name = uind_col.index_name AND uind_col.table_name = '$table'";
} }
@ -317,7 +319,7 @@ class OraclePlatform extends AbstractPlatform
public function getListUsersSql() public function getListUsersSql()
{ {
return 'SELECT * FROM sys.dba_users'; return 'SELECT * FROM all_users';
} }
public function getListViewsSql() public function getListViewsSql()
@ -399,6 +401,24 @@ END;';
return $sql; return $sql;
} }
public function getListTableForeignKeysSql($table)
{
$table = strtoupper($table);
return "SELECT rel.constraint_name, rel.position, col.column_name AS local_column, ".
" rel.table_name, rel.column_name AS foreign_column, cc.delete_rule ".
"FROM (user_tab_columns col ".
"JOIN user_cons_columns con ".
" ON col.table_name = con.table_name ".
" AND col.column_name = con.column_name ".
"JOIN user_constraints cc ".
" ON con.constraint_name = cc.constraint_name ".
"JOIN user_cons_columns rel ".
" ON cc.r_constraint_name = rel.constraint_name ".
" AND con.position = rel.position) ".
"WHERE cc.constraint_type = 'R' AND col.table_name = '".$table."'";
}
public function getListTableConstraintsSql($table) public function getListTableConstraintsSql($table)
{ {
$table = strtoupper($table); $table = strtoupper($table);
@ -579,4 +599,14 @@ END;';
} }
return $schemaElementName; return $schemaElementName;
} }
/**
* Whether the platform supports sequences.
*
* @return boolean
*/
public function supportsSequences()
{
return true;
}
} }

View File

@ -341,7 +341,7 @@ class PostgreSqlPlatform extends AbstractPlatform
public function getListTableForeignKeysSql($table, $database = null) public function getListTableForeignKeysSql($table, $database = null)
{ {
return "SELECT pg_catalog.pg_get_constraintdef(oid, true) as condef return "SELECT r.name, pg_catalog.pg_get_constraintdef(r.oid, true) as condef
FROM pg_catalog.pg_constraint r FROM pg_catalog.pg_constraint r
WHERE r.conrelid = WHERE r.conrelid =
( (

View File

@ -23,6 +23,7 @@ namespace Doctrine\DBAL\Schema;
use \Doctrine\DBAL\Types; use \Doctrine\DBAL\Types;
use \Doctrine\Common\DoctrineException; use \Doctrine\Common\DoctrineException;
use \Doctrine\DBAL\DBALException;
/** /**
* Base class for schema managers. Schema managers are used to inspect and/or * Base class for schema managers. Schema managers are used to inspect and/or
@ -210,32 +211,10 @@ abstract class AbstractSchemaManager
} }
/** /**
* List the indexes for a given table * List the indexes for a given table returning an array of Index instances.
*
* @example
* $indexes = array(
* 'primary' => array(
* 'name' => 'primary',
* 'columns' => array('id'),
* 'unique' => true,
* 'primary' => true,
* ),
* 'fieldUnq' => array(
* 'name' => 'fieldUnq',
* 'columns' => array('foo', 'bar'),
* 'unique' => true,
* 'primary' => false,
* ),
* 'fieldIdx' => array(
* 'name' => 'fieldIdx',
* 'columns' => array('baz'),
* 'unique' => false,
* 'primary' => false,
* ),
* );
* *
* @param string $table The name of the table * @param string $table The name of the table
* @return array $tableIndexes * @return Index[] $tableIndexes
*/ */
public function listTableIndexes($table) public function listTableIndexes($table)
{ {
@ -899,9 +878,13 @@ abstract class AbstractSchemaManager
return $list; return $list;
} }
/**
* @param array $sequence
* @return Sequence
*/
protected function _getPortableSequenceDefinition($sequence) protected function _getPortableSequenceDefinition($sequence)
{ {
return $sequence; throw DBALException::notSupported('Sequences');
} }
protected function _getPortableTableConstraintsList($tableConstraints) protected function _getPortableTableConstraintsList($tableConstraints)
@ -923,21 +906,21 @@ abstract class AbstractSchemaManager
protected function _getPortableTableColumnList($tableColumns) protected function _getPortableTableColumnList($tableColumns)
{ {
$list = array(); $list = array();
foreach ($tableColumns as $key => $value) { foreach ($tableColumns as $key => $column) {
if ($value = $this->_getPortableTableColumnDefinition($value)) { if ($column = $this->_getPortableTableColumnDefinition($column)) {
if (is_string($value['type'])) { $list[$column->getName()] = $column;
$value['type'] = \Doctrine\DBAL\Types\Type::getType($value['type']);
}
$list[$value['name']] = $value;
} }
} }
return $list; return $list;
} }
protected function _getPortableTableColumnDefinition($tableColumn) /**
{ * Get Table Column Definition
return $tableColumn; *
} * @param array $tableColumn
* @return Column
*/
abstract protected function _getPortableTableColumnDefinition($tableColumn);
/** /**
* Aggregate and group the index results according to the required data result. * Aggregate and group the index results according to the required data result.
@ -967,7 +950,12 @@ abstract class AbstractSchemaManager
} }
} }
return $result; $indexes = array();
foreach($result AS $indexKey => $data) {
$indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
}
return $indexes;
} }
protected function _getPortableTablesList($tables) protected function _getPortableTablesList($tables)

View File

@ -132,7 +132,11 @@ class Column extends AbstractAsset
*/ */
public function setLength($length) public function setLength($length)
{ {
$this->_length = (int)$length; if($length !== null) {
$this->_length = (int)$length;
} else {
$this->_length = null;
}
return $this; return $this;
} }

View File

@ -226,11 +226,11 @@ class Comparator
*/ */
public function diffForeignKey($key1, $key2) public function diffForeignKey($key1, $key2)
{ {
if ($key1->getLocalColumnNames() != $key2->getLocalColumnNames()) { if ($key1->getLocalColumns() != $key2->getLocalColumns()) {
return true; return true;
} }
if ($key1->getForeignColumnNames() != $key2->getForeignColumnNames()) { if ($key1->getForeignColumns() != $key2->getForeignColumns()) {
return true; return true;
} }

View File

@ -70,7 +70,7 @@ class ForeignKeyConstraint extends AbstractAsset implements Constraint
/** /**
* @return array * @return array
*/ */
public function getLocalColumnNames() public function getLocalColumns()
{ {
return $this->_localColumnNames; return $this->_localColumnNames;
} }
@ -86,7 +86,7 @@ class ForeignKeyConstraint extends AbstractAsset implements Constraint
/** /**
* @return array * @return array
*/ */
public function getForeignColumnNames() public function getForeignColumns()
{ {
return $this->_foreignColumnNames; return $this->_foreignColumnNames;
} }

View File

@ -239,9 +239,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
'fixed' => (bool) $fixed 'fixed' => (bool) $fixed
); );
$column = array( $options = array(
'name' => $tableColumn['Field'],
'type' => $type,
'length' => $length, 'length' => $length,
'unsigned' => (bool)$unsigned, 'unsigned' => (bool)$unsigned,
'fixed' => (bool)$fixed, 'fixed' => (bool)$fixed,
@ -249,7 +247,7 @@ class MySqlSchemaManager extends AbstractSchemaManager
'notnull' => (bool) ($tableColumn['Null'] != 'YES'), 'notnull' => (bool) ($tableColumn['Null'] != 'YES'),
'scale' => null, 'scale' => null,
'precision' => null, 'precision' => null,
'platformDetails' => array( 'platformOptions' => array(
'primary' => (strtolower($tableColumn['Key']) == 'pri') ? true : false, 'primary' => (strtolower($tableColumn['Key']) == 'pri') ? true : false,
'unique' => (strtolower($tableColumn['Key']) == 'uni') ? true :false, 'unique' => (strtolower($tableColumn['Key']) == 'uni') ? true :false,
'autoincrement' => (bool) (strpos($tableColumn['Extra'], 'auto_increment') !== false), 'autoincrement' => (bool) (strpos($tableColumn['Extra'], 'auto_increment') !== false),
@ -257,22 +255,24 @@ class MySqlSchemaManager extends AbstractSchemaManager
); );
if ($scale !== null && $precision !== null) { if ($scale !== null && $precision !== null) {
$column['scale'] = $scale; $options['scale'] = $scale;
$column['precision'] = $precision; $options['precision'] = $precision;
} }
return $column; return new Column($tableColumn['Field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
} }
public function _getPortableTableForeignKeyDefinition($tableForeignKey) public function _getPortableTableForeignKeyDefinition($tableForeignKey)
{ {
$tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER); $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
$foreignKey = array(
'table' => $tableForeignKey['referenced_table_name'], return new ForeignKeyConstraint(
'local' => $tableForeignKey['column_name'], (array)$tableForeignKey['column_name'],
'foreign' => $tableForeignKey['referenced_column_name'] $tableForeignKey['referenced_table_name'],
(array)$tableForeignKey['referenced_column_name'],
$tableForeignKey['constraint_name'],
array()
); );
return $foreignKey;
} }
/** /**

View File

@ -48,7 +48,6 @@ class OracleSchemaManager extends AbstractSchemaManager
return array( return array(
'user' => $user['username'], 'user' => $user['username'],
'password' => $user['password']
); );
} }
@ -68,13 +67,13 @@ class OracleSchemaManager extends AbstractSchemaManager
*/ */
protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
{ {
$tableIndexes = \array_change_key_case($tableIndexes, CASE_LOWER);
$indexBuffer = array(); $indexBuffer = array();
foreach ( $tableIndexes as $tableIndex ) { foreach ( $tableIndexes as $tableIndex ) {
$keyName = $tableIndex['name']; $tableIndex = \array_change_key_case($tableIndex, CASE_LOWER);
if ( $keyName == $tableName.'_pkey' ) { $keyName = strtolower($tableIndex['name']);
if ( strtolower($tableIndex['is_primary']) == "p" ) {
$keyName = 'primary'; $keyName = 'primary';
$buffer['primary'] = true; $buffer['primary'] = true;
$buffer['non_unique'] = false; $buffer['non_unique'] = false;
@ -86,7 +85,7 @@ class OracleSchemaManager extends AbstractSchemaManager
$buffer['column_name'] = $tableIndex['column_name']; $buffer['column_name'] = $tableIndex['column_name'];
$indexBuffer[] = $buffer; $indexBuffer[] = $buffer;
} }
parent::_getPortableTableIndexesList($indexBuffer, $tableName); return parent::_getPortableTableIndexesList($indexBuffer, $tableName);
} }
protected function _getPortableTableColumnDefinition($tableColumn) protected function _getPortableTableColumnDefinition($tableColumn)
@ -179,17 +178,8 @@ class OracleSchemaManager extends AbstractSchemaManager
$length = null; $length = null;
} }
$decl = array( $options = array(
'type' => $type,
'length' => $length,
'unsigned' => $unsigned,
'fixed' => $fixed
);
return array(
'name' => $tableColumn['column_name'],
'notnull' => (bool) ($tableColumn['nullable'] === 'N'), 'notnull' => (bool) ($tableColumn['nullable'] === 'N'),
'type' => $type,
'fixed' => (bool) $fixed, 'fixed' => (bool) $fixed,
'unsigned' => (bool) $unsigned, 'unsigned' => (bool) $unsigned,
'default' => $tableColumn['data_default'], 'default' => $tableColumn['data_default'],
@ -198,6 +188,54 @@ class OracleSchemaManager extends AbstractSchemaManager
'scale' => $scale, 'scale' => $scale,
'platformDetails' => array(), 'platformDetails' => array(),
); );
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();
foreach ($tableForeignKeys as $key => $value) {
$value = \array_change_key_case($value, CASE_LOWER);
if (!isset($list[$value['constraint_name']])) {
$list[$value['constraint_name']] = array(
'name' => $value['constraint_name'],
'local' => array(),
'foreign' => array(),
'foreignTable' => $value['table_name'],
'onDelete' => $value['delete_rule'],
);
}
$list[$value['constraint_name']]['local'][$value['position']] = $value['local_column'];
$list[$value['constraint_name']]['foreign'][$value['position']] = $value['foreign_column'];
}
$result = array();
foreach($list AS $constraint) {
$result[] = new ForeignKeyConstraint(
array_values($constraint['local']), $constraint['foreignTable'],
array_values($constraint['foreign']), $constraint['name'],
array('onDelete' => $constraint['onDelete'])
);
}
return $result;
}
protected function _getPortableSequenceDefinition($sequence)
{
$sequence = \array_change_key_case($sequence, CASE_LOWER);
return new Sequence($sequence['sequence_name'], $sequence['increment_by'], $sequence['min_value']);
} }
protected function _getPortableTableConstraintDefinition($tableConstraint) protected function _getPortableTableConstraintDefinition($tableConstraint)

View File

@ -35,15 +35,26 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
{ {
protected function _getPortableTableForeignKeyDefinition($tableForeignKey) protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
{ {
preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $tableForeignKey['condef'], $values); $onUpdate = null;
$onDelete = null;
if ((strpos(',', $values[1]) === false) && (strpos(',', $values[3]) === false)) { if(preg_match('(ON UPDATE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
return array( $onUpdate = $match[1];
'table' => $values[2],
'local' => $values[1],
'foreign' => $values[3]
);
} }
if(preg_match('(ON DELETE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
$onDelete = $match[1];
}
if(preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $tableForeignKey['condef'], $values)) {
$localColumns = explode(",", $values[1]);
$foreignColumns = explode(",", $values[3]);
$foreignTable = $values[2];
}
return new ForeignKeyConstraint(
$localColumns, $foreignTable, $foreignColumns, $tableForeignKey['name'],
array('onUpdate' => $onUpdate, 'onDelete' => $onDelete)
);
} }
public function dropDatabase($database) public function dropDatabase($database)
@ -299,9 +310,7 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
$type = 'string'; $type = 'string';
} }
$description = array( $options = array(
'name' => $tableColumn['field'],
'type' => $type,
'length' => $length, 'length' => $length,
'notnull' => (bool) $tableColumn['isnotnull'], 'notnull' => (bool) $tableColumn['isnotnull'],
'default' => $tableColumn['default'], 'default' => $tableColumn['default'],
@ -313,6 +322,6 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
'platformDetails' => array(), 'platformDetails' => array(),
); );
return $description; return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
} }
} }

View File

@ -245,14 +245,24 @@ class Schema extends AbstractAsset
return $dropSqlCollector->getQueries(); return $dropSqlCollector->getQueries();
} }
public function migrateTo(Schema $schema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) /**
* @param Schema $schema
* @param AbstractPlatform $platform
*/
public function getMigrateToSql(Schema $schema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{ {
$comparator = new Comparator();
$schemaDiff = $comparator->compare($this, $schema);
} }
public function migrateFrom(Schema $schema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform) /**
* @param Schema $schema
* @param AbstractPlatform $platform
*/
public function getMigrateFromSql(Schema $schema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{ {
$comparator = new Comparator();
$schemaDiff = $comparator->compare($schema, $this);
} }
/** /**

View File

@ -242,9 +242,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
$length = null; $length = null;
} }
return array( $options = array(
'name' => $tableColumn['name'],
'type' => $type,
'length' => $length, 'length' => $length,
'unsigned' => (bool) $unsigned, 'unsigned' => (bool) $unsigned,
'fixed' => $fixed, 'fixed' => $fixed,
@ -256,5 +254,7 @@ class SqliteSchemaManager extends AbstractSchemaManager
'primary' => (bool) $tableColumn['pk'], 'primary' => (bool) $tableColumn['pk'],
), ),
); );
return new Column($tableColumn['name'], \Doctrine\DBAL\Types\Type::getType($type), $options);
} }
} }

View File

@ -142,8 +142,8 @@ class CreateSchemaSqlCollector implements Visitor
$fkConstraintArray = array( $fkConstraintArray = array(
'tableName' => $fkConstraint->getName(), 'tableName' => $fkConstraint->getName(),
'foreignTable' => $fkConstraint->getForeignTableName(), 'foreignTable' => $fkConstraint->getForeignTableName(),
'local' => $fkConstraint->getLocalColumnNames(), 'local' => $fkConstraint->getLocalColumns(),
'foreign' => $fkConstraint->getForeignColumnNames(), 'foreign' => $fkConstraint->getForeignColumns(),
'onUpdate' => ($fkConstraint->hasOption('onUpdate')?$fkConstraint->getOption('onUpdate'):null), 'onUpdate' => ($fkConstraint->hasOption('onUpdate')?$fkConstraint->getOption('onUpdate'):null),
'onDelete' => ($fkConstraint->hasOption('onDelete')?$fkConstraint->getOption('onDelete'):null), 'onDelete' => ($fkConstraint->hasOption('onDelete')?$fkConstraint->getOption('onDelete'):null),
); );

View File

@ -41,25 +41,4 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
return 'SELECT * from mysql.user'; return 'SELECT * from mysql.user';
} }
public function testListTableForeignKeys()
{
$data['options'] = array('type' => 'innodb');
$this->createTestTable('list_table_foreign_keys_test1', $data);
$this->createTestTable('list_table_foreign_keys_test2', $data);
$definition = array(
'name' => 'testing',
'local' => 'foreign_key_test',
'foreign' => 'id',
'foreignTable' => 'list_table_foreign_keys_test2'
);
$this->_sm->createForeignKey('list_table_foreign_keys_test1', $definition);
$tableForeignKeys = $this->_sm->listTableForeignKeys('list_table_foreign_keys_test1');
$this->assertEquals(1, count($tableForeignKeys));
$this->assertEquals('list_table_foreign_keys_test2', $tableForeignKeys[0]['table']);
$this->assertEquals('foreign_key_test', $tableForeignKeys[0]['local']);
$this->assertEquals('id', $tableForeignKeys[0]['foreign']);
}
} }

View File

@ -10,8 +10,6 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
public function setUp() public function setUp()
{ {
$this->markTestSkipped('Somehow they all dont work because of privledges or other stuff.');
parent::setUp(); parent::setUp();
if(!isset($GLOBALS['db_username'])) { if(!isset($GLOBALS['db_username'])) {
@ -34,13 +32,6 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->_sm->listTriggers(); $this->_sm->listTriggers();
} }
public function testListSequences()
{
$this->createTestTable('list_sequences_test');
$sequences = $this->_sm->listSequences();
$this->assertEquals(true, in_array('LIST_SEQUENCES_TEST_SEQ', $sequences));
}
public function testListTableConstraints() public function testListTableConstraints()
{ {
$this->createTestTable('test_constraints'); $this->createTestTable('test_constraints');
@ -73,11 +64,6 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertEquals('TEST_CREATE_VIEW', $view['name']); $this->assertEquals('TEST_CREATE_VIEW', $view['name']);
} }
public function testListTableForeignKeys()
{
$this->markTestSkipped('Not yet implemented');
}
public function testRenameTable() public function testRenameTable()
{ {
$this->_sm->tryMethod('DropTable', 'list_tables_test'); $this->_sm->tryMethod('DropTable', 'list_tables_test');

View File

@ -22,28 +22,6 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->assertEquals(array('list_table_constraints_test_pkey'), $tableConstraints); $this->assertEquals(array('list_table_constraints_test_pkey'), $tableConstraints);
} }
/*public function testListTableColumns()
{
$this->createTestTable('list_tables_test');
$columns = $this->_sm->listTableColumns('list_tables_test');
$this->assertEquals('id', $columns[0]['name']);
$this->assertEquals(true, $columns[0]['primary']);
$this->assertEquals('Doctrine\DBAL\Types\IntegerType', get_class($columns[0]['type']));
$this->assertEquals(null, $columns[0]['length']);
$this->assertEquals(false, $columns[0]['fixed']);
$this->assertEquals(true, $columns[0]['notnull']);
$this->assertEquals(null, $columns[0]['default']);
$this->assertEquals('test', $columns[1]['name']);
$this->assertEquals(false, $columns[1]['primary']);
$this->assertEquals('Doctrine\DBAL\Types\StringType', get_class($columns[1]['type']));
$this->assertEquals(255, $columns[1]['length']);
$this->assertEquals(false, $columns[1]['fixed']);
$this->assertEquals(false, $columns[1]['notnull']);
$this->assertEquals(null, $columns[1]['default']);
}*/
public function testListUsers() public function testListUsers()
{ {
$users = $this->_sm->listUsers(); $users = $this->_sm->listUsers();
@ -63,25 +41,4 @@ class PostgreSqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
{ {
return 'SELECT usename, passwd FROM pg_user'; return 'SELECT usename, passwd FROM pg_user';
} }
public function testListTableForeignKeys()
{
$data['options'] = array('type' => 'innodb');
$this->createTestTable('list_table_foreign_keys_test1', $data);
$this->createTestTable('list_table_foreign_keys_test2', $data);
$definition = array(
'name' => 'testing',
'local' => 'foreign_key_test',
'foreign' => 'id',
'foreignTable' => 'list_table_foreign_keys_test2'
);
$this->_sm->createForeignKey('list_table_foreign_keys_test1', $definition);
$tableForeignKeys = $this->_sm->listTableForeignKeys('list_table_foreign_keys_test1');
$this->assertEquals(1, count($tableForeignKeys));
$this->assertEquals('list_table_foreign_keys_test2', $tableForeignKeys[0]['table']);
$this->assertEquals('foreign_key_test', $tableForeignKeys[0]['local']);
$this->assertEquals('id', $tableForeignKeys[0]['foreign']);
}
} }

View File

@ -8,6 +8,31 @@ require_once __DIR__ . '/../../../TestInit.php';
class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
{ {
public function testListSequences()
{
if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
$this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
}
$this->_sm->createSequence('list_sequences_test_seq', 10, 20);
$sequences = $this->_sm->listSequences();
$this->assertType('array', $sequences, 'listSequences() should return an array.');
$foundSequence = null;
foreach($sequences AS $sequence) {
$this->assertType('Doctrine\DBAL\Schema\Sequence', $sequence, 'Array elements of listSequences() should be Sequence instances.');
if(strtolower($sequence->getName()) == 'list_sequences_test_seq') {
$foundSequence = $sequence;
}
}
$this->assertNotNull($foundSequence, "Sequence with name 'list_sequences_test_seq' was not found.");
$this->assertEquals(20, $foundSequence->getAllocationSize(), "Allocation Size is expected to be 20.");
$this->assertEquals(10, $foundSequence->getInitialValue(), "Initial Value is expected to be 10.");
}
public function testListFunctions() public function testListFunctions()
{ {
$funcs = $this->_sm->listFunctions(); $funcs = $this->_sm->listFunctions();
@ -29,7 +54,7 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$databases = \array_map('strtolower', $databases); $databases = \array_map('strtolower', $databases);
$this->assertEquals(true, in_array('test_create_database', $databases)); $this->assertEquals(true, \in_array('test_create_database', $databases));
} }
public function testListTables() public function testListTables()
@ -37,8 +62,8 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->createTestTable('list_tables_test'); $this->createTestTable('list_tables_test');
$tables = $this->_sm->listTables(); $tables = $this->_sm->listTables();
$tables = \array_change_key_case($tables, CASE_LOWER); $this->assertType('array', $tables);
$tables = \array_map('strtolower', $tables);
$this->assertEquals(true, in_array('list_tables_test', $tables)); $this->assertEquals(true, in_array('list_tables_test', $tables));
} }
@ -83,87 +108,64 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$columns = \array_change_key_case($columns, CASE_LOWER); $columns = \array_change_key_case($columns, CASE_LOWER);
$this->assertArrayHasKey('id', $columns); $this->assertArrayHasKey('id', $columns);
$this->assertEquals('id', strtolower($columns['id']['name'])); $this->assertEquals('id', strtolower($columns['id']->getname()));
$this->assertType('Doctrine\DBAL\Types\IntegerType', $columns['id']['type']); $this->assertType('Doctrine\DBAL\Types\IntegerType', $columns['id']->gettype());
$this->assertEquals(null, $columns['id']['length']); $this->assertEquals(false, $columns['id']->getunsigned());
$this->assertEquals(null, $columns['id']['precision']); $this->assertEquals(true, $columns['id']->getnotnull());
$this->assertEquals(null, $columns['id']['scale']); $this->assertEquals(null, $columns['id']->getdefault());
$this->assertEquals(false, $columns['id']['unsigned']); $this->assertType('array', $columns['id']->getPlatformOptions());
$this->assertEquals(false, $columns['id']['fixed']);
$this->assertEquals(true, $columns['id']['notnull']);
$this->assertEquals(null, $columns['id']['default']);
$this->assertType('array', $columns['id']['platformDetails']);
$this->assertArrayHasKey('test', $columns); $this->assertArrayHasKey('test', $columns);
$this->assertEquals('test', strtolower($columns['test']['name'])); $this->assertEquals('test', strtolower($columns['test']->getname()));
$this->assertType('Doctrine\DBAL\Types\StringType', $columns['test']['type']); $this->assertType('Doctrine\DBAL\Types\StringType', $columns['test']->gettype());
$this->assertEquals(255, $columns['test']['length']); $this->assertEquals(255, $columns['test']->getlength());
$this->assertEquals(null, $columns['test']['precision']); $this->assertEquals(false, $columns['test']->getfixed());
$this->assertEquals(null, $columns['test']['scale']); $this->assertEquals(false, $columns['test']->getnotnull());
$this->assertEquals(false, $columns['test']['unsigned']); $this->assertEquals(null, $columns['test']->getdefault());
$this->assertEquals(false, $columns['test']['fixed']); $this->assertType('array', $columns['test']->getPlatformOptions());
$this->assertEquals(false, $columns['test']['notnull']);
$this->assertEquals(null, $columns['test']['default']);
$this->assertType('array', $columns['test']['platformDetails']);
$this->assertEquals('foo', strtolower($columns['foo']['name'])); $this->assertEquals('foo', strtolower($columns['foo']->getname()));
$this->assertType('Doctrine\DBAL\Types\TextType', $columns['foo']['type']); $this->assertType('Doctrine\DBAL\Types\TextType', $columns['foo']->gettype());
$this->assertEquals(null, $columns['foo']['length']); $this->assertEquals(null, $columns['foo']->getlength());
$this->assertEquals(null, $columns['foo']['precision']); $this->assertEquals(false, $columns['foo']->getunsigned());
$this->assertEquals(null, $columns['foo']['scale']); $this->assertEquals(false, $columns['foo']->getfixed());
$this->assertEquals(false, $columns['foo']['unsigned']); $this->assertEquals(true, $columns['foo']->getnotnull());
$this->assertEquals(false, $columns['foo']['fixed']); $this->assertEquals(null, $columns['foo']->getdefault());
$this->assertEquals(true, $columns['foo']['notnull']); $this->assertType('array', $columns['foo']->getPlatformOptions());
$this->assertEquals(null, $columns['foo']['default']);
$this->assertType('array', $columns['foo']['platformDetails']);
$this->assertEquals('bar', strtolower($columns['bar']['name'])); $this->assertEquals('bar', strtolower($columns['bar']->getname()));
$this->assertType('Doctrine\DBAL\Types\DecimalType', $columns['bar']['type']); $this->assertType('Doctrine\DBAL\Types\DecimalType', $columns['bar']->gettype());
$this->assertEquals(null, $columns['bar']['length']); $this->assertEquals(null, $columns['bar']->getlength());
$this->assertEquals(10, $columns['bar']['precision']); $this->assertEquals(10, $columns['bar']->getprecision());
$this->assertEquals(4, $columns['bar']['scale']); $this->assertEquals(4, $columns['bar']->getscale());
$this->assertEquals(false, $columns['bar']['unsigned']); $this->assertEquals(false, $columns['bar']->getunsigned());
$this->assertEquals(false, $columns['bar']['fixed']); $this->assertEquals(false, $columns['bar']->getfixed());
$this->assertEquals(false, $columns['bar']['notnull']); $this->assertEquals(false, $columns['bar']->getnotnull());
$this->assertEquals(null, $columns['bar']['default']); $this->assertEquals(null, $columns['bar']->getdefault());
$this->assertType('array', $columns['bar']['platformDetails']); $this->assertType('array', $columns['bar']->getPlatformOptions());
$this->assertEquals('baz1', strtolower($columns['baz1']['name'])); $this->assertEquals('baz1', strtolower($columns['baz1']->getname()));
$this->assertType('Doctrine\DBAL\Types\DateTimeType', $columns['baz1']['type']); $this->assertType('Doctrine\DBAL\Types\DateTimeType', $columns['baz1']->gettype());
$this->assertEquals(null, $columns['baz1']['length']); $this->assertEquals(false, $columns['baz1']->getnotnull());
$this->assertEquals(null, $columns['baz1']['precision']); $this->assertEquals(null, $columns['baz1']->getdefault());
$this->assertEquals(null, $columns['baz1']['scale']); $this->assertType('array', $columns['baz1']->getPlatformOptions());
$this->assertEquals(false, $columns['baz1']['unsigned']);
$this->assertEquals(false, $columns['baz1']['fixed']);
$this->assertEquals(false, $columns['baz1']['notnull']);
$this->assertEquals(null, $columns['baz1']['default']);
$this->assertType('array', $columns['baz1']['platformDetails']);
$this->assertEquals('baz2', strtolower($columns['baz2']['name'])); $this->assertEquals('baz2', strtolower($columns['baz2']->getname()));
$this->assertContains($columns['baz2']['type']->getName(), array('Time', 'Date', 'DateTime')); $this->assertContains($columns['baz2']->gettype()->getName(), array('Time', 'Date', 'DateTime'));
$this->assertEquals(null, $columns['baz2']['length']); $this->assertEquals(false, $columns['baz2']->getnotnull());
$this->assertEquals(null, $columns['baz2']['precision']); $this->assertEquals(null, $columns['baz2']->getdefault());
$this->assertEquals(null, $columns['baz2']['scale']); $this->assertType('array', $columns['baz2']->getPlatformOptions());
$this->assertEquals(false, $columns['baz2']['unsigned']);
$this->assertEquals(false, $columns['baz2']['fixed']);
$this->assertEquals(false, $columns['baz2']['notnull']);
$this->assertEquals(null, $columns['baz2']['default']);
$this->assertType('array', $columns['baz2']['platformDetails']);
$this->assertEquals('baz3', strtolower($columns['baz3']['name'])); $this->assertEquals('baz3', strtolower($columns['baz3']->getname()));
$this->assertContains($columns['baz2']['type']->getName(), array('Time', 'Date', 'DateTime')); $this->assertContains($columns['baz2']->gettype()->getName(), array('Time', 'Date', 'DateTime'));
$this->assertEquals(null, $columns['baz3']['length']); $this->assertEquals(false, $columns['baz3']->getnotnull());
$this->assertEquals(null, $columns['baz3']['precision']); $this->assertEquals(null, $columns['baz3']->getdefault());
$this->assertEquals(null, $columns['baz3']['scale']); $this->assertType('array', $columns['baz3']->getPlatformOptions());
$this->assertEquals(false, $columns['baz3']['unsigned']);
$this->assertEquals(false, $columns['baz3']['fixed']);
$this->assertEquals(false, $columns['baz3']['notnull']);
$this->assertEquals(null, $columns['baz3']['default']);
$this->assertType('array', $columns['baz3']['platformDetails']);
} }
public function testListTableIndexes() public function testListTableIndexes()
{ {
$data = array();
$data['options'] = array( $data['options'] = array(
'indexes' => array( 'indexes' => array(
'test_index_name' => array( 'test_index_name' => array(
@ -186,19 +188,19 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->assertEquals(3, count($tableIndexes)); $this->assertEquals(3, count($tableIndexes));
$this->assertEquals(array('id'), $tableIndexes['primary']['columns']); $this->assertEquals(array('id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
$this->assertTrue($tableIndexes['primary']['unique']); $this->assertTrue($tableIndexes['primary']->isUnique());
$this->assertTrue($tableIndexes['primary']['primary']); $this->assertTrue($tableIndexes['primary']->isPrimary());
$this->assertEquals('test_index_name', $tableIndexes['test_index_name']['name']); $this->assertEquals('test_index_name', $tableIndexes['test_index_name']->getName());
$this->assertEquals(array('test'), $tableIndexes['test_index_name']['columns']); $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test_index_name']->getColumns()));
$this->assertTrue($tableIndexes['test_index_name']['unique']); $this->assertTrue($tableIndexes['test_index_name']->isUnique());
$this->assertFalse($tableIndexes['test_index_name']['primary']); $this->assertFalse($tableIndexes['test_index_name']->isPrimary());
$this->assertEquals('test_composite_idx', $tableIndexes['test_composite_idx']['name']); $this->assertEquals('test_composite_idx', $tableIndexes['test_composite_idx']->getName());
$this->assertEquals(array('id', 'test'), $tableIndexes['test_composite_idx']['columns']); $this->assertEquals(array('id', 'test'), array_map('strtolower', $tableIndexes['test_composite_idx']->getColumns()));
$this->assertFalse($tableIndexes['test_composite_idx']['unique']); $this->assertFalse($tableIndexes['test_composite_idx']->isUnique());
$this->assertFalse($tableIndexes['test_composite_idx']['primary']); $this->assertFalse($tableIndexes['test_composite_idx']->isPrimary());
} }
public function testDropAndCreateIndex() public function testDropAndCreateIndex()
@ -214,11 +216,48 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
$this->_sm->dropAndCreateIndex('test_create_index', 'test', $index); $this->_sm->dropAndCreateIndex('test_create_index', 'test', $index);
$tableIndexes = $this->_sm->listTableIndexes('test_create_index'); $tableIndexes = $this->_sm->listTableIndexes('test_create_index');
$this->assertType('array', $tableIndexes);
$this->assertEquals('test', $tableIndexes['test']['name']); $this->assertEquals('test', $tableIndexes['test']->getName());
$this->assertEquals(array('test'), $tableIndexes['test']['columns']); $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test']->getColumns()));
$this->assertTrue($tableIndexes['test']['unique']); $this->assertTrue($tableIndexes['test']->isUnique());
$this->assertFalse($tableIndexes['test']['primary']); $this->assertFalse($tableIndexes['test']->isPrimary());
}
public function testListForeignKeys()
{
if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped('Does not support foreign key constraints.');
}
$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',
);
$this->_sm->createForeignKey('test_create_fk1', $definition);
$fkeys = $this->_sm->listTableForeignKeys('test_create_fk1');
$this->assertEquals(1, count($fkeys));
$this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
$this->assertEquals(array('foreign_key_test'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
$this->assertEquals(array('id'), array_map('strtolower', $fkeys[0]->getForeignColumns()));
$this->assertEquals('test_create_fk2', strtolower($fkeys[0]->getForeignTableName()));
if($fkeys[0]->hasOption('onUpdate')) {
$this->assertEquals('CASCADE', $fkeys[0]->getOption('onUpdate'));
}
if($fkeys[0]->hasOption('onDelete')) {
$this->assertEquals('CASCADE', $fkeys[0]->getOption('onDelete'));
}
} }
protected function getCreateExampleViewSql() protected function getCreateExampleViewSql()

View File

@ -135,6 +135,8 @@ class OptimisticTest extends \Doctrine\Tests\OrmFunctionalTestCase
$q->setParameter('name', 'Testing'); $q->setParameter('name', 'Testing');
$test = $q->getSingleResult(); $test = $q->getSingleResult();
$this->assertType('DateTime', $test->version);
// Manually increment the version datetime column // Manually increment the version datetime column
$format = $this->_em->getConnection()->getDatabasePlatform()->getDateTimeFormatString(); $format = $this->_em->getConnection()->getDatabasePlatform()->getDateTimeFormatString();
$this->_conn->execute('UPDATE optimistic_timestamp SET version = ? WHERE id = ?', array(date($format, strtotime($test->version->format($format)) + 3600), $test->id)); $this->_conn->execute('UPDATE optimistic_timestamp SET version = ? WHERE id = ?', array(date($format, strtotime($test->version->format($format)) + 3600), $test->id));

View File

@ -65,6 +65,8 @@ class MySqlSchemaToolTest extends \Doctrine\Tests\OrmFunctionalTestCase
public function testGetUpdateSchemaSql() public function testGetUpdateSchemaSql()
{ {
$this->markTestSkipped('Update Schema Tool stuff wont be needed anymore soon!');
$classes = array( $classes = array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\SchemaToolEntityA') $this->_em->getClassMetadata(__NAMESPACE__ . '\SchemaToolEntityA')
); );