AbstractSchemaManager.php 22.5 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
17
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
18 19
 */

20
namespace Doctrine\DBAL\Schema;
romanb's avatar
romanb committed
21

22 23 24
use Doctrine\DBAL\Types;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
25

romanb's avatar
romanb committed
26
/**
27
 * Base class for schema managers. Schema managers are used to inspect and/or
28
 * modify the database schema/structure.
romanb's avatar
romanb committed
29 30 31 32
 *
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
33
 * @author      Roman Borschel <roman@code-factory.org>
34
 * @author      Jonathan H. Wage <jonwage@gmail.com>
35
 * @author      Benjamin Eberlei <kontakt@beberlei.de>
romanb's avatar
romanb committed
36 37
 * @since       2.0
 */
38
abstract class AbstractSchemaManager
romanb's avatar
romanb committed
39
{
40 41 42
    /**
     * Holds instance of the Doctrine connection for this schema manager
     *
43
     * @var \Doctrine\DBAL\Connection
44
     */
romanb's avatar
romanb committed
45 46
    protected $_conn;

47 48 49
    /**
     * Holds instance of the database platform used for this schema manager
     *
50
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
51 52 53 54 55 56
     */
    protected $_platform;

    /**
     * Constructor. Accepts the Connection instance to manage the schema for
     *
57
     * @param \Doctrine\DBAL\Connection $conn
58
     */
59
    public function __construct(\Doctrine\DBAL\Connection $conn)
60 61
    {
        $this->_conn = $conn;
62
        $this->_platform = $this->_conn->getDatabasePlatform();
63 64
    }

65 66 67 68 69 70 71 72 73 74
    /**
     * Return associated platform.
     *
     * @return \Doctrine\DBAL\Platform\AbstractPlatform
     */
    public function getDatabasePlatform()
    {
        return $this->_platform;
    }

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    /**
     * Try any method on the schema manager. Normally a method throws an 
     * exception when your DBMS doesn't support it or if an error occurs.
     * This method allows you to try and method on your SchemaManager
     * instance and will return false if it does not work or is not supported.
     *
     * <code>
     * $result = $sm->tryMethod('dropView', 'view_name');
     * </code>
     *
     * @return mixed
     */
    public function tryMethod()
    {
        $args = func_get_args();
        $method = $args[0];
        unset($args[0]);
        $args = array_values($args);

        try {
            return call_user_func_array(array($this, $method), $args);
        } catch (\Exception $e) {
            return false;
        }
    }

romanb's avatar
romanb committed
101
    /**
102
     * List the available databases for this connection
romanb's avatar
romanb committed
103
     *
104
     * @return array $databases
romanb's avatar
romanb committed
105 106 107
     */
    public function listDatabases()
    {
108
        $sql = $this->_platform->getListDatabasesSQL();
109 110 111 112

        $databases = $this->_conn->fetchAll($sql);

        return $this->_getPortableDatabasesList($databases);
romanb's avatar
romanb committed
113 114 115
    }

    /**
116
     * List the available sequences for this connection
romanb's avatar
romanb committed
117
     *
118
     * @return Sequence[]
romanb's avatar
romanb committed
119
     */
120
    public function listSequences($database = null)
romanb's avatar
romanb committed
121
    {
122 123 124
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
125
        $sql = $this->_platform->getListSequencesSQL($database);
126 127 128 129

        $sequences = $this->_conn->fetchAll($sql);

        return $this->_getPortableSequencesList($sequences);
romanb's avatar
romanb committed
130 131 132
    }

    /**
133
     * List the columns for a given table.
romanb's avatar
romanb committed
134
     *
135 136 137 138 139 140 141
     * In contrast to other libraries and to the old version of Doctrine,
     * this column definition does try to contain the 'primary' field for
     * the reason that it is not portable accross different RDBMS. Use
     * {@see listTableIndexes($tableName)} to retrieve the primary key
     * of a table. We're a RDBMS specifies more details these are held
     * in the platformDetails array.
     *
142
     * @param string $table The name of the table.
143
     * @param string $database
144
     * @return Column[]
romanb's avatar
romanb committed
145
     */
146
    public function listTableColumns($table, $database = null)
romanb's avatar
romanb committed
147
    {
148 149 150 151 152
        if (!$database) {
            $database = $this->_conn->getDatabase();
        }

        $sql = $this->_platform->getListTableColumnsSQL($table, $database);
153 154 155 156

        $tableColumns = $this->_conn->fetchAll($sql);

        return $this->_getPortableTableColumnList($tableColumns);
romanb's avatar
romanb committed
157 158 159
    }

    /**
160
     * List the indexes for a given table returning an array of Index instances.
romanb's avatar
romanb committed
161
     *
162 163
     * Keys of the portable indexes list are all lower-cased.
     *
164
     * @param string $table The name of the table
165
     * @return Index[] $tableIndexes
romanb's avatar
romanb committed
166 167 168
     */
    public function listTableIndexes($table)
    {
169
        $sql = $this->_platform->getListTableIndexesSQL($table, $this->_conn->getDatabase());
170 171 172

        $tableIndexes = $this->_conn->fetchAll($sql);

173
        return $this->_getPortableTableIndexesList($tableIndexes, $table);
romanb's avatar
romanb committed
174 175
    }

176 177 178 179 180 181 182 183 184 185 186 187 188
    /**
     * Return true if all the given tables exist.
     * 
     * @param array $tableNames
     * @return bool
     */
    public function tablesExist($tableNames)
    {
        $tableNames = array_map('strtolower', (array)$tableNames);
        return count($tableNames) == count(\array_intersect($tableNames, array_map('strtolower', $this->listTableNames())));
    }


romanb's avatar
romanb committed
189
    /**
190
     * Return a list of all tables in the current database
romanb's avatar
romanb committed
191
     *
192
     * @return array
romanb's avatar
romanb committed
193
     */
194
    public function listTableNames()
romanb's avatar
romanb committed
195
    {
196
        $sql = $this->_platform->getListTablesSQL();
197 198 199

        $tables = $this->_conn->fetchAll($sql);

200 201 202 203 204 205 206 207 208 209 210
        return $this->_getPortableTablesList($tables);
    }

    /**
     * List the tables for this connection
     *
     * @return Table[]
     */
    public function listTables()
    {
        $tableNames = $this->listTableNames();
211

212
        $tables = array();
213
        foreach ($tableNames AS $tableName) {
214
            $tables[] = $this->listTableDetails($tableName);
215 216 217
        }

        return $tables;
romanb's avatar
romanb committed
218 219
    }

220 221 222 223 224 225 226 227 228 229 230 231 232
    /**
     * @param  string $tableName
     * @return Table
     */
    public function listTableDetails($tableName)
    {
        $columns = $this->listTableColumns($tableName);
        $foreignKeys = array();
        if ($this->_platform->supportsForeignKeyConstraints()) {
            $foreignKeys = $this->listTableForeignKeys($tableName);
        }
        $indexes = $this->listTableIndexes($tableName);

233
        return new Table($tableName, $columns, $indexes, $foreignKeys, false, array());
234 235
    }

romanb's avatar
romanb committed
236
    /**
237
     * List the views this connection has
romanb's avatar
romanb committed
238
     *
239
     * @return View[]
romanb's avatar
romanb committed
240
     */
241
    public function listViews()
romanb's avatar
romanb committed
242
    {
243
        $database = $this->_conn->getDatabase();
244
        $sql = $this->_platform->getListViewsSQL($database);
245 246 247
        $views = $this->_conn->fetchAll($sql);

        return $this->_getPortableViewsList($views);
romanb's avatar
romanb committed
248 249
    }

250 251 252 253
    /**
     * List the foreign keys for the given table
     *
     * @param string $table  The name of the table
254
     * @return ForeignKeyConstraint[]
255 256 257 258 259 260
     */
    public function listTableForeignKeys($table, $database = null)
    {
        if (is_null($database)) {
            $database = $this->_conn->getDatabase();
        }
261
        $sql = $this->_platform->getListTableForeignKeysSQL($table, $database);
262 263 264 265 266
        $tableForeignKeys = $this->_conn->fetchAll($sql);

        return $this->_getPortableTableForeignKeysList($tableForeignKeys);
    }

267 268
    /* drop*() Methods */

romanb's avatar
romanb committed
269
    /**
270 271 272
     * Drops a database.
     * 
     * NOTE: You can not drop the database this SchemaManager is currently connected to.
romanb's avatar
romanb committed
273
     *
274
     * @param string $database The name of the database to drop
romanb's avatar
romanb committed
275
     */
276
    public function dropDatabase($database)
romanb's avatar
romanb committed
277
    {
278
        $this->_execSql($this->_platform->getDropDatabaseSQL($database));
romanb's avatar
romanb committed
279 280 281
    }

    /**
282
     * Drop the given table
romanb's avatar
romanb committed
283
     *
284
     * @param string $table The name of the table to drop
romanb's avatar
romanb committed
285 286 287
     */
    public function dropTable($table)
    {
288
        $this->_execSql($this->_platform->getDropTableSQL($table));
romanb's avatar
romanb committed
289 290 291
    }

    /**
292
     * Drop the index from the given table
romanb's avatar
romanb committed
293
     *
294 295
     * @param Index|string $index  The name of the index
     * @param string|Table $table The name of the table
romanb's avatar
romanb committed
296
     */
297
    public function dropIndex($index, $table)
romanb's avatar
romanb committed
298
    {
299
        if($index instanceof Index) {
300
            $index = $index->getQuotedName($this->_platform);
301 302
        }

303
        $this->_execSql($this->_platform->getDropIndexSQL($index, $table));
romanb's avatar
romanb committed
304 305 306
    }

    /**
307
     * Drop the constraint from the given table
romanb's avatar
romanb committed
308
     *
309
     * @param Constraint $constraint
310
     * @param string $table   The name of the table
romanb's avatar
romanb committed
311
     */
312
    public function dropConstraint(Constraint $constraint, $table)
romanb's avatar
romanb committed
313
    {
314
        $this->_execSql($this->_platform->getDropConstraintSQL($constraint, $table));
romanb's avatar
romanb committed
315 316 317
    }

    /**
romanb's avatar
romanb committed
318
     * Drops a foreign key from a table.
romanb's avatar
romanb committed
319
     *
320 321
     * @param ForeignKeyConstraint|string $table The name of the table with the foreign key.
     * @param Table|string $name  The name of the foreign key.
322
     * @return boolean $result
romanb's avatar
romanb committed
323
     */
324
    public function dropForeignKey($foreignKey, $table)
romanb's avatar
romanb committed
325
    {
326
        $this->_execSql($this->_platform->getDropForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
327 328 329
    }

    /**
romanb's avatar
romanb committed
330
     * Drops a sequence with a given name.
romanb's avatar
romanb committed
331
     *
romanb's avatar
romanb committed
332
     * @param string $name The name of the sequence to drop.
romanb's avatar
romanb committed
333
     */
334
    public function dropSequence($name)
romanb's avatar
romanb committed
335
    {
336
        $this->_execSql($this->_platform->getDropSequenceSQL($name));
romanb's avatar
romanb committed
337 338
    }

339 340 341 342 343 344 345 346
    /**
     * Drop a view
     *
     * @param string $name The name of the view
     * @return boolean $result
     */
    public function dropView($name)
    {
347
        $this->_execSql($this->_platform->getDropViewSQL($name));
348 349 350 351
    }

    /* create*() Methods */

romanb's avatar
romanb committed
352
    /**
romanb's avatar
romanb committed
353
     * Creates a new database.
romanb's avatar
romanb committed
354
     *
romanb's avatar
romanb committed
355
     * @param string $database The name of the database to create.
romanb's avatar
romanb committed
356
     */
romanb's avatar
romanb committed
357
    public function createDatabase($database)
romanb's avatar
romanb committed
358
    {
359
        $this->_execSql($this->_platform->getCreateDatabaseSQL($database));
romanb's avatar
romanb committed
360 361 362
    }

    /**
romanb's avatar
romanb committed
363
     * Create a new table.
romanb's avatar
romanb committed
364
     *
365
     * @param Table $table
366
     * @param int $createFlags
romanb's avatar
romanb committed
367
     */
368
    public function createTable(Table $table)
romanb's avatar
romanb committed
369
    {
370
        $createFlags = AbstractPlatform::CREATE_INDEXES|AbstractPlatform::CREATE_FOREIGNKEYS;
371
        $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags));
romanb's avatar
romanb committed
372 373 374
    }

    /**
375
     * Create a new sequence
romanb's avatar
romanb committed
376
     *
377
     * @param Sequence $sequence
378
     * @throws Doctrine\DBAL\ConnectionException     if something fails at database level
romanb's avatar
romanb committed
379
     */
