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 | |
---|
20 | namespace Doctrine\DBAL\Schema\Visitor; |
---|
21 | |
---|
22 | use Doctrine\DBAL\Platforms\AbstractPlatform, |
---|
23 | Doctrine\DBAL\Schema\Table, |
---|
24 | Doctrine\DBAL\Schema\Schema, |
---|
25 | Doctrine\DBAL\Schema\Column, |
---|
26 | Doctrine\DBAL\Schema\ForeignKeyConstraint, |
---|
27 | Doctrine\DBAL\Schema\Constraint, |
---|
28 | Doctrine\DBAL\Schema\Sequence, |
---|
29 | Doctrine\DBAL\Schema\Index; |
---|
30 | |
---|
31 | /** |
---|
32 | * Remove assets from a schema that are not in the default namespace. |
---|
33 | * |
---|
34 | * Some databases such as MySQL support cross databases joins, but don't |
---|
35 | * allow to call DDLs to a database from another connected database. |
---|
36 | * Before a schema is serialized into SQL this visitor can cleanup schemas with |
---|
37 | * non default namespaces. |
---|
38 | * |
---|
39 | * This visitor filters all these non-default namespaced tables and sequences |
---|
40 | * and removes them from the SChema instance. |
---|
41 | * |
---|
42 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
43 | * @since 2.2 |
---|
44 | */ |
---|
45 | class RemoveNamespacedAssets implements Visitor |
---|
46 | { |
---|
47 | /** |
---|
48 | * @var Schema |
---|
49 | */ |
---|
50 | private $schema; |
---|
51 | |
---|
52 | /** |
---|
53 | * @param Schema $schema |
---|
54 | */ |
---|
55 | public function acceptSchema(Schema $schema) |
---|
56 | { |
---|
57 | $this->schema = $schema; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * @param Table $table |
---|
62 | */ |
---|
63 | public function acceptTable(Table $table) |
---|
64 | { |
---|
65 | if ( ! $table->isInDefaultNamespace($this->schema->getName()) ) { |
---|
66 | $this->schema->dropTable($table->getName()); |
---|
67 | } |
---|
68 | } |
---|
69 | /** |
---|
70 | * @param Sequence $sequence |
---|
71 | */ |
---|
72 | public function acceptSequence(Sequence $sequence) |
---|
73 | { |
---|
74 | if ( ! $sequence->isInDefaultNamespace($this->schema->getName()) ) { |
---|
75 | $this->schema->dropSequence($sequence->getName()); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * @param Column $column |
---|
81 | */ |
---|
82 | public function acceptColumn(Table $table, Column $column) |
---|
83 | { |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * @param Table $localTable |
---|
88 | * @param ForeignKeyConstraint $fkConstraint |
---|
89 | */ |
---|
90 | public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
---|
91 | { |
---|
92 | // The table may already be deleted in a previous |
---|
93 | // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that |
---|
94 | // point to nowhere. |
---|
95 | if ( ! $this->schema->hasTable($fkConstraint->getForeignTableName())) { |
---|
96 | $localTable->removeForeignKey($fkConstraint->getName()); |
---|
97 | return; |
---|
98 | } |
---|
99 | |
---|
100 | $foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName()); |
---|
101 | if ( ! $foreignTable->isInDefaultNamespace($this->schema->getName()) ) { |
---|
102 | $localTable->removeForeignKey($fkConstraint->getName()); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * @param Table $table |
---|
108 | * @param Index $index |
---|
109 | */ |
---|
110 | public function acceptIndex(Table $table, Index $index) |
---|
111 | { |
---|
112 | } |
---|
113 | } |
---|