Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema / Visitor / CreateSchemaSqlCollector.php
1 <?php
2 /*
3  *  $Id$
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the MIT license. For more information, see
19  * <http://www.doctrine-project.org>.
20  */
21
22 namespace Doctrine\DBAL\Schema\Visitor;
23
24 use Doctrine\DBAL\Platforms\AbstractPlatform,
25     Doctrine\DBAL\Schema\Table,
26     Doctrine\DBAL\Schema\Schema,
27     Doctrine\DBAL\Schema\Column,
28     Doctrine\DBAL\Schema\ForeignKeyConstraint,
29     Doctrine\DBAL\Schema\Constraint,
30     Doctrine\DBAL\Schema\Sequence,
31     Doctrine\DBAL\Schema\Index;
32
33 class CreateSchemaSqlCollector implements Visitor
34 {
35     /**
36      * @var array
37      */
38     private $_createTableQueries = array();
39
40     /**
41      * @var array
42      */
43     private $_createSequenceQueries = array();
44
45     /**
46      * @var array
47      */
48     private $_createFkConstraintQueries = array();
49
50     /**
51      *
52      * @var \Doctrine\DBAL\Platforms\AbstractPlatform
53      */
54     private $_platform = null;
55
56     /**
57      * @param AbstractPlatform $platform
58      */
59     public function __construct(AbstractPlatform $platform)
60     {
61         $this->_platform = $platform;
62     }
63
64     /**
65      * @param Schema $schema
66      */
67     public function acceptSchema(Schema $schema)
68     {
69
70     }
71
72     /**
73      * Generate DDL Statements to create the accepted table with all its dependencies.
74      *
75      * @param Table $table
76      */
77     public function acceptTable(Table $table)
78     {
79         $namespace = $this->getNamespace($table);
80
81         $this->_createTableQueries[$namespace] = array_merge(
82             $this->_createTableQueries[$namespace],
83             $this->_platform->getCreateTableSQL($table)
84         );
85     }
86
87     public function acceptColumn(Table $table, Column $column)
88     {
89
90     }
91
92     /**
93      * @param Table $localTable
94      * @param ForeignKeyConstraint $fkConstraint
95      */
96     public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
97     {
98         $namespace = $this->getNamespace($localTable);
99
100         if ($this->_platform->supportsForeignKeyConstraints()) {
101             $this->_createFkConstraintQueries[$namespace] = array_merge(
102                 $this->_createFkConstraintQueries[$namespace],
103                 (array) $this->_platform->getCreateForeignKeySQL(
104                     $fkConstraint, $localTable
105                 )
106             );
107         }
108     }
109
110     /**
111      * @param Table $table
112      * @param Index $index
113      */
114     public function acceptIndex(Table $table, Index $index)
115     {
116
117     }
118
119     /**
120      * @param Sequence $sequence
121      */
122     public function acceptSequence(Sequence $sequence)
123     {
124         $namespace = $this->getNamespace($sequence);
125
126         $this->_createSequenceQueries[$namespace] = array_merge(
127             $this->_createSequenceQueries[$namespace],
128             (array)$this->_platform->getCreateSequenceSQL($sequence)
129         );
130     }
131
132     private function getNamespace($asset)
133     {
134         $namespace = $asset->getNamespaceName() ?: 'default';
135         if ( !isset($this->_createTableQueries[$namespace])) {
136             $this->_createTableQueries[$namespace] = array();
137             $this->_createSequenceQueries[$namespace] = array();
138             $this->_createFkConstraintQueries[$namespace] = array();
139         }
140
141         return $namespace;
142     }
143
144     /**
145      * @return array
146      */
147     public function resetQueries()
148     {
149         $this->_createTableQueries = array();
150         $this->_createSequenceQueries = array();
151         $this->_createFkConstraintQueries = array();
152     }
153
154     /**
155      * Get all queries collected so far.
156      *
157      * @return array
158      */
159     public function getQueries()
160     {
161         $sql = array();
162         foreach (array_keys($this->_createTableQueries) as $namespace) {
163             if ($this->_platform->supportsSchemas()) {
164                 // TODO: Create Schema here
165             }
166         }
167         foreach ($this->_createTableQueries as $schemaSql) {
168             $sql = array_merge($sql, $schemaSql);
169         }
170         foreach ($this->_createSequenceQueries as $schemaSql) {
171             $sql = array_merge($sql, $schemaSql);
172         }
173         foreach ($this->_createFkConstraintQueries as $schemaSql) {
174             $sql = array_merge($sql, $schemaSql);
175         }
176         return $sql;
177     }
178 }