Failed Conditions
Push — master ( 30b923...92920e )
by Marco
19s queued 13s
created

AbstractPlatform::getDropTableSQL()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.8142

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 28
ccs 10
cts 15
cp 0.6667
rs 8.8333
c 0
b 0
f 0
cc 7
nc 10
nop 1
crap 8.8142
1
<?php
2
3
namespace Doctrine\DBAL\Platforms;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\DBAL\DBALException;
7
use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
8
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
9
use Doctrine\DBAL\Event\SchemaAlterTableEventArgs;
10
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
11
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
12
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
13
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
14
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
15
use Doctrine\DBAL\Events;
16
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
17
use Doctrine\DBAL\Schema\Column;
18
use Doctrine\DBAL\Schema\ColumnDiff;
19
use Doctrine\DBAL\Schema\Constraint;
20
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
21
use Doctrine\DBAL\Schema\Identifier;
22
use Doctrine\DBAL\Schema\Index;
23
use Doctrine\DBAL\Schema\Sequence;
24
use Doctrine\DBAL\Schema\Table;
25
use Doctrine\DBAL\Schema\TableDiff;
26
use Doctrine\DBAL\TransactionIsolationLevel;
27
use Doctrine\DBAL\Types;
28
use Doctrine\DBAL\Types\Type;
29
use InvalidArgumentException;
30
use UnexpectedValueException;
31
use const E_USER_DEPRECATED;
32
use function addcslashes;
33
use function array_map;
34
use function array_merge;
35
use function array_unique;
36
use function array_values;
37
use function assert;
38
use function count;
39
use function explode;
40
use function func_get_arg;
41
use function func_get_args;
42
use function func_num_args;
43
use function implode;
44
use function in_array;
45
use function is_array;
46
use function is_bool;
47
use function is_int;
48
use function is_string;
49
use function preg_quote;
50
use function preg_replace;
51
use function sprintf;
52
use function str_replace;
53
use function strlen;
54
use function strpos;
55
use function strtolower;
56
use function strtoupper;
57
use function trigger_error;
58
59
/**
60
 * Base class for all DatabasePlatforms. The DatabasePlatforms are the central
61
 * point of abstraction of platform-specific behaviors, features and SQL dialects.
62
 * They are a passive source of information.
63
 *
64
 * @todo Remove any unnecessary methods.
65
 */