380
    public function createSequence($sequence)
romanb's avatar
romanb committed
381
    {
382
        $this->_execSql($this->_platform->getCreateSequenceSQL($sequence));
romanb's avatar
romanb committed
383 384 385
    }

    /**
386
     * Create a constraint on a table
romanb's avatar
romanb committed
387
     *
388 389 390 391
     * @param Constraint $constraint
     * @param string|Table $table
     */
    public function createConstraint(Constraint $constraint, $table)
romanb's avatar
romanb committed
392
    {
393
        $this->_execSql($this->_platform->getCreateConstraintSQL($constraint, $table));
romanb's avatar
romanb committed
394 395 396
    }

    /**
397
     * Create a new index on a table
romanb's avatar
romanb committed
398
     *
399
     * @param Index     $index
romanb's avatar
romanb committed
400 401
     * @param string    $table         name of the table on which the index is to be created
     */
402
    public function createIndex(Index $index, $table)
romanb's avatar
romanb committed
403
    {
404
        $this->_execSql($this->_platform->getCreateIndexSQL($index, $table));
romanb's avatar
romanb committed
405 406 407
    }

    /**
408
     * Create a new foreign key
romanb's avatar
romanb committed
409
     *
410 411
     * @param ForeignKeyConstraint  $foreignKey    ForeignKey instance
     * @param string|Table          $table         name of the table on which the foreign key is to be created
romanb's avatar
romanb committed
412
     */
