From e3314dae16178c4542be3e1ac48f4d3b9b94d531 Mon Sep 17 00:00:00 2001 From: beberlei Date: Mon, 30 Nov 2009 23:12:26 +0000 Subject: [PATCH] [2.0] DC-169 - New method AbstractSchemaManager::createSchema() which creates a schema instance with full tables and sequences, additionally listTables() was rewritten to return a full Table instance instead of just the table names. --- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 2 +- .../DBAL/Schema/AbstractSchemaManager.php | 45 +++++++++++++++++-- .../DBAL/Schema/MySqlSchemaManager.php | 2 +- .../DBAL/Schema/PostgreSqlSchemaManager.php | 19 +++++--- lib/Doctrine/DBAL/Schema/Schema.php | 10 ++++- .../DBAL/Schema/SqliteSchemaManager.php | 2 +- lib/Doctrine/DBAL/Schema/Table.php | 7 ++- .../Schema/OracleSchemaManagerTest.php | 3 +- .../SchemaManagerFunctionalTestCase.php | 36 ++++++++++++++- 9 files changed, 107 insertions(+), 19 deletions(-) diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index ebaa7ccd0..7f224c6d0 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -367,7 +367,7 @@ class MySqlPlatform extends AbstractPlatform public function getListTablesSql() { - return 'SHOW TABLES'; + return 'SHOW FULL TABLES WHERE Table_type = "BASE TABLE"'; } public function getListTableColumnsSql($table) diff --git a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php index 4fd657331..6ce6abff9 100644 --- a/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/AbstractSchemaManager.php @@ -43,14 +43,14 @@ abstract class AbstractSchemaManager /** * Holds instance of the Doctrine connection for this schema manager * - * @var object \Doctrine\DBAL\Connection + * @var \Doctrine\DBAL\Connection */ protected $_conn; /** * Holds instance of the database platform used for this schema manager * - * @var string + * @var \Doctrine\DBAL\Platform\AbstractPlatform */ protected $_platform; @@ -228,7 +228,7 @@ abstract class AbstractSchemaManager /** * List the tables for this connection * - * @return array $tables + * @return array(int => Table) */ public function listTables() { @@ -236,7 +236,28 @@ abstract class AbstractSchemaManager $tables = $this->_conn->fetchAll($sql); - return $this->_getPortableTablesList($tables); + $tableNames = $this->_getPortableTablesList($tables); + $tables = array(); + + foreach ($tableNames AS $tableName) { + $columns = $this->listTableColumns($tableName); + $foreignKeys = array(); + if ($this->_platform->supportsForeignKeyConstraints()) { + $foreignKeys = $this->listTableForeignKeys($tableName); + } + $indexes = $this->listTableIndexes($tableName); + + $idGeneratorType = Table::ID_NONE; + foreach ($columns AS $column) { + if ($column->hasPlatformOption('autoincrement') && $column->getPlatformOption('autoincrement')) { + $idGeneratorType = Table::ID_IDENTITY; + } + } + + $tables[] = new Table($tableName, $columns, $indexes, $foreignKeys, $idGeneratorType, array()); + } + + return $tables; } /** @@ -1028,4 +1049,20 @@ abstract class AbstractSchemaManager $this->_conn->executeUpdate($query); } } + + /** + * Create a schema instance for the current database. + * + * @return Schema + */ + public function createSchema() + { + $sequences = array(); + if($this->_platform->supportsSequences()) { + $sequences = $this->listSequences(); + } + $tables = $this->listTables(); + + return new Schema($tables, $sequences); + } } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php b/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php index 05dd5cd0f..c8f8e441f 100644 --- a/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/MySqlSchemaManager.php @@ -44,7 +44,7 @@ class MySqlSchemaManager extends AbstractSchemaManager protected function _getPortableTableDefinition($table) { - return end($table); + return array_shift($table); } protected function _getPortableUserDefinition($user) diff --git a/lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php b/lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php index 9ce1ef7ec..a36f811d5 100644 --- a/lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php @@ -201,25 +201,30 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager $scale = null; $dbType = strtolower($tableColumn['type']); - + + $autoincrement = false; switch ($dbType) { case 'smallint': case 'int2': $type = 'smallint'; $length = null; break; + case 'serial': + case 'serial4': + $autoincrement = true; + // break missing intentionally case 'int': case 'int4': case 'integer': - case 'serial': - case 'serial4': $type = 'integer'; $length = null; break; - case 'bigint': - case 'int8': case 'bigserial': case 'serial8': + $autoincrement = true; + // break missing intentionally + case 'bigint': + case 'int8': $type = 'bigint'; $length = null; break; @@ -320,7 +325,9 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager 'scale' => $scale, 'fixed' => $fixed, 'unsigned' => false, - 'platformDetails' => array(), + 'platformDetails' => array( + 'autoincrement' => $autoincrement, + ), ); return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options); diff --git a/lib/Doctrine/DBAL/Schema/Schema.php b/lib/Doctrine/DBAL/Schema/Schema.php index 541c0fb32..e1b90a3ff 100644 --- a/lib/Doctrine/DBAL/Schema/Schema.php +++ b/lib/Doctrine/DBAL/Schema/Schema.php @@ -65,7 +65,7 @@ class Schema extends AbstractAsset */ protected function _addTable(Table $table) { - $tableName = $table->getName(); + $tableName = strtolower($table->getName()); if(isset($this->_tables[$tableName])) { throw SchemaException::tableAlreadyExists($tableName); } @@ -78,7 +78,7 @@ class Schema extends AbstractAsset */ protected function _addSequence(Sequence $sequence) { - $seqName = $sequence->getName(); + $seqName = strtolower($sequence->getName()); if (isset($this->_sequences[$seqName])) { throw SchemaException::sequenceAlreadyExists($seqName); } @@ -101,6 +101,7 @@ class Schema extends AbstractAsset */ public function getTable($tableName) { + $tableName = strtolower($tableName); if (!isset($this->_tables[$tableName])) { throw SchemaException::tableDoesNotExist($tableName); } @@ -116,6 +117,7 @@ class Schema extends AbstractAsset */ public function hasTable($tableName) { + $tableName = strtolower($tableName); return isset($this->_tables[$tableName]); } @@ -125,6 +127,7 @@ class Schema extends AbstractAsset */ public function hasSequence($sequenceName) { + $sequenceName = strtolower($sequenceName); return isset($this->_sequences[$sequenceName]); } @@ -135,6 +138,7 @@ class Schema extends AbstractAsset */ public function getSequence($sequenceName) { + $sequenceName = strtolower($sequenceName); if(!$this->hasSequence($sequenceName)) { throw SchemaException::sequenceDoesNotExist($sequenceName); } @@ -187,6 +191,7 @@ class Schema extends AbstractAsset */ public function dropTable($tableName) { + $tableName = strtolower($tableName); $table = $this->getTable($tableName); unset($this->_tables[$tableName]); return $this; @@ -213,6 +218,7 @@ class Schema extends AbstractAsset */ public function dropSequence($sequenceName) { + $sequenceName = strtolower($sequenceName); unset($this->_sequences[$sequenceName]); return $this; } diff --git a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php index 659b6161e..ca76a68f6 100644 --- a/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/SqliteSchemaManager.php @@ -251,7 +251,7 @@ class SqliteSchemaManager extends AbstractSchemaManager 'precision' => $precision, 'scale' => $scale, 'platformDetails' => array( - 'primary' => (bool) $tableColumn['pk'], + 'autoincrement' => (bool) $tableColumn['pk'], ), ); diff --git a/lib/Doctrine/DBAL/Schema/Table.php b/lib/Doctrine/DBAL/Schema/Table.php index fd2a8219b..ce3e63628 100644 --- a/lib/Doctrine/DBAL/Schema/Table.php +++ b/lib/Doctrine/DBAL/Schema/Table.php @@ -215,6 +215,7 @@ class Table extends AbstractAsset */ public function renameColumn($oldColumnName, $newColumnName) { + $columnName = strtolower($columnName); $column = $this->getColumn($oldColumnName); $this->dropColumn($oldColumnName); @@ -231,6 +232,7 @@ class Table extends AbstractAsset */ public function changeColumn($columnName, array $options) { + $columnName = strtolower($columnName); $column = $this->getColumn($columnName); $column->setOptions($options); return $this; @@ -244,6 +246,7 @@ class Table extends AbstractAsset */ public function dropColumn($columnName) { + $columnName = strtolower($columnName); $column = $this->getColumn($columnName); unset($this->_columns[$columnName]); return $this; @@ -320,7 +323,7 @@ class Table extends AbstractAsset */ protected function _addColumn(Column $column) { - $columnName = $column->getName(); + $columnName = strtolower($column->getName()); if (isset($this->_columns[$columnName])) { throw SchemaException::columnAlreadyExists($this->_name, $columnName); } @@ -415,6 +418,7 @@ class Table extends AbstractAsset */ public function hasColumn($columnName) { + $columnName = strtolower($columnName); return isset($this->_columns[$columnName]); } @@ -426,6 +430,7 @@ class Table extends AbstractAsset */ public function getColumn($columnName) { + $columnName = strtolower($columnName); if (!$this->hasColumn($columnName)) { throw SchemaException::columnDoesNotExist($columnName); } diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php index fa98909ef..4ebb1e328 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/OracleSchemaManagerTest.php @@ -73,6 +73,7 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase $this->_sm->renameTable('list_tables_test', 'list_tables_test_new_name'); $tables = $this->_sm->listTables(); - $this->assertEquals(true, in_array('LIST_TABLES_TEST_NEW_NAME', $tables)); + + $this->assertHasTable($tables, 'list_tables_test_new_name'); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php index 5ffff628a..5c0af4ee6 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -63,8 +63,19 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest $tables = $this->_sm->listTables(); $this->assertType('array', $tables); - $tables = \array_map('strtolower', $tables); - $this->assertEquals(true, in_array('list_tables_test', $tables)); + $this->assertTrue(count($tables) > 0); + + $foundTable = false; + foreach ($tables AS $table) { + $this->assertType('Doctrine\DBAL\Schema\Table', $table); + if (strtolower($table->getName()) == 'list_tables_test') { + $foundTable = true; + + $this->assertTrue($table->hasColumn('id')); + $this->assertTrue($table->hasColumn('test')); + $this->assertTrue($table->hasColumn('foreign_key_test')); + } + } } public function testListTableColumns() @@ -287,6 +298,15 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest $this->assertTrue($found, "'test_create_view' View was not found in listViews()."); } + public function testCreateSchema() + { + $this->createTestTable('test_table'); + + $schema = $this->_sm->createSchema(); + + $this->assertTrue($schema->hasTable('test_table')); + } + /** * @var \Doctrine\DBAL\Schema\AbstractSchemaManager */ @@ -338,4 +358,16 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest $this->_sm->dropAndCreateTable($name, $columns, $options); } + + protected function assertHasTable($tables, $tableName) + { + $foundTable = false; + foreach ($tables AS $table) { + $this->assertType('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.'); + if (strtolower($table->getName()) == 'list_tables_test_new_name') { + $foundTable = true; + } + } + $this->assertTrue($foundTable, "Could not find new table"); + } } \ No newline at end of file