66
abstract class AbstractPlatform
67
{
68
    public const CREATE_INDEXES = 1;
69
70
    public const CREATE_FOREIGNKEYS = 2;
71
72
    /**
73
     * @deprecated Use DateIntervalUnit::INTERVAL_UNIT_SECOND.
74
     */
75
    public const DATE_INTERVAL_UNIT_SECOND = DateIntervalUnit::SECOND;
76
77
    /**
78
     * @deprecated Use DateIntervalUnit::MINUTE.
79
     */
80
    public const DATE_INTERVAL_UNIT_MINUTE = DateIntervalUnit::MINUTE;
81
82
    /**
83
     * @deprecated Use DateIntervalUnit::HOUR.
84
     */
85
    public const DATE_INTERVAL_UNIT_HOUR = DateIntervalUnit::HOUR;
86
87
    /**
88
     * @deprecated Use DateIntervalUnit::DAY.
89
     */
90
    public const DATE_INTERVAL_UNIT_DAY = DateIntervalUnit::DAY;
91
92
    /**
93
     * @deprecated Use DateIntervalUnit::WEEK.
94
     */
95
    public const DATE_INTERVAL_UNIT_WEEK = DateIntervalUnit::WEEK;
96
97
    /**
98
     * @deprecated Use DateIntervalUnit::MONTH.
99
     */
100
    public const DATE_INTERVAL_UNIT_MONTH = DateIntervalUnit::MONTH;
101
102
    /**
103
     * @deprecated Use DateIntervalUnit::QUARTER.
104
     */
105
    public const DATE_INTERVAL_UNIT_QUARTER = DateIntervalUnit::QUARTER;
106
107
    /**
108
     * @deprecated Use DateIntervalUnit::QUARTER.
109
     */
110
    public const DATE_INTERVAL_UNIT_YEAR = DateIntervalUnit::YEAR;
111
112
    /**
113
     * @deprecated Use TrimMode::UNSPECIFIED.
114
     */
115
    public const TRIM_UNSPECIFIED = TrimMode::UNSPECIFIED;
116
117
    /**
118
     * @deprecated Use TrimMode::LEADING.
119
     */
120
    public const TRIM_LEADING = TrimMode::LEADING;
121
122
    /**
123
     * @deprecated Use TrimMode::TRAILING.
124
     */
125
    public const TRIM_TRAILING = TrimMode::TRAILING;
126
127
    /**
128
     * @deprecated Use TrimMode::BOTH.
129
     */
130
    public const TRIM_BOTH = TrimMode::BOTH;
131
132
    /** @var string[]|null */
133
    protected $doctrineTypeMapping = null;
134
135
    /**
136
     * Contains a list of all columns that should generate parseable column comments for type-detection
137
     * in reverse engineering scenarios.
138
     *
139
     * @var string[]|null
140
     */
141
    protected $doctrineTypeComments = null;
142
143
    /** @var EventManager */
144
    protected $_eventManager;
145
146
    /**
147
     * Holds the KeywordList instance for the current platform.
148
     *
149
     * @var KeywordList|null
150
     */
151
    protected $_keywords;
152
153 95434
    public function __construct()
154
    {
155 95434
    }
156
157
    /**
158
     * Sets the EventManager used by the Platform.
159
     */
160 2419
    public function setEventManager(EventManager $eventManager)
161
    {
162 2419
        $this->_eventManager = $eventManager;
163 2419
    }
164
165
    /**
166
     * Gets the EventManager used by the Platform.
167
     *
168
     * @return EventManager
169
     */
170 1617
    public function getEventManager()
171
    {
172 1617
        return $this->_eventManager;
173
    }
174
175
    /**
176
     * Returns the SQL snippet that declares a boolean column.
177
     *
178
     * @param mixed[] $columnDef
179
     *
180
     * @return string
181
     */
182
    abstract public function getBooleanTypeDeclarationSQL(array $columnDef);
183
184
    /**
185
     * Returns the SQL snippet that declares a 4 byte integer column.
186
     *
187
     * @param mixed[] $columnDef
188
     *
189
     * @return string
190
     */
191
    abstract public function getIntegerTypeDeclarationSQL(array $columnDef);
192
193
    /**
194
     * Returns the SQL snippet that declares an 8 byte integer column.
195
     *
196
     * @param mixed[] $columnDef
197
     *
198
     * @return string
199
     */
200
    abstract public function getBigIntTypeDeclarationSQL(array $columnDef);
201
202
    /**
203
     * Returns the SQL snippet that declares a 2 byte integer column.
204
     *
205
     * @param mixed[] $columnDef
206
     *
207
     * @return string
208
     */
209
    abstract public function getSmallIntTypeDeclarationSQL(array $columnDef);
210
211
    /**
212
     * Returns the SQL snippet that declares common properties of an integer column.
213
     *
214
     * @param mixed[] $columnDef
215
     *
216
     * @return string
217
     */
218
    abstract protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef);
219
220
    /**
221
     * Lazy load Doctrine Type Mappings.
222
     *
223
     * @return void
224
     */
225
    abstract protected function initializeDoctrineTypeMappings();
226
227
    /**
228
     * Initializes Doctrine Type Mappings with the platform defaults
229
     * and with all additional type mappings.
230
     *
231
     * @return void
232
     */
233 2687
    private function initializeAllDoctrineTypeMappings()
234
    {
235 2687
        $this->initializeDoctrineTypeMappings();
236
237 2687
        foreach (Type::getTypesMap() as $typeName => $className) {
238 2687
            foreach (Type::getType($typeName)->getMappedDatabaseTypes($this) as $dbType) {
239 1584
                $this->doctrineTypeMapping[$dbType] = $typeName;
240
            }
241
        }
242 2687
    }
243
244
    /**
245
     * Returns the SQL snippet used to declare a VARCHAR column type.
246
     *
247
     * @param mixed[] $field
248
     *
249
     * @return string
250
     */
251 9474
    public function getVarcharTypeDeclarationSQL(array $field)
252
    {
253 9474
        if (! isset($field['length'])) {
254 2234
            $field['length'] = $this->getVarcharDefaultLength();
255
        }
256
257 9474
        $fixed = $field['fixed'] ?? false;
258
259 9474
        $maxLength = $fixed
260 1337
            ? $this->getCharMaxLength()
261 9474
            : $this->getVarcharMaxLength();
262
263 9474
        if ($field['length'] > $maxLength) {
264
            return $this->getClobTypeDeclarationSQL($field);
265
        }
266
267 9474
        return $this->getVarcharTypeDeclarationSQLSnippet($field['length'], $fixed);
268
    }
