Passed
Pull Request — main (#122)
by Andreas
02:02
created

CratePlatform::getDateFormatString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Licensed to CRATE Technology GmbH("Crate") under one or more contributor
4
 * license agreements.  See the NOTICE file distributed with this work for
5
 * additional information regarding copyright ownership.  Crate licenses
6
 * this file to you under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.  You may
8
 * obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
15
 * License for the specific language governing permissions and limitations
16
 * under the License.
17
 *
18
 * However, if you have executed another commercial license agreement
19
 * with Crate these terms will supersede the license and you may use the
20
 * software solely pursuant to the terms of the relevant commercial agreement.
21
 */
22
namespace Crate\DBAL\Platforms;
23
24
use Crate\DBAL\Types\MapType;
25
use Crate\DBAL\Types\TimestampType;
26
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
27
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
28
use Doctrine\DBAL\Events;
29
use Doctrine\DBAL\Exception as DBALException;
30
use Doctrine\DBAL\Platforms\AbstractPlatform;
31
use Doctrine\DBAL\Schema\Identifier;
32
use Doctrine\DBAL\Schema\Index;
33
use Doctrine\DBAL\Schema\Table;
34
use Doctrine\DBAL\Schema\TableDiff;
35
use Doctrine\DBAL\Types\Type;
36
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
37
use InvalidArgumentException;
38
39
class CratePlatform extends AbstractPlatform
40
{
41
42
    const TIMESTAMP_FORMAT =  'Y-m-d\TH:i:s';
43
    const TIMESTAMP_FORMAT_TZ =  'Y-m-d\TH:i:sO';
44
    const TABLE_WHERE_CLAUSE_FORMAT = '%s.table_name = %s AND %s.schema_name = %s';
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 260
    public function __construct()
50
    {
51 260
        $this->initializeDoctrineTypeMappings();
52 260
        if (!Type::hasType(MapType::NAME)) {
53
            Type::addType(MapType::NAME, 'Crate\DBAL\Types\MapType');
54
        }
55 260
        if (!Type::hasType(TimestampType::NAME)) {
56
            Type::addType(TimestampType::NAME, 'Crate\DBAL\Types\TimestampType');
57
        }
58 260
        Type::overrideType('array', 'Crate\DBAL\Types\ArrayType');
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64 2
    public function getSubstringExpression($value, $from = 0, $length = null)
65
    {
66 2
        if ($length === null) {
67 2
            return 'SUBSTR(' . $value . ', ' . $from . ')';
68
        }
69
70 2
        return 'SUBSTR(' . $value . ', ' . $from . ', ' . $length . ')';
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 2
    public function getNowExpression()
77
    {
78 2
        throw DBALException::notSupported(__METHOD__);
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 2
    public function getRegexpExpression()
85
    {
86 2
        return 'LIKE';
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92 2
    public function getDateDiffExpression($date1, $date2)
93
    {
94 2
        throw DBALException::notSupported(__METHOD__);
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100 8
    public function supportsSequences()
101
    {
102 8
        return false;
103
    }
104
105
    /**
106
     * If we want to support Schemas, we need to implement
107
     * getListNamespacesSQL and getCreateSchemaSQL methods
108
     *
109
     * {@inheritDoc}
110
     */
111 10
    public function supportsSchemas()
112
    {
113 10
        return false;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119 2
    public function supportsIdentityColumns()
120
    {
121 2
        return true;
122
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127 2
    public function supportsIndexes()
128
    {
129 2
        return false;
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 114
    public function supportsCommentOnStatement()
136
    {
137 114
        return false;
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143 10
    public function supportsForeignKeyConstraints()
144
    {
145 10
        return false;
146
    }
147
148
    /**
149
     * {@inheritDoc}
150
     */
151 2
    public function supportsForeignKeyOnUpdate()
152
    {
153 2
        return false;
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159 2
    public function supportsViews()
160
    {
161 2
        return false;
162
    }
163
164
    /**
165
     * {@inheritDoc}
166
     */
167 2
    public function prefersSequences()
168
    {
169 2
        return false;
170
    }
171
172
    /**
173
     * {@inheritDoc}
174
     */
175 2
    public function getListDatabasesSQL()
176
    {
177 2
        throw DBALException::notSupported(__METHOD__);
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     */
183
    public function getListTablesSQL()
184
    {
185
        return "SELECT table_name, schema_name FROM information_schema.tables " .
186
               "WHERE schema_name = 'doc' OR schema_name = 'blob'";
187
    }
188
189
    /**
190
     * {@inheritDoc}
191
     */
192 8
    public function getListTableColumnsSQL($table, $database = null)
193
    {
194 8
        return "SELECT * from information_schema.columns c " .
195 8
               "WHERE " . $this->getTableWhereClause($table);
196
    }
197
198
    /**
199
     * {@inheritDoc}
200
     */
201
    public function getListTableConstraintsSQL($table, $database = null)
0 ignored issues
show
Unused Code introduced by
The parameter $database 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

201
    public function getListTableConstraintsSQL($table, /** @scrutinizer ignore-unused */ $database = null)

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...
202
    {
203
        return "SELECT c.constraint_name, c.constraint_type " .
204
               "FROM information_schema.table_constraints c " .
205
               "WHERE " . $this->getTableWhereClause($table) . " AND constraint_type = 'PRIMARY KEY'";
206
    }
207
208
    /**
209
     * {@inheritDoc}
210
     */
211 8
    public function getListTableIndexesSQL($table, $currentDatabase = null)
212
    {
213 8
        return "SELECT c.constraint_name, c.constraint_type, k.column_name " .
214 8
               "FROM information_schema.table_constraints c " .
215 8
               "JOIN information_schema.key_column_usage k on c.constraint_name = k.constraint_name " .
216 8
               "WHERE " . $this->getTableWhereClause($table);
217
    }
218
219 10
    private function getTableWhereClause($table, $tableAlias = 'c')
220
    {
221 10
        if (strpos($table, '.') !== false) {
222
            [$schema, $table] = explode('.', $table);
223
            $schema = $this->quoteStringLiteral($schema);
224
        } else {
225 10
            $schema = $this->quoteStringLiteral('doc');
226
        }
227
228 10
        $table = new Identifier($table);
229 10
        $table = $this->quoteStringLiteral($table->getName());
230
231 10
        return sprintf(
232 10
            $this->getTableWhereClauseFormat(),
233 10
            $tableAlias,
234 10
            $table,
235 10
            $tableAlias,
236 10
            $schema
237 10
        );
238
    }
239
240
    /**
241
     * Return sprintf format string for usage at getTableWhereClause
242
     *
243
     * @return string
244
     */
245
    protected function getTableWhereClauseFormat()
246
    {
247
        return self::TABLE_WHERE_CLAUSE_FORMAT;
248
    }
249
250
    /**
251
     * {@inheritDoc}
252
     */
253 6
    public function getAlterTableSQL(TableDiff $diff)
254
    {
255 6
        $sql = array();
256 6
        $commentsSQL = array();
257 6
        $columnSql = array();
258
259 6
        foreach ($diff->addedColumns as $column) {
260 4
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
261
                continue;
262
            }
263
264 4
            $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
265 4
            $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\DBAL\Schema\TableDiff::$name has been deprecated: Use {@see getOldTable()} instead. ( Ignorable by Annotation )

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

265
            $sql[] = 'ALTER TABLE ' . /** @scrutinizer ignore-deprecated */ $diff->name . ' ' . $query;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
266 4
            if ($comment = $this->getColumnComment($column)) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...orm::getColumnComment() has been deprecated: This method will be removed without replacement. ( Ignorable by Annotation )

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

266
            if ($comment = /** @scrutinizer ignore-deprecated */ $this->getColumnComment($column)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
267
                $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\DBAL\Schema\TableDiff::$name has been deprecated: Use {@see getOldTable()} instead. ( Ignorable by Annotation )

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

267
                $commentsSQL[] = $this->getCommentOnColumnSQL(/** @scrutinizer ignore-deprecated */ $diff->name, $column->getName(), $comment);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
268
            }
269
        }
270
271 6
        if (count($diff->removedColumns) > 0) {
272
            throw DBALException::notSupported("Alter Table: drop columns");
273
        }
274 6
        if (count($diff->changedColumns) > 0) {
275
            throw DBALException::notSupported("Alter Table: change column options");
276
        }
277 6
        if (count($diff->renamedColumns) > 0) {
278
            throw DBALException::notSupported("Alter Table: rename columns");
279
        }
280
281 6
        $tableSql = array();
282
283 6
        if (!$this->onSchemaAlterTable($diff, $tableSql)) {
284 6
            if ($diff->newName !== false) {
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\DBAL\Schema\TableDiff::$newName has been deprecated: Rename tables via {@link AbstractSchemaManager::renameTable()} instead. ( Ignorable by Annotation )

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

284
            if (/** @scrutinizer ignore-deprecated */ $diff->newName !== false) {

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
285
                throw DBALException::notSupported("Alter Table: rename table");
286
            }
287
288 6
            $sql = array_merge($sql, $this->getPreAlterTableIndexForeignKeySQL($diff), $commentsSQL);
289
        }
290
291 6
        return array_merge($sql, $tableSql, $columnSql);
292
    }
293
294
    /**
295
     * {@inheritDoc}
296
     */
297 124
    public function getColumnDeclarationSQL($name, array $column)
298
    {
299 124
        if (isset($column['columnDefinition'])) {
300 64
            $columnDef = $this->getCustomTypeDeclarationSQL($column);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...tomTypeDeclarationSQL() has been deprecated. ( Ignorable by Annotation )

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

300
            $columnDef = /** @scrutinizer ignore-deprecated */ $this->getCustomTypeDeclarationSQL($column);
Loading history...
301
        } else {
302 122
            $typeDecl = $column['type']->getSqlDeclaration($column, $this);
303 122
            $columnDef = $typeDecl;
304
        }
305
306 124
        return $name . ' ' . $columnDef;
307
    }
308
309
    /**
310
     * Generate table index column declaration
311
     * @codeCoverageIgnore
312
     */
313
    public function getIndexDeclarationSQL($name, Index $index)
314
    {
315
        $columns = $index->getQuotedColumns($this);
316
        $name = new Identifier($name);
317
318
        if (count($columns) == 0) {
319
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
320
        }
321
322
        return 'INDEX ' . $name->getQuotedName($this) .
323
               ' USING FULLTEXT ('. $this->getIndexFieldDeclarationListSQL($index) . ')';
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...eldDeclarationListSQL() has been deprecated. ( Ignorable by Annotation )

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

323
               ' USING FULLTEXT ('. /** @scrutinizer ignore-deprecated */ $this->getIndexFieldDeclarationListSQL($index) . ')';
Loading history...
324
    }
325
326
    /**
327
     * {@inheritDoc}
328
     *
329
     * Crate wants boolean values converted to the strings 'true'/'false'.
330
     */
331 4
    public function convertBooleans($item)
332
    {
333 4
        if (is_array($item)) {
334 2
            foreach ($item as $key => $value) {
335 2
                if (is_bool($value)) {
336 2
                    $item[$key] = ($value) ? 'true' : 'false';
337 2
                } elseif (is_numeric($value)) {
338 2
                    $item[$key] = ($value > 0) ? 'true' : 'false';
339
                }
340
            }
341
        } else {
342 4
            if (is_bool($item)) {
343 4
                $item = ($item) ? 'true' : 'false';
344 2
            } elseif (is_numeric($item)) {
345 2
                $item = ($item > 0) ? 'true' : 'false';
346
            }
347
        }
348
349 4
        return $item;
350
    }
351
352
    /**
353
     * {@inheritDoc}
354
     */
355 22
    public function getBooleanTypeDeclarationSQL(array $field)
356
    {
357 22
        return 'BOOLEAN';
358
    }
359
360
    /**
361
     * {@inheritDoc}
362
     */
363 110
    public function getIntegerTypeDeclarationSQL(array $field)
364
    {
365 110
        return 'INTEGER';
366
    }
367
368
    /**
369
     * {@inheritDoc}
370
     */
371
    public function getBigIntTypeDeclarationSQL(array $field)
372
    {
373
        return 'LONG';
374
    }
375
376
    /**
377
     * {@inheritDoc}
378
     */
379
    public function getSmallIntTypeDeclarationSQL(array $field)
380
    {
381
        return 'SHORT';
382
    }
383
384
    /**
385
     * {@inheritDoc}
386
     */
387
    public function getFloatDeclarationSQL(array $field)
388
    {
389
        return 'DOUBLE';
390
    }
391
392
    /**
393
     * {@inheritDoc}
394
     */
395
    public function getDecimalTypeDeclarationSQL(array $columnDef)
396
    {
397
        return 'DOUBLE';
398
    }
399
400
    /**
401
     * {@inheritDoc}
402
     */
403 50
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
404
    {
405 50
        return 'TIMESTAMP';
406
    }
407
408
    /**
409
     * {@inheritDoc}
410
     */
411 2
    public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
412
    {
413 2
        return 'TIMESTAMP';
414
    }
415
416
    /**
417
     * {@inheritDoc}
418
     */
419 2
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
420
    {
421 2
        return 'TIMESTAMP';
422
    }
423
424
    /**
425
     * {@inheritDoc}
426
     */
427 2
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
428
    {
429 2
        return 'TIMESTAMP';
430
    }
431
432
    /**
433
     * {@inheritDoc}
434
     */
435
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
436
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
437
    {
438
        return '';
439
    }
440
441
    /**
442
     * {@inheritDoc}
443
     */
444
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
445
    {
446
        return 'STRING';
447
    }
448
449
    /**
450
     * {@inheritDoc}
451
     */
452
    public function getClobTypeDeclarationSQL(array $field)
453
    {
454
        return 'STRING';
455
    }
456
457
    /**
458
     * {@inheritDoc}
459
     */
460
    public function getName()
461
    {
462
        return 'crate';
463
    }
464
465
    /**
466
     * {@inheritDoc}
467
     *
468
     * PostgreSQL returns all column names in SQL result sets in lowercase.
469
     */
470 2
    public function getSQLResultCasing($column)
471
    {
472 2
        return strtolower($column);
473
    }
474
475
    /**
476
     * {@inheritDoc}
477
     */
478 4
    public function getDateTimeTzFormatString()
479
    {
480 4
        return self::TIMESTAMP_FORMAT_TZ;
481
    }
482
483
    /**
484
     * {@inheritDoc}
485
     */
486 10
    public function getDateTimeFormatString()
487
    {
488 10
        return self::TIMESTAMP_FORMAT;
489
    }
490
491
    /**
492
     * {@inheritDoc}
493
     */
494 2
    public function getDateFormatString()
495
    {
496 2
        return self::TIMESTAMP_FORMAT;
497
    }
498
499
    /**
500
     * {@inheritDoc}
501
     */
502 2
    public function getTimeFormatString()
503
    {
504 2
        return self::TIMESTAMP_FORMAT;
505
    }
506
507
    /**
508
     * {@inheritDoc}
509
     */
510 2
    public function getTruncateTableSQL($tableName, $cascade = false)
511
    {
512 2
        throw DBALException::notSupported(__METHOD__);
513
    }
514
515
    /**
516
     * {@inheritDoc}
517
     */
518 2
    public function getReadLockSQL()
519
    {
520 2
        throw DBALException::notSupported(__METHOD__);
521
    }
522
523
    /**
524
     * {@inheritDoc}
525
     */
526 20
    protected function initializeDoctrineTypeMappings()
527
    {
528 20
        $this->doctrineTypeMapping = array(
529 20
            'short'         => 'smallint',
530 20
            'integer'       => 'integer',
531 20
            'long'          => 'bigint',
532 20
            'int'           => 'integer',
533 20
            'bool'          => 'boolean',
534 20
            'boolean'       => 'boolean',
535 20
            'string'        => 'string',
536 20
            'float'         => 'float',
537 20
            'double'        => 'float',
538 20
            'timestamp'     => 'timestamp',
539 20
            'object'        => 'map',
540 20
            'array'         => 'array',
541 20
        );
542
    }
543
544
    /**
545
     * {@inheritDoc}
546
     */
547 14
    public function getDoctrineTypeMapping($dbType)
548
    {
549
        // typed arrays will always end up in the same generic php array type
550 14
        if (substr_compare($dbType, 'array', -5) === 0) {
551 2
            $dbType = 'array';
552
        }
553 14
        return parent::getDoctrineTypeMapping($dbType);
554
    }
555
556
557
    /**
558
     * {@inheritDoc}
559
     */
560 108
    public function getVarcharMaxLength()
561
    {
562 108
        return PHP_INT_MAX;
563
    }
564
565
    /**
566
     * {@inheritDoc}
567
     */
568 54
    protected function getReservedKeywordsClass()
569
    {
570 54
        return 'Crate\DBAL\Platforms\Keywords\CrateKeywords';
571
    }
572
573
    /**
574
     * {@inheritDoc}
575
     */
576 2
    public function getBlobTypeDeclarationSQL(array $field)
577
    {
578 2
        throw DBALException::notSupported(__METHOD__);
579
    }
580
581
    /**
582
     * {@inheritDoc}
583
     * Gets the SQL statement(s) to create a table with the specified name, columns and constraints
584
     * on this platform.
585
     *
586
     * @param Table $table The name of the table.
587
     * @param integer $createFlags
588
     *
589
     * @return array The sequence of SQL statements.
590
     */
591 122
    public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES)
592
    {
593 122
        if (!is_int($createFlags)) {
0 ignored issues
show
introduced by
The condition is_int($createFlags) is always true.
Loading history...
594
            $msg = "Second argument of CratePlatform::getCreateTableSQL() has to be integer.";
595
            throw new InvalidArgumentException($msg);
596
        }
597
598 122
        if (count($table->getColumns()) === 0) {
599 4
            throw DBALException::noColumnsSpecifiedForTable($table->getName());
600
        }
601
602 118
        $tableName = $table->getQuotedName($this);
603 118
        $options = $table->getOptions();
604 118
        $options['uniqueConstraints'] = array();
605 118
        $options['indexes'] = array();
606 118
        $options['primary'] = array();
607
608 118
        if (($createFlags&self::CREATE_INDEXES) > 0) {
609 118
            foreach ($table->getIndexes() as $index) {
610
                /* @var $index Index */
611 84
                if ($index->isPrimary()) {
612 74
                    $platform = $this;
613 74
                    $options['primary'] = array_map(function ($columnName) use ($table, $platform) {
614 74
                        return $table->getColumn($columnName)->getQuotedName($platform);
615 74
                    }, $index->getColumns());
616 74
                    $options['primary_index'] = $index;
617 10
                } elseif ($index->isUnique()) {
618 4
                    throw DBALException::notSupported(
619 4
                        "Unique constraints are not supported. Use `primary key` instead"
620 4
                    );
621
                } else {
622 6
                    $options['indexes'][$index->getName()] = $index;
623
                }
624
            }
625
        }
626
627 114
        $columnSql = array();
628 114
        $columns = array();
629
630 114
        foreach ($table->getColumns() as $column) {
631 114
            if (null !== $this->_eventManager &&
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\DBAL\Platforms\...latform::$_eventManager has been deprecated. ( Ignorable by Annotation )

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

631
            if (null !== /** @scrutinizer ignore-deprecated */ $this->_eventManager &&
Loading history...
632 114
                $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)
0 ignored issues
show
introduced by
The constant Doctrine\DBAL\Events::onSchemaCreateTableColumn has been deprecated. ( Ignorable by Annotation )

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

632
                $this->_eventManager->hasListeners(/** @scrutinizer ignore-deprecated */ Events::onSchemaCreateTableColumn)
Loading history...
Deprecated Code introduced by
The property Doctrine\DBAL\Platforms\...latform::$_eventManager has been deprecated. ( Ignorable by Annotation )

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

632
                /** @scrutinizer ignore-deprecated */ $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)
Loading history...
633
            ) {
634 2
                $eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Event\Sche...ateTableColumnEventArgs has been deprecated. ( Ignorable by Annotation )

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

634
                $eventArgs = /** @scrutinizer ignore-deprecated */ new SchemaCreateTableColumnEventArgs($column, $table, $this);
Loading history...
635 2
                $this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
0 ignored issues
show
introduced by
The constant Doctrine\DBAL\Events::onSchemaCreateTableColumn has been deprecated. ( Ignorable by Annotation )

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

635
                $this->_eventManager->dispatchEvent(/** @scrutinizer ignore-deprecated */ Events::onSchemaCreateTableColumn, $eventArgs);
Loading history...
Deprecated Code introduced by
The property Doctrine\DBAL\Platforms\...latform::$_eventManager has been deprecated. ( Ignorable by Annotation )

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

635
                /** @scrutinizer ignore-deprecated */ $this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
Loading history...
636
637 2
                $columnSql = array_merge($columnSql, $eventArgs->getSql());
638
639 2
                if ($eventArgs->isDefaultPrevented()) {
640
                    continue;
641
                }
642
            }
643 114
            $columns[$column->getQuotedName($this)] = self::prepareColumnData($this, $column, $options['primary']);
644
        }
645
646 112
        if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
0 ignored issues
show
Deprecated Code introduced by
The property Doctrine\DBAL\Platforms\...latform::$_eventManager has been deprecated. ( Ignorable by Annotation )

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

646
        if (null !== $this->_eventManager && /** @scrutinizer ignore-deprecated */ $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
Loading history...
introduced by
The constant Doctrine\DBAL\Events::onSchemaCreateTable has been deprecated. ( Ignorable by Annotation )

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

646
        if (null !== $this->_eventManager && $this->_eventManager->hasListeners(/** @scrutinizer ignore-deprecated */ Events::onSchemaCreateTable)) {
Loading history...
647 2
            $eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Event\SchemaCreateTableEventArgs has been deprecated. ( Ignorable by Annotation )

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

647
            $eventArgs = /** @scrutinizer ignore-deprecated */ new SchemaCreateTableEventArgs($table, $columns, $options, $this);
Loading history...
648 2
            $this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
0 ignored issues
show
introduced by
The constant Doctrine\DBAL\Events::onSchemaCreateTable has been deprecated. ( Ignorable by Annotation )

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

648
            $this->_eventManager->dispatchEvent(/** @scrutinizer ignore-deprecated */ Events::onSchemaCreateTable, $eventArgs);
Loading history...
Deprecated Code introduced by
The property Doctrine\DBAL\Platforms\...latform::$_eventManager has been deprecated. ( Ignorable by Annotation )

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

648
            /** @scrutinizer ignore-deprecated */ $this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
Loading history...
649
650 2
            if ($eventArgs->isDefaultPrevented()) {
651
                return array_merge($eventArgs->getSql(), $columnSql);
652
            }
653
        }
654
655 112
        $sql = $this->_getCreateTableSQL($tableName, $columns, $options);
656 110
        if ($this->supportsCommentOnStatement()) {
657
            foreach ($table->getColumns() as $column) {
658
                if ($this->getColumnComment($column)) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...orm::getColumnComment() has been deprecated: This method will be removed without replacement. ( Ignorable by Annotation )

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

658
                if (/** @scrutinizer ignore-deprecated */ $this->getColumnComment($column)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
659
                    $sql[] = $this->getCommentOnColumnSQL(
660
                        $tableName,
661
                        $column->getName(),
662
                        $this->getColumnComment($column)
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...orm::getColumnComment() has been deprecated: This method will be removed without replacement. ( Ignorable by Annotation )

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

662
                        /** @scrutinizer ignore-deprecated */ $this->getColumnComment($column)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
663
                    );
664
                }
665
            }
666
        }
667
668 110
        return array_merge($sql, $columnSql);
669
    }
670
671
    /**
672
     * {@inheritDoc}
673
     */
674
    // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
675 112
    protected function _getCreateTableSQL($name, array $columns, array $options = array())
676
    {
677 112
        $columnListSql = $this->getColumnDeclarationListSQL($columns);
678
679 112
        if (isset($options['primary']) && ! empty($options['primary'])) {
680 74
            $keyColumns = array_unique(array_values($options['primary']));
681 74
            $columnListSql .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
682
        }
683
684 112
        if (isset($options['indexes']) && ! empty($options['indexes'])) {
685 6
            foreach ($options['indexes'] as $index => $definition) {
686 6
                $columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
687
            }
688
        }
689
 
690 112
        if (isset($options['foreignKeys'])) {
691
            throw DBALException::notSupported("Create Table: foreign keys");
692
        }
693
694 112
        $query = 'CREATE TABLE ' . $name . ' (' . $columnListSql . ')';
695 112
        $query .= $this->buildShardingOptions($options);
696 112
        $query .= $this->buildPartitionOptions($options);
697 110
        $query .= $this->buildTableOptions($options);
698 110
        return array($query);
699
    }
700
701
    /**
702
     * Build SQL for table options
703
     *
704
     * @param mixed[] $options
705
     *
706
     * @return string
707
     */
708 110
    private function buildTableOptions(array $options)
709
    {
710 110
        if (! isset($options['table_options'])) {
711 106
            return '';
712
        }
713
714 4
        $tableOptions = [];
715 4
        foreach ($options['table_options'] as $key => $val) {
716 4
            $tableOptions[] = sprintf('"%s" = %s', $key, $this->quoteStringLiteral($val));
717
        }
718 4
        if (count($tableOptions) == 0) {
719
            return '';
720
        }
721
722 4
        return sprintf(' WITH (%s)', implode(', ', $tableOptions));
723
    }
724
725
    /**
726
     * Build SQL for sharding options.
727
     *
728
     * @param mixed[] $options
729
     *
730
     * @return string
731
     */
732 112
    private function buildShardingOptions(array $options)
733
    {
734 112
        $shardingOptions = [];
735
736 112
        if (isset($options['sharding_routing_column'])) {
737 4
            $columnName = new Identifier($options['sharding_routing_column']);
738 4
            $shardingOptions[] = sprintf('BY (%s)', $columnName->getQuotedName($this));
739
        }
740 112
        if (isset($options['sharding_num_shards'])) {
741 4
            $shardingOptions[] = sprintf("INTO %d SHARDS", $options['sharding_num_shards']);
742
        }
743
744 112
        if (count($shardingOptions) == 0) {
745 108
            return '';
746
        }
747
748 4
        return sprintf(" CLUSTERED %s", implode(' ', $shardingOptions));
749
    }
750
751
    /**
752
     * Build SQL for partition options.
753
     *
754
     * @param mixed[] $options
755
     *
756
     * @return string
757
     */
758 112
    private function buildPartitionOptions(array $options)
759
    {
760 112
        if (! isset($options['partition_columns'])) {
761 106
            return '';
762
        }
763 6
        $columns = $options['partition_columns'];
764 6
        if (! is_array($columns)) {
765 2
            throw new InvalidArgumentException(sprintf("Expecting array type at 'partition_columns'"));
766
        }
767 4
        $quotedNames = [];
768 4
        foreach ($columns as $name) {
769 4
            $name = new Identifier($name);
770 4
            $quotedNames[] = $name->getQuotedName($this);
771
        }
772
773 4
        return sprintf(" PARTITIONED BY (%s)", implode(', ', $quotedNames));
774
    }
775
776
    /**
777
     * @param \Doctrine\DBAL\Schema\Column $column The name of the table.
778
     * @param array List of primary key column names
0 ignored issues
show
Bug introduced by
The type Crate\DBAL\Platforms\List was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
779
     *
780
     * @return array The column data as associative array.
781
     * @throws DBALException
782
     */
783 118
    public static function prepareColumnData(AbstractPlatform $platform, $column, $primaries = array())
784
    {
785 118
        if ($column->hasCustomSchemaOption("unique") ? $column->getCustomSchemaOption("unique") : false) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Col...hasCustomSchemaOption() has been deprecated: Use {@link hasPlatformOption()} instead ( Ignorable by Annotation )

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

785
        if (/** @scrutinizer ignore-deprecated */ $column->hasCustomSchemaOption("unique") ? $column->getCustomSchemaOption("unique") : false) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Col...getCustomSchemaOption() has been deprecated: Use {@link getPlatformOption()} instead ( Ignorable by Annotation )

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

785
        if ($column->hasCustomSchemaOption("unique") ? /** @scrutinizer ignore-deprecated */ $column->getCustomSchemaOption("unique") : false) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
786 2
            throw DBALException::notSupported("Unique constraints are not supported. Use `primary key` instead");
787
        }
788
789 116
        $columnData = array();
790 116
        $columnData['name'] = $column->getQuotedName($platform);
791 116
        $columnData['type'] = $column->getType();
792 116
        $columnData['length'] = $column->getLength();
793 116
        $columnData['notnull'] = $column->getNotNull();
794 116
        $columnData['fixed'] = $column->getFixed();
795 116
        $columnData['unique'] = false;
796 116
        $columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption("version") : false;
797
798 116
        if (strtolower($columnData['type']->getName()) == $platform->getVarcharTypeDeclarationSQLSnippet(0, false)
799 116
                && $columnData['length'] === null) {
800
            $columnData['length'] = 255;
801
        }
802
803 116
        $columnData['unsigned'] = $column->getUnsigned();
804 116
        $columnData['precision'] = $column->getPrecision();
805 116
        $columnData['scale'] = $column->getScale();
806 116
        $columnData['default'] = $column->getDefault();
807 116
        $columnData['columnDefinition'] = $column->getColumnDefinition();
808 116
        $columnData['autoincrement'] = $column->getAutoincrement();
809 116
        $columnData['comment'] = $platform->getColumnComment($column);
810 116
        $columnData['platformOptions'] = $column->getPlatformOptions();
811
812 116
        if (in_array($column->getName(), $primaries)) {
813 72
            $columnData['primary'] = true;
814
        }
815 116
        return $columnData;
816
    }
817
818
    /**
819
     * {@inheritDoc}
820
     */
821 2
    public function getCreateDatabaseSQL($database)
822
    {
823 2
        throw DBALException::notSupported(__METHOD__);
824
    }
825
826
    /**
827
     * {@inheritDoc}
828
     */
829 2
    public function getDropDatabaseSQL($database)
830
    {
831 2
        throw DBALException::notSupported(__METHOD__);
832
    }
833
    
834
    /**
835
     * {@inheritDoc}
836
     */
837
    public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
838
    {
839
        throw DBALException::notSupported(__METHOD__);
840
    }
841
    
842
    /**
843
     * {@inheritDoc}
844
     */
845 2
    public function getGuidTypeDeclarationSQL(array $field)
846
    {
847 2
        throw DBALException::notSupported(__METHOD__);
848
    }
849
850
    /**
851
     * Returns the SQL query to return the CrateDB specific table options associated
852
     * with a given table.
853
     *
854
     * @return string
855
     */
856 6
    public function getTableOptionsSQL(string $table) : string
857
    {
858 6
        return "SELECT clustered_by, number_of_shards, partitioned_by, number_of_replicas, column_policy, settings " .
859 6
               "FROM information_schema.tables c " .
860 6
               "WHERE " . $this->getTableWhereClause($table);
861
    }
862
863
    /**
864
     * {@inheritDoc}
865
     */
866 10
    public function getCurrentDatabaseExpression(): string
867
    {
868
        // Added by Doctrine 3.
869 10
        return 'CURRENT_DATABASE()';
870
    }
871
}
872