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\Platforms\AbstractPlatform;
28 * @link www.doctrine-project.org
29 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
30 * @license http://ez.no/licenses/new_bsd New BSD License
33 * @author Benjamin Eberlei <kontakt@beberlei.de>
40 * @var array(string=>ezcDbSchemaTable)
42 public $newTables = array();
47 * @var array(string=>ezcDbSchemaTableDiff)
49 public $changedTables = array();
54 * @var array(string=>Table)
56 public $removedTables = array();
61 public $newSequences = array();
66 public $changedSequences = array();
71 public $removedSequences = array();
76 public $orphanedForeignKeys = array();
79 * Constructs an SchemaDiff object.
81 * @param array(string=>Table) $newTables
82 * @param array(string=>TableDiff) $changedTables
83 * @param array(string=>bool) $removedTables
85 public function __construct($newTables = array(), $changedTables = array(), $removedTables = array())
87 $this->newTables = $newTables;
88 $this->changedTables = $changedTables;
89 $this->removedTables = $removedTables;
93 * The to save sql mode ensures that the following things don't happen:
95 * 1. Tables are deleted
96 * 2. Sequences are deleted
97 * 3. Foreign Keys which reference tables that would otherwise be deleted.
99 * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
101 * @param AbstractPlatform $platform
104 public function toSaveSql(AbstractPlatform $platform)
106 return $this->_toSql($platform, true);
110 * @param AbstractPlatform $platform
113 public function toSql(AbstractPlatform $platform)
115 return $this->_toSql($platform, false);
119 * @param AbstractPlatform $platform
120 * @param bool $saveMode
123 protected function _toSql(AbstractPlatform $platform, $saveMode = false)
127 if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
128 foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
129 $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
133 if ($platform->supportsSequences() == true) {
134 foreach ($this->changedSequences as $sequence) {
135 $sql[] = $platform->getAlterSequenceSQL($sequence);
138 if ($saveMode === false) {
139 foreach ($this->removedSequences as $sequence) {
140 $sql[] = $platform->getDropSequenceSQL($sequence);
144 foreach ($this->newSequences as $sequence) {
145 $sql[] = $platform->getCreateSequenceSQL($sequence);
149 $foreignKeySql = array();
150 foreach ($this->newTables as $table) {
153 $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
156 if ($platform->supportsForeignKeyConstraints()) {
157 foreach ($table->getForeignKeys() as $foreignKey) {
158 $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
162 $sql = array_merge($sql, $foreignKeySql);
164 if ($saveMode === false) {
165 foreach ($this->removedTables as $table) {
166 $sql[] = $platform->getDropTableSQL($table);
170 foreach ($this->changedTables as $tableDiff) {
171 $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));