269
270
    /**
271
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
272
     *
273
     * @param mixed[] $field The column definition.
274
     *
275
     * @return string
276
     */
277 862
    public function getBinaryTypeDeclarationSQL(array $field)
278
    {
279 862
        if (! isset($field['length'])) {
280 512
            $field['length'] = $this->getBinaryDefaultLength();
281
        }
282
283 862
        $fixed = $field['fixed'] ?? false;
284
285 862
        $maxLength = $this->getBinaryMaxLength();
286
287 862
        if ($field['length'] > $maxLength) {
288 494
            if ($maxLength > 0) {
289 324
                @trigger_error(sprintf(
290 324
                    'Binary field length %d is greater than supported by the platform (%d). Reduce the field length or use a BLOB field instead.',
291 324
                    $field['length'],
292 324
                    $maxLength
293 324
                ), E_USER_DEPRECATED);
294
            }
295
296 494
            return $this->getBlobTypeDeclarationSQL($field);
297
        }
298
299 530
        return $this->getBinaryTypeDeclarationSQLSnippet($field['length'], $fixed);
300
    }
301
302
    /**
303
     * Returns the SQL snippet to declare a GUID/UUID field.
304
     *
305
     * By default this maps directly to a CHAR(36) and only maps to more
306
     * special datatypes when the underlying databases support this datatype.
307
     *
308
     * @param mixed[] $field
309
     *
310
     * @return string
311
     */
312 176
    public function getGuidTypeDeclarationSQL(array $field)
313
    {
314 176
        $field['length'] = 36;
315 176
        $field['fixed']  = true;
316
317 176
        return $this->getVarcharTypeDeclarationSQL($field);
318
    }
319
320
    /**
321
     * Returns the SQL snippet to declare a JSON field.
322
     *
323
     * By default this maps directly to a CLOB and only maps to more
324
     * special datatypes when the underlying databases support this datatype.
325
     *
326
     * @param mixed[] $field
327
     *
328
     * @return string
329
     */
330 560
    public function getJsonTypeDeclarationSQL(array $field)
331
    {
332 560
        return $this->getClobTypeDeclarationSQL($field);
333
    }
334
335
    /**
336
     * @param int  $length
337
     * @param bool $fixed
338
     *
339
     * @return string
340
     *
341
     * @throws DBALException If not supported on this platform.
342
     */
343
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
0 ignored issues
show
Unused Code introduced by Steve Müller
The parameter $fixed is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

343
    protected function getVarcharTypeDeclarationSQLSnippet($length, /** @scrutinizer ignore-unused */ $fixed)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
344
    {
345
        throw DBALException::notSupported('VARCHARs not supported by Platform.');
346
    }
347
348
    /**
349
     * Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
350
     *
351
     * @param int  $length The length of the column.
352
     * @param bool $fixed  Whether the column length is fixed.
353
     *
354
     * @return string
355
     *
356
     * @throws DBALException If not supported on this platform.
357
     */
358
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
0 ignored issues
show
Unused Code introduced by Benjamin Eberlei
The parameter $fixed is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

358
    protected function getBinaryTypeDeclarationSQLSnippet($length, /** @scrutinizer ignore-unused */ $fixed)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
359
    {
360
        throw DBALException::notSupported('BINARY/VARBINARY column types are not supported by this platform.');
361
    }
362
363
    /**
364
     * Returns the SQL snippet used to declare a CLOB column type.
365
     *
366
     * @param mixed[] $field
367
     *
368
     * @return string
369
     */
370
    abstract public function getClobTypeDeclarationSQL(array $field);
371
372
    /**
373
     * Returns the SQL Snippet used to declare a BLOB column type.
374
     *
375
     * @param mixed[] $field
376
     *
377
     * @return string
378
     */
379
    abstract public function getBlobTypeDeclarationSQL(array $field);
380
381
    /**
382
     * Gets the name of the platform.
383
     *
384
     * @return string
385
     */
386
    abstract public function getName();
387
388
    /**
389
     * Registers a doctrine type to be used in conjunction with a column type of this platform.
390
     *
391
     * @param string $dbType
392
     * @param string $doctrineType
393
     *
394
     * @throws DBALException If the type is not found.
395
     */
396 1492
    public function registerDoctrineTypeMapping($dbType, $doctrineType)
