source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/DBAL/Schema/SchemaDiff.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 5.2 KB
Line 
1<?php
2/*
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.
14 *
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18 */
19
20namespace Doctrine\DBAL\Schema;
21
22use \Doctrine\DBAL\Platforms\AbstractPlatform;
23
24/**
25 * Schema Diff
26 *
27 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
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
31 * @since   2.0
32 * @version $Revision$
33 * @author  Benjamin Eberlei <kontakt@beberlei.de>
34 */
35class SchemaDiff
36{
37    /**
38     * All added tables
39     *
40     * @var array(string=>ezcDbSchemaTable)
41     */
42    public $newTables = array();
43
44    /**
45     * All changed tables
46     *
47     * @var array(string=>ezcDbSchemaTableDiff)
48     */
49    public $changedTables = array();
50
51    /**
52     * All removed tables
53     *
54     * @var array(string=>Table)
55     */
56    public $removedTables = array();
57
58    /**
59     * @var array
60     */
61    public $newSequences = array();
62
63    /**
64     * @var array
65     */
66    public $changedSequences = array();
67
68    /**
69     * @var array
70     */
71    public $removedSequences = array();
72
73    /**
74     * @var array
75     */
76    public $orphanedForeignKeys = array();
77
78    /**
79     * Constructs an SchemaDiff object.
80     *
81     * @param array(string=>Table)      $newTables
82     * @param array(string=>TableDiff)  $changedTables
83     * @param array(string=>bool)       $removedTables
84     */
85    public function __construct($newTables = array(), $changedTables = array(), $removedTables = array())
86    {
87        $this->newTables = $newTables;
88        $this->changedTables = $changedTables;
89        $this->removedTables = $removedTables;
90    }
91
92    /**
93     * The to save sql mode ensures that the following things don't happen:
94     *
95     * 1. Tables are deleted
96     * 2. Sequences are deleted
97     * 3. Foreign Keys which reference tables that would otherwise be deleted.
98     *
99     * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
100     *
101     * @param AbstractPlatform $platform
102     * @return array
103     */
104    public function toSaveSql(AbstractPlatform $platform)
105    {
106        return $this->_toSql($platform, true);
107    }
108
109    /**
110     * @param AbstractPlatform $platform
111     * @return array
112     */
113    public function toSql(AbstractPlatform $platform)
114    {
115        return $this->_toSql($platform, false);
116    }
117
118    /**
119     * @param AbstractPlatform $platform
120     * @param bool $saveMode
121     * @return array
122     */
123    protected function _toSql(AbstractPlatform $platform, $saveMode = false)
124    {
125        $sql = array();
126
127        if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
128            foreach ($this->orphanedForeignKeys AS $orphanedForeignKey) {
129                $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
130            }
131        }
132
133        if ($platform->supportsSequences() == true) {
134            foreach ($this->changedSequences AS $sequence) {
135                $sql[] = $platform->getAlterSequenceSQL($sequence);
136            }
137
138            if ($saveMode === false) {
139                foreach ($this->removedSequences AS $sequence) {
140                    $sql[] = $platform->getDropSequenceSQL($sequence);
141                }
142            }
143
144            foreach ($this->newSequences AS $sequence) {
145                $sql[] = $platform->getCreateSequenceSQL($sequence);
146            }
147        }
148
149        $foreignKeySql = array();
150        foreach ($this->newTables AS $table) {
151            $sql = array_merge(
152                $sql,
153                $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
154            );
155
156            if ($platform->supportsForeignKeyConstraints()) {
157                foreach ($table->getForeignKeys() AS $foreignKey) {
158                    $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
159                }
160            }
161        }
162        $sql = array_merge($sql, $foreignKeySql);
163
164        if ($saveMode === false) {
165            foreach ($this->removedTables AS $table) {
166                $sql[] = $platform->getDropTableSQL($table);
167            }
168        }
169
170        foreach ($this->changedTables AS $tableDiff) {
171            $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
172        }
173
174        return $sql;
175    }
176}
Note: See TracBrowser for help on using the repository browser.