Failed Conditions
Pull Request — master (#2825)
by Sébastien
11:02
created

MySqlPlatform::getUnsignedDeclaration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Platforms;
21
22
use Doctrine\DBAL\Connection;
23
use Doctrine\DBAL\Schema\Identifier;
24
use Doctrine\DBAL\Schema\Index;
25
use Doctrine\DBAL\Schema\Table;
26
use Doctrine\DBAL\Schema\TableDiff;
27
use Doctrine\DBAL\Types\BlobType;
28
use Doctrine\DBAL\Types\TextType;
29
use Doctrine\DBAL\Types\Type;
30
31
/**
32
 * The MySqlPlatform provides the behavior, features and SQL dialect of the
33
 * MySQL database platform. This platform represents a MySQL 5.0 or greater platform that
34
 * uses the InnoDB storage engine.
35
 *
36
 * @since  2.0
37
 * @author Roman Borschel <[email protected]>
38
 * @author Benjamin Eberlei <[email protected]>
39
 * @todo   Rename: MySQLPlatform
40
 */
41
class MySqlPlatform extends AbstractPlatform
42
{
43
    const LENGTH_LIMIT_TINYTEXT   = 255;
44
    const LENGTH_LIMIT_TEXT       = 65535;
45
    const LENGTH_LIMIT_MEDIUMTEXT = 16777215;
46
47
    const LENGTH_LIMIT_TINYBLOB   = 255;
48
    const LENGTH_LIMIT_BLOB       = 65535;
49
    const LENGTH_LIMIT_MEDIUMBLOB = 16777215;
50
51
    /**
52
     * Adds MySQL-specific LIMIT clause to the query
53
     * 18446744073709551615 is 2^64-1 maximum of unsigned BIGINT the biggest limit possible
54
     *
55
     * @param string  $query
56
     * @param integer $limit
57
     * @param integer $offset
58
     *
59
     * @return string
60
     */
61 9
    protected function doModifyLimitQuery($query, $limit, $offset)
62
    {
63 9
        if ($limit !== null) {
64 6
            $query .= ' LIMIT ' . $limit;
65 6
            if ($offset !== null) {
66 6
                $query .= ' OFFSET ' . $offset;
67
            }
68 3
        } elseif ($offset !== null) {
69 3
            $query .= ' LIMIT 18446744073709551615 OFFSET ' . $offset;
70
        }
71
72 9
        return $query;
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78 82
    public function getIdentifierQuoteCharacter()
79
    {
80 82
        return '`';
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86 3
    public function getRegexpExpression()
87
    {
88 3
        return 'RLIKE';
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function getGuidExpression()
95
    {
96
        return 'UUID()';
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 View Code Duplication
    public function getLocateExpression($str, $substr, $startPos = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        if ($startPos == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
105
            return 'LOCATE(' . $substr . ', ' . $str . ')';
106
        }
107
108
        return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114 3
    public function getConcatExpression()
115
    {
116 3
        $args = func_get_args();
117
118 3
        return 'CONCAT(' . join(', ', (array) $args) . ')';
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
125
    {
126
        $function = '+' === $operator ? 'DATE_ADD' : 'DATE_SUB';
127
128
        return $function . '(' . $date . ', INTERVAL ' . $interval . ' ' . $unit . ')';
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134
    public function getDateDiffExpression($date1, $date2)
135
    {
136
        return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142 3
    public function getListDatabasesSQL()
143
    {
144 3
        return 'SHOW DATABASES';
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150
    public function getListTableConstraintsSQL($table)
151
    {
152
        return 'SHOW INDEX FROM ' . $table;
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     *
158
     * Two approaches to listing the table indexes. The information_schema is
159
     * preferred, because it doesn't cause problems with SQL keywords such as "order" or "table".
160
     */
161 6 View Code Duplication
    public function getListTableIndexesSQL($table, $currentDatabase = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
    {
163 6
        if ($currentDatabase) {
164 6
            $currentDatabase = $this->quoteStringLiteral($currentDatabase);
165 6
            $table = $this->quoteStringLiteral($table);
166
167
            return "SELECT TABLE_NAME AS `Table`, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, ".
168
                   "SEQ_IN_INDEX AS Seq_in_index, COLUMN_NAME AS Column_Name, COLLATION AS Collation, ".
169
                   "CARDINALITY AS Cardinality, SUB_PART AS Sub_Part, PACKED AS Packed, " .
170
                   "NULLABLE AS `Null`, INDEX_TYPE AS Index_Type, COMMENT AS Comment " .
171 6
                   "FROM information_schema.STATISTICS WHERE TABLE_NAME = " . $table . " AND TABLE_SCHEMA = " . $currentDatabase;
172
        }
173
174
        return 'SHOW INDEX FROM ' . $table;
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     */
180 3
    public function getListViewsSQL($database)
181
    {
182 3
        $database = $this->quoteStringLiteral($database);
183
184 3
        return "SELECT * FROM information_schema.VIEWS WHERE TABLE_SCHEMA = " . $database;
185
    }
186
187
    /**
188
     * {@inheritDoc}
189
     */
190 9
    public function getListTableForeignKeysSQL($table, $database = null)
191
    {
192 9
        $table = $this->quoteStringLiteral($table);
193
194 9
        if (null !== $database) {
195 6
            $database = $this->quoteStringLiteral($database);
196
        }
197
198
        $sql = "SELECT DISTINCT k.`CONSTRAINT_NAME`, k.`COLUMN_NAME`, k.`REFERENCED_TABLE_NAME`, ".
199
               "k.`REFERENCED_COLUMN_NAME` /*!50116 , c.update_rule, c.delete_rule */ ".
200
               "FROM information_schema.key_column_usage k /*!50116 ".
201
               "INNER JOIN information_schema.referential_constraints c ON ".
202
               "  c.constraint_name = k.constraint_name AND ".
203 9
               "  c.table_name = $table */ WHERE k.table_name = $table";
204
205 9
        $databaseNameSql = null === $database ? 'DATABASE()' : $database;
206
207 9
        $sql .= " AND k.table_schema = $databaseNameSql /*!50116 AND c.constraint_schema = $databaseNameSql */";
208 9
        $sql .= " AND k.`REFERENCED_COLUMN_NAME` is not NULL";
209
210 9
        return $sql;
211
    }
212
213
    /**
214
     * {@inheritDoc}
215
     */
216
    public function getCreateViewSQL($name, $sql)
217
    {
218
        return 'CREATE VIEW ' . $name . ' AS ' . $sql;
219
    }
220
221
    /**
222
     * {@inheritDoc}
223
     */
224
    public function getDropViewSQL($name)
225
    {
226
        return 'DROP VIEW '. $name;
227
    }
228
229
    /**
230
     * {@inheritDoc}
231
     */
232 45
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
233
    {
234 45
        return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
235 45
                : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241 3
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
242
    {
243 3
        return $fixed ? 'BINARY(' . ($length ?: 255) . ')' : 'VARBINARY(' . ($length ?: 255) . ')';
244
    }
245
246
    /**
247
     * Gets the SQL snippet used to declare a CLOB column type.
248
     *     TINYTEXT   : 2 ^  8 - 1 = 255
249
     *     TEXT       : 2 ^ 16 - 1 = 65535
250
     *     MEDIUMTEXT : 2 ^ 24 - 1 = 16777215
251
     *     LONGTEXT   : 2 ^ 32 - 1 = 4294967295
252
     *
253
     * @param array $field
254
     *
255
     * @return string
256
     */
257 16 View Code Duplication
    public function getClobTypeDeclarationSQL(array $field)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
    {
259 16
        if ( ! empty($field['length']) && is_numeric($field['length'])) {
260 4
            $length = $field['length'];
261
262 4
            if ($length <= static::LENGTH_LIMIT_TINYTEXT) {
263 3
                return 'TINYTEXT';
264
            }
265
266 4
            if ($length <= static::LENGTH_LIMIT_TEXT) {
267 4
                return 'TEXT';
268
            }
269
270 3
            if ($length <= static::LENGTH_LIMIT_MEDIUMTEXT) {
271 3
                return 'MEDIUMTEXT';
272
            }
273
        }
274
275 15
        return 'LONGTEXT';
276
    }
277
278
    /**
279
     * {@inheritDoc}
280
     */
281 3 View Code Duplication
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
282
    {
283 3
        if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
284 3
            return 'TIMESTAMP';
285
        }
286
287 3
        return 'DATETIME';
288
    }
289
290
    /**
291
     * {@inheritDoc}
292
     */
293
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
294
    {
295
        return 'DATE';
296
    }
297
298
    /**
299
     * {@inheritDoc}
300
     */
301
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
302
    {
303
        return 'TIME';
304
    }
305
306
    /**
307
     * {@inheritDoc}
308
     */
309 3
    public function getBooleanTypeDeclarationSQL(array $field)
310
    {
311 3
        return 'TINYINT(1)';
312
    }
313
314
    /**
315
     * Obtain DBMS specific SQL code portion needed to set the COLLATION
316
     * of a field declaration to be used in statements like CREATE TABLE.
317
     *
318
     * @deprecated Deprecated since version 2.5, Use {@link self::getColumnCollationDeclarationSQL()} instead.
319
     *
320
     * @param string $collation name of the collation
321
     *
322
     * @return string  DBMS specific SQL code portion needed to set the COLLATION
323
     *                 of a field declaration.
324
     */
325
    public function getCollationFieldDeclaration($collation)
326
    {
327
        return $this->getColumnCollationDeclarationSQL($collation);
328
    }
329
330
    /**
331
     * {@inheritDoc}
332
     *
333
     * MySql prefers "autoincrement" identity columns since sequences can only
334
     * be emulated with a table.
335
     */
336 3
    public function prefersIdentityColumns()
337
    {
338 3
        return true;
339
    }
340
341
    /**
342
     * {@inheritDoc}
343
     *
344
     * MySql supports this through AUTO_INCREMENT columns.
345
     */
346 3
    public function supportsIdentityColumns()
347
    {
348 3
        return true;
349
    }
350
351
    /**
352
     * {@inheritDoc}
353
     */
354 98
    public function supportsInlineColumnComments()
355
    {
356 98
        return true;
357
    }
358
359
    /**
360
     * {@inheritDoc}
361
     */
362
    public function supportsColumnCollation()
363
    {
364
        return true;
365
    }
366
367
    /**
368
     * {@inheritDoc}
369
     */
370
    public function getListTablesSQL()
371
    {
372
        return "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'";
373
    }
374
375
    /**
376
     * {@inheritDoc}
377
     */
378 6 View Code Duplication
    public function getListTableColumnsSQL($table, $database = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
379
    {
380 6
        $table = $this->quoteStringLiteral($table);
381
382 6
        if ($database) {
383 3
            $database = $this->quoteStringLiteral($database);
384
        } else {
385 3
            $database = 'DATABASE()';
386
        }
387
388
        return "SELECT COLUMN_NAME AS Field, COLUMN_TYPE AS Type, IS_NULLABLE AS `Null`, ".
389
               "COLUMN_KEY AS `Key`, COLUMN_DEFAULT AS `Default`, EXTRA AS Extra, COLUMN_COMMENT AS Comment, " .
390
               "CHARACTER_SET_NAME AS CharacterSet, COLLATION_NAME AS Collation ".
391 6
               "FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = " . $database . " AND TABLE_NAME = " . $table;
392
    }
393
394
    /**
395
     * {@inheritDoc}
396
     */
397 3
    public function getCreateDatabaseSQL($name)
398
    {
399 3
        return 'CREATE DATABASE ' . $name;
400
    }
401
402
    /**
403
     * {@inheritDoc}
404
     */
405 3
    public function getDropDatabaseSQL($name)
406
    {
407 3
        return 'DROP DATABASE ' . $name;
408
    }
409
410
    /**
411
     * {@inheritDoc}
412
     */
413 44
    protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
414
    {
415 44
        $queryFields = $this->getColumnDeclarationListSQL($columns);
416
417 44
        if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
418
            foreach ($options['uniqueConstraints'] as $index => $definition) {
419
                $queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($index, $definition);
420
            }
421
        }
422
423
        // add all indexes
424 44
        if (isset($options['indexes']) && ! empty($options['indexes'])) {
425 20
            foreach ($options['indexes'] as $index => $definition) {
426 20
                $queryFields .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
427
            }
428
        }
429
430
        // attach all primary keys
431 44 View Code Duplication
        if (isset($options['primary']) && ! empty($options['primary'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
432 15
            $keyColumns = array_unique(array_values($options['primary']));
433 15
            $queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
434
        }
435
436 44
        $query = 'CREATE ';
437
438 44
        if (!empty($options['temporary'])) {
439
            $query .= 'TEMPORARY ';
440
        }
441
442 44
        $query .= 'TABLE ' . $tableName . ' (' . $queryFields . ') ';
443 44
        $query .= $this->buildTableOptions($options);
444 44
        $query .= $this->buildPartitionOptions($options);
445
446 44
        $sql[]  = $query;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sql was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sql = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
447 44
        $engine = 'INNODB';
448
449 44
        if (isset($options['engine'])) {
450 9
            $engine = strtoupper(trim($options['engine']));
451
        }
452
453
        // Propagate foreign key constraints only for InnoDB.
454 44 View Code Duplication
        if (isset($options['foreignKeys']) && $engine === 'INNODB') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
455 6
            foreach ((array) $options['foreignKeys'] as $definition) {
456 6
                $sql[] = $this->getCreateForeignKeySQL($definition, $tableName);
457
            }
458
        }
459
460 44
        return $sql;
461
    }
462
463
    /**
464
     * Tells whether a field type supports declaration of a default value.
465
     *
466
     * MySQL (as of 5.7.19) does not support default values for Blob and Text
467
     * columns while MariaDB 10.2.1 does.
468
     */
469 70
    protected function isDefaultValueSupportedForType(Type $field): bool
470
    {
471 70
        return !($field instanceof TextType || $field instanceof BlobType);
472
    }
473
474
    /**
475
     * {@inheritdoc}
476
     */
477 104
    public function getDefaultValueDeclarationSQL($field)
478
    {
479
        // Unset the default value if the given field type does not allow default values.
480 104
        if (!$this->isDefaultValueSupportedForType($field['type'])) {
481 6
            $field['default'] = null;
482
        }
483
484 104
        return parent::getDefaultValueDeclarationSQL($field);
485
    }
486
487
    /**
488
     * Build SQL for table options
489
     *
490
     * @param array $options
491
     *
492
     * @return string
493
     */
494 44
    private function buildTableOptions(array $options)
495
    {
496 44
        if (isset($options['table_options'])) {
497
            return $options['table_options'];
498
        }
499
500 44
        $tableOptions = [];
501
502
        // Charset
503 44
        if ( ! isset($options['charset'])) {
504 44
            $options['charset'] = 'utf8';
505
        }
506
507 44
        $tableOptions[] = sprintf('DEFAULT CHARACTER SET %s', $options['charset']);
508
509
        // Collate
510 44
        if ( ! isset($options['collate'])) {
511 44
            $options['collate'] = 'utf8_unicode_ci';
512
        }
513
514 44
        $tableOptions[] = sprintf('COLLATE %s', $options['collate']);
515
516
        // Engine
517 44
        if ( ! isset($options['engine'])) {
518 35
            $options['engine'] = 'InnoDB';
519
        }
520
521 44
        $tableOptions[] = sprintf('ENGINE = %s', $options['engine']);
522
523
        // Auto increment
524 44
        if (isset($options['auto_increment'])) {
525
            $tableOptions[] = sprintf('AUTO_INCREMENT = %s', $options['auto_increment']);
526
        }
527
528
        // Comment
529 44 View Code Duplication
        if (isset($options['comment'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
530
            $comment = trim($options['comment'], " '");
531
532
            $tableOptions[] = sprintf("COMMENT = %s ", $this->quoteStringLiteral($comment));
533
        }
534
535
        // Row format
536 44
        if (isset($options['row_format'])) {
537
            $tableOptions[] = sprintf('ROW_FORMAT = %s', $options['row_format']);
538
        }
539
540 44
        return implode(' ', $tableOptions);
541
    }
542
543
    /**
544
     * Build SQL for partition options.
545
     *
546
     * @param array $options
547
     *
548
     * @return string
549
     */
550 44
    private function buildPartitionOptions(array $options)
551
    {
552 44
        return (isset($options['partition_options']))
553
            ? ' ' . $options['partition_options']
554 44
            : '';
555
    }
556
557
    /**
558
     * {@inheritDoc}
559
     */
560 79
    public function getAlterTableSQL(TableDiff $diff)
561
    {
562 79
        $columnSql = [];
563 79
        $queryParts = [];
564 79
        if ($diff->newName !== false) {
565 6
            $queryParts[] = 'RENAME TO ' . $diff->getNewName()->getQuotedName($this);
566
        }
567
568 79 View Code Duplication
        foreach ($diff->addedColumns as $column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
569 18
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
570
                continue;
571
            }
572
573 18
            $columnArray = $column->toArray();
574 18
            $columnArray['comment'] = $this->getColumnComment($column);
575 18
            $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
576
        }
577
578 79 View Code Duplication
        foreach ($diff->removedColumns as $column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
579 9
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
580
                continue;
581
            }
582
583 9
            $queryParts[] =  'DROP ' . $column->getQuotedName($this);
584
        }
585
586 79 View Code Duplication
        foreach ($diff->changedColumns as $columnDiff) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
587 23
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
588
                continue;
589
            }
590
591
            /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
592 23
            $column = $columnDiff->column;
593 23
            $columnArray = $column->toArray();
594
595
            // Don't propagate default value changes for unsupported column types.
596 23
            if ($columnDiff->hasChanged('default') &&
597 23
                count($columnDiff->changedProperties) === 1 &&
598 23
                !$this->isDefaultValueSupportedForType($columnArray['type'])
599
                //($columnArray['type'] instanceof TextType || $columnArray['type'] instanceof BlobType)
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
600
            ) {
601 2
                continue;
602
            }
603
604 21
            $columnArray['comment'] = $this->getColumnComment($column);
605 21
            $queryParts[] =  'CHANGE ' . ($columnDiff->getOldColumnName()->getQuotedName($this)) . ' '
606 21
                    . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
607
        }
608
609 79 View Code Duplication
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
610 12
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
611
                continue;
612
            }
613
614 12
            $oldColumnName = new Identifier($oldColumnName);
615 12
            $columnArray = $column->toArray();
616 12
            $columnArray['comment'] = $this->getColumnComment($column);
617 12
            $queryParts[] =  'CHANGE ' . $oldColumnName->getQuotedName($this) . ' '
618 12
                    . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
619
        }
620
621 79
        if (isset($diff->addedIndexes['primary'])) {
622 10
            $keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns()));
623 10
            $queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
624 10
            unset($diff->addedIndexes['primary']);
625
        }
626
627 79
        $sql = [];
628 79
        $tableSql = [];
629
630 79 View Code Duplication
        if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
631 79
            if (count($queryParts) > 0) {
632 40
                $sql[] = 'ALTER TABLE ' . $diff->getName($this)->getQuotedName($this) . ' ' . implode(", ", $queryParts);
633
            }
634 79
            $sql = array_merge(
635 79
                $this->getPreAlterTableIndexForeignKeySQL($diff),
636 79
                $sql,
637 79
                $this->getPostAlterTableIndexForeignKeySQL($diff)
638
            );
639
        }
640
641 79
        return array_merge($sql, $tableSql, $columnSql);
642
    }
643
644
    /**
645
     * {@inheritDoc}
646
     */
647 79
    protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
648
    {
649 79
        $sql = [];
650 79
        $table = $diff->getName($this)->getQuotedName($this);
651
652 79
        foreach ($diff->changedIndexes as $changedIndex) {
653 16
            $sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $changedIndex));
654
        }
655
656 79
        foreach ($diff->removedIndexes as $remKey => $remIndex) {
657 9
            $sql = array_merge($sql, $this->getPreAlterTableAlterPrimaryKeySQL($diff, $remIndex));
658
659 9
            foreach ($diff->addedIndexes as $addKey => $addIndex) {
660 3
                if ($remIndex->getColumns() == $addIndex->getColumns()) {
661
662 3
                    $indexClause = 'INDEX ' . $addIndex->getName();
663
664 3
                    if ($addIndex->isPrimary()) {
665
                        $indexClause = 'PRIMARY KEY';
666 3
                    } elseif ($addIndex->isUnique()) {
667 3
                        $indexClause = 'UNIQUE INDEX ' . $addIndex->getName();
668
                    }
669
670 3
                    $query = 'ALTER TABLE ' . $table . ' DROP INDEX ' . $remIndex->getName() . ', ';
671 3
                    $query .= 'ADD ' . $indexClause;
672 3
                    $query .= ' (' . $this->getIndexFieldDeclarationListSQL($addIndex->getQuotedColumns($this)) . ')';
673
674 3
                    $sql[] = $query;
675
676 3
                    unset($diff->removedIndexes[$remKey]);
677 3
                    unset($diff->addedIndexes[$addKey]);
678
679 9
                    break;
680
                }
681
            }
682
        }
683
684 79
        $engine = 'INNODB';
685
686 79
        if ($diff->fromTable instanceof Table && $diff->fromTable->hasOption('engine')) {
687 3
            $engine = strtoupper(trim($diff->fromTable->getOption('engine')));
688
        }
689
690
        // Suppress foreign key constraint propagation on non-supporting engines.
691 79
        if ('INNODB' !== $engine) {
692 3
            $diff->addedForeignKeys   = [];
693 3
            $diff->changedForeignKeys = [];
694 3
            $diff->removedForeignKeys = [];
695
        }
696
697 79
        $sql = array_merge(
698 79
            $sql,
699 79
            $this->getPreAlterTableAlterIndexForeignKeySQL($diff),
700 79
            parent::getPreAlterTableIndexForeignKeySQL($diff),
701 79
            $this->getPreAlterTableRenameIndexForeignKeySQL($diff)
702
        );
703
704 79
        return $sql;
705
    }
706
707
    /**
708
     * @param TableDiff $diff
709
     * @param Index     $index
710
     *
711
     * @return string[]
712
     */
713 25
    private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $index)
714
    {
715 25
        $sql = [];
716
717 25
        if (! $index->isPrimary() || ! $diff->fromTable instanceof Table) {
718 9
            return $sql;
719
        }
720
721 16
        $tableName = $diff->getName($this)->getQuotedName($this);
722
723
        // Dropping primary keys requires to unset autoincrement attribute on the particular column first.
724 16 View Code Duplication
        foreach ($index->getColumns() as $columnName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
725 16
            if (! $diff->fromTable->hasColumn($columnName)) {
726 3
                continue;
727
            }
728
729 16
            $column = $diff->fromTable->getColumn($columnName);
730
731 16
            if ($column->getAutoincrement() === true) {
732 9
                $column->setAutoincrement(false);
733
734 9
                $sql[] = 'ALTER TABLE ' . $tableName . ' MODIFY ' .
735 9
                    $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
736
737
                // original autoincrement information might be needed later on by other parts of the table alteration
738 16
                $column->setAutoincrement(true);
739
            }
740
        }
741
742 16
        return $sql;
743
    }
744
745
    /**
746
     * @param TableDiff $diff The table diff to gather the SQL for.
747
     *
748
     * @return array
749
     */
750 79
    private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff)
751
    {
752 79
        $sql = [];
753 79
        $table = $diff->getName($this)->getQuotedName($this);
754
755 79
        foreach ($diff->changedIndexes as $changedIndex) {
756
            // Changed primary key
757 16
            if ($changedIndex->isPrimary() && $diff->fromTable instanceof Table) {
758 13 View Code Duplication
                foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
759 13
                    $column = $diff->fromTable->getColumn($columnName);
760
761
                    // Check if an autoincrement column was dropped from the primary key.
762 13
                    if ($column->getAutoincrement() && ! in_array($columnName, $changedIndex->getColumns())) {
763
                        // The autoincrement attribute needs to be removed from the dropped column
764
                        // before we can drop and recreate the primary key.
765 3
                        $column->setAutoincrement(false);
766
767 3
                        $sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' .
768 3
                            $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
769
770
                        // Restore the autoincrement attribute as it might be needed later on
771
                        // by other parts of the table alteration.
772 16
                        $column->setAutoincrement(true);
773
                    }
774
                }
775
            }
776
        }
777
778 79
        return $sql;
779
    }
780
781
    /**
782
     * @param TableDiff $diff The table diff to gather the SQL for.
783
     *
784
     * @return array
785
     */
786 53
    protected function getPreAlterTableRenameIndexForeignKeySQL(TableDiff $diff)
787
    {
788 53
        $sql = [];
789 53
        $tableName = $diff->getName($this)->getQuotedName($this);
790
791 53 View Code Duplication
        foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
792 2
            if (! in_array($foreignKey, $diff->changedForeignKeys, true)) {
793 2
                $sql[] = $this->getDropForeignKeySQL($foreignKey, $tableName);
794
            }
795
        }
796
797 53
        return $sql;
798
    }
799
800
    /**
801
     * Returns the remaining foreign key constraints that require one of the renamed indexes.
802
     *
803
     * "Remaining" here refers to the diff between the foreign keys currently defined in the associated
804
     * table and the foreign keys to be removed.
805
     *
806
     * @param TableDiff $diff The table diff to evaluate.
807
     *
808
     * @return array
809
     */
810 53
    private function getRemainingForeignKeyConstraintsRequiringRenamedIndexes(TableDiff $diff)
811
    {
812 53
        if (empty($diff->renamedIndexes) || ! $diff->fromTable instanceof Table) {
813 43
            return [];
814
        }
815
816 10
        $foreignKeys = [];
817
        /** @var \Doctrine\DBAL\Schema\ForeignKeyConstraint[] $remainingForeignKeys */
818 10
        $remainingForeignKeys = array_diff_key(
819 10
            $diff->fromTable->getForeignKeys(),
820 10
            $diff->removedForeignKeys
821
        );
822
823 10
        foreach ($remainingForeignKeys as $foreignKey) {
824 2
            foreach ($diff->renamedIndexes as $index) {
825 2
                if ($foreignKey->intersectsIndexColumns($index)) {
826 2
                    $foreignKeys[] = $foreignKey;
827
828 2
                    break;
829
                }
830
            }
831
        }
832
833 10
        return $foreignKeys;
834
    }
835
836
    /**
837
     * {@inheritdoc}
838
     */
839 79
    protected function getPostAlterTableIndexForeignKeySQL(TableDiff $diff)
840
    {
841 79
        return array_merge(
842 79
            parent::getPostAlterTableIndexForeignKeySQL($diff),
843 79
            $this->getPostAlterTableRenameIndexForeignKeySQL($diff)
844
        );
845
    }
846
847
    /**
848
     * @param TableDiff $diff The table diff to gather the SQL for.
849
     *
850
     * @return array
851
     */
852 53
    protected function getPostAlterTableRenameIndexForeignKeySQL(TableDiff $diff)
853
    {
854 53
        $sql = [];
855 53
        $tableName = (false !== $diff->newName)
856 4
            ? $diff->getNewName()->getQuotedName($this)
857 53
            : $diff->getName($this)->getQuotedName($this);
858
859 53 View Code Duplication
        foreach ($this->getRemainingForeignKeyConstraintsRequiringRenamedIndexes($diff) as $foreignKey) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
860 2
            if (! in_array($foreignKey, $diff->changedForeignKeys, true)) {
861 2
                $sql[] = $this->getCreateForeignKeySQL($foreignKey, $tableName);
862
            }
863
        }
864
865 53
        return $sql;
866
    }
867
868
    /**
869
     * {@inheritDoc}
870
     */
871 45 View Code Duplication
    protected function getCreateIndexSQLFlags(Index $index)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
872
    {
873 45
        $type = '';
874 45
        if ($index->isUnique()) {
875 9
            $type .= 'UNIQUE ';
876 36
        } elseif ($index->hasFlag('fulltext')) {
877 3
            $type .= 'FULLTEXT ';
878 33
        } elseif ($index->hasFlag('spatial')) {
879 3
            $type .= 'SPATIAL ';
880
        }
881
882 45
        return $type;
883
    }
884
885
    /**
886
     * {@inheritDoc}
887
     */
888 56
    public function getIntegerTypeDeclarationSQL(array $field)
889
    {
890 56
        return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
891
    }
892
893
    /**
894
     * {@inheritDoc}
895
     */
896
    public function getBigIntTypeDeclarationSQL(array $field)
897
    {
898
        return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
899
    }
900
901
    /**
902
     * {@inheritDoc}
903
     */
904
    public function getSmallIntTypeDeclarationSQL(array $field)
905
    {
906
        return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
907
    }
908
909
    /**
910
     * {@inheritdoc}
911
     */
912 18
    public function getFloatDeclarationSQL(array $field)
913
    {
914 18
        return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($field);
915
    }
916
917
    /**
918
     * {@inheritdoc}
919
     */
920 18
    public function getDecimalTypeDeclarationSQL(array $columnDef)
921
    {
922 18
        return parent::getDecimalTypeDeclarationSQL($columnDef) . $this->getUnsignedDeclaration($columnDef);
923
    }
924
925
    /**
926
     * Get unsigned declaration for a column.
927
     *
928
     * @param array $columnDef
929
     *
930
     * @return string
931
     */
932 92
    private function getUnsignedDeclaration(array $columnDef)
933
    {
934 92
        return ! empty($columnDef['unsigned']) ? ' UNSIGNED' : '';
935
    }
936
937
    /**
938
     * {@inheritDoc}
939
     */
940 56
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
941
    {
942 56
        $autoinc = '';
943 56
        if ( ! empty($columnDef['autoincrement'])) {
944 9
            $autoinc = ' AUTO_INCREMENT';
945
        }
946
947 56
        return $this->getUnsignedDeclaration($columnDef) . $autoinc;
948
    }
949
950
    /**
951
     * {@inheritDoc}
952
     */
953 21
    public function getAdvancedForeignKeyOptionsSQL(\Doctrine\DBAL\Schema\ForeignKeyConstraint $foreignKey)
954
    {
955 21
        $query = '';
956 21
        if ($foreignKey->hasOption('match')) {
957
            $query .= ' MATCH ' . $foreignKey->getOption('match');
958
        }
959 21
        $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
960
961 21
        return $query;
962
    }
963
964
    /**
965
     * {@inheritDoc}
966
     */
967 32 View Code Duplication
    public function getDropIndexSQL($index, $table=null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
968
    {
969 32
        if ($index instanceof Index) {
970 22
            $indexName = $index->getQuotedName($this);
971 10
        } elseif (is_string($index)) {
972 10
            $indexName = $index;
973
        } else {
974
            throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
975
        }
976
977 32
        if ($table instanceof Table) {
978
            $table = $table->getQuotedName($this);
979 32
        } elseif (!is_string($table)) {
980
            throw new \InvalidArgumentException('MysqlPlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
981
        }
982
983 32
        if ($index instanceof Index && $index->isPrimary()) {
984
            // mysql primary keys are always named "PRIMARY",
985
            // so we cannot use them in statements because of them being keyword.
986 19
            return $this->getDropPrimaryKeySQL($table);
987
        }
988
989 13
        return 'DROP INDEX ' . $indexName . ' ON ' . $table;
990
    }
991
992
    /**
993
     * @param string $table
994
     *
995
     * @return string
996
     */
997 19
    protected function getDropPrimaryKeySQL($table)
998
    {
999 19
        return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
1000
    }
1001
1002
    /**
1003
     * {@inheritDoc}
1004
     */
1005 3
    public function getSetTransactionIsolationSQL($level)
1006
    {
1007 3
        return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
1008
    }
1009
1010
    /**
1011
     * {@inheritDoc}
1012
     */
1013 6
    public function getName()
1014
    {
1015 6
        return 'mysql';
1016
    }
1017
1018
    /**
1019
     * {@inheritDoc}
1020
     */
1021
    public function getReadLockSQL()
1022
    {
1023
        return 'LOCK IN SHARE MODE';
1024
    }
1025
1026
    /**
1027
     * {@inheritDoc}
1028
     */
1029 17
    protected function initializeDoctrineTypeMappings()
1030
    {
1031 17
        $this->doctrineTypeMapping = [
1032
            'tinyint'       => 'boolean',
1033
            'smallint'      => 'smallint',
1034
            'mediumint'     => 'integer',
1035
            'int'           => 'integer',
1036
            'integer'       => 'integer',
1037
            'bigint'        => 'bigint',
1038
            'tinytext'      => 'text',
1039
            'mediumtext'    => 'text',
1040
            'longtext'      => 'text',
1041
            'text'          => 'text',
1042
            'varchar'       => 'string',
1043
            'string'        => 'string',
1044
            'char'          => 'string',
1045
            'date'          => 'date',
1046
            'datetime'      => 'datetime',
1047
            'timestamp'     => 'datetime',
1048
            'time'          => 'time',
1049
            'float'         => 'float',
1050
            'double'        => 'float',
1051
            'real'          => 'float',
1052
            'decimal'       => 'decimal',
1053
            'numeric'       => 'decimal',
1054
            'year'          => 'date',
1055
            'longblob'      => 'blob',
1056
            'blob'          => 'blob',
1057
            'mediumblob'    => 'blob',
1058
            'tinyblob'      => 'blob',
1059
            'binary'        => 'binary',
1060
            'varbinary'     => 'binary',
1061
            'set'           => 'simple_array',
1062
        ];
1063 17
    }
1064
1065
    /**
1066
     * {@inheritDoc}
1067
     */
1068 45
    public function getVarcharMaxLength()
1069
    {
1070 45
        return 65535;
1071
    }
1072
1073
    /**
1074
     * {@inheritdoc}
1075
     */
1076 6
    public function getBinaryMaxLength()
1077
    {
1078 6
        return 65535;
1079
    }
1080
1081
    /**
1082
     * {@inheritDoc}
1083
     */
1084 62
    protected function getReservedKeywordsClass()
1085
    {
1086 62
        return Keywords\MySQLKeywords::class;
1087
    }
1088
1089
    /**
1090
     * {@inheritDoc}
1091
     *
1092
     * MySQL commits a transaction implicitly when DROP TABLE is executed, however not
1093
     * if DROP TEMPORARY TABLE is executed.
1094
     */
1095 View Code Duplication
    public function getDropTemporaryTableSQL($table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1096
    {
1097
        if ($table instanceof Table) {
1098
            $table = $table->getQuotedName($this);
1099
        } elseif (!is_string($table)) {
1100
            throw new \InvalidArgumentException('getDropTemporaryTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
1101
        }
1102
1103
        return 'DROP TEMPORARY TABLE ' . $table;
1104
    }
1105
1106
    /**
1107
     * Gets the SQL Snippet used to declare a BLOB column type.
1108
     *     TINYBLOB   : 2 ^  8 - 1 = 255
1109
     *     BLOB       : 2 ^ 16 - 1 = 65535
1110
     *     MEDIUMBLOB : 2 ^ 24 - 1 = 16777215
1111
     *     LONGBLOB   : 2 ^ 32 - 1 = 4294967295
1112
     *
1113
     * @param array $field
1114
     *
1115
     * @return string
1116
     */
1117 9 View Code Duplication
    public function getBlobTypeDeclarationSQL(array $field)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1118
    {
1119 9
        if ( ! empty($field['length']) && is_numeric($field['length'])) {
1120 6
            $length = $field['length'];
1121
1122 6
            if ($length <= static::LENGTH_LIMIT_TINYBLOB) {
1123 3
                return 'TINYBLOB';
1124
            }
1125
1126 6
            if ($length <= static::LENGTH_LIMIT_BLOB) {
1127 3
                return 'BLOB';
1128
            }
1129
1130 6
            if ($length <= static::LENGTH_LIMIT_MEDIUMBLOB) {
1131 6
                return 'MEDIUMBLOB';
1132
            }
1133
        }
1134
1135 9
        return 'LONGBLOB';
1136
    }
1137
1138
    /**
1139
     * {@inheritdoc}
1140
     */
1141 82
    public function quoteStringLiteral($str)
1142
    {
1143 82
        $str = str_replace('\\', '\\\\', $str); // MySQL requires backslashes to be escaped aswell.
1144
1145 82
        return parent::quoteStringLiteral($str);
1146
    }
1147
1148
    /**
1149
     * {@inheritdoc}
1150
     */
1151 1
    public function getDefaultTransactionIsolationLevel()
1152
    {
1153 1
        return Connection::TRANSACTION_REPEATABLE_READ;
1154
    }
1155
}
1156