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.
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>.
22 namespace Doctrine\DBAL\Schema\Visitor;
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;
33 class CreateSchemaSqlCollector implements Visitor
38 private $_createTableQueries = array();
43 private $_createSequenceQueries = array();
48 private $_createFkConstraintQueries = array();
52 * @var \Doctrine\DBAL\Platforms\AbstractPlatform
54 private $_platform = null;
57 * @param AbstractPlatform $platform
59 public function __construct(AbstractPlatform $platform)
61 $this->_platform = $platform;
65 * @param Schema $schema
67 public function acceptSchema(Schema $schema)
73 * Generate DDL Statements to create the accepted table with all its dependencies.
77 public function acceptTable(Table $table)
79 $namespace = $this->getNamespace($table);
81 $this->_createTableQueries[$namespace] = array_merge(
82 $this->_createTableQueries[$namespace],
83 $this->_platform->getCreateTableSQL($table)
87 public function acceptColumn(Table $table, Column $column)
93 * @param Table $localTable
94 * @param ForeignKeyConstraint $fkConstraint
96 public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
98 $namespace = $this->getNamespace($localTable);
100 if ($this->_platform->supportsForeignKeyConstraints()) {
101 $this->_createFkConstraintQueries[$namespace] = array_merge(
102 $this->_createFkConstraintQueries[$namespace],
103 (array) $this->_platform->getCreateForeignKeySQL(
104 $fkConstraint, $localTable
111 * @param Table $table
112 * @param Index $index
114 public function acceptIndex(Table $table, Index $index)
120 * @param Sequence $sequence
122 public function acceptSequence(Sequence $sequence)
124 $namespace = $this->getNamespace($sequence);
126 $this->_createSequenceQueries[$namespace] = array_merge(
127 $this->_createSequenceQueries[$namespace],
128 (array)$this->_platform->getCreateSequenceSQL($sequence)
132 private function getNamespace($asset)
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();
147 public function resetQueries()
149 $this->_createTableQueries = array();
150 $this->_createSequenceQueries = array();
151 $this->_createFkConstraintQueries = array();
155 * Get all queries collected so far.
159 public function getQueries()
162 foreach (array_keys($this->_createTableQueries) as $namespace) {
163 if ($this->_platform->supportsSchemas()) {
164 // TODO: Create Schema here
167 foreach ($this->_createTableQueries as $schemaSql) {
168 $sql = array_merge($sql, $schemaSql);
170 foreach ($this->_createSequenceQueries as $schemaSql) {
171 $sql = array_merge($sql, $schemaSql);
173 foreach ($this->_createFkConstraintQueries as $schemaSql) {
174 $sql = array_merge($sql, $schemaSql);