123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?php
- namespace Doctrine\DBAL\Schema;
- use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
- use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
- use Doctrine\DBAL\Schema\Visitor\Visitor;
- class Schema extends AbstractAsset
- {
-
- protected $_tables = array();
-
- protected $_sequences = array();
-
- protected $_schemaConfig = false;
-
- public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
- {
- if ($schemaConfig == null) {
- $schemaConfig = new SchemaConfig();
- }
- $this->_schemaConfig = $schemaConfig;
- $this->_setName($schemaConfig->getName() ?: 'public');
- foreach ($tables AS $table) {
- $this->_addTable($table);
- }
- foreach ($sequences AS $sequence) {
- $this->_addSequence($sequence);
- }
- }
-
- public function hasExplicitForeignKeyIndexes()
- {
- return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
- }
-
- protected function _addTable(Table $table)
- {
- $tableName = $table->getFullQualifiedName($this->getName());
- if(isset($this->_tables[$tableName])) {
- throw SchemaException::tableAlreadyExists($tableName);
- }
- $this->_tables[$tableName] = $table;
- $table->setSchemaConfig($this->_schemaConfig);
- }
-
- protected function _addSequence(Sequence $sequence)
- {
- $seqName = $sequence->getFullQualifiedName($this->getName());
- if (isset($this->_sequences[$seqName])) {
- throw SchemaException::sequenceAlreadyExists($seqName);
- }
- $this->_sequences[$seqName] = $sequence;
- }
-
- public function getTables()
- {
- return $this->_tables;
- }
-
- public function getTable($tableName)
- {
- $tableName = $this->getFullQualifiedAssetName($tableName);
- if (!isset($this->_tables[$tableName])) {
- throw SchemaException::tableDoesNotExist($tableName);
- }
- return $this->_tables[$tableName];
- }
-
- private function getFullQualifiedAssetName($name)
- {
- if ($this->isQuoted($name)) {
- $name = $this->trimQuotes($name);
- }
- if (strpos($name, ".") === false) {
- $name = $this->getName() . "." . $name;
- }
- return strtolower($name);
- }
-
- public function hasTable($tableName)
- {
- $tableName = $this->getFullQualifiedAssetName($tableName);
- return isset($this->_tables[$tableName]);
- }
-
- public function getTableNames()
- {
- return array_keys($this->_tables);
- }
- public function hasSequence($sequenceName)
- {
- $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
- return isset($this->_sequences[$sequenceName]);
- }
-
- public function getSequence($sequenceName)
- {
- $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
- if(!$this->hasSequence($sequenceName)) {
- throw SchemaException::sequenceDoesNotExist($sequenceName);
- }
- return $this->_sequences[$sequenceName];
- }
-
- public function getSequences()
- {
- return $this->_sequences;
- }
-
- public function createTable($tableName)
- {
- $table = new Table($tableName);
- $this->_addTable($table);
- return $table;
- }
-
- public function renameTable($oldTableName, $newTableName)
- {
- $table = $this->getTable($oldTableName);
- $table->_setName($newTableName);
- $this->dropTable($oldTableName);
- $this->_addTable($table);
- return $this;
- }
-
- public function dropTable($tableName)
- {
- $tableName = $this->getFullQualifiedAssetName($tableName);
- $table = $this->getTable($tableName);
- unset($this->_tables[$tableName]);
- return $this;
- }
-
- public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
- {
- $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
- $this->_addSequence($seq);
- return $seq;
- }
-
- public function dropSequence($sequenceName)
- {
- $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
- unset($this->_sequences[$sequenceName]);
- return $this;
- }
-
- public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
- {
- $sqlCollector = new CreateSchemaSqlCollector($platform);
- $this->visit($sqlCollector);
- return $sqlCollector->getQueries();
- }
-
- public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
- {
- $dropSqlCollector = new DropSchemaSqlCollector($platform);
- $this->visit($dropSqlCollector);
- return $dropSqlCollector->getQueries();
- }
-
- public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
- {
- $comparator = new Comparator();
- $schemaDiff = $comparator->compare($this, $toSchema);
- return $schemaDiff->toSql($platform);
- }
-
- public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
- {
- $comparator = new Comparator();
- $schemaDiff = $comparator->compare($fromSchema, $this);
- return $schemaDiff->toSql($platform);
- }
-
- public function visit(Visitor $visitor)
- {
- $visitor->acceptSchema($this);
- foreach ($this->_tables AS $table) {
- $table->visit($visitor);
- }
- foreach ($this->_sequences AS $sequence) {
- $sequence->visit($visitor);
- }
- }
-
- public function __clone()
- {
- foreach ($this->_tables AS $k => $table) {
- $this->_tables[$k] = clone $table;
- }
- foreach ($this->_sequences AS $k => $sequence) {
- $this->_sequences[$k] = clone $sequence;
- }
- }
- }
|