Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema / Synchronizer / SingleDatabaseSynchronizer.php
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 namespace Doctrine\DBAL\Schema\Synchronizer;
20
21 use Doctrine\DBAL\Connection;
22 use Doctrine\DBAL\Schema\Schema;
23 use Doctrine\DBAL\Schema\Comparator;
24 use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
25
26 /**
27  * Schema Synchronizer for Default DBAL Connection
28  *
29  * @author Benjamin Eberlei <kontakt@beberlei.de>
30  */
31 class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
32 {
33     /**
34      * @var Doctrine\DBAL\Platforms\AbstractPlatform
35      */
36     private $platform;
37
38     public function __construct(Connection $conn)
39     {
40         parent::__construct($conn);
41         $this->platform = $conn->getDatabasePlatform();
42     }
43
44     /**
45      * Get the SQL statements that can be executed to create the schema.
46      *
47      * @param Schema $createSchema
48      * @return array
49      */
50     public function getCreateSchema(Schema $createSchema)
51     {
52         return $createSchema->toSql($this->platform);
53     }
54
55     /**
56      * Get the SQL Statements to update given schema with the underlying db.
57      *
58      * @param Schema $toSchema
59      * @param bool $noDrops
60      * @return array
61      */
62     public function getUpdateSchema(Schema $toSchema, $noDrops = false)
63     {
64         $comparator = new Comparator();
65         $sm         = $this->conn->getSchemaManager();
66
67         $fromSchema = $sm->createSchema();
68         $schemaDiff = $comparator->compare($fromSchema, $toSchema);
69
70         if ($noDrops) {
71             return $schemaDiff->toSaveSql($this->platform);
72         }
73
74         return $schemaDiff->toSql($this->platform);
75     }
76
77     /**
78      * Get the SQL Statements to drop the given schema from underlying db.
79      *
80      * @param Schema $dropSchema
81      * @return array
82      */
83     public function getDropSchema(Schema $dropSchema)
84     {
85         $visitor    = new DropSchemaSqlCollector($this->platform);
86         $sm         = $this->conn->getSchemaManager();
87
88         $fullSchema = $sm->createSchema();
89
90         foreach ($fullSchema->getTables() as $table) {
91             if ( $dropSchema->hasTable($table->getName())) {
92                 $visitor->acceptTable($table);
93             }
94
95             foreach ($table->getForeignKeys() as $foreignKey) {
96                 if ( ! $dropSchema->hasTable($table->getName())) {
97                     continue;
98                 }
99
100                 if ( ! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
101                     continue;
102                 }
103
104                 $visitor->acceptForeignKey($table, $foreignKey);
105             }
106         }
107
108         if ( ! $this->platform->supportsSequences()) {
109             return $visitor->getQueries();
110         }
111
112         foreach ($dropSchema->getSequences() as $sequence) {
113             $visitor->acceptSequence($sequence);
114         }
115
116         foreach ($dropSchema->getTables() as $table) {
117             /* @var $sequence Table */
118             if ( ! $table->hasPrimaryKey()) {
119                 continue;
120             }
121
122             $columns = $table->getPrimaryKey()->getColumns();
123             if (count($columns) > 1) {
124                 continue;
125             }
126
127             $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
128             if ($fullSchema->hasSequence($checkSequence)) {
129                 $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
130             }
131         }
132
133         return $visitor->getQueries();
134     }
135
136     /**
137      * Get the SQL statements to drop all schema assets from underlying db.
138      *
139      * @return array
140      */
141     public function getDropAllSchema()
142     {
143         $sm      = $this->conn->getSchemaManager();
144         $visitor = new DropSchemaSqlCollector($this->platform);
145
146         /* @var $schema \Doctrine\DBAL\Schema\Schema */
147         $schema  = $sm->createSchema();
148         $schema->visit($visitor);
149
150         return $visitor->getQueries();
151     }
152
153     /**
154      * Create the Schema
155      *
156      * @param Schema $createSchema
157      * @return void
158      */
159     public function createSchema(Schema $createSchema)
160     {
161         $this->processSql($this->getCreateSchema($createSchema));
162     }
163
164     /**
165      * Update the Schema to new schema version.
166      *
167      * @param Schema $toSchema
168      * @param bool $noDrops
169      * @return void
170      */
171     public function updateSchema(Schema $toSchema, $noDrops = false)
172     {
173         $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
174     }
175
176     /**
177      * Drop the given database schema from the underlying db.
178      *
179      * @param Schema $dropSchema
180      * @return void
181      */
182     public function dropSchema(Schema $dropSchema)
183     {
184         $this->processSqlSafely($this->getDropSchema($dropSchema));
185     }
186
187     /**
188      * Drop all assets from the underyling db.
189      *
190      * @return void
191      */
192     public function dropAllSchema()
193     {
194         $this->processSql($this->getDropAllSchema());
195     }
196 }
197