413
    public function createForeignKey(ForeignKeyConstraint $foreignKey, $table)
romanb's avatar
romanb committed
414
    {
415
        $this->_execSql($this->_platform->getCreateForeignKeySQL($foreignKey, $table));
romanb's avatar
romanb committed
416 417
    }

418 419 420
    /**
     * Create a new view
     *
421
     * @param View $view
422
     */
423
    public function createView(View $view)
424
    {
425
        $this->_execSql($this->_platform->getCreateViewSQL($view->getQuotedName($this->_platform), $view->getSql()));
426 427
    }

428 429
    /* dropAndCreate*() Methods */

430
    /**
431 432
     * Drop and create a constraint
     *
433 434
     * @param Constraint    $constraint
     * @param string        $table
435 436 437
     * @see dropConstraint()
     * @see createConstraint()
     */
438
    public function dropAndCreateConstraint(Constraint $constraint, $table)
439
    {
440 441
        $this->tryMethod('dropConstraint', $constraint, $table);
        $this->createConstraint($constraint, $table);
442 443 444 445 446
    }

    /**
     * Drop and create a new index on a table
     *
447 448
     * @param string|Table $table         name of the table on which the index is to be created
     * @param Index $index
449
     */
450
    public function dropAndCreateIndex(Index $index, $table)
