AbstractPlatform.php 101 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18 19
 */

20 21
namespace Doctrine\DBAL\Platforms;

Benjamin Morel's avatar
Benjamin Morel committed
22 23
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Connection;
24
use Doctrine\DBAL\Schema\Identifier;
Benjamin Morel's avatar
Benjamin Morel committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
use Doctrine\DBAL\Types;
use Doctrine\DBAL\Schema\Constraint;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Events;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
45 46 47 48 49 50

/**
 * Base class for all DatabasePlatforms. The DatabasePlatforms are the central
 * point of abstraction of platform-specific behaviors, features and SQL dialects.
 * They are a passive source of information.
 *
Benjamin Morel's avatar
Benjamin Morel committed
51 52 53 54 55 56 57 58
 * @link   www.doctrine-project.org
 * @since  2.0
 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author Jonathan Wage <jonwage@gmail.com>
 * @author Roman Borschel <roman@code-factory.org>
 * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
 * @author Benjamin Eberlei <kontakt@beberlei.de>
 * @todo   Remove any unnecessary methods.
59
 */
60
abstract class AbstractPlatform
61
{
62
    /**
63
     * @var integer
64 65 66 67
     */
    const CREATE_INDEXES = 1;

    /**
68
     * @var integer
69 70 71
     */
    const CREATE_FOREIGNKEYS = 2;

72 73 74 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 101 102 103 104 105 106 107 108 109 110 111
    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_SECOND = 'SECOND';

    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_MINUTE = 'MINUTE';

    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_HOUR = 'HOUR';

    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_DAY = 'DAY';

    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_WEEK = 'WEEK';

    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_MONTH = 'MONTH';

    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_QUARTER = 'QUARTER';

    /**
     * @var string
     */
    const DATE_INTERVAL_UNIT_YEAR = 'YEAR';

112
    /**
113
     * @var integer
114 115 116 117
     */
    const TRIM_UNSPECIFIED = 0;

    /**
118
     * @var integer
119 120 121 122
     */
    const TRIM_LEADING = 1;

    /**
123
     * @var integer
124 125 126 127
     */
    const TRIM_TRAILING = 2;

    /**
128
     * @var integer
129 130 131
     */
    const TRIM_BOTH = 3;

132
    /**
Benjamin Morel's avatar
Benjamin Morel committed
133
     * @var array|null
134 135 136
     */
    protected $doctrineTypeMapping = null;

137 138 139 140
    /**
     * Contains a list of all columns that should generate parseable column comments for type-detection
     * in reverse engineering scenarios.
     *
Benjamin Morel's avatar
Benjamin Morel committed
141
     * @var array|null
142 143 144
     */
    protected $doctrineTypeComments = null;

145
    /**
Konstantin Kuklin's avatar
Konstantin Kuklin committed
146
     * @var \Doctrine\Common\EventManager
147 148 149
     */
    protected $_eventManager;

150 151 152
    /**
     * Holds the KeywordList instance for the current platform.
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
153
     * @var \Doctrine\DBAL\Platforms\Keywords\KeywordList
154 155 156
     */
    protected $_keywords;

157 158 159
    /**
     * Constructor.
     */
Benjamin Morel's avatar
Benjamin Morel committed
160 161 162
    public function __construct()
    {
    }
163

164 165 166
    /**
     * Sets the EventManager used by the Platform.
     *
167
     * @param \Doctrine\Common\EventManager $eventManager
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
     */
    public function setEventManager(EventManager $eventManager)
    {
        $this->_eventManager = $eventManager;
    }

    /**
     * Gets the EventManager used by the Platform.
     *
     * @return \Doctrine\Common\EventManager
     */
    public function getEventManager()
    {
        return $this->_eventManager;
    }

184
    /**
Benjamin Morel's avatar
Benjamin Morel committed
185
     * Returns the SQL snippet that declares a boolean column.
186 187
     *
     * @param array $columnDef
188
     *
189 190 191 192 193
     * @return string
     */
    abstract public function getBooleanTypeDeclarationSQL(array $columnDef);

    /**
Benjamin Morel's avatar
Benjamin Morel committed
194
     * Returns the SQL snippet that declares a 4 byte integer column.
195 196
     *
     * @param array $columnDef
197
     *
198 199 200 201 202
     * @return string
     */
    abstract public function getIntegerTypeDeclarationSQL(array $columnDef);

    /**
Benjamin Morel's avatar
Benjamin Morel committed
203
     * Returns the SQL snippet that declares an 8 byte integer column.
204 205
     *
     * @param array $columnDef
206
     *
207 208 209 210 211
     * @return string
     */
    abstract public function getBigIntTypeDeclarationSQL(array $columnDef);

    /**
Benjamin Morel's avatar
Benjamin Morel committed
212
     * Returns the SQL snippet that declares a 2 byte integer column.
213 214
     *
     * @param array $columnDef
215
     *
216 217 218 219 220
     * @return string
     */
    abstract public function getSmallIntTypeDeclarationSQL(array $columnDef);

    /**
Benjamin Morel's avatar
Benjamin Morel committed
221
     * Returns the SQL snippet that declares common properties of an integer column.
222 223
     *
     * @param array $columnDef
Benjamin Morel's avatar
Benjamin Morel committed
224
     *
225 226 227 228 229
     * @return string
     */
    abstract protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef);

    /**
Benjamin Morel's avatar
Benjamin Morel committed
230
     * Lazy load Doctrine Type Mappings.
231 232 233 234 235
     *
     * @return void
     */
    abstract protected function initializeDoctrineTypeMappings();

236
    /**
Benjamin Morel's avatar
Benjamin Morel committed
237
     * Initializes Doctrine Type Mappings with the platform defaults
238
     * and with all additional type mappings.
Benjamin Morel's avatar
Benjamin Morel committed
239 240
     *
     * @return void
241 242 243 244 245 246 247 248 249 250 251 252
     */
    private function initializeAllDoctrineTypeMappings()
    {
        $this->initializeDoctrineTypeMappings();

        foreach (Type::getTypesMap() as $typeName => $className) {
            foreach (Type::getType($typeName)->getMappedDatabaseTypes($this) as $dbType) {
                $this->doctrineTypeMapping[$dbType] = $typeName;
            }
        }
    }

253
    /**
Benjamin Morel's avatar
Benjamin Morel committed
254
     * Returns the SQL snippet used to declare a VARCHAR column type.
255 256
     *
     * @param array $field
257
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
258
     * @return string
259
     */
260 261 262 263 264 265 266 267 268 269 270
    public function getVarcharTypeDeclarationSQL(array $field)
    {
        if ( !isset($field['length'])) {
            $field['length'] = $this->getVarcharDefaultLength();
        }

        $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;

        if ($field['length'] > $this->getVarcharMaxLength()) {
            return $this->getClobTypeDeclarationSQL($field);
        }
271 272

        return $this->getVarcharTypeDeclarationSQLSnippet($field['length'], $fixed);
273 274
    }

Steve Müller's avatar
Steve Müller committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    /**
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
     *
     * @param array $field The column definition.
     *
     * @return string
     */
    public function getBinaryTypeDeclarationSQL(array $field)
    {
        if ( ! isset($field['length'])) {
            $field['length'] = $this->getBinaryDefaultLength();
        }

        $fixed = isset($field['fixed']) ? $field['fixed'] : false;

        if ($field['length'] > $this->getBinaryMaxLength()) {
            return $this->getBlobTypeDeclarationSQL($field);
        }

        return $this->getBinaryTypeDeclarationSQLSnippet($field['length'], $fixed);
    }

297
    /**
Benjamin Morel's avatar
Benjamin Morel committed
298
     * Returns the SQL snippet to declare a GUID/UUID field.
299
     *
300
     * By default this maps directly to a CHAR(36) and only maps to more
301 302 303
     * special datatypes when the underlying databases support this datatype.
     *
     * @param array $field
304
     *
305 306
     * @return string
     */
307
    public function getGuidTypeDeclarationSQL(array $field)
308
    {
309 310 311
        $field['length'] = 36;
        $field['fixed']  = true;

312 313 314
        return $this->getVarcharTypeDeclarationSQL($field);
    }

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    /**
     * Returns the SQL snippet to declare a JSON field.
     *
     * By default this maps directly to a CLOB and only maps to more
     * special datatypes when the underlying databases support this datatype.
     *
     * @param array $field
     *
     * @return string
     */
    public function getJsonTypeDeclarationSQL(array $field)
    {
        return $this->getClobTypeDeclarationSQL($field);
    }

Christophe Coevoet's avatar
Christophe Coevoet committed
330
    /**
331
     * @param integer $length
Christophe Coevoet's avatar
Christophe Coevoet committed
332
     * @param boolean $fixed
333
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
334
     * @return string
335
     *
Benjamin Morel's avatar
Benjamin Morel committed
336
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
Christophe Coevoet's avatar
Christophe Coevoet committed
337
     */
338 339 340 341
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
    {
        throw DBALException::notSupported('VARCHARs not supported by Platform.');
    }
342

Steve Müller's avatar
Steve Müller committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    /**
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
     *
     * @param integer $length The length of the column.
     * @param boolean $fixed  Whether the column length is fixed.
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
    {
        throw DBALException::notSupported('BINARY/VARBINARY column types are not supported by this platform.');
    }

358
    /**
Benjamin Morel's avatar
Benjamin Morel committed
359
     * Returns the SQL snippet used to declare a CLOB column type.
360 361
     *
     * @param array $field
362 363
     *
     * @return string
364 365 366
     */
    abstract public function getClobTypeDeclarationSQL(array $field);

367
    /**
Benjamin Morel's avatar
Benjamin Morel committed
368
     * Returns the SQL Snippet used to declare a BLOB column type.
369 370
     *
     * @param array $field
371 372
     *
     * @return string
373 374 375
     */
    abstract public function getBlobTypeDeclarationSQL(array $field);

376 377 378 379 380 381 382
    /**
     * Gets the name of the platform.
     *
     * @return string
     */
    abstract public function getName();