397
    {
398 1492
        if ($this->doctrineTypeMapping === null) {
399 1485
            $this->initializeAllDoctrineTypeMappings();
400
        }
401
402 1492
        if (! Types\Type::hasType($doctrineType)) {
403 486
            throw DBALException::typeNotFound($doctrineType);
404
        }
405
406 1006
        $dbType                             = strtolower($dbType);
407 1006
        $this->doctrineTypeMapping[$dbType] = $doctrineType;
408
409 1006
        $doctrineType = Type::getType($doctrineType);
410
411 1006
        if (! $doctrineType->requiresSQLCommentHint($this)) {
412 520
            return;
413
        }
414
415 486
        $this->markDoctrineTypeCommented($doctrineType);
416 486
    }
417
418
    /**
419
     * Gets the Doctrine type that is mapped for the given database column type.
420
     *
421
     * @param string $dbType
422
     *
423
     * @return string
424
     *
425
     * @throws DBALException
426
     */
427 3069
    public function getDoctrineTypeMapping($dbType)
428
    {
429 3069
        if ($this->doctrineTypeMapping === null) {
430 581
            $this->initializeAllDoctrineTypeMappings();
431
        }
432
433 3069
        $dbType = strtolower($dbType);
434
435 3069
        if (! isset($this->doctrineTypeMapping[$dbType])) {
436 486
            throw new DBALException('Unknown database type ' . $dbType . ' requested, ' . static::class . ' may not support it.');
437
        }
438
439 2583
        return $this->doctrineTypeMapping[$dbType];
440
    }
441
442
    /**
443
     * Checks if a database type is currently supported by this platform.
444
     *
445
     * @param string $dbType
446
     *
447
     * @return bool
448
     */
449 676
    public function hasDoctrineTypeMappingFor($dbType)
450
    {
451 676
        if ($this->doctrineTypeMapping === null) {
452 621
            $this->initializeAllDoctrineTypeMappings();
453
        }
454
455 676
        $dbType = strtolower($dbType);
456
457 676
        return isset($this->doctrineTypeMapping[$dbType]);
458
    }
459
460
    /**
461
     * Initializes the Doctrine Type comments instance variable for in_array() checks.
462
     *
463
     * @return void
464
     */
465 24404
    protected function initializeCommentedDoctrineTypes()
466
    {
467 24404
        $this->doctrineTypeComments = [];
468
469 24404
        foreach (Type::getTypesMap() as $typeName => $className) {
470 24404
            $type = Type::getType($typeName);
471
472 24404
            if (! $type->requiresSQLCommentHint($this)) {
473 24404
                continue;
474
            }
475
476 24404
            $this->doctrineTypeComments[] = $typeName;
477
        }
478 24404
    }
479
480
    /**
481
     * Is it necessary for the platform to add a parsable type comment to allow reverse engineering the given type?
482
     *
483
     * @return bool
484
     */
485 28445
    public function isCommentedDoctrineType(Type $doctrineType)
486
    {
487 28445
        if ($this->doctrineTypeComments === null) {
488 23918
            $this->initializeCommentedDoctrineTypes();
489
        }
490
491 28445
        assert(is_array($this->doctrineTypeComments));
492
493 28445
        return in_array($doctrineType->getName(), $this->doctrineTypeComments);
494
    }
495
496
    /**
497
     * Marks this type as to be commented in ALTER TABLE and CREATE TABLE statements.
498
     *
499
     * @param string|Type $doctrineType
500
     *
501
     * @return void
502
     */
503 486
    public function markDoctrineTypeCommented($doctrineType)
504
    {
505 486
        if ($this->doctrineTypeComments === null) {
506 486
            $this->initializeCommentedDoctrineTypes();
507
        }
508
509 486
        assert(is_array($this->doctrineTypeComments));
510
511 486
        $this->doctrineTypeComments[] = $doctrineType instanceof Type ? $doctrineType->getName() : $doctrineType;
512 486
    }
513
514
    /**
515
     * Gets the comment to append to a column comment that helps parsing this type in reverse engineering.
516
     *
517
     * @return string
518
     */
519 1290
    public function getDoctrineTypeComment(Type $doctrineType)
520
    {
521 1290
        return '(DC2Type:' . $doctrineType->getName() . ')';
522
    }
523
524
    /**
525
     * Gets the comment of a passed column modified by potential doctrine type comment hints.
526
     *
527
     * @return string|null
528
     */
529 15838
    protected function getColumnComment(Column $column)
