Passed
Push — main ( 27b0e4...52d055 )
by Andreas
02:03
created

CratePlatform::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
ccs 6
cts 8
cp 0.75
rs 10
cc 3
nc 4
nop 0
crap 3.1406
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\DBALException;
27
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
28
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
29
use Doctrine\DBAL\Events;
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 124
    public function __construct()
50
    {
51 124
        parent::__construct();
52 124
        $this->initializeDoctrineTypeMappings();
53 124
        if (!Type::hasType(MapType::NAME)) {
54
            Type::addType(MapType::NAME, 'Crate\DBAL\Types\MapType');
55
        }
56 124
        if (!Type::hasType(TimestampType::NAME)) {
57
            Type::addType(TimestampType::NAME, 'Crate\DBAL\Types\TimestampType');
58
        }
59 124
        Type::overrideType('array', 'Crate\DBAL\Types\ArrayType');
60 124
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65 1
    public function getSubstringExpression($value, $from = 0, $length = null)
66
    {
67 1
        if ($length === null) {
68 1
            return 'SUBSTR(' . $value . ', ' . $from . ')';
69
        }
70
71 1
        return 'SUBSTR(' . $value . ', ' . $from . ', ' . $length . ')';
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 1
    public function getNowExpression()
78
    {
79 1
        throw DBALException::notSupported(__METHOD__);
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85 1
    public function getRegexpExpression()
86
    {
87 1
        return 'LIKE';
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 1
    public function getDateDiffExpression($date1, $date2)
94
    {
95 1
        throw DBALException::notSupported(__METHOD__);
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 3
    public function supportsSequences()
102
    {
103 3
        return false;
104
    }
105
106
    /**
107
     * If we want to support Schemas, we need to implement 
108
     * getListNamespacesSQL and getCreateSchemaSQL methods
109
     * 
110
     * {@inheritDoc}
111
     */
112 3
    public function supportsSchemas()
113
    {
114 3
        return false;
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 1
    public function supportsIdentityColumns()
121
    {
122 1
        return true;
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 1
    public function supportsIndexes()
129
    {
130 1
        return false;
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136 59
    public function supportsCommentOnStatement()
137
    {
138 59
        return false;
139
    }
140
141
    /**
142
     * {@inheritDoc}
143
     */
144 7
    public function supportsForeignKeyConstraints()
145
    {
146 7
        return false;
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152 1
    public function supportsForeignKeyOnUpdate()
153
    {
154 1
        return false;
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160 1
    public function supportsViews()
161
    {
162 1
        return false;
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168 1
    public function prefersSequences()
169
    {
170 1
        return false;
171
    }
172
173
    /**
174
     * {@inheritDoc}
175
     */
176 1
    public function getListDatabasesSQL()
177
    {
178 1
        throw DBALException::notSupported(__METHOD__);
179
    }
180
181
    /**
182
     * {@inheritDoc}
183
     */
184
    public function getListTablesSQL()
185
    {
186
        return "SELECT table_name, schema_name FROM information_schema.tables " .
187
               "WHERE schema_name = 'doc' OR schema_name = 'blob'";
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193 4
    public function getListTableColumnsSQL($table, $database = null)
194
    {
195
        return "SELECT * from information_schema.columns c " .
196 4
               "WHERE " . $this->getTableWhereClause($table);
197
    }
198
199
    /**
200
     * {@inheritDoc}
201
     */
202
    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

202
    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...
203
    {
204
        return "SELECT c.constraint_name, c.constraint_type from information_schema.table_constraints c " .
205
               "WHERE " . $this->getTableWhereClause($table) . " AND constraint_type = 'PRIMARY KEY'";
206
    }
207
208
    /**
209
     * {@inheritDoc}
210
     */
211 4
    public function getListTableIndexesSQL($table, $currentDatabase = null)
212
    {
213
        return "SELECT c.constraint_name, c.constraint_type, k.column_name from information_schema.table_constraints c " .
214
               "JOIN information_schema.key_column_usage k on c.constraint_name = k.constraint_name " .
215 4
               "WHERE " . $this->getTableWhereClause($table);
216
    }
217
218 5
    private function getTableWhereClause($table, $tableAlias = 'c')
219
    {
220 5
        if (strpos($table, '.') !== false) {
221
            [$schema, $table] = explode('.', $table);
222
            $schema = $this->quoteStringLiteral($schema);
223
        } else {
224 5
            $schema = $this->quoteStringLiteral('doc');
225
        }
226
227 5
        $table = new Identifier($table);
228 5
        $table = $this->quoteStringLiteral($table->getName());
229
230 5
        return sprintf(
231 5
            $this->getTableWhereClauseFormat(),
232
            $tableAlias,
233
            $table,
234
            $tableAlias,
235
            $schema
236
        );
237
    }
238
239
    /**
240
     * Return sprintf format string for usage at getTableWhereClause
241
     *
242
     * @return string
243
     */
244
    protected function getTableWhereClauseFormat()
245
    {
246
      return self::TABLE_WHERE_CLAUSE_FORMAT;
247
    }
248
249
    /**
250
     * {@inheritDoc}
251
     */
252 5
    public function getAlterTableSQL(TableDiff $diff)
253
    {
254 5
        $sql = array();
255 5
        $commentsSQL = array();
256 5
        $columnSql = array();
257
258 5
        foreach ($diff->addedColumns as $column) {
259 2
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
260
                continue;
261
            }
262
263 2
            $query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
264 2
            $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
265 2
            if ($comment = $this->getColumnComment($column)) {
266
                $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
267
            }
268
        }
269
270 5
        if (count($diff->removedColumns) > 0) {
271
            throw DBALException::notSupported("Alter Table: drop columns");
272
        }
273 5
        if (count($diff->changedColumns) > 0) {
274
            throw DBALException::notSupported("Alter Table: change column options");
275
        }
276 5
        if (count($diff->renamedColumns) > 0) {
277
            throw DBALException::notSupported("Alter Table: rename columns");
278
        }
279
280 5
        $tableSql = array();
281
282 5
        if (!$this->onSchemaAlterTable($diff, $tableSql)) {
283 5
            if ($diff->newName !== false) {
284
                throw DBALException::notSupported("Alter Table: rename table");
285
            }
286
287 5
            $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...bleIndexForeignKeySQL() 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

287
            $sql = array_merge($sql, /** @scrutinizer ignore-deprecated */ $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL);
Loading history...
288
        }
289
290 5
        return array_merge($sql, $tableSql, $columnSql);
291
    }
292
293
    /**
294
     * {@inheritDoc}
295
     */
296 63
    public function getColumnDeclarationSQL($name, array $field)
297
    {
298 63
        if (isset($field['columnDefinition'])) {
299 34
            $columnDef = $this->getCustomTypeDeclarationSQL($field);
300
        } else {
301 62
            $typeDecl = $field['type']->getSqlDeclaration($field, $this);
302 62
            $columnDef = $typeDecl;
303
        }
304
305 63
        return $name . ' ' . $columnDef;
306
    }
307
308
    /**
309
     * Generate table index column declaration
310
     * @codeCoverageIgnore
311
     */
312
    public function getIndexDeclarationSQL($name, Index $index)
313
    {
314
        $columns = $index->getQuotedColumns($this);
315
        $name = new Identifier($name);
316
317
        if (count($columns) == 0) {
318
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
319
        }
320
321
        return 'INDEX ' . $name->getQuotedName($this) . ' USING FULLTEXT ('. $this->getIndexFieldDeclarationListSQL($columns) . ')';
322
    }
323
324
    /**
325
     * {@inheritDoc}
326
     *
327
     * Crate wants boolean values converted to the strings 'true'/'false'.
328
     */
329 2
    public function convertBooleans($item)
330
    {
331 2
        if (is_array($item)) {
332 1
            foreach ($item as $key => $value) {
333 1
                if (is_bool($value)) {
334 1
                    $item[$key] = ($value) ? 'true' : 'false';
335 1
                } elseif (is_numeric($value)) {
336 1
                    $item[$key] = ($value > 0) ? 'true' : 'false';
337
                }
338
            }
339
        } else {
340 2
            if (is_bool($item)) {
341 2
                $item = ($item) ? 'true' : 'false';
342 1
            } elseif (is_numeric($item)) {
343 1
                $item = ($item > 0) ? 'true' : 'false';
344
            }
345
        }
346
347 2
        return $item;
348
    }
349
350
    /**
351
     * {@inheritDoc}
352
     */
353 11
    public function getBooleanTypeDeclarationSQL(array $field)
354
    {
355 11
        return 'BOOLEAN';
356
    }
357
358
    /**
359
     * {@inheritDoc}
360
     */
361 57
    public function getIntegerTypeDeclarationSQL(array $field)
362
    {
363 57
        return 'INTEGER';
364
    }
365
366
    /**
367
     * {@inheritDoc}
368
     */
369
    public function getBigIntTypeDeclarationSQL(array $field)
370
    {
371
        return 'LONG';
372
    }
373
374
    /**
375
     * {@inheritDoc}
376
     */
377
    public function getSmallIntTypeDeclarationSQL(array $field)
378
    {
379
        return 'SHORT';
380
    }
381
382
    /**
383
     * {@inheritDoc}
384
     */
385
    public function getFloatDeclarationSQL(array $field)
386
    {
387
        return 'DOUBLE';
388
    }
389
390
    /**
391
     * {@inheritDoc}
392
     */
393
    public function getDecimalTypeDeclarationSQL(array $columnDef)
394
    {
395
        return 'DOUBLE';
396
    }
397
398
    /**
399
     * {@inheritDoc}
400
     */
401 26
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
402
    {
403 26
        return 'TIMESTAMP';
404
    }
405
406
    /**
407
     * {@inheritDoc}
408
     */
409 1
    public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
410
    {
411 1
        return 'TIMESTAMP';
412
    }
413
414
    /**
415
     * {@inheritDoc}
416
     */
417 1
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
418
    {
419 1
        return 'TIMESTAMP';
420
    }
421
422
    /**
423
     * {@inheritDoc}
424
     */
425 1
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
426
    {
427 1
        return 'TIMESTAMP';
428
    }
429
430
    /**
431
     * {@inheritDoc}
432
     */
433
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
434
    {
435
        return '';
436
    }
437
438
    /**
439
     * {@inheritDoc}
440
     */
441
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
442
    {
443
        return 'STRING';
444
    }
445
446
    /**
447
     * {@inheritDoc}
448
     */
449
    public function getClobTypeDeclarationSQL(array $field)
450
    {
451
        return 'STRING';
452
    }
453
454
    /**
455
     * {@inheritDoc}
456
     */
457 2
    public function getName()
458
    {
459 2
        return 'crate';
460
    }
461
462
    /**
463
     * {@inheritDoc}
464
     *
465
     * PostgreSQL returns all column names in SQL result sets in lowercase.
466
     */
467 1
    public function getSQLResultCasing($column)
468
    {
469 1
        return strtolower($column);
470
    }
471
472
    /**
473
     * {@inheritDoc}
474
     */
475 2
    public function getDateTimeTzFormatString()
476
    {
477 2
        return self::TIMESTAMP_FORMAT_TZ;
478
    }
479
480
    /**
481
     * {@inheritDoc}
482
     */
483 5
    public function getDateTimeFormatString()
484
    {
485 5
        return self::TIMESTAMP_FORMAT;
486
    }
487
488
    /**
489
     * {@inheritDoc}
490
     */
491 1
    public function getDateFormatString()
492
    {
493 1
        return self::TIMESTAMP_FORMAT;
494
    }
495
496
    /**
497
     * {@inheritDoc}
498
     */
499 1
    public function getTimeFormatString()
500
    {
501 1
        return self::TIMESTAMP_FORMAT;
502
    }
503
504
    /**
505
     * {@inheritDoc}
506
     */
507 1
    public function getTruncateTableSQL($tableName, $cascade = false)
508
    {
509 1
        throw DBALException::notSupported(__METHOD__);
510
    }
511
512
    /**
513
     * {@inheritDoc}
514
     */
515 1
    public function getReadLockSQL()
516
    {
517 1
        throw DBALException::notSupported(__METHOD__);
518
    }
519
520
    /**
521
     * {@inheritDoc}
522
     */
523 10
    protected function initializeDoctrineTypeMappings()
524
    {
525 10
        $this->doctrineTypeMapping = array(
526
            'short'         => 'smallint',
527
            'integer'       => 'integer',
528
            'long'          => 'bigint',
529
            'int'           => 'integer',
530
            'bool'          => 'boolean',
531
            'boolean'       => 'boolean',
532
            'string'        => 'string',
533
            'float'         => 'float',
534
            'double'        => 'float',
535
            'timestamp'     => 'timestamp',
536
            'object'        => 'map',
537
            'array'         => 'array',
538
        );
539 10
    }
540
541
    /**
542
     * {@inheritDoc}
543
     */
544 6
    public function getDoctrineTypeMapping($dbType)
545
    {
546
        // typed arrays will always end up in the same generic php array type
547 6
        if (substr_compare($dbType, 'array', -5) === 0) {
548 1
            $dbType = 'array';
549
        }
550 6
        return parent::getDoctrineTypeMapping($dbType);
551
    }
552
553
554
    /**
555
     * {@inheritDoc}
556
     */
557 54
    public function getVarcharMaxLength()
558
    {
559 54
        return PHP_INT_MAX;
560
    }
561
562
    /**
563
     * {@inheritDoc}
564
     */
565 27
    protected function getReservedKeywordsClass()
566
    {
567 27
        return 'Crate\DBAL\Platforms\Keywords\CrateKeywords';
568
    }
569
570
    /**
571
     * {@inheritDoc}
572
     */
573 1
    public function getBlobTypeDeclarationSQL(array $field)
574
    {
575 1
        throw DBALException::notSupported(__METHOD__);
576
    }
577
578
    /**
579
     * {@inheritDoc}
580
     * Gets the SQL statement(s) to create a table with the specified name, columns and constraints
581
     * on this platform.
582
     *
583
     * @param Table $table The name of the table.
584
     * @param integer $createFlags
585
     *
586
     * @return array The sequence of SQL statements.
587
     */
588 63
    public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES)
589
    {
590 63
        if (!is_int($createFlags)) {
0 ignored issues
show
introduced by
The condition is_int($createFlags) is always true.
Loading history...
591
            $msg = "Second argument of CratePlatform::getCreateTableSQL() has to be integer.";
592
            throw new InvalidArgumentException($msg);
593
        }
594
595 63
        if (count($table->getColumns()) === 0) {
596 2
            throw DBALException::noColumnsSpecifiedForTable($table->getName());
597
        }
598
599 61
        $tableName = $table->getQuotedName($this);
600 61
        $options = $table->getOptions();
601 61
        $options['uniqueConstraints'] = array();
602 61
        $options['indexes'] = array();
603 61
        $options['primary'] = array();
604
605 61
        if (($createFlags&self::CREATE_INDEXES) > 0) {
606 61
            foreach ($table->getIndexes() as $index) {
607
                /* @var $index Index */
608 44
                if ($index->isPrimary()) {
609 39
                    $platform = $this;
610
                    $options['primary'] = array_map(function ($columnName) use ($table, $platform) {
611 39
                        return $table->getColumn($columnName)->getQuotedName($platform);
612 39
                    }, $index->getColumns());
613 39
                    $options['primary_index'] = $index;
614 5
                } else if ($index->isUnique()) {
615 2
                    throw DBALException::notSupported("Unique constraints are not supported. Use `primary key` instead");
616
                } else {
617 3
                    $options['indexes'][$index->getName()] = $index;
618
                }
619
            }
620
        }
621
622 59
        $columnSql = array();
623 59
        $columns = array();
624
625 59
        foreach ($table->getColumns() as $column) {
626 59
            if (null !== $this->_eventManager &&
627 59
                $this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)) {
628
629 1
                $eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
630 1
                $this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
631
632 1
                $columnSql = array_merge($columnSql, $eventArgs->getSql());
633
634 1
                if ($eventArgs->isDefaultPrevented()) {
635
                    continue;
636
                }
637
            }
638 59
            $columns[$column->getQuotedName($this)] = self::prepareColumnData($this, $column, $options['primary']);
639
        }
640
641 58
        if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
642 1
            $eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
643 1
            $this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
644
645 1
            if ($eventArgs->isDefaultPrevented()) {
646
                return array_merge($eventArgs->getSql(), $columnSql);
647
            }
648
        }
649
650 58
        $sql = $this->_getCreateTableSQL($tableName, $columns, $options);
651 57
        if ($this->supportsCommentOnStatement()) {
652
            foreach ($table->getColumns() as $column) {
653
                if ($this->getColumnComment($column)) {
654
                    $sql[] = $this->getCommentOnColumnSQL(
655
                        $tableName,
656
                        $column->getName(),
657
                        $this->getColumnComment($column)
658
                    );
659
                }
660
            }
661
        }
662
663 57
        return array_merge($sql, $columnSql);
664
    }
665
666
    /**
667
     * {@inheritDoc}
668
     */
669 58
    protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
670
    {
671 58
        $columnListSql = $this->getColumnDeclarationListSQL($columns);
672
673 58
        if (isset($options['primary']) && ! empty($options['primary'])) {
674 39
            $keyColumns = array_unique(array_values($options['primary']));
675 39
            $columnListSql .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
676
        }
677
678 58
        if (isset($options['indexes']) && ! empty($options['indexes'])) {
679 3
            foreach ($options['indexes'] as $index => $definition) {
680 3
                $columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
681
            }
682
        }
683
 
684 58
        if (isset($options['foreignKeys'])) {
685
            throw DBALException::notSupported("Create Table: foreign keys");
686
        }
687
688 58
        $query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql . ')';
689 58
        $query .= $this->buildShardingOptions($options);
690 58
        $query .= $this->buildPartitionOptions($options);
691 57
        $query .= $this->buildTableOptions($options);
692 57
        return array($query);
693
    }
694
695
    /**
696
     * Build SQL for table options
697
     *
698
     * @param mixed[] $options
699
     *
700
     * @return string
701
     */
702 57
    private function buildTableOptions(array $options)
703
    {
704 57
        if (! isset($options['table_options'])) {
705 55
            return '';
706
        }
707
708 2
        $tableOptions = [];
709 2
        foreach($options['table_options'] as $key => $val) {
710 2
            $tableOptions[] = sprintf('"%s" = %s', $key, $this->quoteStringLiteral($val));
711
        }
712 2
        if (count($tableOptions) == 0) {
713
            return '';
714
        }
715
716 2
        return sprintf(' WITH (%s)', implode(', ', $tableOptions));
717
    }
718
719
    /**
720
     * Build SQL for sharding options.
721
     *
722
     * @param mixed[] $options
723
     *
724
     * @return string
725
     */
726 58
    private function buildShardingOptions(array $options)
727
    {
728 58
        $shardingOptions = [];
729
730 58
        if (isset($options['sharding_routing_column'])) {
731 2
            $columnName = new Identifier($options['sharding_routing_column']);
732 2
            $shardingOptions[] = sprintf('BY (%s)', $columnName->getQuotedName($this));
733
        }
734 58
        if (isset($options['sharding_num_shards'])) {
735 2
            $shardingOptions[] = sprintf("INTO %d SHARDS", $options['sharding_num_shards']);
736
        }
737
738 58
        if (count($shardingOptions) == 0) {
739 56
            return '';
740
        }
741
742 2
        return sprintf(" CLUSTERED %s", implode(' ', $shardingOptions));
743
    }
744
745
    /**
746
     * Build SQL for partition options.
747
     *
748
     * @param mixed[] $options
749
     *
750
     * @return string
751
     */
752 58
    private function buildPartitionOptions(array $options)
753
    {
754 58
        if (! isset($options['partition_columns'])) {
755 55
            return '';
756
        }
757 3
        $columns = $options['partition_columns'];
758 3
        if (! is_array($columns)) {
759 1
            throw new InvalidArgumentException(sprintf("Expecting array type at 'partition_columns'"));
760
        }
761 2
        $quotedNames = [];
762 2
        foreach ($columns as $name) {
763 2
            $name = new Identifier($name);
764 2
            $quotedNames[] = $name->getQuotedName($this);
765
        }
766
767 2
        return sprintf(" PARTITIONED BY (%s)", implode(', ', $quotedNames));
768
    }
769
770
    /**
771
     * @param \Doctrine\DBAL\Schema\Column $column The name of the table.
772
     * @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...
773
     *
774
     * @return array The column data as associative array.
775
     * @throws DBALException
776
     */
777 61
    public static function prepareColumnData(AbstractPlatform $platform, $column, $primaries = array())
778
    {
779 61
        if ($column->hasCustomSchemaOption("unique") ? $column->getCustomSchemaOption("unique") : false) {
780 1
            throw DBALException::notSupported("Unique constraints are not supported. Use `primary key` instead");
781
        }
782
783 60
        $columnData = array();
784 60
        $columnData['name'] = $column->getQuotedName($platform);
785 60
        $columnData['type'] = $column->getType();
786 60
        $columnData['length'] = $column->getLength();
787 60
        $columnData['notnull'] = $column->getNotNull();
788 60
        $columnData['fixed'] = $column->getFixed();
789 60
        $columnData['unique'] = false;
790 60
        $columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption("version") : false;
791
792 60
        if (strtolower($columnData['type']) == $platform->getVarcharTypeDeclarationSQLSnippet(0, false)
793 60
                && $columnData['length'] === null) {
794
            $columnData['length'] = 255;
795
        }
796
797 60
        $columnData['unsigned'] = $column->getUnsigned();
798 60
        $columnData['precision'] = $column->getPrecision();
799 60
        $columnData['scale'] = $column->getScale();
800 60
        $columnData['default'] = $column->getDefault();
801 60
        $columnData['columnDefinition'] = $column->getColumnDefinition();
802 60
        $columnData['autoincrement'] = $column->getAutoincrement();
803 60
        $columnData['comment'] = $platform->getColumnComment($column);
804 60
        $columnData['platformOptions'] = $column->getPlatformOptions();
805
806 60
        if (in_array($column->getName(), $primaries)) {
807 38
            $columnData['primary'] = true;
808
        }
809 60
        return $columnData;
810
    }
811
812
    /**
813
     * {@inheritDoc}
814
     */
815 1
    public function getCreateDatabaseSQL($database)
816
    {
817 1
        throw DBALException::notSupported(__METHOD__);
818
    }
819
820
    /**
821
     * {@inheritDoc}
822
     */
823 1
    public function getDropDatabaseSQL($database)
824
    {
825 1
        throw DBALException::notSupported(__METHOD__);
826
    }
827
    
828
    /**
829
     * {@inheritDoc}
830
     */
831 1
    public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table)
832
    {
833 1
        throw DBALException::notSupported(__METHOD__);
834
    }
835
    
836
    /**
837
     * {@inheritDoc}
838
     */
839 1
    public function getGuidTypeDeclarationSQL(array $field)
840
    {
841 1
        throw DBALException::notSupported(__METHOD__);
842
    }
843
844
    /**
845
     * Returns the SQL query to return the CrateDB specific table options associated
846
     * with a given table.
847
     *
848
     * @return string
849
     */
850 3
    public function getTableOptionsSQL(string $table) : string
851
    {
852
        return "SELECT clustered_by, number_of_shards, partitioned_by, number_of_replicas, column_policy, settings " .
853
		"FROM information_schema.tables c " .
854 3
		"WHERE " . $this->getTableWhereClause($table);
855
    }
856
}
857