451
    {
452
        $this->tryMethod('dropIndex', $index->getQuotedName($this->_platform), $table);
453
        $this->createIndex($index, $table);
454 455 456 457 458
    }

    /**
     * Drop and create a new foreign key
     *
459 460
     * @param ForeignKeyConstraint  $foreignKey    associative array that defines properties of the foreign key to be created.
     * @param string|Table          $table         name of the table on which the foreign key is to be created
461
     */
462
    public function dropAndCreateForeignKey(ForeignKeyConstraint $foreignKey, $table)
463
    {
464 465
        $this->tryMethod('dropForeignKey', $foreignKey, $table);
        $this->createForeignKey($foreignKey, $table);
466 467 468 469 470
    }

    /**
     * Drop and create a new sequence
     *
471
     * @param Sequence $sequence
472 473
     * @throws Doctrine\DBAL\ConnectionException     if something fails at database level
     */
474
    public function dropAndCreateSequence(Sequence $sequence)
475
    {
Benjamin Eberlei's avatar
Benjamin Eberlei committed
476 477
        $this->tryMethod('dropSequence', $sequence->getQuotedName($this->_platform));
        $this->createSequence($sequence);
478 479 480 481 482
    }

    /**
     * Drop and create a new table.
     *
483
     * @param Table $table
484
     */
485
    public function dropAndCreateTable(Table $table)
