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>
45
* @var array(string=>ezcDbSchemaTable)
47
public $newTables = array();
52
* @var array(string=>ezcDbSchemaTableDiff)
54
public $changedTables = array();
59
* @var array(string=>Table)
61
public $removedTables = array();
66
public $newSequences = array();
71
public $changedSequences = array();
76
public $removedSequences = array();
81
public $orphanedForeignKeys = array();
84
* Constructs an SchemaDiff object.
86
* @param array(string=>Table) $newTables
87
* @param array(string=>TableDiff) $changedTables
88
* @param array(string=>bool) $removedTables
89
* @param Schema $fromSchema
91
public function __construct($newTables = array(), $changedTables = array(), $removedTables = array(), Schema $fromSchema = null)
93
$this->newTables = $newTables;
94
$this->changedTables = $changedTables;
95
$this->removedTables = $removedTables;
96
$this->fromSchema = $fromSchema;
100
* The to save sql mode ensures that the following things don't happen:
102
* 1. Tables are deleted
103
* 2. Sequences are deleted
104
* 3. Foreign Keys which reference tables that would otherwise be deleted.
106
* This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
108
* @param AbstractPlatform $platform
111
public function toSaveSql(AbstractPlatform $platform)
113
return $this->_toSql($platform, true);
117
* @param AbstractPlatform $platform
120
public function toSql(AbstractPlatform $platform)
122
return $this->_toSql($platform, false);
126
* @param AbstractPlatform $platform
127
* @param bool $saveMode
130
protected function _toSql(AbstractPlatform $platform, $saveMode = false)
134
if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
135
foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
136
$sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
140
if ($platform->supportsSequences() == true) {
141
foreach ($this->changedSequences as $sequence) {
142
$sql[] = $platform->getAlterSequenceSQL($sequence);
145
if ($saveMode === false) {
146
foreach ($this->removedSequences as $sequence) {
147
$sql[] = $platform->getDropSequenceSQL($sequence);
151
foreach ($this->newSequences as $sequence) {
152
$sql[] = $platform->getCreateSequenceSQL($sequence);
156
$foreignKeySql = array();
157
foreach ($this->newTables as $table) {
160
$platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
163
if ($platform->supportsForeignKeyConstraints()) {
164
foreach ($table->getForeignKeys() as $foreignKey) {
165
$foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
169
$sql = array_merge($sql, $foreignKeySql);
171
if ($saveMode === false) {
172
foreach ($this->removedTables as $table) {
173
$sql[] = $platform->getDropTableSQL($table);
177
foreach ($this->changedTables as $tableDiff) {
178
$sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));