530
    {
531 15838
        $comment = $column->getComment();
532
533 15838
        if ($this->isCommentedDoctrineType($column->getType())) {
534 1290
            $comment .= $this->getDoctrineTypeComment($column->getType());
535
        }
536
537 15838
        return $comment;
538
    }
539
540
    /**
541
     * Gets the character used for identifier quoting.
542
     *
543
     * @return string
544
     */
545 8261
    public function getIdentifierQuoteCharacter()
546
    {
547 8261
        return '"';
548
    }
549
550
    /**
551
     * Gets the string portion that starts an SQL comment.
552
     *
553
     * @return string
554
     */
555
    public function getSqlCommentStartString()
556
    {
557
        return '--';
558
    }
559
560
    /**
561
     * Gets the string portion that ends an SQL comment.
562
     *
563
     * @return string
564
     */
565
    public function getSqlCommentEndString()
566
    {
567
        return "\n";
568
    }
569
570
    /**
571
     * Gets the maximum length of a char field.
572
     */
573 1222
    public function getCharMaxLength() : int
574
    {
575 1222
        return $this->getVarcharMaxLength();
576
    }
577
578
    /**
579
     * Gets the maximum length of a varchar field.
580
     *
581
     * @return int
582
     */
583 3515
    public function getVarcharMaxLength()
584
    {
585 3515
        return 4000;
586
    }
587
588
    /**
589
     * Gets the default length of a varchar field.
590
     *
591
     * @return int
592
     */
593 1910
    public function getVarcharDefaultLength()
594
    {
595 1910
        return 255;
596
    }
597
598
    /**
599
     * Gets the maximum length of a binary field.
600
     *
601
     * @return int
602
     */
603
    public function getBinaryMaxLength()
604
    {
605
        return 4000;
606
    }
607
608
    /**
609
     * Gets the default length of a binary field.
610
     *
611
     * @return int
612
     */
613 396
    public function getBinaryDefaultLength()
614
    {
615 396
        return 255;
616
    }
617
618
    /**
619
     * Gets all SQL wildcard characters of the platform.
620
     *
621
     * @return string[]
622
     */
623
    public function getWildcards()
624
    {
625
        return ['%', '_'];
626
    }
627
628
    /**
629
     * Returns the regular expression operator.
630
     *
631
     * @return string
632
     *
633
     * @throws DBALException If not supported on this platform.
634
     */
635 135
    public function getRegexpExpression()
636
    {
637 135
        throw DBALException::notSupported(__METHOD__);
638
    }
639
640
    /**
641
     * Returns the global unique identifier expression.
642
     *
643
     * @deprecated Use application-generated UUIDs instead
644
     *
645
     * @return string
646
     *
647
     * @throws DBALException If not supported on this platform.
648
     */
649
    public function getGuidExpression()
650
    {
651
        throw DBALException::notSupported(__METHOD__);
652
    }
653
654
    /**
655
     * Returns the SQL snippet to get the average value of a column.
656
     *
657
     * @param string $column The column to use.
658
     *
659
     * @return string Generated SQL including an AVG aggregate function.
660
     */
661
    public function getAvgExpression($column)
662
    {
663
        return 'AVG(' . $column . ')';
664
    }
665
666
    /**
667
     * Returns the SQL snippet to get the number of rows (without a NULL value) of a column.
668
     *
669
     * If a '*' is used instead of a column the number of selected rows is returned.
670
     *
671
     * @param string|int $column The column to use.
672
     *
673
     * @return string Generated SQL including a COUNT aggregate function.
674
     */
675
    public function getCountExpression($column)
676
    {
677
        return 'COUNT(' . $column . ')';
678
    }
679
680
    /**
681
     * Returns the SQL snippet to get the highest value of a column.
682
     *
683
     * @param string $column The column to use.
684
     *
685
     * @return string Generated SQL including a MAX aggregate function.
686
     */
687
    public function getMaxExpression($column)
688
    {
689
        return 'MAX(' . $column . ')';
690
    }
691
692
    /**
693
     * Returns the SQL snippet to get the lowest value of a column.
694
     *
695
     * @param string $column The column to use.
696
     *
697
     * @return string Generated SQL including a MIN aggregate function.
698
     */
699
    public function getMinExpression($column)
700
    {
701
        return 'MIN(' . $column . ')';
702
    }
703
704
    /**
705
     * Returns the SQL snippet to get the total sum of a column.
706
     *
707
     * @param string $column The column to use.
708
     *
709
     * @return string Generated SQL including a SUM aggregate function.
710
     */
711
    public function getSumExpression($column)
