3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Schema;
22 use Doctrine\DBAL\Schema\Visitor\CreateSchemaSqlCollector;
23 use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
24 use Doctrine\DBAL\Schema\Visitor\Visitor;
27 * Object representation of a database schema
29 * Different vendors have very inconsistent naming with regard to the concept
30 * of a "schema". Doctrine understands a schema as the entity that conceptually
31 * wraps a set of database objects such as tables, sequences, indexes and
32 * foreign keys that belong to each other into a namespace. A Doctrine Schema
33 * has nothing to do with the "SCHEMA" defined as in PostgreSQL, it is more
34 * related to the concept of "DATABASE" that exists in MySQL and PostgreSQL.
36 * Every asset in the doctrine schema has a name. A name consists of either a
37 * namespace.local name pair or just a local unqualified name.
39 * The abstraction layer that covers a PostgreSQL schema is the namespace of an
40 * database object (asset). A schema can have a name, which will be used as
41 * default namespace for the unqualified database objects that are created in
44 * In the case of MySQL where cross-database queries are allowed this leads to
45 * databases being "misinterpreted" as namespaces. This is intentional, however
46 * the CREATE/DROP SQL visitors will just filter this queries and do not
47 * execute them. Only the queries for the currently connected database are
51 * @link www.doctrine-project.org
53 * @author Benjamin Eberlei <kontakt@beberlei.de>
55 class Schema extends AbstractAsset
60 protected $_tables = array();
65 protected $_sequences = array();
70 protected $_schemaConfig = false;
73 * @param array $tables
74 * @param array $sequences
76 * @param array $triggers
77 * @param SchemaConfig $schemaConfig
79 public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
81 if ($schemaConfig == null) {
82 $schemaConfig = new SchemaConfig();
84 $this->_schemaConfig = $schemaConfig;
85 $this->_setName($schemaConfig->getName() ?: 'public');
87 foreach ($tables as $table) {
88 $this->_addTable($table);
91 foreach ($sequences as $sequence) {
92 $this->_addSequence($sequence);
99 public function hasExplicitForeignKeyIndexes()
101 return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
105 * @param Table $table
107 protected function _addTable(Table $table)
109 $tableName = $table->getFullQualifiedName($this->getName());
110 if(isset($this->_tables[$tableName])) {
111 throw SchemaException::tableAlreadyExists($tableName);
114 $this->_tables[$tableName] = $table;
115 $table->setSchemaConfig($this->_schemaConfig);
119 * @param Sequence $sequence
121 protected function _addSequence(Sequence $sequence)
123 $seqName = $sequence->getFullQualifiedName($this->getName());
124 if (isset($this->_sequences[$seqName])) {
125 throw SchemaException::sequenceAlreadyExists($seqName);
127 $this->_sequences[$seqName] = $sequence;
131 * Get all tables of this schema.
135 public function getTables()
137 return $this->_tables;
141 * @param string $tableName
144 public function getTable($tableName)
146 $tableName = $this->getFullQualifiedAssetName($tableName);
147 if (!isset($this->_tables[$tableName])) {
148 throw SchemaException::tableDoesNotExist($tableName);
151 return $this->_tables[$tableName];
157 private function getFullQualifiedAssetName($name)
159 if ($this->isIdentifierQuoted($name)) {
160 $name = $this->trimQuotes($name);
162 if (strpos($name, ".") === false) {
163 $name = $this->getName() . "." . $name;
165 return strtolower($name);
169 * Does this schema have a table with the given name?
171 * @param string $tableName
174 public function hasTable($tableName)
176 $tableName = $this->getFullQualifiedAssetName($tableName);
177 return isset($this->_tables[$tableName]);
181 * Get all table names, prefixed with a schema name, even the default one
186 public function getTableNames()
188 return array_keys($this->_tables);
191 public function hasSequence($sequenceName)
193 $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
194 return isset($this->_sequences[$sequenceName]);
198 * @throws SchemaException
199 * @param string $sequenceName
200 * @return \Doctrine\DBAL\Schema\Sequence
202 public function getSequence($sequenceName)
204 $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
205 if(!$this->hasSequence($sequenceName)) {
206 throw SchemaException::sequenceDoesNotExist($sequenceName);
208 return $this->_sequences[$sequenceName];
212 * @return \Doctrine\DBAL\Schema\Sequence[]
214 public function getSequences()
216 return $this->_sequences;
222 * @param string $tableName
225 public function createTable($tableName)
227 $table = new Table($tableName);
228 $this->_addTable($table);
230 foreach ($this->_schemaConfig->getDefaultTableOptions() as $name => $value) {
231 $table->addOption($name, $value);
240 * @param string $oldTableName
241 * @param string $newTableName
244 public function renameTable($oldTableName, $newTableName)
246 $table = $this->getTable($oldTableName);
247 $table->_setName($newTableName);
249 $this->dropTable($oldTableName);
250 $this->_addTable($table);
255 * Drop a table from the schema.
257 * @param string $tableName
260 public function dropTable($tableName)
262 $tableName = $this->getFullQualifiedAssetName($tableName);
263 $table = $this->getTable($tableName);
264 unset($this->_tables[$tableName]);
269 * Create a new sequence
271 * @param string $sequenceName
272 * @param int $allocationSize
273 * @param int $initialValue
276 public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
278 $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
279 $this->_addSequence($seq);
284 * @param string $sequenceName
287 public function dropSequence($sequenceName)
289 $sequenceName = $this->getFullQualifiedAssetName($sequenceName);
290 unset($this->_sequences[$sequenceName]);
295 * Return an array of necessary sql queries to create the schema on the given platform.
297 * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
300 public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
302 $sqlCollector = new CreateSchemaSqlCollector($platform);
303 $this->visit($sqlCollector);
305 return $sqlCollector->getQueries();
309 * Return an array of necessary sql queries to drop the schema on the given platform.
311 * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
314 public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
316 $dropSqlCollector = new DropSchemaSqlCollector($platform);
317 $this->visit($dropSqlCollector);
319 return $dropSqlCollector->getQueries();
323 * @param Schema $toSchema
324 * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
326 public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
328 $comparator = new Comparator();
329 $schemaDiff = $comparator->compare($this, $toSchema);
330 return $schemaDiff->toSql($platform);
334 * @param Schema $fromSchema
335 * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
337 public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
339 $comparator = new Comparator();
340 $schemaDiff = $comparator->compare($fromSchema, $this);
341 return $schemaDiff->toSql($platform);
345 * @param Visitor $visitor
347 public function visit(Visitor $visitor)
349 $visitor->acceptSchema($this);
351 foreach ($this->_tables as $table) {
352 $table->visit($visitor);
354 foreach ($this->_sequences as $sequence) {
355 $sequence->visit($visitor);
360 * Cloning a Schema triggers a deep clone of all related assets.
364 public function __clone()
366 foreach ($this->_tables as $k => $table) {
367 $this->_tables[$k] = clone $table;
369 foreach ($this->_sequences as $k => $sequence) {
370 $this->_sequences[$k] = clone $sequence;