MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
Schema.php
Ir para a documentação deste ficheiro.
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 LGPL. For more information, see
19 * <http://www.doctrine-project.org>.
20 */
21
22namespace Doctrine\DBAL\Schema;
23
27
37class Schema extends AbstractAsset
38{
42 protected $_tables = array();
43
47 protected $_sequences = array();
48
52 protected $_schemaConfig = false;
53
61 public function __construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
62 {
63 if ($schemaConfig == null) {
64 $schemaConfig = new SchemaConfig();
65 }
66 $this->_schemaConfig = $schemaConfig;
67
68 foreach ($tables AS $table) {
69 $this->_addTable($table);
70 }
71 foreach ($sequences AS $sequence) {
72 $this->_addSequence($sequence);
73 }
74 }
75
80 {
81 return $this->_schemaConfig->hasExplicitForeignKeyIndexes();
82 }
83
87 protected function _addTable(Table $table)
88 {
89 $tableName = strtolower($table->getName());
90 if(isset($this->_tables[$tableName])) {
92 }
93
94 $this->_tables[$tableName] = $table;
95 $table->setSchemaConfig($this->_schemaConfig);
96 }
97
101 protected function _addSequence(Sequence $sequence)
102 {
103 $seqName = strtolower($sequence->getName());
104 if (isset($this->_sequences[$seqName])) {
106 }
107 $this->_sequences[$seqName] = $sequence;
108 }
109
115 public function getTables()
116 {
117 return $this->_tables;
118 }
119
124 public function getTable($tableName)
125 {
126 $tableName = strtolower($tableName);
127 if (!isset($this->_tables[$tableName])) {
128 throw SchemaException::tableDoesNotExist($tableName);
129 }
130
131 return $this->_tables[$tableName];
132 }
133
140 public function hasTable($tableName)
141 {
142 $tableName = strtolower($tableName);
143 return isset($this->_tables[$tableName]);
144 }
145
150 public function hasSequence($sequenceName)
151 {
152 $sequenceName = strtolower($sequenceName);
153 return isset($this->_sequences[$sequenceName]);
154 }
155
161 public function getSequence($sequenceName)
162 {
163 $sequenceName = strtolower($sequenceName);
164 if(!$this->hasSequence($sequenceName)) {
165 throw SchemaException::sequenceDoesNotExist($sequenceName);
166 }
167 return $this->_sequences[$sequenceName];
168 }
169
173 public function getSequences()
174 {
175 return $this->_sequences;
176 }
177
184 public function createTable($tableName)
185 {
186 $table = new Table($tableName);
187 $this->_addTable($table);
188 return $table;
189 }
190
198 public function renameTable($oldTableName, $newTableName)
199 {
200 $table = $this->getTable($oldTableName);
201 $table->_setName($newTableName);
202
203 $this->dropTable($oldTableName);
204 $this->_addTable($table);
205 return $this;
206 }
207
214 public function dropTable($tableName)
215 {
216 $tableName = strtolower($tableName);
217 $table = $this->getTable($tableName);
218 unset($this->_tables[$tableName]);
219 return $this;
220 }
221
230 public function createSequence($sequenceName, $allocationSize=1, $initialValue=1)
231 {
232 $seq = new Sequence($sequenceName, $allocationSize, $initialValue);
233 $this->_addSequence($seq);
234 return $seq;
235 }
236
241 public function dropSequence($sequenceName)
242 {
243 $sequenceName = strtolower($sequenceName);
244 unset($this->_sequences[$sequenceName]);
245 return $this;
246 }
247
254 public function toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
255 {
256 $sqlCollector = new CreateSchemaSqlCollector($platform);
257 $this->visit($sqlCollector);
258
259 return $sqlCollector->getQueries();
260 }
261
268 public function toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
269 {
270 $dropSqlCollector = new DropSchemaSqlCollector($platform);
271 $this->visit($dropSqlCollector);
272
273 return $dropSqlCollector->getQueries();
274 }
275
280 public function getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
281 {
282 $comparator = new Comparator();
283 $schemaDiff = $comparator->compare($this, $toSchema);
284 return $schemaDiff->toSql($platform);
285 }
286
291 public function getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
292 {
293 $comparator = new Comparator();
294 $schemaDiff = $comparator->compare($fromSchema, $this);
295 return $schemaDiff->toSql($platform);
296 }
297
301 public function visit(Visitor $visitor)
302 {
303 $visitor->acceptSchema($this);
304
305 foreach ($this->_tables AS $table) {
306 $table->visit($visitor);
307 }
308 foreach ($this->_sequences AS $sequence) {
309 $sequence->visit($visitor);
310 }
311 }
312
318 public function __clone()
319 {
320 foreach ($this->_tables AS $k => $table) {
321 $this->_tables[$k] = clone $table;
322 }
323 foreach ($this->_sequences AS $k => $sequence) {
324 $this->_sequences[$k] = clone $sequence;
325 }
326 }
327}
static sequenceDoesNotExist($sequenceName)
static sequenceAlreadyExists($sequenceName)
toDropSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
Definição Schema.php:268
_addTable(Table $table)
Definição Schema.php:87
getMigrateToSql(Schema $toSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
Definição Schema.php:280
createSequence($sequenceName, $allocationSize=1, $initialValue=1)
Definição Schema.php:230
visit(Visitor $visitor)
Definição Schema.php:301
renameTable($oldTableName, $newTableName)
Definição Schema.php:198
__construct(array $tables=array(), array $sequences=array(), SchemaConfig $schemaConfig=null)
Definição Schema.php:61
hasSequence($sequenceName)
Definição Schema.php:150
toSql(\Doctrine\DBAL\Platforms\AbstractPlatform $platform)
Definição Schema.php:254
getMigrateFromSql(Schema $fromSchema, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
Definição Schema.php:291
dropSequence($sequenceName)
Definição Schema.php:241
getSequence($sequenceName)
Definição Schema.php:161
_addSequence(Sequence $sequence)
Definição Schema.php:101
setSchemaConfig(SchemaConfig $schemaConfig)
Definição Table.php:110