712
    {
713
        return 'SUM(' . $column . ')';
714
    }
715
716
    // scalar functions
717
718
    /**
719
     * Returns the SQL snippet to get the md5 sum of a field.
720
     *
721
     * Note: Not SQL92, but common functionality.
722
     *
723
     * @param string $column
724
     *
725
     * @return string
726
     */
727
    public function getMd5Expression($column)
728
    {
729
        return 'MD5(' . $column . ')';
730
    }
731
732
    /**
733
     * Returns the SQL snippet to get the length of a text field.
734
     *
735
     * @param string $column
736
     *
737
     * @return string
738
     */
739
    public function getLengthExpression($column)
740
    {
741
        return 'LENGTH(' . $column . ')';
742
    }
743
744
    /**
745
     * Returns the SQL snippet to get the squared value of a column.
746
     *
747
     * @param string $column The column to use.
748
     *
749
     * @return string Generated SQL including an SQRT aggregate function.
750
     */
751
    public function getSqrtExpression($column)
752
    {
753
        return 'SQRT(' . $column . ')';
754
    }
755
756
    /**
757
     * Returns the SQL snippet to round a numeric field to the number of decimals specified.
758
     *
759
     * @param string $column
760
     * @param int    $decimals
761
     *
762
     * @return string
763
     */
764
    public function getRoundExpression($column, $decimals = 0)
765
    {
766
        return 'ROUND(' . $column . ', ' . $decimals . ')';
767
    }
768
769
    /**
770
     * Returns the SQL snippet to get the remainder of the division operation $expression1 / $expression2.
771
     *
772
     * @param string $expression1
773
     * @param string $expression2
774
     *
775
     * @return string
776
     */
777
    public function getModExpression($expression1, $expression2)
778
    {
779
        return 'MOD(' . $expression1 . ', ' . $expression2 . ')';
780
    }
781
782
    /**
783
     * Returns the SQL snippet to trim a string.
784
     *
785
     * @param string      $str  The expression to apply the trim to.
786
     * @param int         $mode The position of the trim (leading/trailing/both).
787
     * @param string|bool $char The char to trim, has to be quoted already. Defaults to space.
788
     *
789
     * @return string
790
     */
791 864
    public function getTrimExpression($str, $mode = TrimMode::UNSPECIFIED, $char = false)
792
    {
793 864
        $expression = '';
794
795 864
        switch ($mode) {
796
            case TrimMode::LEADING:
797 216
                $expression = 'LEADING ';
798 216
                break;
799
800
            case TrimMode::TRAILING:
801 216
                $expression = 'TRAILING ';
802 216
                break;
803
804
            case TrimMode::BOTH:
805 216
                $expression = 'BOTH ';
806 216
                break;
807
        }
808
809 864
        if ($char !== false) {
810 672
            $expression .= $char . ' ';
0 ignored issues
show
Bug introduced by Steve Müller
Are you sure $char of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

810
            $expression .= /** @scrutinizer ignore-type */ $char . ' ';
Loading history...
811
        }
812
813 864
        if ($mode || $char !== false) {
814 816
            $expression .= 'FROM ';
815
        }
816
817 864
        return 'TRIM(' . $expression . $str . ')';
818
    }
819
820
    /**
821
     * Returns the SQL snippet to trim trailing space characters from the expression.
822
     *
823
     * @param string $str Literal string or column name.
824
     *
825
     * @return string
826
     */
827 108
    public function getRtrimExpression($str)
828
    {
829 108
        return 'RTRIM(' . $str . ')';
830
    }
831
832
    /**
833
     * Returns the SQL snippet to trim leading space characters from the expression.
834
     *
835
     * @param string $str Literal string or column name.
836
     *
837
     * @return string
838
     */
839 108
    public function getLtrimExpression($str)
840
    {
841 108
        return 'LTRIM(' . $str . ')';
842
    }
843
844
    /**
845
     * Returns the SQL snippet to change all characters from the expression to uppercase,
846
     * according to the current character set mapping.
847
     *
848
     * @param string $str Literal string or column name.
849
     *
850
     * @return string
851
     */
852
    public function getUpperExpression($str)
853
    {
854
        return 'UPPER(' . $str . ')';
855
    }
856
857
    /**
858
     * Returns the SQL snippet to change all characters from the expression to lowercase,
859
     * according to the current character set mapping.
860
     *
861
     * @param string $str Literal string or column name.
862
     *
863
     * @return string
864
     */
865
    public function getLowerExpression($str)
