~pvigo/+junk/owncloud-14.04

« back to all changes in this revision

Viewing changes to usr/share/owncloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Schema/SchemaDiff.php

  • Committer: Pablo Vigo
  • Date: 2014-12-15 13:36:46 UTC
  • Revision ID: pvigo@xtec.cat-20141215133646-7d6it90e1dbsijc2
2

Show diffs side-by-side

added added

removed removed

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