MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
AbstractSchemaManager.php
Ir para a documentação deste ficheiro.
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
20namespace Doctrine\DBAL\Schema;
21
25
40{
46 protected $_conn;
47
53 protected $_platform;
54
60 public function __construct(\Doctrine\DBAL\Connection $conn)
61 {
62 $this->_conn = $conn;
63 $this->_platform = $this->_conn->getDatabasePlatform();
64 }
65
71 public function getDatabasePlatform()
72 {
73 return $this->_platform;
74 }
75
88 public function tryMethod()
89 {
90 $args = func_get_args();
91 $method = $args[0];
92 unset($args[0]);
93 $args = array_values($args);
94
95 try {
96 return call_user_func_array(array($this, $method), $args);
97 } catch (\Exception $e) {
98 return false;
99 }
100 }
101
107 public function listDatabases()
108 {
109 $sql = $this->_platform->getListDatabasesSQL();
110
111 $databases = $this->_conn->fetchAll($sql);
112
113 return $this->_getPortableDatabasesList($databases);
114 }
115
121 public function listSequences($database = null)
122 {
123 if (is_null($database)) {
124 $database = $this->_conn->getDatabase();
125 }
126 $sql = $this->_platform->getListSequencesSQL($database);
127
128 $sequences = $this->_conn->fetchAll($sql);
129
130 return $this->_getPortableSequencesList($sequences);
131 }
132
146 public function listTableColumns($table)
147 {
148 $sql = $this->_platform->getListTableColumnsSQL($table);
149
150 $tableColumns = $this->_conn->fetchAll($sql);
151
152 return $this->_getPortableTableColumnList($tableColumns);
153 }
154
163 public function listTableIndexes($table)
164 {
165 $sql = $this->_platform->getListTableIndexesSQL($table);
166
167 $tableIndexes = $this->_conn->fetchAll($sql);
168
169 return $this->_getPortableTableIndexesList($tableIndexes, $table);
170 }
171
178 public function tablesExist($tableNames)
179 {
180 $tableNames = array_map('strtolower', (array)$tableNames);
181 return count($tableNames) == count(\array_intersect($tableNames, array_map('strtolower', $this->listTableNames())));
182 }
183
184
190 public function listTableNames()
191 {
192 $sql = $this->_platform->getListTablesSQL();
193
194 $tables = $this->_conn->fetchAll($sql);
195
196 return $this->_getPortableTablesList($tables);
197 }
198
204 public function listTables()
205 {
206 $tableNames = $this->listTableNames();
207
208 $tables = array();
209 foreach ($tableNames AS $tableName) {
210 $tables[] = $this->listTableDetails($tableName);
211 }
212
213 return $tables;
214 }
215
220 public function listTableDetails($tableName)
221 {
222 $columns = $this->listTableColumns($tableName);
223 $foreignKeys = array();
224 if ($this->_platform->supportsForeignKeyConstraints()) {
225 $foreignKeys = $this->listTableForeignKeys($tableName);
226 }
227 $indexes = $this->listTableIndexes($tableName);
228
229 return new Table($tableName, $columns, $indexes, $foreignKeys, false, array());
230 }
231
237 public function listViews()
238 {
239 $database = $this->_conn->getDatabase();
240 $sql = $this->_platform->getListViewsSQL($database);
241 $views = $this->_conn->fetchAll($sql);
242
243 return $this->_getPortableViewsList($views);
244 }
245
252 public function listTableForeignKeys($table, $database = null)
253 {
254 if (is_null($database)) {
255 $database = $this->_conn->getDatabase();
256 }
257 $sql = $this->_platform->getListTableForeignKeysSQL($table, $database);
258 $tableForeignKeys = $this->_conn->fetchAll($sql);
259
260 return $this->_getPortableTableForeignKeysList($tableForeignKeys);
261 }
262
263 /* drop*() Methods */
264
272 public function dropDatabase($database)
273 {
274 $this->_execSql($this->_platform->getDropDatabaseSQL($database));
275 }
276
282 public function dropTable($table)
283 {
284 $this->_execSql($this->_platform->getDropTableSQL($table));
285 }
286
293 public function dropIndex($index, $table)
294 {
295 if($index instanceof Index) {
296 $index = $index->getQuotedName($this->_platform);
297 }
298
299 $this->_execSql($this->_platform->getDropIndexSQL($index, $table));
300 }
301
308 public function dropConstraint(Constraint $constraint, $table)
309 {
310 $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table));
311 }
312
320 public function dropForeignKey($foreignKey, $table)
321 {
322 $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
323 }
324
330 public function dropSequence($name)
331 {
332 $this->_execSql($this->_platform->getDropSequenceSQL($name));
333 }
334
341 public function dropView($name)
342 {
343 $this->_execSql($this->_platform->getDropViewSQL($name));
344 }
345
346 /* create*() Methods */
347
353 public function createDatabase($database)
354 {
355 $this->_execSql($this->_platform->getCreateDatabaseSQL($database));
356 }
357
364 public function createTable(Table $table)
365 {
366 $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS;
367 $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags));
368 }
369
376 public function createSequence($sequence)
377 {
378 $this->_execSql($this->_platform->getCreateSequenceSQL($sequence));
379 }
380
387 public function createConstraint(Constraint $constraint, $table)
388 {
389 $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table));
390 }
391
398 public function createIndex(Index $index, $table)
399 {
400 $this->_execSql($this->_platform->getCreateIndexSQL($index, $table));
401 }
402
409 public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
410 {
411 $this->_execSql($this->_platform->getCreateForeignKeySQL($foreignKey, $table));
412 }
413
419 public function createView(View $view)
420 {
421 $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql()));
422 }
423
424 /* dropAndCreate*() Methods */
425
434 public function dropAndCreateConstraint(Constraint $constraint, $table)
435 {
436 $this->tryMethod('dropConstraint', $constraint, $table);
437 $this->createConstraint($constraint, $table);
438 }
439
446 public function dropAndCreateIndex(Index $index, $table)
447 {
448 $this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table);
449 $this->createIndex($index, $table);
450 }
451
458 public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
459 {
460 $this->tryMethod('dropForeignKey', $foreignKey, $table);
461 $this->createForeignKey($foreignKey, $table);
462 }
463
470 public function dropAndCreateSequence(Sequence $sequence)
471 {
472 $this->tryMethod('createSequence', $seqName, $start, $allocationSize);
473 $this->createSequence($seqName, $start, $allocationSize);
474 }
475
481 public function dropAndCreateTable(Table $table)
482 {
483 $this->tryMethod('dropTable', $table->getQuotedName($this->_platform));
484 $this->createTable($table);
485 }
486
492 public function dropAndCreateDatabase($database)
493 {
494 $this->tryMethod('dropDatabase', $database);
495 $this->createDatabase($database);
496 }
497
503 public function dropAndCreateView(View $view)
504 {
505 $this->tryMethod('dropView', $view->getQuotedName($this->_platform));
506 $this->createView($view);
507 }
508
509 /* alterTable() Methods */
510
516 public function alterTable(TableDiff $tableDiff)
517 {
518 $queries = $this->_platform->getAlterTableSQL($tableDiff);
519 if (is_array($queries) && count($queries)) {
520 foreach ($queries AS $ddlQuery) {
521 $this->_execSql($ddlQuery);
522 }
523 }
524 }
525
532 public function renameTable($name, $newName)
533 {
534 $tableDiff = new TableDiff($name);
535 $tableDiff->newName = $newName;
536 $this->alterTable($tableDiff);
537 }
538
544 protected function _getPortableDatabasesList($databases)
545 {
546 $list = array();
547 foreach ($databases as $key => $value) {
548 if ($value = $this->_getPortableDatabaseDefinition($value)) {
549 $list[] = $value;
550 }
551 }
552 return $list;
553 }
554
555 protected function _getPortableDatabaseDefinition($database)
556 {
557 return $database;
558 }
559
560 protected function _getPortableFunctionsList($functions)
561 {
562 $list = array();
563 foreach ($functions as $key => $value) {
564 if ($value = $this->_getPortableFunctionDefinition($value)) {
565 $list[] = $value;
566 }
567 }
568 return $list;
569 }
570
571 protected function _getPortableFunctionDefinition($function)
572 {
573 return $function;
574 }
575
576 protected function _getPortableTriggersList($triggers)
577 {
578 $list = array();
579 foreach ($triggers as $key => $value) {
580 if ($value = $this->_getPortableTriggerDefinition($value)) {
581 $list[] = $value;
582 }
583 }
584 return $list;
585 }
586
587 protected function _getPortableTriggerDefinition($trigger)
588 {
589 return $trigger;
590 }
591
592 protected function _getPortableSequencesList($sequences)
593 {
594 $list = array();
595 foreach ($sequences as $key => $value) {
596 if ($value = $this->_getPortableSequenceDefinition($value)) {
597 $list[] = $value;
598 }
599 }
600 return $list;
601 }
602
607 protected function _getPortableSequenceDefinition($sequence)
608 {
609 throw DBALException::notSupported('Sequences');
610 }
611
620 protected function _getPortableTableColumnList($tableColumns)
621 {
622 $list = array();
623 foreach ($tableColumns as $key => $column) {
624 if ($column = $this->_getPortableTableColumnDefinition($column)) {
625 $name = strtolower($column->getQuotedName($this->_platform));
626 $list[$name] = $column;
627 }
628 }
629 return $list;
630 }
631
638 abstract protected function _getPortableTableColumnDefinition($tableColumn);
639
647 protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
648 {
649 $result = array();
650 foreach($tableIndexRows AS $tableIndex) {
651 $indexName = $keyName = $tableIndex['key_name'];
652 if($tableIndex['primary']) {
653 $keyName = 'primary';
654 }
655 $keyName = strtolower($keyName);
656
657 if(!isset($result[$keyName])) {
658 $result[$keyName] = array(
659 'name' => $indexName,
660 'columns' => array($tableIndex['column_name']),
661 'unique' => $tableIndex['non_unique'] ? false : true,
662 'primary' => $tableIndex['primary'],
663 );
664 } else {
665 $result[$keyName]['columns'][] = $tableIndex['column_name'];
666 }
667 }
668
669 $indexes = array();
670 foreach($result AS $indexKey => $data) {
671 $indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
672 }
673
674 return $indexes;
675 }
676
677 protected function _getPortableTablesList($tables)
678 {
679 $list = array();
680 foreach ($tables as $key => $value) {
681 if ($value = $this->_getPortableTableDefinition($value)) {
682 $list[] = $value;
683 }
684 }
685 return $list;
686 }
687
688 protected function _getPortableTableDefinition($table)
689 {
690 return $table;
691 }
692
693 protected function _getPortableUsersList($users)
694 {
695 $list = array();
696 foreach ($users as $key => $value) {
697 if ($value = $this->_getPortableUserDefinition($value)) {
698 $list[] = $value;
699 }
700 }
701 return $list;
702 }
703
704 protected function _getPortableUserDefinition($user)
705 {
706 return $user;
707 }
708
709 protected function _getPortableViewsList($views)
710 {
711 $list = array();
712 foreach ($views as $key => $value) {
713 if ($view = $this->_getPortableViewDefinition($value)) {
714 $viewName = strtolower($view->getQuotedName($this->_platform));
715 $list[$viewName] = $view;
716 }
717 }
718 return $list;
719 }
720
721 protected function _getPortableViewDefinition($view)
722 {
723 return false;
724 }
725
726 protected function _getPortableTableForeignKeysList($tableForeignKeys)
727 {
728 $list = array();
729 foreach ($tableForeignKeys as $key => $value) {
730 if ($value = $this->_getPortableTableForeignKeyDefinition($value)) {
731 $list[] = $value;
732 }
733 }
734 return $list;
735 }
736
737 protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
738 {
739 return $tableForeignKey;
740 }
741
742 protected function _execSql($sql)
743 {
744 foreach ((array) $sql as $query) {
745 $this->_conn->executeUpdate($query);
746 }
747 }
748
754 public function createSchema()
755 {
756 $sequences = array();
757 if($this->_platform->supportsSequences()) {
758 $sequences = $this->listSequences();
759 }
760 $tables = $this->listTables();
761
762 return new Schema($tables, $sequences, $this->createSchemaConfig());
763 }
764
770 public function createSchemaConfig()
771 {
772 $schemaConfig = new SchemaConfig();
773 $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());
774
775 return $schemaConfig;
776 }
777}
static notSupported($method)
getQuotedName(AbstractPlatform $platform)
__construct(\Doctrine\DBAL\Connection $conn)
_getPortableTableIndexesList($tableIndexRows, $tableName=null)
dropAndCreateConstraint(Constraint $constraint, $table)
dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
createConstraint(Constraint $constraint, $table)
dropConstraint(Constraint $constraint, $table)
createForeignKey(ForeignKeyConstraint $foreignKey, $table)