486
    {
487
        $this->tryMethod('dropTable', $table->getQuotedName($this->_platform));
488
        $this->createTable($table);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    }

    /**
     * Drop and creates a new database.
     *
     * @param string $database The name of the database to create.
     */
    public function dropAndCreateDatabase($database)
    {
        $this->tryMethod('dropDatabase', $database);
        $this->createDatabase($database);
    }

    /**
     * Drop and create a new view
504
     *
505
     * @param View $view
506
     */
507
    public function dropAndCreateView(View $view)
508
    {
509
        $this->tryMethod('dropView', $view->getQuotedName($this->_platform));
510
        $this->createView($view);
511 512
    }

513 514
    /* alterTable() Methods */

romanb's avatar
romanb committed
515
    /**
516
     * Alter an existing tables schema
romanb's avatar
romanb committed
517
     *
518 519 520 521
     * @param TableDiff $tableDiff
     */
    public function alterTable(TableDiff $tableDiff)
    {
522
        $queries = $this->_platform->getAlterTableSQL($tableDiff);
523 524 525 526
        if (is_array($queries) && count($queries)) {
            foreach ($queries AS $ddlQuery) {
                $this->_execSql($ddlQuery);
            }
527
        }
528 529
    }

530 531 532 533 534 535 536 537
    /**
     * Rename a given table to another name
     *
     * @param string $name     The current name of the table
     * @param string $newName  The new name of the table
     */
    public function renameTable($name, $newName)
    {
538 539 540
        $tableDiff = new TableDiff($name);
        $tableDiff->newName = $newName;
        $this->alterTable($tableDiff);
541 542
    }

543 544 545 546 547
    /**
     * Methods for filtering return values of list*() methods to convert
     * the native DBMS data definition to a portable Doctrine definition
     */

548 549
    protected function _getPortableDatabasesList($databases)
    {
550
        $list = array();
551
        foreach ($databases as $key => $value) {
552 553 554
            if ($value = $this->_getPortableDatabaseDefinition($value)) {
                $list[] = $value;
            }
555
        }
556
        return $list;
557 558
    }

559 560 561 562 563
    protected function _getPortableDatabaseDefinition($database)
    {
        return $database;
    }

564 565
    protected function _getPortableFunctionsList($functions)
    {
566
        $list = array();
567
        foreach ($functions as $key => $value) {
568 569 570
            if ($value = $this->_getPortableFunctionDefinition($value)) {
                $list[] = $value;
            }
571
        }
572
        return $list;
573 574
    }

575 576 577 578 579
    protected function _getPortableFunctionDefinition($function)
    {
        return $function;
    }

580 581
    protected function _getPortableTriggersList($triggers)
    {
582
        $list = array();
583
        foreach ($triggers as $key => $value) {
584 585 586
            if ($value = $this->_getPortableTriggerDefinition($value)) {
                $list[] = $value;
            }
587
        }
588
        return $list;
589 590
    }

591 592 593 594 595
    protected function _getPortableTriggerDefinition($trigger)
    {
        return $trigger;
    }

596 597
    protected function _getPortableSequencesList($sequences)
    {
598
        $list = array();
599
        foreach ($sequences as $key => $value) {
600 601 602
            if ($value = $this->_getPortableSequenceDefinition($value)) {
                $list[] = $value;
            }
603
        }
604
        return $list;
605 606
    }

607 608 609 610
    /**
     * @param array $sequence
     * @return Sequence
     */
611 612
    protected function _getPortableSequenceDefinition($sequence)
    {
613
        throw DBALException::notSupported('Sequences');
614 615
    }

616 617 618 619 620 621 622 623
    /**
     * Independent of the database the keys of the column list result are lowercased.
     *
     * The name of the created column instance however is kept in its case.
     *
     * @param  array $tableColumns
     * @return array
     */
624 625
    protected function _getPortableTableColumnList($tableColumns)
    {
626
        $list = array();
627 628
        foreach ($tableColumns as $key => $column) {
            if ($column = $this->_getPortableTableColumnDefinition($column)) {
629
                $name = strtolower($column->getQuotedName($this->_platform));
630
                $list[$name] = $column;
631
            }
632
        }
633
        return $list;
634 635
    }

636 637 638 639 640 641 642
    /**
     * Get Table Column Definition
     *
     * @param array $tableColumn
     * @return Column
     */
    abstract protected function _getPortableTableColumnDefinition($tableColumn);
643