383
    /**
Benjamin Morel's avatar
Benjamin Morel committed
384
     * Registers a doctrine type to be used in conjunction with a column type of this platform.
385 386 387
     *
     * @param string $dbType
     * @param string $doctrineType
388
     *
Benjamin Morel's avatar
Benjamin Morel committed
389
     * @throws \Doctrine\DBAL\DBALException If the type is not found.
390 391 392 393
     */
    public function registerDoctrineTypeMapping($dbType, $doctrineType)
    {
        if ($this->doctrineTypeMapping === null) {
394
            $this->initializeAllDoctrineTypeMappings();
395 396 397 398 399 400 401 402
        }

        if (!Types\Type::hasType($doctrineType)) {
            throw DBALException::typeNotFound($doctrineType);
        }

        $dbType = strtolower($dbType);
        $this->doctrineTypeMapping[$dbType] = $doctrineType;
403 404 405 406 407 408

        $doctrineType = Type::getType($doctrineType);

        if ($doctrineType->requiresSQLCommentHint($this)) {
            $this->markDoctrineTypeCommented($doctrineType);
        }
409 410 411
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
412
     * Gets the Doctrine type that is mapped for the given database column type.
413
     *
Benjamin Morel's avatar
Benjamin Morel committed
414
     * @param string $dbType
415
     *
416
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
417 418
     *
     * @throws \Doctrine\DBAL\DBALException
419 420 421 422
     */
    public function getDoctrineTypeMapping($dbType)
    {
        if ($this->doctrineTypeMapping === null) {
423
            $this->initializeAllDoctrineTypeMappings();
424
        }
425

426
        $dbType = strtolower($dbType);
427

428
        if (!isset($this->doctrineTypeMapping[$dbType])) {
429 430
            throw new \Doctrine\DBAL\DBALException("Unknown database type ".$dbType." requested, " . get_class($this) . " may not support it.");
        }
431 432

        return $this->doctrineTypeMapping[$dbType];
433 434
    }

435
    /**
Benjamin Morel's avatar
Benjamin Morel committed
436
     * Checks if a database type is currently supported by this platform.
437 438
     *
     * @param string $dbType
439 440
     *
     * @return boolean
441 442 443 444
     */
    public function hasDoctrineTypeMappingFor($dbType)
    {
        if ($this->doctrineTypeMapping === null) {
445
            $this->initializeAllDoctrineTypeMappings();
446 447 448
        }

        $dbType = strtolower($dbType);
Benjamin Morel's avatar
Benjamin Morel committed
449

450 451 452
        return isset($this->doctrineTypeMapping[$dbType]);
    }

453
    /**
Benjamin Morel's avatar
Benjamin Morel committed
454
     * Initializes the Doctrine Type comments instance variable for in_array() checks.
455 456 457 458 459
     *
     * @return void
     */
    protected function initializeCommentedDoctrineTypes()
    {
460 461 462 463
        $this->doctrineTypeComments = array();

        foreach (Type::getTypesMap() as $typeName => $className) {
            $type = Type::getType($typeName);
Benjamin Eberlei's avatar
Benjamin Eberlei committed
464

465 466 467 468
            if ($type->requiresSQLCommentHint($this)) {
                $this->doctrineTypeComments[] = $typeName;
            }
        }
469 470 471 472 473
    }

    /**
     * Is it necessary for the platform to add a parsable type comment to allow reverse engineering the given type?
     *
Benjamin Morel's avatar
Benjamin Morel committed
474
     * @param \Doctrine\DBAL\Types\Type $doctrineType
475
     *
476
     * @return boolean
477 478 479 480 481 482 483 484 485 486 487
     */
    public function isCommentedDoctrineType(Type $doctrineType)
    {
        if ($this->doctrineTypeComments === null) {
            $this->initializeCommentedDoctrineTypes();
        }

        return in_array($doctrineType->getName(), $this->doctrineTypeComments);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
488
     * Marks this type as to be commented in ALTER TABLE and CREATE TABLE statements.
489
     *
Benjamin Morel's avatar
Benjamin Morel committed
490
     * @param string|\Doctrine\DBAL\Types\Type $doctrineType
491
     *
492 493
     * @return void
     */
494
    public function markDoctrineTypeCommented($doctrineType)
495 496 497 498
    {
        if ($this->doctrineTypeComments === null) {
            $this->initializeCommentedDoctrineTypes();
        }
499

500
        $this->doctrineTypeComments[] = $doctrineType instanceof Type ? $doctrineType->getName() : $doctrineType;
501 502 503
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
504 505 506
     * Gets the comment to append to a column comment that helps parsing this type in reverse engineering.
     *
     * @param \Doctrine\DBAL\Types\Type $doctrineType
507
     *
508 509 510 511 512 513 514
     * @return string
     */
    public function getDoctrineTypeComment(Type $doctrineType)
    {
        return '(DC2Type:' . $doctrineType->getName() . ')';
    }

515
    /**
Benjamin Morel's avatar
Benjamin Morel committed
516 517 518
     * Gets the comment of a passed column modified by potential doctrine type comment hints.
     *
     * @param \Doctrine\DBAL\Schema\Column $column
519
     *
520 521 522 523 524
     * @return string
     */
    protected function getColumnComment(Column $column)
    {
        $comment = $column->getComment();
525

526 527 528
        if ($this->isCommentedDoctrineType($column->getType())) {
            $comment .= $this->getDoctrineTypeComment($column->getType());
        }
529

530 531 532
        return $comment;
    }

533 534 535 536 537 538 539 540 541
    /**
     * Gets the character used for identifier quoting.
     *
     * @return string
     */
    public function getIdentifierQuoteCharacter()
    {
        return '"';
    }
542

543 544 545 546 547 548 549 550 551
    /**
     * Gets the string portion that starts an SQL comment.
     *
     * @return string
     */
    public function getSqlCommentStartString()
    {
        return "--";
    }
552

553
    /**
554
     * Gets the string portion that ends an SQL comment.
555 556 557 558 559 560 561
     *
     * @return string
     */
    public function getSqlCommentEndString()
    {
        return "\n";
    }
562

563 564 565 566 567 568
    /**
     * Gets the maximum length of a varchar field.
     *
     * @return integer
     */
    public function getVarcharMaxLength()
569 570 571 572 573 574 575 576 577 578
    {
        return 4000;
    }

    /**
     * Gets the default length of a varchar field.
     *
     * @return integer
     */
    public function getVarcharDefaultLength()
579 580 581
    {
        return 255;
    }
582

Steve Müller's avatar
Steve Müller committed
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    /**
     * Gets the maximum length of a binary field.
     *
     * @return integer
     */
    public function getBinaryMaxLength()
    {
        return 4000;
    }

    /**
     * Gets the default length of a binary field.
     *
     * @return integer
     */
    public function getBinaryDefaultLength()
    {
        return 255;
    }

603 604 605 606 607 608 609 610 611
    /**
     * Gets all SQL wildcard characters of the platform.
     *
     * @return array
     */
    public function getWildcards()
    {
        return array('%', '_');
    }
612

613 614 615 616
    /**
     * Returns the regular expression operator.
     *
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
617 618
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
619 620 621
     */
    public function getRegexpExpression()
    {
622
        throw DBALException::notSupported(__METHOD__);
623
    }
624

625
    /**
Benjamin Morel's avatar
Benjamin Morel committed
626 627 628
     * Returns the global unique identifier expression.
     *
     * @return string
629
     *
Benjamin Morel's avatar
Benjamin Morel committed
630
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
631 632 633 634
     */
    public function getGuidExpression()
    {
        throw DBALException::notSupported(__METHOD__);
635 636 637
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
638
     * Returns the SQL snippet to get the average value of a column.
639
     *
Benjamin Morel's avatar
Benjamin Morel committed
640
     * @param string $column The column to use.
641
     *
Benjamin Morel's avatar
Benjamin Morel committed
642
     * @return string Generated SQL including an AVG aggregate function.
643 644 645
     */
    public function getAvgExpression($column)
    {
Benjamin Morel's avatar
Benjamin Morel committed
646
        return 'AVG(' . $column . ')';
647 648 649
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
650
     * Returns the SQL snippet to get the number of rows (without a NULL value) of a column.
651
     *
Benjamin Morel's avatar
Benjamin Morel committed
652
     * If a '*' is used instead of a column the number of selected rows is returned.
653
     *
Benjamin Morel's avatar
Benjamin Morel committed
654
     * @param string|integer $column The column to use.
655
     *
Benjamin Morel's avatar
Benjamin Morel committed
656
     * @return string Generated SQL including a COUNT aggregate function.
657 658 659 660 661 662 663
     */
    public function getCountExpression($column)
    {
        return 'COUNT(' . $column . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
664 665 666
     * Returns the SQL snippet to get the highest value of a column.
     *
     * @param string $column The column to use.
667
     *
Benjamin Morel's avatar
Benjamin Morel committed
668
     * @return string Generated SQL including a MAX aggregate function.
669 670 671 672 673 674 675
     */
    public function getMaxExpression($column)
    {
        return 'MAX(' . $column . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
676
     * Returns the SQL snippet to get the lowest value of a column.
677
     *
Benjamin Morel's avatar
Benjamin Morel committed
678 679 680
     * @param string $column The column to use.
     *
     * @return string Generated SQL including a MIN aggregate function.
681 682 683 684 685 686 687
     */
    public function getMinExpression($column)
    {
        return 'MIN(' . $column . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
688
     * Returns the SQL snippet to get the total sum of a column.
689
     *
Benjamin Morel's avatar
Benjamin Morel committed
690 691 692
     * @param string $column The column to use.
     *
     * @return string Generated SQL including a SUM aggregate function.
693 694 695 696 697 698 699 700 701
     */
    public function getSumExpression($column)
    {
        return 'SUM(' . $column . ')';
    }

    // scalar functions

    /**
Benjamin Morel's avatar
Benjamin Morel committed
702
     * Returns the SQL snippet to get the md5 sum of a field.
703
     *
Benjamin Morel's avatar
Benjamin Morel committed
704
     * Note: Not SQL92, but common functionality.
705
     *
706
     * @param string $column
Benjamin Morel's avatar
Benjamin Morel committed
707
     *
708 709 710 711 712 713 714 715
     * @return string
     */
    public function getMd5Expression($column)
    {
        return 'MD5(' . $column . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
716
     * Returns the SQL snippet to get the length of a text field.
717
     *
718
     * @param string $column
719
     *
720 721 722 723 724 725 726
     * @return string
     */
    public function getLengthExpression($column)
    {
        return 'LENGTH(' . $column . ')';
    }

727
    /**
Benjamin Morel's avatar
Benjamin Morel committed
728
     * Returns the SQL snippet to get the squared value of a column.
729
     *
Benjamin Morel's avatar
Benjamin Morel committed
730
     * @param string $column The column to use.
731
     *
Benjamin Morel's avatar
Benjamin Morel committed
732
     * @return string Generated SQL including an SQRT aggregate function.
733 734 735 736 737 738
     */
    public function getSqrtExpression($column)
    {
        return 'SQRT(' . $column . ')';
    }

739
    /**
Benjamin Morel's avatar
Benjamin Morel committed
740
     * Returns the SQL snippet to round a numeric field to the number of decimals specified.
741
     *
Benjamin Morel's avatar
Benjamin Morel committed
742
     * @param string  $column
743
     * @param integer $decimals
744
     *
745 746 747 748 749 750 751 752
     * @return string
     */
    public function getRoundExpression($column, $decimals = 0)
    {
        return 'ROUND(' . $column . ', ' . $decimals . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
753
     * Returns the SQL snippet to get the remainder of the division operation $expression1 / $expression2.
754 755 756
     *
     * @param string $expression1
     * @param string $expression2
757
     *
758 759 760 761 762 763 764 765
     * @return string
     */
    public function getModExpression($expression1, $expression2)
    {
        return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
766
     * Returns the SQL snippet to trim a string.
767
     *
Benjamin Morel's avatar
Benjamin Morel committed
768 769 770
     * @param string         $str  The expression to apply the trim to.
     * @param integer        $pos  The position of the trim (leading/trailing/both).
     * @param string|boolean $char The char to trim, has to be quoted already. Defaults to space.
771
     *
772 773
     * @return string
     */
774
    public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
775
    {
Steve Müller's avatar
Steve Müller committed
776
        $expression = '';
777

778 779
        switch ($pos) {
            case self::TRIM_LEADING:
Steve Müller's avatar
Steve Müller committed
780
                $expression = 'LEADING ';
781 782 783
                break;

            case self::TRIM_TRAILING:
Steve Müller's avatar
Steve Müller committed
784
                $expression = 'TRAILING ';
785 786 787
                break;

            case self::TRIM_BOTH:
Steve Müller's avatar
Steve Müller committed
788
                $expression = 'BOTH ';
789
                break;
790 791
        }

Steve Müller's avatar
Steve Müller committed
792 793 794 795 796 797 798 799 800
        if (false !== $char) {
            $expression .= $char . ' ';
        }

        if ($pos || false !== $char) {
            $expression .= 'FROM ';
        }

        return 'TRIM(' . $expression . $str . ')';
801 802 803
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
804
     * Returns the SQL snippet to trim trailing space characters from the expression.
805
     *
Benjamin Morel's avatar
Benjamin Morel committed
806
     * @param string $str Literal string or column name.
807
     *
808 809 810 811 812 813 814 815
     * @return string
     */
    public function getRtrimExpression($str)
    {
        return 'RTRIM(' . $str . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
816
     * Returns the SQL snippet to trim leading space characters from the expression.
817
     *
Benjamin Morel's avatar
Benjamin Morel committed
818
     * @param string $str Literal string or column name.
819
     *
820 821 822 823 824 825 826 827
     * @return string
     */
    public function getLtrimExpression($str)
    {
        return 'LTRIM(' . $str . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
828 829
     * Returns the SQL snippet to change all characters from the expression to uppercase,
     * according to the current character set mapping.
830
     *
Benjamin Morel's avatar
Benjamin Morel committed
831
     * @param string $str Literal string or column name.
832
     *
833 834 835 836 837 838 839 840
     * @return string
     */
    public function getUpperExpression($str)
    {
        return 'UPPER(' . $str . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
841 842
     * Returns the SQL snippet to change all characters from the expression to lowercase,
     * according to the current character set mapping.
843
     *
Benjamin Morel's avatar
Benjamin Morel committed
844
     * @param string $str Literal string or column name.
845
     *
846 847 848 849 850 851 852 853
     * @return string
     */
    public function getLowerExpression($str)
    {
        return 'LOWER(' . $str . ')';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
854
     * Returns the SQL snippet to get the position of the first occurrence of substring $substr in string $str.
855
     *
Benjamin Morel's avatar
Benjamin Morel committed
856 857 858
     * @param string          $str      Literal string.
     * @param string          $substr   Literal string to find.
     * @param integer|boolean $startPos Position to start at, beginning of string by default.
859
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
860
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
861 862
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
863
     */
864
    public function getLocateExpression($str, $substr, $startPos = false)
865
    {
866
        throw DBALException::notSupported(__METHOD__);
867 868 869
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
870
     * Returns the SQL snippet to get the current system date.
871 872 873 874 875 876 877 878 879
     *
     * @return string
     */
    public function getNowExpression()
    {
        return 'NOW()';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
880
     * Returns a SQL snippet to get a substring inside an SQL statement.
881 882 883
     *
     * Note: Not SQL92, but common functionality.
     *
Benjamin Morel's avatar
Benjamin Morel committed
884
     * SQLite only supports the 2 parameter variant of this function.
885
     *
Benjamin Morel's avatar
Benjamin Morel committed
886 887 888
     * @param string       $value  An sql string literal or column name/alias.
     * @param integer      $from   Where to start the substring portion.
     * @param integer|null $length The substring portion length.
889
     *
890
     * @return string
891
     */
892
    public function getSubstringExpression($value, $from, $length = null)
893
    {
894
        if ($length === null) {
895 896
            return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
        }
897 898

        return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $length . ')';
899 900 901
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
902
     * Returns a SQL snippet to concatenate the given expressions.
903
     *
Benjamin Morel's avatar
Benjamin Morel committed
904
     * Accepts an arbitrary number of string parameters. Each parameter must contain an expression.
905
     *
906 907 908 909
     * @return string
     */
    public function getConcatExpression()
    {
910
        return join(' || ', func_get_args());
911 912 913 914 915 916 917 918 919 920 921 922 923
    }

    /**
     * Returns the SQL for a logical not.
     *
     * Example:
     * <code>
     * $q = new Doctrine_Query();
     * $e = $q->expr;
     * $q->select('*')->from('table')
     *   ->where($e->eq('id', $e->not('null'));
     * </code>
     *
924
     * @param string $expression
925
     *
Benjamin Morel's avatar
Benjamin Morel committed
926
     * @return string The logical expression.
927 928 929
     */
    public function getNotExpression($expression)
    {
930
        return 'NOT(' . $expression . ')';
931 932 933
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
934
     * Returns the SQL that checks if an expression is null.
935
     *
Benjamin Morel's avatar
Benjamin Morel committed
936
     * @param string $expression The expression that should be compared to null.
937
     *
Benjamin Morel's avatar
Benjamin Morel committed
938
     * @return string The logical expression.
939 940 941 942 943 944 945
     */
    public function getIsNullExpression($expression)
    {
        return $expression . ' IS NULL';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
946
     * Returns the SQL that checks if an expression is not null.
947
     *
Benjamin Morel's avatar
Benjamin Morel committed
948
     * @param string $expression The expression that should be compared to null.
949
     *
Benjamin Morel's avatar
Benjamin Morel committed
950
     * @return string The logical expression.
951 952 953 954 955 956 957
     */
    public function getIsNotNullExpression($expression)
    {
        return $expression . ' IS NOT NULL';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
958
     * Returns the SQL that checks if an expression evaluates to a value between two values.
959 960 961 962 963 964 965
     *
     * The parameter $expression is checked if it is between $value1 and $value2.
     *
     * Note: There is a slight difference in the way BETWEEN works on some databases.
     * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
     * independence you should avoid using between().
     *
Benjamin Morel's avatar
Benjamin Morel committed
966 967 968
     * @param string $expression The value to compare to.
     * @param string $value1     The lower value to compare with.
     * @param string $value2     The higher value to compare with.
969
     *
Benjamin Morel's avatar
Benjamin Morel committed
970
     * @return string The logical expression.
971 972 973 974 975 976
     */
    public function getBetweenExpression($expression, $value1, $value2)
    {
        return $expression . ' BETWEEN ' .$value1 . ' AND ' . $value2;
    }

Benjamin Morel's avatar
Benjamin Morel committed
977 978 979 980 981 982 983
    /**
     * Returns the SQL to get the arccosine of a value.
     *
     * @param string $value
     *
     * @return string
     */
984 985 986 987 988
    public function getAcosExpression($value)
    {
        return 'ACOS(' . $value . ')';
    }

Benjamin Morel's avatar
Benjamin Morel committed
989 990 991 992 993 994 995
    /**
     * Returns the SQL to get the sine of a value.
     *
     * @param string $value
     *
     * @return string
     */
996 997 998 999 1000
    public function getSinExpression($value)
    {
        return 'SIN(' . $value . ')';
    }

Benjamin Morel's avatar
Benjamin Morel committed
1001 1002 1003 1004 1005
    /**
     * Returns the SQL to get the PI value.
     *
     * @return string
     */
1006 1007 1008 1009 1010
    public function getPiExpression()
    {
        return 'PI()';
    }

Benjamin Morel's avatar
Benjamin Morel committed
1011 1012 1013 1014 1015 1016 1017
    /**
     * Returns the SQL to get the cosine of a value.
     *
     * @param string $value
     *
     * @return string
     */
1018 1019 1020 1021
    public function getCosExpression($value)
    {
        return 'COS(' . $value . ')';
    }
1022

1023
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1024
     * Returns the SQL to calculate the difference in days between the two passed dates.
1025
     *
Benjamin Morel's avatar
Benjamin Morel committed
1026
     * Computes diff = date1 - date2.
1027 1028 1029
     *
     * @param string $date1
     * @param string $date2
1030
     *
1031
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1032 1033
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1034 1035 1036 1037
     */
    public function getDateDiffExpression($date1, $date2)
    {
        throw DBALException::notSupported(__METHOD__);
1038 1039
    }

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    /**
     * Returns the SQL to add the number of given seconds to a date.
     *
     * @param string  $date
     * @param integer $seconds
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateAddSecondsExpression($date, $seconds)
    {
        return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, self::DATE_INTERVAL_UNIT_SECOND);
    }

    /**
     * Returns the SQL to subtract the number of given seconds from a date.
     *
     * @param string  $date
     * @param integer $seconds
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateSubSecondsExpression($date, $seconds)
    {
        return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, self::DATE_INTERVAL_UNIT_SECOND);
    }

    /**
     * Returns the SQL to add the number of given minutes to a date.
     *
     * @param string  $date
     * @param integer $minutes
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateAddMinutesExpression($date, $minutes)
    {
        return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, self::DATE_INTERVAL_UNIT_MINUTE);
    }

    /**
     * Returns the SQL to subtract the number of given minutes from a date.
     *
     * @param string  $date
     * @param integer $minutes
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateSubMinutesExpression($date, $minutes)
    {
        return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, self::DATE_INTERVAL_UNIT_MINUTE);
    }

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    /**
     * Returns the SQL to add the number of given hours to a date.
     *
     * @param string  $date
     * @param integer $hours
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateAddHourExpression($date, $hours)
    {
1112
        return $this->getDateArithmeticIntervalExpression($date, '+', $hours, self::DATE_INTERVAL_UNIT_HOUR);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    }

    /**
     * Returns the SQL to subtract the number of given hours to a date.
     *
     * @param string  $date
     * @param integer $hours
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateSubHourExpression($date, $hours)
    {
1127
        return $this->getDateArithmeticIntervalExpression($date, '-', $hours, self::DATE_INTERVAL_UNIT_HOUR);
1128 1129 1130
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1131
     * Returns the SQL to add the number of given days to a date.
1132
     *
Benjamin Morel's avatar
Benjamin Morel committed
1133
     * @param string  $date
1134 1135
     * @param integer $days
     *
1136
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1137 1138
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1139 1140 1141
     */
    public function getDateAddDaysExpression($date, $days)
    {
1142
        return $this->getDateArithmeticIntervalExpression($date, '+', $days, self::DATE_INTERVAL_UNIT_DAY);
1143 1144 1145
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1146
     * Returns the SQL to subtract the number of given days to a date.
1147
     *
Benjamin Morel's avatar
Benjamin Morel committed
1148
     * @param string  $date
1149 1150
     * @param integer $days
     *
1151
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1152 1153
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1154 1155 1156
     */
    public function getDateSubDaysExpression($date, $days)
    {
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        return $this->getDateArithmeticIntervalExpression($date, '-', $days, self::DATE_INTERVAL_UNIT_DAY);
    }

    /**
     * Returns the SQL to add the number of given weeks to a date.
     *
     * @param string  $date
     * @param integer $weeks
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateAddWeeksExpression($date, $weeks)
    {
        return $this->getDateArithmeticIntervalExpression($date, '+', $weeks, self::DATE_INTERVAL_UNIT_WEEK);
    }

    /**
     * Returns the SQL to subtract the number of given weeks from a date.
     *
     * @param string  $date
     * @param integer $weeks
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateSubWeeksExpression($date, $weeks)
    {
        return $this->getDateArithmeticIntervalExpression($date, '-', $weeks, self::DATE_INTERVAL_UNIT_WEEK);
1188 1189 1190
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1191
     * Returns the SQL to add the number of given months to a date.
1192
     *
Benjamin Morel's avatar
Benjamin Morel committed
1193
     * @param string  $date
1194 1195
     * @param integer $months
     *
1196
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1197 1198
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1199 1200 1201
     */
    public function getDateAddMonthExpression($date, $months)
    {
1202
        return $this->getDateArithmeticIntervalExpression($date, '+', $months, self::DATE_INTERVAL_UNIT_MONTH);
1203 1204 1205
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1206
     * Returns the SQL to subtract the number of given months to a date.
1207
     *
Benjamin Morel's avatar
Benjamin Morel committed
1208
     * @param string  $date
1209 1210
     * @param integer $months
     *
1211
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1212 1213
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1214 1215
     */
    public function getDateSubMonthExpression($date, $months)
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
    {
        return $this->getDateArithmeticIntervalExpression($date, '-', $months, self::DATE_INTERVAL_UNIT_MONTH);
    }

    /**
     * Returns the SQL to add the number of given quarters to a date.
     *
     * @param string  $date
     * @param integer $quarters
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateAddQuartersExpression($date, $quarters)
    {
        return $this->getDateArithmeticIntervalExpression($date, '+', $quarters, self::DATE_INTERVAL_UNIT_QUARTER);
    }

    /**
     * Returns the SQL to subtract the number of given quarters from a date.
     *
     * @param string  $date
     * @param integer $quarters
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateSubQuartersExpression($date, $quarters)
    {
        return $this->getDateArithmeticIntervalExpression($date, '-', $quarters, self::DATE_INTERVAL_UNIT_QUARTER);
    }

    /**
     * Returns the SQL to add the number of given years to a date.
     *
     * @param string  $date
     * @param integer $years
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateAddYearsExpression($date, $years)
    {
        return $this->getDateArithmeticIntervalExpression($date, '+', $years, self::DATE_INTERVAL_UNIT_YEAR);
    }

    /**
     * Returns the SQL to subtract the number of given years from a date.
     *
     * @param string  $date
     * @param integer $years
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getDateSubYearsExpression($date, $years)
    {
        return $this->getDateArithmeticIntervalExpression($date, '-', $years, self::DATE_INTERVAL_UNIT_YEAR);
    }

    /**
     * Returns the SQL for a date arithmetic expression.
     *
     * @param string  $date     The column or literal representing a date to perform the arithmetic operation on.
     * @param string  $operator The arithmetic operator (+ or -).
     * @param integer $interval The interval that shall be calculated into the date.
     * @param string  $unit     The unit of the interval that shall be calculated into the date.
     *                          One of the DATE_INTERVAL_UNIT_* constants.
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
1294 1295 1296 1297
    {
        throw DBALException::notSupported(__METHOD__);
    }

1298
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1299
     * Returns the SQL bit AND comparison expression.
1300
     *
Benjamin Morel's avatar
Benjamin Morel committed
1301 1302
     * @param string $value1
     * @param string $value2
1303
     *
Benjamin Morel's avatar
Benjamin Morel committed
1304
     * @return string
1305 1306 1307 1308 1309
     */
    public function getBitAndComparisonExpression($value1, $value2)
    {
        return '(' . $value1 . ' & ' . $value2 . ')';
    }
Fabio B. Silva's avatar
Fabio B. Silva committed
1310

1311
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1312
     * Returns the SQL bit OR comparison expression.
1313
     *
Benjamin Morel's avatar
Benjamin Morel committed
1314 1315
     * @param string $value1
     * @param string $value2
1316
     *
Benjamin Morel's avatar
Benjamin Morel committed
1317
     * @return string
1318 1319 1320 1321 1322 1323
     */
    public function getBitOrComparisonExpression($value1, $value2)
    {
        return '(' . $value1 . ' | ' . $value2 . ')';
    }

Benjamin Morel's avatar
Benjamin Morel committed
1324 1325
    /**
     * Returns the FOR UPDATE expression.
1326
     *
Benjamin Morel's avatar
Benjamin Morel committed
1327 1328
     * @return string
     */
1329
    public function getForUpdateSQL()
1330 1331 1332
    {
        return 'FOR UPDATE';
    }
1333

1334 1335 1336
    /**
     * Honors that some SQL vendors such as MsSql use table hints for locking instead of the ANSI SQL FOR UPDATE specification.
     *
1337 1338 1339
     * @param string       $fromClause The FROM clause to append the hint for the given lock mode to.
     * @param integer|null $lockMode   One of the Doctrine\DBAL\LockMode::* constants. If null is given, nothing will
     *                                 be appended to the FROM clause.
1340
     *
1341 1342 1343 1344 1345 1346 1347 1348
     * @return string
     */
    public function appendLockHint($fromClause, $lockMode)
    {
        return $fromClause;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1349
     * Returns the SQL snippet to append to any SELECT statement which locks rows in shared read lock.
1350
     *
1351
     * This defaults to the ANSI SQL "FOR UPDATE", which is an exclusive lock (Write). Some database
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
     * vendors allow to lighten this constraint up to be a real read lock.
     *
     * @return string
     */
    public function getReadLockSQL()
    {
        return $this->getForUpdateSQL();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1362
     * Returns the SQL snippet to append to any SELECT statement which obtains an exclusive lock on the rows.
1363
     *
1364
     * The semantics of this lock mode should equal the SELECT .. FOR UPDATE of the ANSI SQL standard.
1365 1366 1367 1368 1369 1370 1371 1372
     *
     * @return string
     */
    public function getWriteLockSQL()
    {
        return $this->getForUpdateSQL();
    }

1373
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1374
     * Returns the SQL snippet to drop an existing database.
1375
     *
Benjamin Morel's avatar
Benjamin Morel committed
1376
     * @param string $database The name of the database that should be dropped.
1377 1378 1379
     *
     * @return string
     */
1380
    public function getDropDatabaseSQL($database)
1381 1382 1383
    {
        return 'DROP DATABASE ' . $database;
    }
1384

1385
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1386
     * Returns the SQL snippet to drop an existing table.
1387
     *
Benjamin Morel's avatar
Benjamin Morel committed
1388
     * @param \Doctrine\DBAL\Schema\Table|string $table
1389
     *
1390
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1391 1392
     *
     * @throws \InvalidArgumentException
1393
     */
1394
    public function getDropTableSQL($table)
1395
    {
1396 1397
        $tableArg = $table;

1398
        if ($table instanceof Table) {
1399
            $table = $table->getQuotedName($this);
Steve Müller's avatar
Steve Müller committed
1400
        } elseif (!is_string($table)) {
1401
            throw new \InvalidArgumentException('getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
1402 1403
        }

1404
        if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaDropTable)) {
1405
            $eventArgs = new SchemaDropTableEventArgs($tableArg, $this);
1406 1407 1408 1409 1410 1411
            $this->_eventManager->dispatchEvent(Events::onSchemaDropTable, $eventArgs);

            if ($eventArgs->isDefaultPrevented()) {
                return $eventArgs->getSql();
            }
        }
1412

1413 1414
        return 'DROP TABLE ' . $table;
    }
1415

1416
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1417
     * Returns the SQL to safely drop a temporary table WITHOUT implicitly committing an open transaction.
1418
     *
Benjamin Morel's avatar
Benjamin Morel committed
1419
     * @param \Doctrine\DBAL\Schema\Table|string $table
1420
     *
1421 1422 1423 1424 1425 1426 1427
     * @return string
     */
    public function getDropTemporaryTableSQL($table)
    {
        return $this->getDropTableSQL($table);
    }

1428
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1429
     * Returns the SQL to drop an index from a table.
1430
     *
Benjamin Morel's avatar
Benjamin Morel committed
1431 1432
     * @param \Doctrine\DBAL\Schema\Index|string $index
     * @param \Doctrine\DBAL\Schema\Table|string $table
1433
     *
1434
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1435 1436
     *
     * @throws \InvalidArgumentException
1437
     */
1438
    public function getDropIndexSQL($index, $table = null)
1439
    {
1440
        if ($index instanceof Index) {
1441
            $index = $index->getQuotedName($this);
Steve Müller's avatar
Steve Müller committed
1442
        } elseif (!is_string($index)) {
1443
            throw new \InvalidArgumentException('AbstractPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
1444 1445 1446
        }

        return 'DROP INDEX ' . $index;
1447
    }
1448

1449
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1450
     * Returns the SQL to drop a constraint.
1451
     *
Benjamin Morel's avatar
Benjamin Morel committed
1452 1453
     * @param \Doctrine\DBAL\Schema\Constraint|string $constraint
     * @param \Doctrine\DBAL\Schema\Table|string      $table
1454
     *
1455 1456
     * @return string
     */
1457
    public function getDropConstraintSQL($constraint, $table)
1458
    {
1459 1460
        if (! $constraint instanceof Constraint) {
            $constraint = new Identifier($constraint);
1461 1462
        }

1463 1464
        if (! $table instanceof Table) {
            $table = new Identifier($table);
1465 1466
        }

1467 1468 1469
        $constraint = $constraint->getQuotedName($this);
        $table = $table->getQuotedName($this);

1470
        return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $constraint;
1471
    }
1472

1473
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1474
     * Returns the SQL to drop a foreign key.
1475
     *
Benjamin Morel's avatar
Benjamin Morel committed
1476 1477
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint|string $foreignKey
     * @param \Doctrine\DBAL\Schema\Table|string                $table
1478
     *
1479 1480
     * @return string
     */
1481
    public function getDropForeignKeySQL($foreignKey, $table)
1482
    {
1483 1484
        if (! $foreignKey instanceof ForeignKeyConstraint) {
            $foreignKey = new Identifier($foreignKey);
1485 1486
        }

1487 1488
        if (! $table instanceof Table) {
            $table = new Identifier($table);
1489 1490
        }

1491 1492 1493
        $foreignKey = $foreignKey->getQuotedName($this);
        $table = $table->getQuotedName($this);

1494
        return 'ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $foreignKey;
1495
    }
1496

1497
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1498
     * Returns the SQL statement(s) to create a table with the specified name, columns and constraints
1499
     * on this platform.
1500
     *
Benjamin Morel's avatar
Benjamin Morel committed
1501 1502
     * @param \Doctrine\DBAL\Schema\Table   $table
     * @param integer                       $createFlags
1503
     *
1504
     * @return array The sequence of SQL statements.
Benjamin Morel's avatar
Benjamin Morel committed
1505 1506 1507
     *
     * @throws \Doctrine\DBAL\DBALException
     * @throws \InvalidArgumentException
1508
     */
1509
    public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES)
1510
    {
1511
        if ( ! is_int($createFlags)) {
1512
            throw new \InvalidArgumentException("Second argument of AbstractPlatform::getCreateTableSQL() has to be integer.");
1513 1514
        }

1515
        if (count($table->getColumns()) === 0) {
1516 1517 1518
            throw DBALException::noColumnsSpecifiedForTable($table->getName());
        }

1519
        $tableName = $table->getQuotedName($this);
1520 1521 1522 1523 1524
        $options = $table->getOptions();
        $options['uniqueConstraints'] = array();
        $options['indexes'] = array();
        $options['primary'] = array();

1525
        if (($createFlags&self::CREATE_INDEXES) > 0) {
1526
            foreach ($table->getIndexes() as $index) {
1527 1528
                /* @var $index Index */
                if ($index->isPrimary()) {
Steve Müller's avatar
Steve Müller committed
1529
                    $options['primary']       = $index->getQuotedColumns($this);
1530
                    $options['primary_index'] = $index;
1531
                } else {
1532
                    $options['indexes'][$index->getQuotedName($this)] = $index;
1533
                }
1534 1535
            }
        }
1536

1537
        $columnSql = array();
1538
        $columns = array();
1539

1540
        foreach ($table->getColumns() as $column) {
1541
            /* @var \Doctrine\DBAL\Schema\Column $column */
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553

            if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)) {
                $eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
                $this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);

                $columnSql = array_merge($columnSql, $eventArgs->getSql());

                if ($eventArgs->isDefaultPrevented()) {
                    continue;
                }
            }

1554
            $columnData = $column->toArray();
1555
            $columnData['name'] = $column->getQuotedName($this);
1556
            $columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption('version') : false;
1557
            $columnData['comment'] = $this->getColumnComment($column);
1558 1559

            if (strtolower($columnData['type']) == "string" && $columnData['length'] === null) {
1560 1561
                $columnData['length'] = 255;
            }
1562 1563

            if (in_array($column->getName(), $options['primary'])) {
1564 1565 1566 1567 1568 1569
                $columnData['primary'] = true;
            }

            $columns[$columnData['name']] = $columnData;
        }

1570 1571
        if (($createFlags&self::CREATE_FOREIGNKEYS) > 0) {
            $options['foreignKeys'] = array();
1572
            foreach ($table->getForeignKeys() as $fkConstraint) {
1573 1574 1575 1576
                $options['foreignKeys'][] = $fkConstraint;
            }
        }

1577 1578 1579
        if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
            $eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
            $this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
1580

Jan Sorgalla's avatar
Jan Sorgalla committed
1581 1582 1583
            if ($eventArgs->isDefaultPrevented()) {
                return array_merge($eventArgs->getSql(), $columnSql);
            }
1584
        }
1585

1586 1587
        $sql = $this->_getCreateTableSQL($tableName, $columns, $options);
        if ($this->supportsCommentOnStatement()) {
1588
            foreach ($table->getColumns() as $column) {
1589 1590 1591 1592
                $comment = $this->getColumnComment($column);

                if (null !== $comment && '' !== $comment) {
                    $sql[] = $this->getCommentOnColumnSQL($tableName, $column->getQuotedName($this), $comment);
1593 1594 1595
                }
            }
        }
1596

Jan Sorgalla's avatar
Jan Sorgalla committed
1597
        return array_merge($sql, $columnSql);
1598 1599
    }

Benjamin Morel's avatar
Benjamin Morel committed
1600 1601 1602 1603 1604 1605 1606
    /**
     * @param string $tableName
     * @param string $columnName
     * @param string $comment
     *
     * @return string
     */
1607 1608
    public function getCommentOnColumnSQL($tableName, $columnName, $comment)
    {
1609 1610
        $tableName = new Identifier($tableName);
        $columnName = new Identifier($columnName);
1611 1612
        $comment = $this->quoteStringLiteral($comment);

1613 1614
        return "COMMENT ON COLUMN " . $tableName->getQuotedName($this) . "." . $columnName->getQuotedName($this) .
            " IS " . $comment;
1615 1616
    }

1617 1618 1619 1620 1621 1622
    /**
     * Returns the SQL to create inline comment on a column.
     *
     * @param string $comment
     *
     * @return string
1623 1624
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1625 1626 1627
     */
    public function getInlineColumnCommentSQL($comment)
    {
1628 1629 1630 1631
        if (! $this->supportsInlineColumnComments()) {
            throw DBALException::notSupported(__METHOD__);
        }

1632 1633 1634
        return "COMMENT " . $this->quoteStringLiteral($comment);
    }

1635
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1636
     * Returns the SQL used to create a table.
1637
     *
1638
     * @param string $tableName
Benjamin Morel's avatar
Benjamin Morel committed
1639 1640
     * @param array  $columns
     * @param array  $options
1641
     *
1642 1643
     * @return array
     */
1644
    protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
1645
    {
1646
        $columnListSql = $this->getColumnDeclarationListSQL($columns);
1647

1648
        if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
1649
            foreach ($options['uniqueConstraints'] as $name => $definition) {
1650
                $columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition);
1651 1652
            }
        }
1653

1654
        if (isset($options['primary']) && ! empty($options['primary'])) {
1655
            $columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
1656 1657 1658
        }

        if (isset($options['indexes']) && ! empty($options['indexes'])) {
Steve Müller's avatar
Steve Müller committed
1659
            foreach ($options['indexes'] as $index => $definition) {
1660
                $columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
1661 1662 1663
            }
        }

1664
        $query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql;
1665

1666
        $check = $this->getCheckDeclarationSQL($columns);
1667 1668
        if ( ! empty($check)) {
            $query .= ', ' . $check;
1669
        }
1670 1671 1672 1673 1674
        $query .= ')';

        $sql[] = $query;

        if (isset($options['foreignKeys'])) {
1675
            foreach ((array) $options['foreignKeys'] as $definition) {
1676
                $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
1677 1678
            }
        }
1679

1680 1681
        return $sql;
    }
1682

Benjamin Morel's avatar
Benjamin Morel committed
1683 1684 1685
    /**
     * @return string
     */
1686
    public function getCreateTemporaryTableSnippetSQL()
1687 1688 1689
    {
        return "CREATE TEMPORARY TABLE";
    }
1690

1691
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1692
     * Returns the SQL to create a sequence on this platform.
1693
     *
1694
     * @param \Doctrine\DBAL\Schema\Sequence $sequence
1695 1696 1697
     *
     * @return string
     *
Benjamin Morel's avatar
Benjamin Morel committed
1698
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1699
     */
1700
    public function getCreateSequenceSQL(Sequence $sequence)
1701
    {
1702
        throw DBALException::notSupported(__METHOD__);
1703
    }
1704

1705
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1706
     * Returns the SQL to change a sequence on this platform.
1707 1708
     *
     * @param \Doctrine\DBAL\Schema\Sequence $sequence
1709
     *
1710
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1711 1712
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1713
     */
1714
    public function getAlterSequenceSQL(Sequence $sequence)
1715 1716 1717
    {
        throw DBALException::notSupported(__METHOD__);
    }
1718

1719
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1720
     * Returns the SQL to create a constraint on a table on this platform.
1721
     *
Benjamin Morel's avatar
Benjamin Morel committed
1722 1723
     * @param \Doctrine\DBAL\Schema\Constraint   $constraint
     * @param \Doctrine\DBAL\Schema\Table|string $table
1724
     *
1725
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1726 1727
     *
     * @throws \InvalidArgumentException
1728
     */
1729
    public function getCreateConstraintSQL(Constraint $constraint, $table)
1730
    {
1731
        if ($table instanceof Table) {
1732
            $table = $table->getQuotedName($this);
1733 1734
        }

1735
        $query = 'ALTER TABLE ' . $table . ' ADD CONSTRAINT ' . $constraint->getQuotedName($this);
1736

1737
        $columnList = '('. implode(', ', $constraint->getQuotedColumns($this)) . ')';
1738 1739

        $referencesClause = '';
1740
        if ($constraint instanceof Index) {
Steve Müller's avatar
Steve Müller committed
1741
            if ($constraint->isPrimary()) {
1742 1743 1744 1745 1746
                $query .= ' PRIMARY KEY';
            } elseif ($constraint->isUnique()) {
                $query .= ' UNIQUE';
            } else {
                throw new \InvalidArgumentException(
1747
                    'Can only create primary or unique constraints, no common indexes with getCreateConstraintSQL().'
1748 1749
                );
            }
Steve Müller's avatar
Steve Müller committed
1750
        } elseif ($constraint instanceof ForeignKeyConstraint) {
1751 1752
            $query .= ' FOREIGN KEY';

1753
            $referencesClause = ' REFERENCES ' . $constraint->getQuotedForeignTableName($this) .
1754
                ' (' . implode(', ', $constraint->getQuotedForeignColumns($this)) . ')';
1755 1756
        }
        $query .= ' '.$columnList.$referencesClause;
1757 1758 1759

        return $query;
    }
1760

1761
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1762
     * Returns the SQL to create an index on a table on this platform.
1763
     *
Benjamin Morel's avatar
Benjamin Morel committed
1764 1765
     * @param \Doctrine\DBAL\Schema\Index        $index
     * @param \Doctrine\DBAL\Schema\Table|string $table The name of the table on which the index is to be created.
1766
     *
1767
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
1768 1769
     *
     * @throws \InvalidArgumentException
1770
     */
1771
    public function getCreateIndexSQL(Index $index, $table)
1772
    {
1773
        if ($table instanceof Table) {
1774
            $table = $table->getQuotedName($this);
1775
        }
1776
        $name = $index->getQuotedName($this);
1777
        $columns = $index->getQuotedColumns($this);
1778 1779 1780

        if (count($columns) == 0) {
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
1781
        }
1782

1783 1784
        if ($index->isPrimary()) {
            return $this->getCreatePrimaryKeySQL($index, $table);
1785 1786
        }

1787
        $query = 'CREATE ' . $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name . ' ON ' . $table;
1788
        $query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')' . $this->getPartialIndexSQL($index);
1789

1790 1791
        return $query;
    }
1792

1793 1794 1795 1796 1797 1798 1799 1800 1801
    /**
     * Adds condition for partial index.
     *
     * @param \Doctrine\DBAL\Schema\Index $index
     *
     * @return string
     */
    protected function getPartialIndexSQL(Index $index)
    {
1802 1803 1804
        if ($this->supportsPartialIndexes() && $index->hasOption('where')) {
            return  ' WHERE ' . $index->getOption('where');
        }
1805 1806

        return '';
1807 1808
    }

1809
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1810
     * Adds additional flags for index generation.
1811
     *
Benjamin Morel's avatar
Benjamin Morel committed
1812
     * @param \Doctrine\DBAL\Schema\Index $index
1813
     *
1814 1815 1816 1817
     * @return string
     */
    protected function getCreateIndexSQLFlags(Index $index)
    {
1818
        return $index->isUnique() ? 'UNIQUE ' : '';
1819 1820
    }

1821
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1822
     * Returns the SQL to create an unnamed primary key constraint.
1823
     *
Benjamin Morel's avatar
Benjamin Morel committed
1824 1825
     * @param \Doctrine\DBAL\Schema\Index        $index
     * @param \Doctrine\DBAL\Schema\Table|string $table
1826
     *
1827 1828 1829 1830
     * @return string
     */
    public function getCreatePrimaryKeySQL(Index $index, $table)
    {
1831
        return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index->getQuotedColumns($this)) . ')';
1832
    }
1833

1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
    /**
     * Returns the SQL to create a named schema.
     *
     * @param string $schemaName
     *
     * @return string
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getCreateSchemaSQL($schemaName)
    {
        throw DBALException::notSupported(__METHOD__);
    }

1847
    /**
1848
     * Quotes a string so that it can be safely used as a table or column name,
1849
     * even if it is a reserved word of the platform. This also detects identifier
1850
     * chains separated by dot and quotes them independently.
1851
     *
1852
     * NOTE: Just because you CAN use quoted identifiers doesn't mean
Benjamin Morel's avatar
Benjamin Morel committed
1853
     * you SHOULD use them. In general, they end up causing way more
1854 1855
     * problems than they solve.
     *
Benjamin Morel's avatar
Benjamin Morel committed
1856
     * @param string $str The identifier name to be quoted.
1857
     *
Benjamin Morel's avatar
Benjamin Morel committed
1858
     * @return string The quoted identifier string.
1859 1860
     */
    public function quoteIdentifier($str)
1861 1862
    {
        if (strpos($str, ".") !== false) {
1863
            $parts = array_map(array($this, "quoteSingleIdentifier"), explode(".", $str));
1864

1865 1866 1867 1868 1869 1870 1871
            return implode(".", $parts);
        }

        return $this->quoteSingleIdentifier($str);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1872
     * Quotes a single identifier (no dot chain separation).
1873
     *
Benjamin Morel's avatar
Benjamin Morel committed
1874
     * @param string $str The identifier name to be quoted.
1875
     *
Benjamin Morel's avatar
Benjamin Morel committed
1876
     * @return string The quoted identifier string.
1877 1878
     */
    public function quoteSingleIdentifier($str)
1879 1880 1881
    {
        $c = $this->getIdentifierQuoteCharacter();

1882
        return $c . str_replace($c, $c.$c, $str) . $c;
1883
    }
1884

1885
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1886
     * Returns the SQL to create a new foreign key.
1887
     *
Benjamin Morel's avatar
Benjamin Morel committed
1888 1889
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey The foreign key constraint.
     * @param \Doctrine\DBAL\Schema\Table|string         $table      The name of the table on which the foreign key is to be created.
1890
     *
1891 1892
     * @return string
     */
1893
    public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
1894
    {
1895
        if ($table instanceof Table) {
1896
            $table = $table->getQuotedName($this);
1897 1898
        }

1899
        $query = 'ALTER TABLE ' . $table . ' ADD ' . $this->getForeignKeyDeclarationSQL($foreignKey);
1900 1901 1902

        return $query;
    }
1903

1904
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1905
     * Gets the SQL statements for altering an existing table.
1906
     *
Benjamin Morel's avatar
Benjamin Morel committed
1907
     * This method returns an array of SQL statements, since some platforms need several statements.
1908
     *
Benjamin Morel's avatar
Benjamin Morel committed
1909
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
1910
     *
1911
     * @return array
Benjamin Morel's avatar
Benjamin Morel committed
1912 1913
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
1914
     */
1915
    public function getAlterTableSQL(TableDiff $diff)
1916
    {
1917
        throw DBALException::notSupported(__METHOD__);
1918
    }
1919

1920
    /**
Benjamin Morel's avatar
Benjamin Morel committed
1921 1922 1923
     * @param \Doctrine\DBAL\Schema\Column    $column
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
     * @param array                           $columnSql
1924
     *
1925
     * @return boolean
1926 1927 1928 1929 1930 1931 1932
     */
    protected function onSchemaAlterTableAddColumn(Column $column, TableDiff $diff, &$columnSql)
    {
        if (null === $this->_eventManager) {
            return false;
        }

1933
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableAddColumn)) {
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
            return false;
        }

        $eventArgs = new SchemaAlterTableAddColumnEventArgs($column, $diff, $this);
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableAddColumn, $eventArgs);

        $columnSql = array_merge($columnSql, $eventArgs->getSql());

        return $eventArgs->isDefaultPrevented();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1946 1947 1948
     * @param \Doctrine\DBAL\Schema\Column    $column
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
     * @param array                           $columnSql
1949
     *
1950
     * @return boolean
1951 1952 1953 1954 1955 1956 1957
     */
    protected function onSchemaAlterTableRemoveColumn(Column $column, TableDiff $diff, &$columnSql)
    {
        if (null === $this->_eventManager) {
            return false;
        }

1958
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRemoveColumn)) {
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
            return false;
        }

        $eventArgs = new SchemaAlterTableRemoveColumnEventArgs($column, $diff, $this);
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRemoveColumn, $eventArgs);

        $columnSql = array_merge($columnSql, $eventArgs->getSql());

        return $eventArgs->isDefaultPrevented();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1971 1972 1973
     * @param \Doctrine\DBAL\Schema\ColumnDiff $columnDiff
     * @param \Doctrine\DBAL\Schema\TableDiff  $diff
     * @param array                            $columnSql
1974
     *
1975
     * @return boolean
1976 1977 1978 1979 1980 1981 1982
     */
    protected function onSchemaAlterTableChangeColumn(ColumnDiff $columnDiff, TableDiff $diff, &$columnSql)
    {
        if (null === $this->_eventManager) {
            return false;
        }

1983
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableChangeColumn)) {
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
            return false;
        }

        $eventArgs = new SchemaAlterTableChangeColumnEventArgs($columnDiff, $diff, $this);
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableChangeColumn, $eventArgs);

        $columnSql = array_merge($columnSql, $eventArgs->getSql());

        return $eventArgs->isDefaultPrevented();
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
1996 1997 1998 1999
     * @param string                          $oldColumnName
     * @param \Doctrine\DBAL\Schema\Column    $column
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
     * @param array                           $columnSql
2000
     *
2001
     * @return boolean
2002 2003 2004 2005 2006 2007 2008
     */
    protected function onSchemaAlterTableRenameColumn($oldColumnName, Column $column, TableDiff $diff, &$columnSql)
    {
        if (null === $this->_eventManager) {
            return false;
        }

2009
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTableRenameColumn)) {
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
            return false;
        }

        $eventArgs = new SchemaAlterTableRenameColumnEventArgs($oldColumnName, $column, $diff, $this);
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTableRenameColumn, $eventArgs);

        $columnSql = array_merge($columnSql, $eventArgs->getSql());

        return $eventArgs->isDefaultPrevented();
    }
Christophe Coevoet's avatar
Christophe Coevoet committed
2020

2021
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2022 2023
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
     * @param array                           $sql
2024
     *
2025
     * @return boolean
2026 2027 2028 2029 2030 2031 2032
     */
    protected function onSchemaAlterTable(TableDiff $diff, &$sql)
    {
        if (null === $this->_eventManager) {
            return false;
        }

2033
        if ( ! $this->_eventManager->hasListeners(Events::onSchemaAlterTable)) {
2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
            return false;
        }

        $eventArgs = new SchemaAlterTableEventArgs($diff, $this);
        $this->_eventManager->dispatchEvent(Events::onSchemaAlterTable, $eventArgs);

        $sql = array_merge($sql, $eventArgs->getSql());

        return $eventArgs->isDefaultPrevented();
    }
2044

Benjamin Morel's avatar
Benjamin Morel committed
2045 2046 2047 2048 2049
    /**
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
     *
     * @return array
     */
2050 2051
    protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
    {
2052
        $tableName = $diff->getName($this)->getQuotedName($this);
2053

2054 2055
        $sql = array();
        if ($this->supportsForeignKeyConstraints()) {
2056
            foreach ($diff->removedForeignKeys as $foreignKey) {
2057 2058
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
            }
2059
            foreach ($diff->changedForeignKeys as $foreignKey) {
2060 2061 2062 2063
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
            }
        }

2064
        foreach ($diff->removedIndexes as $index) {
2065 2066
            $sql[] = $this->getDropIndexSQL($index, $tableName);
        }
2067
        foreach ($diff->changedIndexes as $index) {
2068 2069 2070 2071 2072
            $sql[] = $this->getDropIndexSQL($index, $tableName);
        }

        return $sql;
    }
2073

Benjamin Morel's avatar
Benjamin Morel committed
2074 2075 2076 2077 2078
    /**
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
     *
     * @return array
     */
2079
    protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
2080
    {
2081 2082
        $tableName = (false !== $diff->newName)
            ? $diff->getNewName()->getQuotedName($this)
2083
            : $diff->getName($this)->getQuotedName($this);
2084

2085
        $sql = array();
2086

2087
        if ($this->supportsForeignKeyConstraints()) {
2088
            foreach ($diff->addedForeignKeys as $foreignKey) {
2089
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2090
            }
2091

2092
            foreach ($diff->changedForeignKeys as $foreignKey) {
2093
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
2094 2095 2096
            }
        }

2097
        foreach ($diff->addedIndexes as $index) {
2098
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2099
        }
2100

2101
        foreach ($diff->changedIndexes as $index) {
2102
            $sql[] = $this->getCreateIndexSQL($index, $tableName);
2103 2104
        }

2105 2106 2107 2108 2109 2110 2111 2112
        foreach ($diff->renamedIndexes as $oldIndexName => $index) {
            $oldIndexName = new Identifier($oldIndexName);
            $sql          = array_merge(
                $sql,
                $this->getRenameIndexSQL($oldIndexName->getQuotedName($this), $index, $tableName)
            );
        }

2113 2114
        return $sql;
    }
2115

2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132
    /**
     * Returns the SQL for renaming an index on a table.
     *
     * @param string                      $oldIndexName The name of the index to rename from.
     * @param \Doctrine\DBAL\Schema\Index $index        The definition of the index to rename to.
     * @param string                      $tableName    The table to rename the given index on.
     *
     * @return array The sequence of SQL statements for renaming the given index.
     */
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
    {
        return array(
            $this->getDropIndexSQL($oldIndexName, $tableName),
            $this->getCreateIndexSQL($index, $tableName)
        );
    }

2133 2134 2135
    /**
     * Common code for alter table statement generation that updates the changed Index and Foreign Key definitions.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2136
     * @param \Doctrine\DBAL\Schema\TableDiff $diff
2137
     *
2138 2139 2140 2141 2142 2143
     * @return array
     */
    protected function _getAlterTableIndexForeignKeySQL(TableDiff $diff)
    {
        return array_merge($this->getPreAlterTableIndexForeignKeySQL($diff), $this->getPostAlterTableIndexForeignKeySQL($diff));
    }
2144

2145
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2146
     * Gets declaration of a number of fields in bulk.
2147
     *
Benjamin Morel's avatar
Benjamin Morel committed
2148 2149 2150 2151 2152
     * @param array $fields A multidimensional associative array.
     *                      The first dimension determines the field name, while the second
     *                      dimension is keyed with the name of the properties
     *                      of the field being declared as array indexes. Currently, the types
     *                      of supported field properties are as follows:
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
     *
     *      length
     *          Integer value that determines the maximum length of the text
     *          field. If this argument is missing the field should be
     *          declared to have the longest length allowed by the DBMS.
     *
     *      default
     *          Text value to be used as default for this field.
     *
     *      notnull
     *          Boolean flag that indicates whether this field is constrained
     *          to not be set to null.
     *      charset
     *          Text value with the default CHARACTER SET for this field.
     *      collation
     *          Text value with the default COLLATION for this field.
     *      unique
     *          unique constraint
     *
     * @return string
     */
2174
    public function getColumnDeclarationListSQL(array $fields)
2175
    {
2176
        $queryFields = array();
2177

2178
        foreach ($fields as $fieldName => $field) {
2179
            $queryFields[] = $this->getColumnDeclarationSQL($fieldName, $field);
2180
        }
2181

2182 2183 2184 2185
        return implode(', ', $queryFields);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2186
     * Obtains DBMS specific SQL code portion needed to declare a generic type
2187 2188
     * field to be used in statements like CREATE TABLE.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2189 2190 2191 2192
     * @param string $name  The name the field to be declared.
     * @param array  $field An associative array with the name of the properties
     *                      of the field being declared as array indexes. Currently, the types
     *                      of supported field properties are as follows:
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212
     *
     *      length
     *          Integer value that determines the maximum length of the text
     *          field. If this argument is missing the field should be
     *          declared to have the longest length allowed by the DBMS.
     *
     *      default
     *          Text value to be used as default for this field.
     *
     *      notnull
     *          Boolean flag that indicates whether this field is constrained
     *          to not be set to null.
     *      charset
     *          Text value with the default CHARACTER SET for this field.
     *      collation
     *          Text value with the default COLLATION for this field.
     *      unique
     *          unique constraint
     *      check
     *          column check constraint
2213 2214
     *      columnDefinition
     *          a string that defines the complete column
2215
     *
Benjamin Morel's avatar
Benjamin Morel committed
2216
     * @return string DBMS specific SQL code portion that should be used to declare the column.
2217
     */
2218
    public function getColumnDeclarationSQL($name, array $field)
2219
    {
2220
        if (isset($field['columnDefinition'])) {
2221
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
2222
        } else {
2223
            $default = $this->getDefaultValueDeclarationSQL($field);
2224

2225
            $charset = (isset($field['charset']) && $field['charset']) ?
2226
                    ' ' . $this->getColumnCharsetDeclarationSQL($field['charset']) : '';
2227

2228
            $collation = (isset($field['collation']) && $field['collation']) ?
2229
                    ' ' . $this->getColumnCollationDeclarationSQL($field['collation']) : '';
2230

2231
            $notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
2232

2233
            $unique = (isset($field['unique']) && $field['unique']) ?
2234
                    ' ' . $this->getUniqueFieldDeclarationSQL() : '';
2235

2236 2237
            $check = (isset($field['check']) && $field['check']) ?
                    ' ' . $field['check'] : '';
2238

2239
            $typeDecl = $field['type']->getSQLDeclaration($field, $this);
2240 2241
            $columnDef = $typeDecl . $charset . $default . $notnull . $unique . $check . $collation;
        }
2242

2243
        if ($this->supportsInlineColumnComments() && isset($field['comment']) && $field['comment'] !== '') {
2244
            $columnDef .= ' ' . $this->getInlineColumnCommentSQL($field['comment']);
2245 2246
        }

2247
        return $name . ' ' . $columnDef;
2248
    }
2249

2250
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2251
     * Returns the SQL snippet that declares a floating point column of arbitrary precision.
2252 2253
     *
     * @param array $columnDef
2254
     *
2255 2256
     * @return string
     */
2257
    public function getDecimalTypeDeclarationSQL(array $columnDef)
2258 2259
    {
        $columnDef['precision'] = ( ! isset($columnDef['precision']) || empty($columnDef['precision']))
2260
            ? 10 : $columnDef['precision'];
2261 2262
        $columnDef['scale'] = ( ! isset($columnDef['scale']) || empty($columnDef['scale']))
            ? 0 : $columnDef['scale'];
2263

2264 2265
        return 'NUMERIC(' . $columnDef['precision'] . ', ' . $columnDef['scale'] . ')';
    }
2266 2267

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2268
     * Obtains DBMS specific SQL code portion needed to set a default value
2269 2270
     * declaration to be used in statements like CREATE TABLE.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2271
     * @param array $field The field definition array.
2272
     *
Benjamin Morel's avatar
Benjamin Morel committed
2273
     * @return string DBMS specific SQL code portion needed to set a default value.
2274
     */
2275
    public function getDefaultValueDeclarationSQL($field)
2276
    {
2277
        $default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
2278

2279
        if (isset($field['default'])) {
2280 2281
            $default = " DEFAULT '".$field['default']."'";
            if (isset($field['type'])) {
2282
                if (in_array((string) $field['type'], array("Integer", "BigInt", "SmallInt"))) {
2283
                    $default = " DEFAULT ".$field['default'];
2284
                } elseif (in_array((string) $field['type'], array('DateTime', 'DateTimeTz')) && $field['default'] == $this->getCurrentTimestampSQL()) {
2285
                    $default = " DEFAULT ".$this->getCurrentTimestampSQL();
2286
                } elseif ((string) $field['type'] == 'Time' && $field['default'] == $this->getCurrentTimeSQL()) {
2287
                    $default = " DEFAULT ".$this->getCurrentTimeSQL();
2288
                } elseif ((string) $field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
2289
                    $default = " DEFAULT ".$this->getCurrentDateSQL();
Steve Müller's avatar
Steve Müller committed
2290
                } elseif ((string) $field['type'] == 'Boolean') {
2291
                    $default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
2292 2293
                }
            }
2294
        }
Benjamin Morel's avatar
Benjamin Morel committed
2295

2296 2297 2298 2299
        return $default;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2300
     * Obtains DBMS specific SQL code portion needed to set a CHECK constraint
2301 2302
     * declaration to be used in statements like CREATE TABLE.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2303
     * @param array $definition The check definition.
2304
     *
Benjamin Morel's avatar
Benjamin Morel committed
2305
     * @return string DBMS specific SQL code portion needed to set a CHECK constraint.
2306
     */
2307
    public function getCheckDeclarationSQL(array $definition)
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
    {
        $constraints = array();
        foreach ($definition as $field => $def) {
            if (is_string($def)) {
                $constraints[] = 'CHECK (' . $def . ')';
            } else {
                if (isset($def['min'])) {
                    $constraints[] = 'CHECK (' . $field . ' >= ' . $def['min'] . ')';
                }

                if (isset($def['max'])) {
                    $constraints[] = 'CHECK (' . $field . ' <= ' . $def['max'] . ')';
                }
            }
        }

        return implode(', ', $constraints);
    }
2326

2327
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2328
     * Obtains DBMS specific SQL code portion needed to set a unique
2329 2330
     * constraint declaration to be used in statements like CREATE TABLE.
     *
2331 2332
     * @param string                      $name  The name of the unique constraint.
     * @param \Doctrine\DBAL\Schema\Index $index The index definition.
2333
     *
Benjamin Morel's avatar
Benjamin Morel committed
2334 2335 2336
     * @return string DBMS specific SQL code portion needed to set a constraint.
     *
     * @throws \InvalidArgumentException
2337
     */
2338
    public function getUniqueConstraintDeclarationSQL($name, Index $index)
2339
    {
2340
        $columns = $index->getQuotedColumns($this);
2341
        $name = new Identifier($name);
2342 2343

        if (count($columns) === 0) {
2344
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
2345
        }
2346

2347
        return 'CONSTRAINT ' . $name->getQuotedName($this) . ' UNIQUE ('
2348
             . $this->getIndexFieldDeclarationListSQL($columns)
2349
             . ')' . $this->getPartialIndexSQL($index);
2350
    }
2351 2352

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2353
     * Obtains DBMS specific SQL code portion needed to set an index
2354 2355
     * declaration to be used in statements like CREATE TABLE.
     *
2356 2357
     * @param string                      $name  The name of the index.
     * @param \Doctrine\DBAL\Schema\Index $index The index definition.
Benjamin Morel's avatar
Benjamin Morel committed
2358 2359
     *
     * @return string DBMS specific SQL code portion needed to set an index.
2360
     *
Benjamin Morel's avatar
Benjamin Morel committed
2361
     * @throws \InvalidArgumentException
2362
     */
2363
    public function getIndexDeclarationSQL($name, Index $index)
2364
    {
2365
        $columns = $index->getQuotedColumns($this);
2366
        $name = new Identifier($name);
2367 2368

        if (count($columns) === 0) {
2369
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
2370 2371
        }

2372
        return $this->getCreateIndexSQLFlags($index) . 'INDEX ' . $name->getQuotedName($this) . ' ('
2373 2374
            . $this->getIndexFieldDeclarationListSQL($columns)
            . ')' . $this->getPartialIndexSQL($index);
2375 2376
    }

2377
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2378
     * Obtains SQL code portion needed to create a custom column,
2379 2380 2381
     * e.g. when a field has the "columnDefinition" keyword.
     * Only "AUTOINCREMENT" and "PRIMARY KEY" are added if appropriate.
     *
2382
     * @param array $columnDef
2383
     *
2384 2385
     * @return string
     */
2386
    public function getCustomTypeDeclarationSQL(array $columnDef)
2387 2388 2389 2390
    {
        return $columnDef['columnDefinition'];
    }

2391
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2392
     * Obtains DBMS specific SQL code portion needed to set an index
2393 2394
     * declaration to be used in statements like CREATE TABLE.
     *
2395
     * @param array $fields
2396
     *
2397 2398
     * @return string
     */
2399
    public function getIndexFieldDeclarationListSQL(array $fields)
2400 2401
    {
        $ret = array();
2402

2403 2404
        foreach ($fields as $field => $definition) {
            if (is_array($definition)) {
2405
                $ret[] = $field;
2406
            } else {
2407
                $ret[] = $definition;
2408 2409
            }
        }
2410

2411 2412 2413 2414
        return implode(', ', $ret);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2415
     * Returns the required SQL string that fits between CREATE ... TABLE
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
     * to create the table as a temporary table.
     *
     * Should be overridden in driver classes to return the correct string for the
     * specific database type.
     *
     * The default is to return the string "TEMPORARY" - this will result in a
     * SQL error for any database that does not support temporary tables, or that
     * requires a different SQL command from "CREATE TEMPORARY TABLE".
     *
     * @return string The string required to be placed between "CREATE" and "TABLE"
     *                to generate a temporary table, if possible.
     */
2428
    public function getTemporaryTableSQL()
2429 2430 2431
    {
        return 'TEMPORARY';
    }
2432

2433 2434 2435
    /**
     * Some vendors require temporary table names to be qualified specially.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2436
     * @param string $tableName
2437
     *
2438 2439 2440 2441 2442 2443 2444
     * @return string
     */
    public function getTemporaryTableName($tableName)
    {
        return $tableName;
    }

2445 2446 2447 2448
    /**
     * Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
     * of a field declaration to be used in statements like CREATE TABLE.
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
2449
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey
2450
     *
Benjamin Morel's avatar
Benjamin Morel committed
2451 2452
     * @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
     *                of a field declaration.
2453
     */
2454
    public function getForeignKeyDeclarationSQL(ForeignKeyConstraint $foreignKey)
2455
    {
2456 2457
        $sql  = $this->getForeignKeyBaseDeclarationSQL($foreignKey);
        $sql .= $this->getAdvancedForeignKeyOptionsSQL($foreignKey);
2458 2459 2460 2461 2462

        return $sql;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2463
     * Returns the FOREIGN KEY query section dealing with non-standard options
2464 2465
     * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
     *
Benjamin Morel's avatar
Benjamin Morel committed
2466
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey The foreign key definition.
2467
     *
2468 2469
     * @return string
     */
2470
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
2471 2472
    {
        $query = '';
2473
        if ($this->supportsForeignKeyOnUpdate() && $foreignKey->hasOption('onUpdate')) {
2474
            $query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onUpdate'));
2475
        }
2476
        if ($foreignKey->hasOption('onDelete')) {
2477
            $query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
2478
        }
Benjamin Morel's avatar
Benjamin Morel committed
2479

2480 2481 2482 2483
        return $query;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2484
     * Returns the given referential action in uppercase if valid, otherwise throws an exception.
2485
     *
Benjamin Morel's avatar
Benjamin Morel committed
2486
     * @param string $action The foreign key referential action.
2487
     *
2488
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2489 2490
     *
     * @throws \InvalidArgumentException if unknown referential action given
2491
     */
2492
    public function getForeignKeyReferentialActionSQL($action)
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
    {
        $upper = strtoupper($action);
        switch ($upper) {
            case 'CASCADE':
            case 'SET NULL':
            case 'NO ACTION':
            case 'RESTRICT':
            case 'SET DEFAULT':
                return $upper;
            default:
2503
                throw new \InvalidArgumentException('Invalid foreign key action: ' . $upper);
2504 2505 2506 2507
        }
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2508
     * Obtains DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
2509 2510
     * of a field declaration to be used in statements like CREATE TABLE.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2511
     * @param \Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey
2512
     *
2513
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2514 2515
     *
     * @throws \InvalidArgumentException
2516
     */
2517
    public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey)
2518 2519
    {
        $sql = '';
2520
        if (strlen($foreignKey->getName())) {
2521
            $sql .= 'CONSTRAINT ' . $foreignKey->getQuotedName($this) . ' ';
2522 2523 2524
        }
        $sql .= 'FOREIGN KEY (';

2525
        if (count($foreignKey->getLocalColumns()) === 0) {
2526
            throw new \InvalidArgumentException("Incomplete definition. 'local' required.");
2527
        }
2528
        if (count($foreignKey->getForeignColumns()) === 0) {
2529
            throw new \InvalidArgumentException("Incomplete definition. 'foreign' required.");
2530
        }
2531
        if (strlen($foreignKey->getForeignTableName()) === 0) {
2532
            throw new \InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
2533 2534
        }

2535
        $sql .= implode(', ', $foreignKey->getQuotedLocalColumns($this))
2536
              . ') REFERENCES '
2537
              . $foreignKey->getQuotedForeignTableName($this) . ' ('
2538
              . implode(', ', $foreignKey->getQuotedForeignColumns($this)) . ')';
2539 2540 2541 2542 2543

        return $sql;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2544
     * Obtains DBMS specific SQL code portion needed to set the UNIQUE constraint
2545 2546
     * of a field declaration to be used in statements like CREATE TABLE.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2547 2548
     * @return string DBMS specific SQL code portion needed to set the UNIQUE constraint
     *                of a field declaration.
2549
     */
2550
    public function getUniqueFieldDeclarationSQL()
2551 2552 2553 2554 2555
    {
        return 'UNIQUE';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2556
     * Obtains DBMS specific SQL code portion needed to set the CHARACTER SET
2557 2558
     * of a field declaration to be used in statements like CREATE TABLE.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2559
     * @param string $charset The name of the charset.
2560
     *
Benjamin Morel's avatar
Benjamin Morel committed
2561 2562
     * @return string DBMS specific SQL code portion needed to set the CHARACTER SET
     *                of a field declaration.
2563
     */
2564
    public function getColumnCharsetDeclarationSQL($charset)
2565 2566 2567 2568 2569
    {
        return '';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2570
     * Obtains DBMS specific SQL code portion needed to set the COLLATION
2571 2572
     * of a field declaration to be used in statements like CREATE TABLE.
     *
Benjamin Morel's avatar
Benjamin Morel committed
2573
     * @param string $collation The name of the collation.
2574
     *
Benjamin Morel's avatar
Benjamin Morel committed
2575 2576
     * @return string DBMS specific SQL code portion needed to set the COLLATION
     *                of a field declaration.
2577
     */
2578
    public function getColumnCollationDeclarationSQL($collation)
2579
    {
2580
        return $this->supportsColumnCollation() ? 'COLLATE ' . $collation : '';
2581
    }
2582

2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
    /**
     * Whether the platform prefers sequences for ID generation.
     * Subclasses should override this method to return TRUE if they prefer sequences.
     *
     * @return boolean
     */
    public function prefersSequences()
    {
        return false;
    }
2593

2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
    /**
     * Whether the platform prefers identity columns (eg. autoincrement) for ID generation.
     * Subclasses should override this method to return TRUE if they prefer identity columns.
     *
     * @return boolean
     */
    public function prefersIdentityColumns()
    {
        return false;
    }
2604

2605 2606
    /**
     * Some platforms need the boolean values to be converted.
2607
     *
romanb's avatar
romanb committed
2608
     * The default conversion in this implementation converts to integers (false => 0, true => 1).
2609
     *
2610 2611
     * Note: if the input is not a boolean the original input might be returned.
     *
2612 2613
     * There are two contexts when converting booleans: Literals and Prepared Statements.
     * This method should handle the literal case
2614
     *
2615
     * @param mixed $item A boolean or an array of them.
2616
     *
2617
     * @return mixed A boolean database value or an array of them.
2618
     */
2619
    public function convertBooleans($item)
2620 2621 2622 2623 2624 2625 2626
    {
        if (is_array($item)) {
            foreach ($item as $k => $value) {
                if (is_bool($value)) {
                    $item[$k] = (int) $value;
                }
            }
Steve Müller's avatar
Steve Müller committed
2627
        } elseif (is_bool($item)) {
romanb's avatar
romanb committed
2628
            $item = (int) $item;
2629
        }
2630

2631 2632
        return $item;
    }
2633

2634
    /**
2635
     * Some platforms have boolean literals that needs to be correctly converted
2636 2637 2638 2639 2640
     *
     * The default conversion tries to convert value into bool "(bool)$item"
     *
     * @param mixed $item
     *
2641
     * @return bool|null
2642 2643 2644
     */
    public function convertFromBoolean($item)
    {
2645
        return null === $item ? null: (bool) $item ;
2646
    }
2647

2648 2649 2650 2651
    /**
     * This method should handle the prepared statements case. When there is no
     * distinction, it's OK to use the same method.
     *
2652 2653 2654
     * Note: if the input is not a boolean the original input might be returned.
     *
     * @param mixed $item A boolean or an array of them.
2655
     *
2656
     * @return mixed A boolean database value or an array of them.
2657
     */
2658
    public function convertBooleansToDatabaseValue($item)
2659
    {
2660
        return $this->convertBooleans($item);
2661 2662
    }

2663
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2664
     * Returns the SQL specific for the platform to get the current date.
2665 2666 2667
     *
     * @return string
     */
2668
    public function getCurrentDateSQL()
2669 2670 2671 2672 2673
    {
        return 'CURRENT_DATE';
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2674
     * Returns the SQL specific for the platform to get the current time.
2675 2676 2677
     *
     * @return string
     */
2678
    public function getCurrentTimeSQL()
2679 2680 2681 2682
    {
        return 'CURRENT_TIME';
    }

2683
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2684
     * Returns the SQL specific for the platform to get the current timestamp
2685 2686 2687
     *
     * @return string
     */
2688
    public function getCurrentTimestampSQL()
2689 2690 2691
    {
        return 'CURRENT_TIMESTAMP';
    }
2692

romanb's avatar
romanb committed
2693
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2694
     * Returns the SQL for a given transaction isolation level Connection constant.
romanb's avatar
romanb committed
2695
     *
2696
     * @param integer $level
2697
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
2698
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2699 2700
     *
     * @throws \InvalidArgumentException
romanb's avatar
romanb committed
2701
     */
2702
    protected function _getTransactionIsolationLevelSQL($level)
romanb's avatar
romanb committed
2703 2704
    {
        switch ($level) {
2705
            case Connection::TRANSACTION_READ_UNCOMMITTED:
romanb's avatar
romanb committed
2706
                return 'READ UNCOMMITTED';
2707
            case Connection::TRANSACTION_READ_COMMITTED:
romanb's avatar
romanb committed
2708
                return 'READ COMMITTED';
2709
            case Connection::TRANSACTION_REPEATABLE_READ:
romanb's avatar
romanb committed
2710
                return 'REPEATABLE READ';
2711
            case Connection::TRANSACTION_SERIALIZABLE:
romanb's avatar
romanb committed
2712 2713
                return 'SERIALIZABLE';
            default:
2714
                throw new \InvalidArgumentException('Invalid isolation level:' . $level);
2715 2716 2717
        }
    }

Benjamin Morel's avatar
Benjamin Morel committed
2718 2719 2720 2721 2722
    /**
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2723
    public function getListDatabasesSQL()
2724
    {
2725
        throw DBALException::notSupported(__METHOD__);
2726 2727
    }

2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739
    /**
     * Returns the SQL statement for retrieving the namespaces defined in the database.
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
    public function getListNamespacesSQL()
    {
        throw DBALException::notSupported(__METHOD__);
    }

Benjamin Morel's avatar
Benjamin Morel committed
2740 2741 2742 2743 2744 2745 2746
    /**
     * @param string $database
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2747
    public function getListSequencesSQL($database)
2748
    {
2749
        throw DBALException::notSupported(__METHOD__);
2750 2751
    }

Benjamin Morel's avatar
Benjamin Morel committed
2752 2753 2754 2755 2756 2757 2758
    /**
     * @param string $table
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2759
    public function getListTableConstraintsSQL($table)
2760
    {
2761
        throw DBALException::notSupported(__METHOD__);
2762 2763
    }

Benjamin Morel's avatar
Benjamin Morel committed
2764 2765 2766 2767 2768 2769 2770 2771
    /**
     * @param string      $table
     * @param string|null $database
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2772
    public function getListTableColumnsSQL($table, $database = null)
2773
    {
2774
        throw DBALException::notSupported(__METHOD__);
2775 2776
    }

Benjamin Morel's avatar
Benjamin Morel committed
2777 2778 2779 2780 2781
    /**
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2782
    public function getListTablesSQL()
2783
    {
2784
        throw DBALException::notSupported(__METHOD__);
2785 2786
    }

Benjamin Morel's avatar
Benjamin Morel committed
2787 2788 2789 2790 2791
    /**
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2792
    public function getListUsersSQL()
2793
    {
2794
        throw DBALException::notSupported(__METHOD__);
2795 2796
    }

2797
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2798
     * Returns the SQL to list all views of a database or user.
2799 2800
     *
     * @param string $database
2801
     *
2802
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2803 2804
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2805
     */
2806
    public function getListViewsSQL($database)
2807
    {
2808
        throw DBALException::notSupported(__METHOD__);
2809 2810
    }

2811
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2812
     * Returns the list of indexes for the current database.
2813
     *
2814 2815
     * The current database parameter is optional but will always be passed
     * when using the SchemaManager API and is the database the given table is in.
2816
     *
2817 2818 2819
     * Attention: Some platforms only support currentDatabase when they
     * are connected with that database. Cross-database information schema
     * requests may be impossible.
2820
     *
2821
     * @param string $table
2822
     * @param string $currentDatabase
2823
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
2824
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2825 2826
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2827 2828
     */
    public function getListTableIndexesSQL($table, $currentDatabase = null)
2829
    {
2830
        throw DBALException::notSupported(__METHOD__);
2831 2832
    }

Benjamin Morel's avatar
Benjamin Morel committed
2833 2834 2835 2836 2837 2838 2839
    /**
     * @param string $table
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2840
    public function getListTableForeignKeysSQL($table)
2841
    {
2842
        throw DBALException::notSupported(__METHOD__);
2843 2844
    }

Benjamin Morel's avatar
Benjamin Morel committed
2845 2846 2847 2848 2849 2850 2851 2852
    /**
     * @param string $name
     * @param string $sql
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2853
    public function getCreateViewSQL($name, $sql)
2854
    {
2855
        throw DBALException::notSupported(__METHOD__);
2856 2857
    }

Benjamin Morel's avatar
Benjamin Morel committed
2858 2859 2860 2861 2862 2863 2864
    /**
     * @param string $name
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2865
    public function getDropViewSQL($name)
2866
    {
2867
        throw DBALException::notSupported(__METHOD__);
2868 2869
    }

2870
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2871
     * Returns the SQL snippet to drop an existing sequence.
2872
     *
jeroendedauw's avatar
jeroendedauw committed
2873
     * @param Sequence|string $sequence
2874 2875
     *
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2876 2877
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2878
     */
2879
    public function getDropSequenceSQL($sequence)
2880
    {
2881
        throw DBALException::notSupported(__METHOD__);
2882 2883
    }

Benjamin Morel's avatar
Benjamin Morel committed
2884 2885 2886 2887 2888 2889 2890
    /**
     * @param string $sequenceName
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
2891
    public function getSequenceNextValSQL($sequenceName)
2892
    {
2893
        throw DBALException::notSupported(__METHOD__);
romanb's avatar
romanb committed
2894
    }
2895

2896
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2897
     * Returns the SQL to create a new database.
2898
     *
Benjamin Morel's avatar
Benjamin Morel committed
2899
     * @param string $database The name of the database that should be created.
2900 2901
     *
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2902 2903
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2904
     */
2905
    public function getCreateDatabaseSQL($database)
2906
    {
2907
        throw DBALException::notSupported(__METHOD__);
2908 2909
    }

romanb's avatar
romanb committed
2910
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2911
     * Returns the SQL to set the transaction isolation level.
romanb's avatar
romanb committed
2912
     *
2913
     * @param integer $level
2914
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
2915
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2916 2917
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
romanb's avatar
romanb committed
2918
     */
2919
    public function getSetTransactionIsolationSQL($level)
romanb's avatar
romanb committed
2920
    {
2921
        throw DBALException::notSupported(__METHOD__);
romanb's avatar
romanb committed
2922
    }
2923

2924
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2925 2926
     * Obtains DBMS specific SQL to be used to create datetime fields in
     * statements like CREATE TABLE.
2927
     *
2928
     * @param array $fieldDeclaration
2929
     *
2930
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2931 2932
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2933
     */
2934
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
2935
    {
2936
        throw DBALException::notSupported(__METHOD__);
2937
    }
2938 2939

    /**
Benjamin Morel's avatar
Benjamin Morel committed
2940
     * Obtains DBMS specific SQL to be used to create datetime with timezone offset fields.
2941
     *
2942
     * @param array $fieldDeclaration
2943
     *
Christophe Coevoet's avatar
Christophe Coevoet committed
2944
     * @return string
2945 2946 2947
     */
    public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
    {
2948
        return $this->getDateTimeTypeDeclarationSQL($fieldDeclaration);
2949
    }
2950 2951


2952
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2953
     * Obtains DBMS specific SQL to be used to create date fields in statements
2954
     * like CREATE TABLE.
2955
     *
2956
     * @param array $fieldDeclaration
2957
     *
2958
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2959 2960
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2961
     */
2962
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
2963
    {
2964
        throw DBALException::notSupported(__METHOD__);
2965
    }
2966

2967
    /**
Benjamin Morel's avatar
Benjamin Morel committed
2968
     * Obtains DBMS specific SQL to be used to create time fields in statements
2969 2970 2971
     * like CREATE TABLE.
     *
     * @param array $fieldDeclaration
2972
     *
2973
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
2974 2975
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
2976
     */
2977
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
2978
    {
2979
        throw DBALException::notSupported(__METHOD__);
2980 2981
    }

Benjamin Morel's avatar
Benjamin Morel committed
2982 2983 2984 2985 2986
    /**
     * @param array $fieldDeclaration
     *
     * @return string
     */
2987 2988 2989 2990 2991
    public function getFloatDeclarationSQL(array $fieldDeclaration)
    {
        return 'DOUBLE PRECISION';
    }

romanb's avatar
romanb committed
2992 2993 2994 2995
    /**
     * Gets the default transaction isolation level of the platform.
     *
     * @return integer The default isolation level.
2996
     *
2997
     * @see Doctrine\DBAL\Connection\TRANSACTION_* constants.
romanb's avatar
romanb committed
2998 2999 3000
     */
    public function getDefaultTransactionIsolationLevel()
    {
3001
        return Connection::TRANSACTION_READ_COMMITTED;
romanb's avatar
romanb committed
3002
    }
3003

3004
    /* supports*() methods */
3005 3006 3007 3008 3009 3010

    /**
     * Whether the platform supports sequences.
     *
     * @return boolean
     */
3011 3012 3013 3014
    public function supportsSequences()
    {
        return false;
    }
3015 3016 3017

    /**
     * Whether the platform supports identity columns.
Benjamin Morel's avatar
Benjamin Morel committed
3018
     *
Pascal Borreli's avatar
Pascal Borreli committed
3019
     * Identity columns are columns that receive an auto-generated value from the
3020 3021 3022 3023
     * database on insert of a row.
     *
     * @return boolean
     */
3024 3025 3026 3027
    public function supportsIdentityColumns()
    {
        return false;
    }
3028

3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059
    /**
     * Whether the platform emulates identity columns through sequences.
     *
     * Some platforms that do not support identity columns natively
     * but support sequences can emulate identity columns by using
     * sequences.
     *
     * @return boolean
     */
    public function usesSequenceEmulatedIdentityColumns()
    {
        return false;
    }

    /**
     * Returns the name of the sequence for a particular identity column in a particular table.
     *
     * @param string $tableName  The name of the table to return the sequence name for.
     * @param string $columnName The name of the identity column in the table to return the sequence name for.
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     *
     * @see    usesSequenceEmulatedIdentityColumns
     */
    public function getIdentitySequenceName($tableName, $columnName)
    {
        throw DBALException::notSupported(__METHOD__);
    }

3060 3061 3062 3063 3064
    /**
     * Whether the platform supports indexes.
     *
     * @return boolean
     */
3065 3066 3067 3068
    public function supportsIndexes()
    {
        return true;
    }
3069

3070 3071 3072 3073 3074 3075 3076 3077 3078 3079
    /**
     * Whether the platform supports partial indexes.
     *
     * @return boolean
     */
    public function supportsPartialIndexes()
    {
        return false;
    }

3080 3081 3082 3083 3084
    /**
     * Whether the platform supports altering tables.
     *
     * @return boolean
     */
3085 3086 3087 3088 3089
    public function supportsAlterTable()
    {
        return true;
    }

3090 3091 3092 3093 3094
    /**
     * Whether the platform supports transactions.
     *
     * @return boolean
     */
3095 3096 3097 3098
    public function supportsTransactions()
    {
        return true;
    }
3099 3100 3101 3102 3103 3104

    /**
     * Whether the platform supports savepoints.
     *
     * @return boolean
     */
3105 3106 3107 3108
    public function supportsSavepoints()
    {
        return true;
    }
3109

3110 3111 3112 3113 3114 3115 3116 3117 3118 3119
    /**
     * Whether the platform supports releasing savepoints.
     *
     * @return boolean
     */
    public function supportsReleaseSavepoints()
    {
        return $this->supportsSavepoints();
    }

3120 3121 3122 3123 3124
    /**
     * Whether the platform supports primary key constraints.
     *
     * @return boolean
     */
3125 3126 3127 3128
    public function supportsPrimaryConstraints()
    {
        return true;
    }
3129 3130

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3131
     * Whether the platform supports foreign key constraints.
3132 3133 3134
     *
     * @return boolean
     */
3135 3136 3137 3138
    public function supportsForeignKeyConstraints()
    {
        return true;
    }
3139 3140

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3141
     * Whether this platform supports onUpdate in foreign key constraints.
3142
     *
3143
     * @return boolean
3144 3145 3146 3147 3148
     */
    public function supportsForeignKeyOnUpdate()
    {
        return ($this->supportsForeignKeyConstraints() && true);
    }
3149

3150 3151
    /**
     * Whether the platform supports database schemas.
3152
     *
3153 3154 3155 3156 3157 3158
     * @return boolean
     */
    public function supportsSchemas()
    {
        return false;
    }
3159

3160
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3161
     * Whether this platform can emulate schemas.
3162 3163 3164 3165 3166
     *
     * Platforms that either support or emulate schemas don't automatically
     * filter a schema for the namespaced elements in {@link
     * AbstractManager#createSchema}.
     *
3167
     * @return boolean
3168 3169 3170 3171 3172 3173
     */
    public function canEmulateSchemas()
    {
        return false;
    }

3174 3175 3176 3177 3178 3179 3180
    /**
     * Returns the default schema name.
     *
     * @return string
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
     */
3181
    public function getDefaultSchemaName()
3182 3183 3184 3185
    {
        throw DBALException::notSupported(__METHOD__);
    }

3186
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3187 3188
     * Whether this platform supports create database.
     *
3189 3190
     * Some databases don't allow to create and drop databases at all or only with certain tools.
     *
3191
     * @return boolean
3192 3193 3194 3195 3196 3197
     */
    public function supportsCreateDropDatabase()
    {
        return true;
    }

3198
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3199
     * Whether the platform supports getting the affected rows of a recent update/delete type query.
3200 3201 3202
     *
     * @return boolean
     */
3203 3204 3205 3206
    public function supportsGettingAffectedRows()
    {
        return true;
    }
3207

3208
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3209
     * Whether this platform support to add inline column comments as postfix.
3210
     *
3211
     * @return boolean
3212 3213 3214 3215 3216 3217 3218
     */
    public function supportsInlineColumnComments()
    {
        return false;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3219
     * Whether this platform support the proprietary syntax "COMMENT ON asset".
3220
     *
3221
     * @return boolean
3222 3223 3224 3225 3226 3227
     */
    public function supportsCommentOnStatement()
    {
        return false;
    }

3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
    /**
     * Does this platform have native guid type.
     *
     * @return boolean
     */
    public function hasNativeGuidType()
    {
        return false;
    }

3238 3239 3240 3241 3242 3243 3244 3245 3246 3247
    /**
     * Does this platform have native JSON type.
     *
     * @return boolean
     */
    public function hasNativeJsonType()
    {
        return false;
    }

3248 3249 3250 3251
    /**
     * @deprecated
     * @todo Remove in 3.0
     */
3252
    public function getIdentityColumnNullInsertSQL()
3253 3254 3255 3256
    {
        return "";
    }

3257
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3258
     * Whether this platform supports views.
3259 3260
     *
     * @return boolean
3261 3262 3263 3264 3265 3266
     */
    public function supportsViews()
    {
        return true;
    }

3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
    /**
     * Does this platform support column collation?
     *
     * @return boolean
     */
    public function supportsColumnCollation()
    {
        return false;
    }

3277
    /**
3278 3279
     * Gets the format string, as accepted by the date() function, that describes
     * the format of a stored datetime value of this platform.
3280
     *
3281
     * @return string The format string.
3282 3283 3284 3285 3286 3287
     */
    public function getDateTimeFormatString()
    {
        return 'Y-m-d H:i:s';
    }

3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298
    /**
     * Gets the format string, as accepted by the date() function, that describes
     * the format of a stored datetime with timezone value of this platform.
     *
     * @return string The format string.
     */
    public function getDateTimeTzFormatString()
    {
        return 'Y-m-d H:i:s';
    }

3299 3300 3301
    /**
     * Gets the format string, as accepted by the date() function, that describes
     * the format of a stored date value of this platform.
3302
     *
3303 3304
     * @return string The format string.
     */
3305 3306
    public function getDateFormatString()
    {
3307
        return 'Y-m-d';
3308
    }
3309

3310 3311 3312
    /**
     * Gets the format string, as accepted by the date() function, that describes
     * the format of a stored time value of this platform.
3313
     *
3314 3315
     * @return string The format string.
     */
3316 3317 3318 3319 3320
    public function getTimeFormatString()
    {
        return 'H:i:s';
    }

3321
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3322
     * Adds an driver-specific LIMIT clause to the query.
3323
     *
Benjamin Morel's avatar
Benjamin Morel committed
3324 3325 3326
     * @param string       $query
     * @param integer|null $limit
     * @param integer|null $offset
3327
     *
3328
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
3329 3330
     *
     * @throws DBALException
3331 3332 3333
     */
    final public function modifyLimitQuery($query, $limit, $offset = null)
    {
3334
        if ($limit !== null) {
3335
            $limit = (int) $limit;
3336 3337
        }

3338
        if ($offset !== null) {
3339
            $offset = (int) $offset;
3340 3341 3342 3343

            if ($offset < 0) {
                throw new DBALException("LIMIT argument offset=$offset is not valid");
            }
3344
            if ($offset > 0 && ! $this->supportsLimitOffset()) {
3345 3346
                throw new DBALException(sprintf("Platform %s does not support offset values in limit queries.", $this->getName()));
            }
3347 3348 3349 3350 3351 3352
        }

        return $this->doModifyLimitQuery($query, $limit, $offset);
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3353
     * Adds an driver-specific LIMIT clause to the query.
3354
     *
3355
     * @param string       $query
Benjamin Morel's avatar
Benjamin Morel committed
3356 3357
     * @param integer|null $limit
     * @param integer|null $offset
3358
     *
3359 3360 3361
     * @return string
     */
    protected function doModifyLimitQuery($query, $limit, $offset)
3362
    {
3363
        if ($limit !== null) {
3364
            $query .= ' LIMIT ' . $limit;
3365 3366
        }

3367
        if ($offset !== null) {
3368 3369 3370
            $query .= ' OFFSET ' . $offset;
        }

3371 3372
        return $query;
    }
3373

3374
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3375
     * Whether the database platform support offsets in modify limit clauses.
3376
     *
3377
     * @return boolean
3378 3379 3380 3381 3382 3383
     */
    public function supportsLimitOffset()
    {
        return true;
    }

3384 3385
    /**
     * Gets the character casing of a column in an SQL result set of this platform.
3386
     *
3387
     * @param string $column The column name for which to get the correct character casing.
3388
     *
3389 3390
     * @return string The column name in the character casing used in SQL result sets.
     */
3391
    public function getSQLResultCasing($column)
3392 3393 3394
    {
        return $column;
    }
3395

3396 3397 3398
    /**
     * Makes any fixes to a name of a schema element (table, sequence, ...) that are required
     * by restrictions of the platform, like a maximum length.
3399
     *
3400
     * @param string $schemaElementName
3401
     *
3402 3403 3404 3405 3406 3407
     * @return string
     */
    public function fixSchemaElementName($schemaElementName)
    {
        return $schemaElementName;
    }
3408

3409
    /**
Pascal Borreli's avatar
Pascal Borreli committed
3410
     * Maximum length of any given database identifier, like tables or column names.
3411
     *
3412
     * @return integer
3413 3414 3415 3416 3417 3418
     */
    public function getMaxIdentifierLength()
    {
        return 63;
    }

3419
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3420
     * Returns the insert SQL for an empty insert statement.
3421
     *
3422 3423
     * @param string $tableName
     * @param string $identifierColumnName
3424
     *
Benjamin Morel's avatar
Benjamin Morel committed
3425
     * @return string
3426
     */
3427
    public function getEmptyIdentityInsertSQL($tableName, $identifierColumnName)
3428 3429 3430
    {
        return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (null)';
    }
3431 3432

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3433
     * Generates a Truncate Table SQL statement for a given table.
3434 3435 3436 3437
     *
     * Cascade is not supported on many platforms but would optionally cascade the truncate by
     * following the foreign keys.
     *
Benjamin Morel's avatar
Benjamin Morel committed
3438 3439
     * @param string  $tableName
     * @param boolean $cascade
3440
     *
3441 3442
     * @return string
     */
3443
    public function getTruncateTableSQL($tableName, $cascade = false)
3444
    {
3445 3446 3447
        $tableIdentifier = new Identifier($tableName);

        return 'TRUNCATE ' . $tableIdentifier->getQuotedName($this);
3448
    }
3449 3450 3451

    /**
     * This is for test reasons, many vendors have special requirements for dummy statements.
3452
     *
3453 3454 3455 3456 3457 3458
     * @return string
     */
    public function getDummySelectSQL()
    {
        return 'SELECT 1';
    }
3459 3460

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3461
     * Returns the SQL to create a new savepoint.
3462 3463
     *
     * @param string $savepoint
3464
     *
3465 3466 3467 3468 3469 3470 3471 3472
     * @return string
     */
    public function createSavePoint($savepoint)
    {
        return 'SAVEPOINT ' . $savepoint;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3473
     * Returns the SQL to release a savepoint.
3474 3475
     *
     * @param string $savepoint
3476
     *
3477 3478 3479 3480 3481 3482 3483 3484
     * @return string
     */
    public function releaseSavePoint($savepoint)
    {
        return 'RELEASE SAVEPOINT ' . $savepoint;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3485
     * Returns the SQL to rollback a savepoint.
3486 3487
     *
     * @param string $savepoint
3488
     *
3489 3490 3491 3492 3493 3494
     * @return string
     */
    public function rollbackSavePoint($savepoint)
    {
        return 'ROLLBACK TO SAVEPOINT ' . $savepoint;
    }
3495 3496

    /**
Benjamin Morel's avatar
Benjamin Morel committed
3497
     * Returns the keyword list instance of this platform.
3498
     *
3499
     * @return \Doctrine\DBAL\Platforms\Keywords\KeywordList
Benjamin Morel's avatar
Benjamin Morel committed
3500 3501
     *
     * @throws \Doctrine\DBAL\DBALException If no keyword list is specified.
3502 3503 3504
     */
    final public function getReservedKeywordsList()
    {
3505
        // Check for an existing instantiation of the keywords class.
3506 3507
        if ($this->_keywords) {
            return $this->_keywords;
3508 3509
        }

3510 3511
        $class = $this->getReservedKeywordsClass();
        $keywords = new $class;
3512
        if ( ! $keywords instanceof \Doctrine\DBAL\Platforms\Keywords\KeywordList) {
3513 3514
            throw DBALException::notSupported(__METHOD__);
        }
3515 3516

        // Store the instance so it doesn't need to be generated on every request.
3517
        $this->_keywords = $keywords;
3518

3519 3520
        return $keywords;
    }
3521

3522
    /**
Benjamin Morel's avatar
Benjamin Morel committed
3523
     * Returns the class name of the reserved keywords list.
3524
     *
3525
     * @return string
Benjamin Morel's avatar
Benjamin Morel committed
3526 3527
     *
     * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
3528 3529 3530 3531 3532
     */
    protected function getReservedKeywordsClass()
    {
        throw DBALException::notSupported(__METHOD__);
    }
3533 3534

    /**
3535 3536 3537 3538
     * Quotes a literal string.
     * This method is NOT meant to fix SQL injections!
     * It is only meant to escape this platform's string literal
     * quote character inside the given literal string.
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
     *
     * @param string $str The literal string to be quoted.
     *
     * @return string The quoted literal string.
     */
    public function quoteStringLiteral($str)
    {
        $c = $this->getStringLiteralQuoteCharacter();

        return $c . str_replace($c, $c . $c, $str) . $c;
    }

    /**
     * Gets the character used for string literal quoting.
     *
     * @return string
     */
    public function getStringLiteralQuoteCharacter()
    {
        return "'";
    }
3560
}