866
    {
867
        return 'LOWER(' . $str . ')';
868
    }
869
870
    /**
871
     * Returns the SQL snippet to get the position of the first occurrence of substring $substr in string $str.
872
     *
873
     * @param string    $str      Literal string.
874
     * @param string    $substr   Literal string to find.
875
     * @param int|false $startPos Position to start at, beginning of string by default.
876
     *
877
     * @return string
878
     *
879
     * @throws DBALException If not supported on this platform.
880
     */
881
    public function getLocateExpression($str, $substr, $startPos = false)
0 ignored issues
show
Unused Code introduced by beberlei
The parameter $substr is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

881
    public function getLocateExpression($str, /** @scrutinizer ignore-unused */ $substr, $startPos = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by beberlei
The parameter $startPos is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

881
    public function getLocateExpression($str, $substr, /** @scrutinizer ignore-unused */ $startPos = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
882
    {
883
        throw DBALException::notSupported(__METHOD__);
884
    }
885
886
    /**
887
     * Returns the SQL snippet to get the current system date.
888
     *
889
     * @return string
890
     */
891
    public function getNowExpression()
892
    {
893
        return 'NOW()';
894
    }
895
896
    /**
897
     * Returns a SQL snippet to get a substring inside an SQL statement.
898
     *
899
     * Note: Not SQL92, but common functionality.
900
     *
901
     * SQLite only supports the 2 parameter variant of this function.
902
     *
903
     * @param string   $value  An sql string literal or column name/alias.
904
     * @param int      $from   Where to start the substring portion.
905
     * @param int|null $length The substring portion length.
906
     *
907
     * @return string
908
     */
909
    public function getSubstringExpression($value, $from, $length = null)
910
    {
911
        if ($length === null) {
912
            return 'SUBSTRING(' . $value . ' FROM ' . $from . ')';
913
        }
914
915
        return 'SUBSTRING(' . $value . ' FROM ' . $from . ' FOR ' . $length . ')';
916
    }
917
918
    /**
919
     * Returns a SQL snippet to concatenate the given expressions.
920
     *
921
     * Accepts an arbitrary number of string parameters. Each parameter must contain an expression.
922
     *
923
     * @return string
924
     */
925 162
    public function getConcatExpression()
926
    {
927 162
        return implode(' || ', func_get_args());
928
    }
929
930
    /**
931
     * Returns the SQL for a logical not.
932
     *
933
     * Example:
934
     * <code>
935
     * $q = new Doctrine_Query();
936
     * $e = $q->expr;
937
     * $q->select('*')->from('table')
938
     *   ->where($e->eq('id', $e->not('null'));
939
     * </code>
940
     *
941
     * @param string $expression
942
     *
943
     * @return string The logical expression.
944
     */
945
    public function getNotExpression($expression)
946
    {
947
        return 'NOT(' . $expression . ')';
948
    }
949
950
    /**
951
     * Returns the SQL that checks if an expression is null.
952
     *
953
     * @param string $expression The expression that should be compared to null.
954
     *
955
     * @return string The logical expression.
956
     */
957 108
    public function getIsNullExpression($expression)
958
    {
959 108
        return $expression . ' IS NULL';
960
    }
961
962
    /**
963
     * Returns the SQL that checks if an expression is not null.
964
     *
965
     * @param string $expression The expression that should be compared to null.
966
     *
967
     * @return string The logical expression.
968
     */
969
    public function getIsNotNullExpression($expression)
970
    {
971
        return $expression . ' IS NOT NULL';
972
    }
973
974
    /**
975
     * Returns the SQL that checks if an expression evaluates to a value between two values.
976
     *
977
     * The parameter $expression is checked if it is between $value1 and $value2.
978
     *
979
     * Note: There is a slight difference in the way BETWEEN works on some databases.
980
     * http://www.w3schools.com/sql/sql_between.asp. If you want complete database
981
     * independence you should avoid using between().
982
     *
983
     * @param string $expression The value to compare to.
984
     * @param string $value1     The lower value to compare with.
985
     * @param string $value2     The higher value to compare with.
986
     *
987
     * @return string The logical expression.
988
     */
989
    public function getBetweenExpression($expression, $value1, $value2)
990
    {
991
        return $expression . ' BETWEEN ' . $value1 . ' AND ' . $value2;
992
    }
993
994
    /**
995
     * Returns the SQL to get the arccosine of a value.
996
     *
997
     * @param string $value
998
     *
999
     * @return string
1000
     */
1001
    public function getAcosExpression($value)
1002
    {
1003
        return 'ACOS(' . $value . ')';
1004
    }
1005
1006
    /**
1007
     * Returns the SQL to get the sine of a value.
1008
     *
1009
     * @param string $value
1010
     *
1011
     * @return string
1012
     */
1013
    public function getSinExpression($value)
1014
    {
1015
        return 'SIN(' . $value . ')';
1016
    }
1017
1018
    /**
1019
     * Returns the SQL to get the PI value.
1020
     *
1021
     * @return string
1022
     */
1023
    public function getPiExpression()
1024
    {
1025
        return 'PI()';
1026
    }
1027
1028
    /**
1029
     * Returns the SQL to get the cosine of a value.
1030
     *
1031
     * @param string $value
1032
     *
1033
     * @return string
1034
     */
1035
    public function getCosExpression($value)
1036
    {
1037
        return 'COS(' . $value . ')';
1038
    }
1039
1040
    /**
1041
     * Returns the SQL to calculate the difference in days between the two passed dates.
1042
     *
1043
     * Computes diff = date1 - date2.
1044
     *
1045
     * @param string $date1
1046
     * @param string $date2
1047
     *
1048
     * @return string
1049
     *
1050
     * @throws DBALException If not supported on this platform.
1051
     */
1052
    public function getDateDiffExpression($date1, $date2)
0 ignored issues
show
Unused Code introduced by Benjamin Eberlei
The parameter $date1 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1052
    public function getDateDiffExpression(/** @scrutinizer ignore-unused */ $date1, $date2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Benjamin Eberlei
The parameter $date2 is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1052
    public function getDateDiffExpression($date1, /** @scrutinizer ignore-unused */ $date2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1053
    {
1054
        throw DBALException::notSupported(__METHOD__);
1055
    }
1056
1057
    /**
1058
     * Returns the SQL to add the number of given seconds to a date.
1059
     *
1060
     * @param string $date
1061
     * @param int    $seconds
1062
     *
1063
     * @return string
1064
     *
1065
     * @throws DBALException If not supported on this platform.
1066
     */
1067 162
    public function getDateAddSecondsExpression($date, $seconds)
1068
    {
1069 162
        return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, DateIntervalUnit::SECOND);
1070
    }
1071
1072
    /**
1073
     * Returns the SQL to subtract the number of given seconds from a date.
1074
     *
1075
     * @param string $date
1076
     * @param int    $seconds
1077
     *
1078
     * @return string
1079
     *
1080
     * @throws DBALException If not supported on this platform.
1081
     */
1082 162
    public function getDateSubSecondsExpression($date, $seconds)
1083
    {
1084 162
        return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, DateIntervalUnit::SECOND);
1085
    }
1086
1087
    /**
1088
     * Returns the SQL to add the number of given minutes to a date.
1089
     *
1090
     * @param string $date
1091
     * @param int    $minutes
1092
     *
1093
     * @return string
1094
     *
1095
     * @throws DBALException If not supported on this platform.
1096
     */
1097 162
    public function getDateAddMinutesExpression($date, $minutes)
1098
    {
1099 162
        return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, DateIntervalUnit::MINUTE);
1100
    }
1101
1102
    /**
1103
     * Returns the SQL to subtract the number of given minutes from a date.
1104
     *
1105
     * @param string $date
1106
     * @param int    $minutes
1107
     *
1108
     * @return string
1109
     *
1110
     * @throws DBALException If not supported on this platform.
1111
     */
1112 162
    public function getDateSubMinutesExpression($date, $minutes)
1113
    {
1114 162
        return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, DateIntervalUnit::MINUTE);
1115
    }
1116
1117
    /**
1118
     * Returns the SQL to add the number of given hours to a date.
1119
     *
1120
     * @param string $date
1121
     * @param int    $hours
1122
     *
1123
     * @return string
1124
     *
1125
     * @throws DBALException If not supported on this platform.
1126
     */
1127 162
    public function getDateAddHourExpression($date, $hours)
1128
    {
1129 162
        return $this->getDateArithmeticIntervalExpression($date, '+', $hours, DateIntervalUnit::HOUR);
1130
    }
1131
1132
    /**
1133
     * Returns the SQL to subtract the number of given hours to a date.
1134
     *
1135
     * @param string $date
1136
     * @param int    $hours
1137
     *
1138
     * @return string
1139
     *
1140
     * @throws DBALException If not supported on this platform.
1141
     */
1142 162