644 645 646
    /**
     * Aggregate and group the index results according to the required data result.
     *
647
     * @param  array $tableIndexRows
648 649 650
     * @param  string $tableName
     * @return array
     */
651
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName=null)
652
    {
653
        $result = array();
654
        foreach($tableIndexRows AS $tableIndex) {
655 656 657 658
            $indexName = $keyName = $tableIndex['key_name'];
            if($tableIndex['primary']) {
                $keyName = 'primary';
            }
659
            $keyName = strtolower($keyName);
660 661 662 663 664 665 666 667 668 669

            if(!isset($result[$keyName])) {
                $result[$keyName] = array(
                    'name' => $indexName,
                    'columns' => array($tableIndex['column_name']),
                    'unique' => $tableIndex['non_unique'] ? false : true,
                    'primary' => $tableIndex['primary'],
                );
            } else {
                $result[$keyName]['columns'][] = $tableIndex['column_name'];
670
            }
671
        }
672

673 674 675 676 677 678
        $indexes = array();
        foreach($result AS $indexKey => $data) {
            $indexes[$indexKey] = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']);
        }

        return $indexes;
679 680
    }

681 682
    protected function _getPortableTablesList($tables)
    {
683
        $list = array();
684
        foreach ($tables as $key => $value) {
685 686 687
            if ($value = $this->_getPortableTableDefinition($value)) {
                $list[] = $value;
            }
688
        }
689
        return $list;
690 691
    }

692 693 694 695 696
    protected function _getPortableTableDefinition($table)
    {
        return $table;
    }

697 698
    protected function _getPortableUsersList($users)
    {
699
        $list = array();
700
        foreach ($users as $key => $value) {
701 702 703
            if ($value = $this->_getPortableUserDefinition($value)) {
                $list[] = $value;
            }
704
        }
705
        return $list;
706 707
    }

708 709 710 711 712
    protected function _getPortableUserDefinition($user)
    {
        return $user;
    }

713 714
    protected function _getPortableViewsList($views)
    {
715
        $list = array();
716
        foreach ($views as $key => $value) {
717
            if ($view = $this->_getPortableViewDefinition($value)) {
718
                $viewName = strtolower($view->getQuotedName($this->_platform));
719
                $list[$viewName] = $view;
720
            }
721
        }
722
        return $list;
723 724
    }

725 726
    protected function _getPortableViewDefinition($view)
    {
727
        return false;
728 729
    }

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
    {
        $list = array();
        foreach ($tableForeignKeys as $key => $value) {
            if ($value = $this->_getPortableTableForeignKeyDefinition($value)) {
                $list[] = $value;
            }
        }
        return $list;
    }

    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
    {
        return $tableForeignKey;
    }
745

romanb's avatar
romanb committed
746
    protected function _execSql($sql)
747 748
    {
        foreach ((array) $sql as $query) {
749
            $this->_conn->executeUpdate($query);
romanb's avatar
romanb committed
750 751
        }
    }
752 753 754 755 756 757 758 759 760 761 762 763 764 765

    /**
     * Create a schema instance for the current database.
     * 
     * @return Schema
     */
    public function createSchema()
    {
        $sequences = array();
        if($this->_platform->supportsSequences()) {
            $sequences = $this->listSequences();
        }
        $tables = $this->listTables();

766
        return new Schema($tables, $sequences, $this->createSchemaConfig());
767 768 769 770 771 772 773 774 775 776 777 778 779
    }

    /**
     * Create the configuration for this schema.
     *
     * @return SchemaConfig
     */
    public function createSchemaConfig()
    {
        $schemaConfig = new SchemaConfig();
        $schemaConfig->setMaxIdentifierLength($this->_platform->getMaxIdentifierLength());

        return $schemaConfig;
780
    }
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

    /**
     * Given a table comment this method tries to extract a typehint for Doctrine Type, or returns
     * the type given as default.
     * 
     * @param  string $comment
     * @param  string $currentType
     * @return string
     */
    public function extractDoctrineTypeFromComment($comment, $currentType)
    {
        if (preg_match("(\(DC2Type:([a-zA-Z0-9]+)\))", $comment, $match)) {
            $currentType = $match[1];
        }
        return $currentType;
    }

    public function removeDoctrineTypeFromComment($comment, $type)
    {
        return str_replace('(DC2Type:'.$type.')', '', $comment);
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
802
}