Passed
Pull Request — master (#3157)
by Sergei
16:37
created

SQLAnywherePlatform::doModifyLimitQuery()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
ccs 11
cts 11
cp 1
cc 5
eloc 10
nc 12
nop 3
crap 5
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\DBALException;
23
use Doctrine\DBAL\LockMode;
24
use Doctrine\DBAL\Schema\Column;
25
use Doctrine\DBAL\Schema\ColumnDiff;
26
use Doctrine\DBAL\Schema\Constraint;
27
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
28
use Doctrine\DBAL\Schema\Identifier;
29
use Doctrine\DBAL\Schema\Index;
30
use Doctrine\DBAL\Schema\Table;
31
use Doctrine\DBAL\Schema\TableDiff;
32
use Doctrine\DBAL\TransactionIsolationLevel;
33
use function array_merge;
34
use function array_unique;
35
use function array_values;
36
use function count;
37
use function explode;
38
use function func_get_args;
39
use function get_class;
40
use function implode;
41
use function is_string;
42
use function preg_replace;
43
use function strlen;
44
use function strpos;
45
use function strtoupper;
46
use function substr;
47
48
/**
49
 * The SQLAnywherePlatform provides the behavior, features and SQL dialect of the
50
 * SAP Sybase SQL Anywhere 10 database platform.
51
 *
52
 * @author Steve Müller <[email protected]>
53
 * @link   www.doctrine-project.org
54
 * @since  2.5
55
 */
56
class SQLAnywherePlatform extends AbstractPlatform
57
{
58
    /**
59
     * @var int
60
     */
61
    const FOREIGN_KEY_MATCH_SIMPLE = 1;
62
    /**
63
     * @var int
64
     */
65
    const FOREIGN_KEY_MATCH_FULL = 2;
66
    /**
67
     * @var int
68
     */
69
    const FOREIGN_KEY_MATCH_SIMPLE_UNIQUE = 129;
70
    /**
71
     * @var int
72
     */
73
    const FOREIGN_KEY_MATCH_FULL_UNIQUE = 130;
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 504
    public function appendLockHint($fromClause, $lockMode)
79
    {
80 28
        switch (true) {
81 476
            case $lockMode === LockMode::NONE:
82 72
                return $fromClause . ' WITH (NOLOCK)';
83
84 432
            case $lockMode === LockMode::PESSIMISTIC_READ:
85 72
                return $fromClause . ' WITH (UPDLOCK)';
86
87 360
            case $lockMode === LockMode::PESSIMISTIC_WRITE:
88 72
                return $fromClause . ' WITH (XLOCK)';
89
90
            default:
91 288
                return $fromClause;
92
        }
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     *
98
     * SQL Anywhere supports a maximum length of 128 bytes for identifiers.
99
     */
100 72
    public function fixSchemaElementName($schemaElementName)
101
    {
102 72
        $maxIdentifierLength = $this->getMaxIdentifierLength();
103
104 72
        if (strlen($schemaElementName) > $maxIdentifierLength) {
105 72
            return substr($schemaElementName, 0, $maxIdentifierLength);
106
        }
107
108 72
        return $schemaElementName;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 504
    public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
115
    {
116 504
        $query = '';
117
118 504
        if ($foreignKey->hasOption('match')) {
119 72
            $query = ' MATCH ' . $this->getForeignKeyMatchClauseSQL($foreignKey->getOption('match'));
120
        }
121
122 504
        $query .= parent::getAdvancedForeignKeyOptionsSQL($foreignKey);
123
124 504
        if ($foreignKey->hasOption('check_on_commit') && (boolean) $foreignKey->getOption('check_on_commit')) {
125 72
            $query .= ' CHECK ON COMMIT';
126
        }
127
128 504
        if ($foreignKey->hasOption('clustered') && (boolean) $foreignKey->getOption('clustered')) {
129 72
            $query .= ' CLUSTERED';
130
        }
131
132 504
        if ($foreignKey->hasOption('for_olap_workload') && (boolean) $foreignKey->getOption('for_olap_workload')) {
133 72
            $query .= ' FOR OLAP WORKLOAD';
134
        }
135
136 504
        return $query;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 1080
    public function getAlterTableSQL(TableDiff $diff)
143
    {
144 1080
        $sql          = [];
145 1080
        $columnSql    = [];
146 1080
        $commentsSQL  = [];
147 1080
        $tableSql     = [];
148 1080
        $alterClauses = [];
149
150
        /** @var \Doctrine\DBAL\Schema\Column $column */
151 1080
        foreach ($diff->addedColumns as $column) {
152 288
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
153
                continue;
154
            }
155
156 288
            $alterClauses[] = $this->getAlterTableAddColumnClause($column);
157
158 288
            $comment = $this->getColumnComment($column);
159
160 288
            if (null !== $comment && '' !== $comment) {
161 72
                $commentsSQL[] = $this->getCommentOnColumnSQL(
162 72
                    $diff->getName($this)->getQuotedName($this),
163 72
                    $column->getQuotedName($this),
164 288
                    $comment
165
                );
166
            }
167
        }
168
169
        /** @var \Doctrine\DBAL\Schema\Column $column */
170 1080
        foreach ($diff->removedColumns as $column) {
171 216
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
172
                continue;
173
            }
174
175 216
            $alterClauses[] = $this->getAlterTableRemoveColumnClause($column);
176
        }
177
178
        /** @var \Doctrine\DBAL\Schema\ColumnDiff $columnDiff */
179 1080
        foreach ($diff->changedColumns as $columnDiff) {
180 576
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
181
                continue;
182
            }
183
184 576
            $alterClause = $this->getAlterTableChangeColumnClause($columnDiff);
185
186 576
            if (null !== $alterClause) {
187 360
                $alterClauses[] = $alterClause;
188
            }
189
190 576
            if ($columnDiff->hasChanged('comment')) {
191 216
                $column = $columnDiff->column;
192
193 216
                $commentsSQL[] = $this->getCommentOnColumnSQL(
194 216
                    $diff->getName($this)->getQuotedName($this),
195 216
                    $column->getQuotedName($this),
196 576
                    $this->getColumnComment($column)
197
                );
198
            }
199
        }
200
201 1080
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
202 288
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
203
                continue;
204
            }
205
206 288
            $sql[] = $this->getAlterTableClause($diff->getName($this)) . ' ' .
207 288
                $this->getAlterTableRenameColumnClause($oldColumnName, $column);
208
        }
209
210 1080
        if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
211 1080
            if ( ! empty($alterClauses)) {
212 432
                $sql[] = $this->getAlterTableClause($diff->getName($this)) . ' ' . implode(", ", $alterClauses);
213
            }
214
215 1080
            $sql = array_merge($sql, $commentsSQL);
216
217 1080
            if ($diff->newName !== false) {
218 144
                $sql[] = $this->getAlterTableClause($diff->getName($this)) . ' ' .
219 144
                    $this->getAlterTableRenameTableClause($diff->getNewName());
220
            }
221
222 1080
            $sql = array_merge(
223 1080
                $this->getPreAlterTableIndexForeignKeySQL($diff),
224 1080
                $sql,
225 1080
                $this->getPostAlterTableIndexForeignKeySQL($diff)
226
            );
227
        }
228
229 1080
        return array_merge($sql, $tableSql, $columnSql);
230
    }
231
232
    /**
233
     * Returns the SQL clause for creating a column in a table alteration.
234
     *
235
     * @param Column $column The column to add.
236
     *
237
     * @return string
238
     */
239 288
    protected function getAlterTableAddColumnClause(Column $column)
240
    {
241 288
        return 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
242
    }
243
244
    /**
245
     * Returns the SQL clause for altering a table.
246
     *
247
     * @param Identifier $tableName The quoted name of the table to alter.
248
     *
249
     * @return string
250
     */
251 576
    protected function getAlterTableClause(Identifier $tableName)
252
    {
253 576
        return 'ALTER TABLE ' . $tableName->getQuotedName($this);
254
    }
255
256
    /**
257
     * Returns the SQL clause for dropping a column in a table alteration.
258
     *
259
     * @param Column $column The column to drop.
260
     *
261
     * @return string
262
     */
263 216
    protected function getAlterTableRemoveColumnClause(Column $column)
264
    {
265 216
        return 'DROP ' . $column->getQuotedName($this);
266
    }
267
268
    /**
269
     * Returns the SQL clause for renaming a column in a table alteration.
270
     *
271
     * @param string $oldColumnName The quoted name of the column to rename.
272
     * @param Column $column        The column to rename to.
273
     *
274
     * @return string
275
     */
276 288
    protected function getAlterTableRenameColumnClause($oldColumnName, Column $column)
277
    {
278 288
        $oldColumnName = new Identifier($oldColumnName);
279
280 288
        return 'RENAME ' . $oldColumnName->getQuotedName($this) .' TO ' . $column->getQuotedName($this);
281
    }
282
283
    /**
284
     * Returns the SQL clause for renaming a table in a table alteration.
285
     *
286
     * @param Identifier $newTableName The quoted name of the table to rename to.
287
     *
288
     * @return string
289
     */
290 144
    protected function getAlterTableRenameTableClause(Identifier $newTableName)
291
    {
292 144
        return 'RENAME ' . $newTableName->getQuotedName($this);
293
    }
294
295
    /**
296
     * Returns the SQL clause for altering a column in a table alteration.
297
     *
298
     * This method returns null in case that only the column comment has changed.
299
     * Changes in column comments have to be handled differently.
300
     *
301
     * @param ColumnDiff $columnDiff The diff of the column to alter.
302
     *
303
     * @return string|null
304
     */
305 576
    protected function getAlterTableChangeColumnClause(ColumnDiff $columnDiff)
306
    {
307 576
        $column = $columnDiff->column;
308
309
        // Do not return alter clause if only comment has changed.
310 576
        if ( ! ($columnDiff->hasChanged('comment') && count($columnDiff->changedProperties) === 1)) {
311
            $columnAlterationClause = 'ALTER ' .
312 360
                $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
313
314 360
            if ($columnDiff->hasChanged('default') && null === $column->getDefault()) {
315
                $columnAlterationClause .= ', ALTER ' . $column->getQuotedName($this) . ' DROP DEFAULT';
316
            }
317
318 360
            return $columnAlterationClause;
319
        }
320
321 216
        return null;
322
    }
323
324
    /**
325
     * {@inheritdoc}
326
     */
327 72
    public function getBigIntTypeDeclarationSQL(array $columnDef)
328
    {
329 72
        $columnDef['integer_type'] = 'BIGINT';
330
331 72
        return $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
332
    }
333
334
    /**
335
     * {@inheritdoc}
336
     */
337 144
    public function getBinaryDefaultLength()
338
    {
339 144
        return 1;
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345 144
    public function getBinaryMaxLength()
346
    {
347 144
        return 32767;
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353 144
    public function getBlobTypeDeclarationSQL(array $field)
354
    {
355 144
        return 'LONG BINARY';
356
    }
357
358
    /**
359
     * {@inheritdoc}
360
     *
361
     * BIT type columns require an explicit NULL declaration
362
     * in SQL Anywhere if they shall be nullable.
363
     * Otherwise by just omitting the NOT NULL clause,
364
     * SQL Anywhere will declare them NOT NULL nonetheless.
365
     */
366 144
    public function getBooleanTypeDeclarationSQL(array $columnDef)
367
    {
368 144
        $nullClause = isset($columnDef['notnull']) && (boolean) $columnDef['notnull'] === false ? ' NULL' : '';
369
370 144
        return 'BIT' . $nullClause;
371
    }
372
373
    /**
374
     * {@inheritdoc}
375
     */
376 216
    public function getClobTypeDeclarationSQL(array $field)
377
    {
378 216
        return 'TEXT';
379
    }
380
381
    /**
382
     * {@inheritdoc}
383
     */
384 576
    public function getCommentOnColumnSQL($tableName, $columnName, $comment)
385
    {
386 576
        $tableName = new Identifier($tableName);
387 576
        $columnName = new Identifier($columnName);
388 576
        $comment = $comment === null ? 'NULL' : $this->quoteStringLiteral($comment);
389
390 576
        return "COMMENT ON COLUMN " . $tableName->getQuotedName($this) . '.' . $columnName->getQuotedName($this) .
391 576
            " IS $comment";
392
    }
393
394
    /**
395
     * {@inheritdoc}
396
     */
397 72
    public function getConcatExpression()
398
    {
399 72
        return 'STRING(' . implode(', ', (array) func_get_args()) . ')';
400
    }
401
402
    /**
403
     * {@inheritdoc}
404
     */
405 216
    public function getCreateConstraintSQL(Constraint $constraint, $table)
406
    {
407 216
        if ($constraint instanceof ForeignKeyConstraint) {
408 72
            return $this->getCreateForeignKeySQL($constraint, $table);
409
        }
410
411 216
        if ($table instanceof Table) {
412 72
            $table = $table->getQuotedName($this);
413
        }
414
415 216
        return 'ALTER TABLE ' . $table .
416 216
               ' ADD ' . $this->getTableConstraintDeclarationSQL($constraint, $constraint->getQuotedName($this));
417
    }
418
419
    /**
420
     * {@inheritdoc}
421
     */
422 72
    public function getCreateDatabaseSQL($database)
423
    {
424 72
        $database = new Identifier($database);
425
426 72
        return "CREATE DATABASE '" . $database->getName() . "'";
427
    }
428
429
    /**
430
     * {@inheritdoc}
431
     *
432
     * Appends SQL Anywhere specific flags if given.
433
     */
434 594
    public function getCreateIndexSQL(Index $index, $table)
435
    {
436 594
        return parent::getCreateIndexSQL($index, $table). $this->getAdvancedIndexOptionsSQL($index);
437
    }
438
439
    /**
440
     * {@inheritdoc}
441
     */
442 108
    public function getCreatePrimaryKeySQL(Index $index, $table)
443
    {
444 108
        if ($table instanceof Table) {
445 72
            $table = $table->getQuotedName($this);
446
        }
447
448 108
        return 'ALTER TABLE ' . $table . ' ADD ' . $this->getPrimaryKeyDeclarationSQL($index);
449
    }
450
451
    /**
452
     * {@inheritdoc}
453
     */
454 72
    public function getCreateTemporaryTableSnippetSQL()
455
    {
456 72
        return 'CREATE ' . $this->getTemporaryTableSQL() . ' TABLE';
457
    }
458
459
    /**
460
     * {@inheritdoc}
461
     */
462 72
    public function getCreateViewSQL($name, $sql)
463
    {
464 72
        return 'CREATE VIEW ' . $name . ' AS ' . $sql;
465
    }
466
467
    /**
468
     * {@inheritdoc}
469
     */
470 144
    public function getCurrentDateSQL()
471
    {
472 144
        return 'CURRENT DATE';
473
    }
474
475
    /**
476
     * {@inheritdoc}
477
     */
478 72
    public function getCurrentTimeSQL()
479
    {
480 72
        return 'CURRENT TIME';
481
    }
482
483
    /**
484
     * {@inheritdoc}
485
     */
486 144
    public function getCurrentTimestampSQL()
487
    {
488 144
        return 'CURRENT TIMESTAMP';
489
    }
490
491
    /**
492
     * {@inheritdoc}
493
     */
494 72
    protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
495
    {
496 72
        $factorClause = '';
497
498 72
        if ('-' === $operator) {
499 72
            $factorClause = '-1 * ';
500
        }
501
502 72
        return 'DATEADD(' . $unit . ', ' . $factorClause . $interval . ', ' . $date . ')';
503
    }
504
505
    /**
506
     * {@inheritdoc}
507
     */
508 72
    public function getDateDiffExpression($date1, $date2)
509
    {
510 72
        return 'DATEDIFF(day, ' . $date2 . ', ' . $date1 . ')';
511
    }
512
513
    /**
514
     * {@inheritdoc}
515
     */
516 108
    public function getDateTimeFormatString()
517
    {
518 108
        return 'Y-m-d H:i:s.u';
519
    }
520
521
    /**
522
     * {@inheritdoc}
523
     */
524 72
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
525
    {
526 72
        return 'DATETIME';
527
    }
528
529
    /**
530
     * {@inheritdoc}
531
     */
532 36
    public function getDateTimeTzFormatString()
533
    {
534 36
        return $this->getDateTimeFormatString();
535
    }
536
537
    /**
538
     * {@inheritdoc}
539
     */
540 72
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
541
    {
542 72
        return 'DATE';
543
    }
544
545
    /**
546
     * {@inheritdoc}
547
     */
548 72
    public function getDefaultTransactionIsolationLevel()
549
    {
550 72
        return TransactionIsolationLevel::READ_UNCOMMITTED;
551
    }
552
553
    /**
554
     * {@inheritdoc}
555
     */
556 72
    public function getDropDatabaseSQL($database)
557
    {
558 72
        $database = new Identifier($database);
559
560 72
        return "DROP DATABASE '" . $database->getName() . "'";
561
    }
562
563
    /**
564
     * {@inheritdoc}
565
     */
566 216
    public function getDropIndexSQL($index, $table = null)
567
    {
568 216
        if ($index instanceof Index) {
569 72
            $index = $index->getQuotedName($this);
570
        }
571
572 216
        if ( ! is_string($index)) {
573 72
            throw new \InvalidArgumentException(
574
                'SQLAnywherePlatform::getDropIndexSQL() expects $index parameter to be string or ' .
575 72
                '\Doctrine\DBAL\Schema\Index.'
576
            );
577
        }
578
579 144
        if ( ! isset($table)) {
580 72
            return 'DROP INDEX ' . $index;
581
        }
582
583 144
        if ($table instanceof Table) {
584 72
            $table = $table->getQuotedName($this);
585
        }
586
587 144
        if ( ! is_string($table)) {
588 72
            throw new \InvalidArgumentException(
589
                'SQLAnywherePlatform::getDropIndexSQL() expects $table parameter to be string or ' .
590 72
                '\Doctrine\DBAL\Schema\Table.'
591
            );
592
        }
593
594 72
        return 'DROP INDEX ' . $table . '.' . $index;
595
    }
596
597
    /**
598
     * {@inheritdoc}
599
     */
600 72
    public function getDropViewSQL($name)
601
    {
602 72
        return 'DROP VIEW ' . $name;
603
    }
604
605
    /**
606
     * {@inheritdoc}
607
     */
608 720
    public function getForeignKeyBaseDeclarationSQL(ForeignKeyConstraint $foreignKey)
609
    {
610 720
        $sql              = '';
611 720
        $foreignKeyName   = $foreignKey->getName();
612 720
        $localColumns     = $foreignKey->getQuotedLocalColumns($this);
613 720
        $foreignColumns   = $foreignKey->getQuotedForeignColumns($this);
614 720
        $foreignTableName = $foreignKey->getQuotedForeignTableName($this);
615
616 720
        if ( ! empty($foreignKeyName)) {
617 432
            $sql .= 'CONSTRAINT ' . $foreignKey->getQuotedName($this) . ' ';
618
        }
619
620 720
        if (empty($localColumns)) {
621 72
            throw new \InvalidArgumentException("Incomplete definition. 'local' required.");
622
        }
623
624 648
        if (empty($foreignColumns)) {
625 72
            throw new \InvalidArgumentException("Incomplete definition. 'foreign' required.");
626
        }
627
628 576
        if (empty($foreignTableName)) {
629 72
            throw new \InvalidArgumentException("Incomplete definition. 'foreignTable' required.");
630
        }
631
632 504
        if ($foreignKey->hasOption('notnull') && (boolean) $foreignKey->getOption('notnull')) {
633 72
            $sql .= 'NOT NULL ';
634
        }
635
636
        return $sql .
637 504
            'FOREIGN KEY (' . $this->getIndexFieldDeclarationListSQL($localColumns) . ') ' .
638 504
            'REFERENCES ' . $foreignKey->getQuotedForeignTableName($this) .
639 504
            ' (' . $this->getIndexFieldDeclarationListSQL($foreignColumns) . ')';
640
    }
641
642
    /**
643
     * Returns foreign key MATCH clause for given type.
644
     *
645
     * @param int $type The foreign key match type
646
     *
647
     * @return string
648
     *
649
     * @throws \InvalidArgumentException if unknown match type given
650
     */
651 216
    public function getForeignKeyMatchClauseSQL($type)
652
    {
653 216
        switch ((int) $type) {
654 216
            case self::FOREIGN_KEY_MATCH_SIMPLE:
655 72
                return 'SIMPLE';
656
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
657 216
            case self::FOREIGN_KEY_MATCH_FULL:
658 72
                return 'FULL';
659
                break;
660 216
            case self::FOREIGN_KEY_MATCH_SIMPLE_UNIQUE:
661 144
                return 'UNIQUE SIMPLE';
662
                break;
663 144
            case self::FOREIGN_KEY_MATCH_FULL_UNIQUE:
664 72
                return 'UNIQUE FULL';
665
            default:
666 72
                throw new \InvalidArgumentException('Invalid foreign key match type: ' . $type);
667
        }
668
    }
669
670
    /**
671
     * {@inheritdoc}
672
     */
673 576
    public function getForeignKeyReferentialActionSQL($action)
674
    {
675
        // NO ACTION is not supported, therefore falling back to RESTRICT.
676 576
        if (strtoupper($action) === 'NO ACTION') {
677 72
            return 'RESTRICT';
678
        }
679
680 504
        return parent::getForeignKeyReferentialActionSQL($action);
681
    }
682
683
    /**
684
     * {@inheritdoc}
685
     */
686 72
    public function getForUpdateSQL()
687
    {
688 72
        return '';
689
    }
690
691
    /**
692
     * {@inheritdoc}
693
     */
694 72
    public function getGuidExpression()
695
    {
696 72
        return 'NEWID()';
697
    }
698
699
    /**
700
     * {@inheritdoc}
701
     */
702 144
    public function getGuidTypeDeclarationSQL(array $field)
703
    {
704 144
        return 'UNIQUEIDENTIFIER';
705
    }
706
707
    /**
708
     * {@inheritdoc}
709
     */
710 144
    public function getIndexDeclarationSQL($name, Index $index)
711
    {
712
        // Index declaration in statements like CREATE TABLE is not supported.
713 144
        throw DBALException::notSupported(__METHOD__);
714
    }
715
716
    /**
717
     * {@inheritdoc}
718
     */
719 792
    public function getIntegerTypeDeclarationSQL(array $columnDef)
720
    {
721 792
        $columnDef['integer_type'] = 'INT';
722
723 792
        return $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
724
    }
725
726
    /**
727
     * {@inheritdoc}
728
     */
729
    public function getListDatabasesSQL()
730
    {
731
        return 'SELECT db_name(number) AS name FROM sa_db_list()';
732
    }
733
734
    /**
735
     * {@inheritdoc}
736
     */
737 72
    public function getListTableColumnsSQL($table, $database = null)
738
    {
739 72
        $user = 'USER_NAME()';
740
741 72
        if (strpos($table, '.') !== false) {
742 72
            list($user, $table) = explode('.', $table);
743 72
            $user = $this->quoteStringLiteral($user);
744
        }
745
746
        return "SELECT    col.column_name,
747
                          COALESCE(def.user_type_name, def.domain_name) AS 'type',
748
                          def.declared_width AS 'length',
749
                          def.scale,
750
                          CHARINDEX('unsigned', def.domain_name) AS 'unsigned',
751
                          IF col.nulls = 'Y' THEN 0 ELSE 1 ENDIF AS 'notnull',
752
                          col.\"default\",
753
                          def.is_autoincrement AS 'autoincrement',
754
                          rem.remarks AS 'comment'
755 72
                FROM      sa_describe_query('SELECT * FROM \"$table\"') AS def
756
                JOIN      SYS.SYSTABCOL AS col
757
                ON        col.table_id = def.base_table_id AND col.column_id = def.base_column_id
758
                LEFT JOIN SYS.SYSREMARK AS rem
759
                ON        col.object_id = rem.object_id
760 72
                WHERE     def.base_owner_name = $user
761
                ORDER BY  def.base_column_id ASC";
762
    }
763
764
    /**
765
     * {@inheritdoc}
766
     *
767
     * @todo Where is this used? Which information should be retrieved?
768
     */
769 144
    public function getListTableConstraintsSQL($table)
770
    {
771 144
        $user = '';
772
773 144
        if (strpos($table, '.') !== false) {
774 72
            list($user, $table) = explode('.', $table);
775 72
            $user = $this->quoteStringLiteral($user);
776 72
            $table = $this->quoteStringLiteral($table);
777
        } else {
778 72
            $table = $this->quoteStringLiteral($table);
779
        }
780
781
        return "SELECT con.*
782
                FROM   SYS.SYSCONSTRAINT AS con
783
                JOIN   SYS.SYSTAB AS tab ON con.table_object_id = tab.object_id
784 144
                WHERE  tab.table_name = $table
785 144
                AND    tab.creator = USER_ID($user)";
786
    }
787
788
    /**
789
     * {@inheritdoc}
790
     */
791 144
    public function getListTableForeignKeysSQL($table)
792
    {
793 144
        $user = '';
794
795 144
        if (strpos($table, '.') !== false) {
796 72
            list($user, $table) = explode('.', $table);
797 72
            $user = $this->quoteStringLiteral($user);
798 72
            $table = $this->quoteStringLiteral($table);
799
        } else {
800 72
            $table = $this->quoteStringLiteral($table);
801
        }
802
803
        return "SELECT    fcol.column_name AS local_column,
804
                          ptbl.table_name AS foreign_table,
805
                          pcol.column_name AS foreign_column,
806
                          idx.index_name,
807
                          IF fk.nulls = 'N'
808
                              THEN 1
809
                              ELSE NULL
810
                          ENDIF AS notnull,
811
                          CASE ut.referential_action
812
                              WHEN 'C' THEN 'CASCADE'
813
                              WHEN 'D' THEN 'SET DEFAULT'
814
                              WHEN 'N' THEN 'SET NULL'
815
                              WHEN 'R' THEN 'RESTRICT'
816
                              ELSE NULL
817
                          END AS  on_update,
818
                          CASE dt.referential_action
819
                              WHEN 'C' THEN 'CASCADE'
820
                              WHEN 'D' THEN 'SET DEFAULT'
821
                              WHEN 'N' THEN 'SET NULL'
822
                              WHEN 'R' THEN 'RESTRICT'
823
                              ELSE NULL
824
                          END AS on_delete,
825
                          IF fk.check_on_commit = 'Y'
826
                              THEN 1
827
                              ELSE NULL
828
                          ENDIF AS check_on_commit, -- check_on_commit flag
829
                          IF ftbl.clustered_index_id = idx.index_id
830
                              THEN 1
831
                              ELSE NULL
832
                          ENDIF AS 'clustered', -- clustered flag
833
                          IF fk.match_type = 0
834
                              THEN NULL
835
                              ELSE fk.match_type
836
                          ENDIF AS 'match', -- match option
837
                          IF pidx.max_key_distance = 1
838
                              THEN 1
839
                              ELSE NULL
840
                          ENDIF AS for_olap_workload -- for_olap_workload flag
841
                FROM      SYS.SYSFKEY AS fk
842
                JOIN      SYS.SYSIDX AS idx
843
                ON        fk.foreign_table_id = idx.table_id
844
                AND       fk.foreign_index_id = idx.index_id
845
                JOIN      SYS.SYSPHYSIDX pidx
846
                ON        idx.table_id = pidx.table_id
847
                AND       idx.phys_index_id = pidx.phys_index_id
848
                JOIN      SYS.SYSTAB AS ptbl
849
                ON        fk.primary_table_id = ptbl.table_id
850
                JOIN      SYS.SYSTAB AS ftbl
851
                ON        fk.foreign_table_id = ftbl.table_id
852
                JOIN      SYS.SYSIDXCOL AS idxcol
853
                ON        idx.table_id = idxcol.table_id
854
                AND       idx.index_id = idxcol.index_id
855
                JOIN      SYS.SYSTABCOL AS pcol
856
                ON        ptbl.table_id = pcol.table_id
857
                AND       idxcol.primary_column_id = pcol.column_id
858
                JOIN      SYS.SYSTABCOL AS fcol
859
                ON        ftbl.table_id = fcol.table_id
860
                AND       idxcol.column_id = fcol.column_id
861
                LEFT JOIN SYS.SYSTRIGGER ut
862
                ON        fk.foreign_table_id = ut.foreign_table_id
863
                AND       fk.foreign_index_id = ut.foreign_key_id
864
                AND       ut.event = 'C'
865
                LEFT JOIN SYS.SYSTRIGGER dt
866
                ON        fk.foreign_table_id = dt.foreign_table_id
867
                AND       fk.foreign_index_id = dt.foreign_key_id
868
                AND       dt.event = 'D'
869 144
                WHERE     ftbl.table_name = $table
870 144
                AND       ftbl.creator = USER_ID($user)
871
                ORDER BY  fk.foreign_index_id ASC, idxcol.sequence ASC";
872
    }
873
874
    /**
875
     * {@inheritdoc}
876
     */
877 144
    public function getListTableIndexesSQL($table, $currentDatabase = null)
878
    {
879 144
        $user = '';
880
881 144
        if (strpos($table, '.') !== false) {
882 72
            list($user, $table) = explode('.', $table);
883 72
            $user = $this->quoteStringLiteral($user);
884 72
            $table = $this->quoteStringLiteral($table);
885
        } else {
886 72
            $table = $this->quoteStringLiteral($table);
887
        }
888
889
        return "SELECT   idx.index_name AS key_name,
890
                         IF idx.index_category = 1
891
                             THEN 1
892
                             ELSE 0
893
                         ENDIF AS 'primary',
894
                         col.column_name,
895
                         IF idx.\"unique\" IN(1, 2, 5)
896
                             THEN 0
897
                             ELSE 1
898
                         ENDIF AS non_unique,
899
                         IF tbl.clustered_index_id = idx.index_id
900
                             THEN 1
901
                             ELSE NULL
902
                         ENDIF AS 'clustered', -- clustered flag
903
                         IF idx.\"unique\" = 5
904
                             THEN 1
905
                             ELSE NULL
906
                         ENDIF AS with_nulls_not_distinct, -- with_nulls_not_distinct flag
907
                         IF pidx.max_key_distance = 1
908
                              THEN 1
909
                              ELSE NULL
910
                          ENDIF AS for_olap_workload -- for_olap_workload flag
911
                FROM     SYS.SYSIDX AS idx
912
                JOIN     SYS.SYSPHYSIDX pidx
913
                ON       idx.table_id = pidx.table_id
914
                AND      idx.phys_index_id = pidx.phys_index_id
915
                JOIN     SYS.SYSIDXCOL AS idxcol
916
                ON       idx.table_id = idxcol.table_id AND idx.index_id = idxcol.index_id
917
                JOIN     SYS.SYSTABCOL AS col
918
                ON       idxcol.table_id = col.table_id AND idxcol.column_id = col.column_id
919
                JOIN     SYS.SYSTAB AS tbl
920
                ON       idx.table_id = tbl.table_id
921 144
                WHERE    tbl.table_name = $table
922 144
                AND      tbl.creator = USER_ID($user)
923
                AND      idx.index_category != 2 -- exclude indexes implicitly created by foreign key constraints
924
                ORDER BY idx.index_id ASC, idxcol.sequence ASC";
925
    }
926
927
    /**
928
     * {@inheritdoc}
929
     */
930
    public function getListTablesSQL()
931
    {
932
        return "SELECT   tbl.table_name
933
                FROM     SYS.SYSTAB AS tbl
934
                JOIN     SYS.SYSUSER AS usr ON tbl.creator = usr.user_id
935
                JOIN     dbo.SYSOBJECTS AS obj ON tbl.object_id = obj.id
936
                WHERE    tbl.table_type IN(1, 3) -- 'BASE', 'GBL TEMP'
937
                AND      usr.user_name NOT IN('SYS', 'dbo', 'rs_systabgroup') -- exclude system users
938
                AND      obj.type = 'U' -- user created tables only
939
                ORDER BY tbl.table_name ASC";
940
    }
941
942
    /**
943
     * {@inheritdoc}
944
     *
945
     * @todo Where is this used? Which information should be retrieved?
946
     */
947
    public function getListUsersSQL()
948
    {
949
        return 'SELECT * FROM SYS.SYSUSER ORDER BY user_name ASC';
950
    }
951
952
    /**
953
     * {@inheritdoc}
954
     */
955
    public function getListViewsSQL($database)
956
    {
957
        return "SELECT   tbl.table_name, v.view_def
958
                FROM     SYS.SYSVIEW v
959
                JOIN     SYS.SYSTAB tbl ON v.view_object_id = tbl.object_id
960
                JOIN     SYS.SYSUSER usr ON tbl.creator = usr.user_id
961
                JOIN     dbo.SYSOBJECTS obj ON tbl.object_id = obj.id
962
                WHERE    usr.user_name NOT IN('SYS', 'dbo', 'rs_systabgroup') -- exclude system users
963
                ORDER BY tbl.table_name ASC";
964
    }
965
966
    /**
967
     * {@inheritdoc}
968
     */
969 72
    public function getLocateExpression($str, $substr, $startPos = false)
970
    {
971 72
        if ($startPos == false) {
972 72
            return 'LOCATE(' . $str . ', ' . $substr . ')';
973
        }
974
975 72
        return 'LOCATE(' . $str . ', ' . $substr . ', ' . $startPos . ')';
976
    }
977
978
    /**
979
     * {@inheritdoc}
980
     */
981 144
    public function getMaxIdentifierLength()
982
    {
983 144
        return 128;
984
    }
985
986
    /**
987
     * {@inheritdoc}
988
     */
989 72
    public function getMd5Expression($column)
990
    {
991 72
        return "HASH(" . $column . ", 'MD5')";
992
    }
993
994
    /**
995
     * {@inheritdoc}
996
     */
997 216
    public function getName()
998
    {
999 216
        return 'sqlanywhere';
1000
    }
1001
1002
    /**
1003
     * Obtain DBMS specific SQL code portion needed to set a primary key
1004
     * declaration to be used in statements like ALTER TABLE.
1005
     *
1006
     * @param Index  $index Index definition
1007
     * @param string $name  Name of the primary key
1008
     *
1009
     * @return string DBMS specific SQL code portion needed to set a primary key
1010
     *
1011
     * @throws \InvalidArgumentException if the given index is not a primary key.
1012
     */
1013 252
    public function getPrimaryKeyDeclarationSQL(Index $index, $name = null)
1014
    {
1015 252
        if ( ! $index->isPrimary()) {
1016
            throw new \InvalidArgumentException(
1017
                'Can only create primary key declarations with getPrimaryKeyDeclarationSQL()'
1018
            );
1019
        }
1020
1021 252
        return $this->getTableConstraintDeclarationSQL($index, $name);
1022
    }
1023
1024
    /**
1025
     * {@inheritdoc}
1026
     */
1027 144
    public function getSetTransactionIsolationSQL($level)
1028
    {
1029 144
        return 'SET TEMPORARY OPTION isolation_level = ' . $this->_getTransactionIsolationLevelSQL($level);
1030
    }
1031
1032
    /**
1033
     * {@inheritdoc}
1034
     */
1035 72
    public function getSmallIntTypeDeclarationSQL(array $columnDef)
1036
    {
1037 72
        $columnDef['integer_type'] = 'SMALLINT';
1038
1039 72
        return $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
1040
    }
1041
1042
    /**
1043
     * Returns the SQL statement for starting an existing database.
1044
     *
1045
     * In SQL Anywhere you can start and stop databases on a
1046
     * database server instance.
1047
     * This is a required statement after having created a new database
1048
     * as it has to be explicitly started to be usable.
1049
     * SQL Anywhere does not automatically start a database after creation!
1050
     *
1051
     * @param string $database Name of the database to start.
1052
     *
1053
     * @return string
1054
     */
1055 72
    public function getStartDatabaseSQL($database)
1056
    {
1057 72
        $database = new Identifier($database);
1058
1059 72
        return "START DATABASE '" . $database->getName() . "' AUTOSTOP OFF";
1060
    }
1061
1062
    /**
1063
     * Returns the SQL statement for stopping a running database.
1064
     *
1065
     * In SQL Anywhere you can start and stop databases on a
1066
     * database server instance.
1067
     * This is a required statement before dropping an existing database
1068
     * as it has to be explicitly stopped before it can be dropped.
1069
     *
1070
     * @param string $database Name of the database to stop.
1071
     *
1072
     * @return string
1073
     */
1074 72
    public function getStopDatabaseSQL($database)
1075
    {
1076 72
        $database = new Identifier($database);
1077
1078 72
        return 'STOP DATABASE "' . $database->getName() . '" UNCONDITIONALLY';
1079
    }
1080
1081
    /**
1082
     * {@inheritdoc}
1083
     */
1084 72
    public function getSubstringExpression($value, $from, $length = null)
1085
    {
1086 72
        if (null === $length) {
1087 72
            return 'SUBSTRING(' . $value . ', ' . $from . ')';
1088
        }
1089
1090 72
        return 'SUBSTRING(' . $value . ', ' . $from . ', ' . $length . ')';
1091
    }
1092
1093
    /**
1094
     * {@inheritdoc}
1095
     */
1096 144
    public function getTemporaryTableSQL()
1097
    {
1098 144
        return 'GLOBAL TEMPORARY';
1099
    }
1100
1101
    /**
1102
     * {@inheritdoc}
1103
     */
1104 72
    public function getTimeFormatString()
1105
    {
1106 72
        return 'H:i:s.u';
1107
    }
1108
1109
    /**
1110
     * {@inheritdoc}
1111
     */
1112 72
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
1113
    {
1114 72
        return 'TIME';
1115
    }
1116
1117
    /**
1118
     * {@inheritdoc}
1119
     */
1120 72
    public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
1121
    {
1122 72
        if ( ! $char) {
1123 4
            switch ($pos) {
1124 68
                case TrimMode::LEADING:
1125 72
                    return $this->getLtrimExpression($str);
1126 68
                case TrimMode::TRAILING:
1127 72
                    return $this->getRtrimExpression($str);
1128
                default:
1129 72
                    return 'TRIM(' . $str . ')';
1130
            }
1131
        }
1132
1133 72
        $pattern = "'%[^' + $char + ']%'";
1134
1135 4
        switch ($pos) {
1136 68
            case TrimMode::LEADING:
1137 72
                return 'SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))';
1138 68
            case TrimMode::TRAILING:
1139 72
                return 'REVERSE(SUBSTR(REVERSE(' . $str . '), PATINDEX(' . $pattern . ', REVERSE(' . $str . '))))';
1140
            default:
1141
                return
1142 72
                    'REVERSE(SUBSTR(REVERSE(SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))), ' .
1143 72
                    'PATINDEX(' . $pattern . ', REVERSE(SUBSTR(' . $str . ', PATINDEX(' . $pattern . ', ' . $str . '))))))';
1144
        }
1145
    }
1146
1147
    /**
1148
     * {@inheritdoc}
1149
     */
1150 144
    public function getTruncateTableSQL($tableName, $cascade = false)
1151
    {
1152 144
        $tableIdentifier = new Identifier($tableName);
1153
1154 144
        return 'TRUNCATE TABLE ' . $tableIdentifier->getQuotedName($this);
1155
    }
1156
1157
    /**
1158
     * {@inheritdoc}
1159
     */
1160 288
    public function getUniqueConstraintDeclarationSQL($name, Index $index)
1161
    {
1162 288
        if ($index->isPrimary()) {
1163
            throw new \InvalidArgumentException(
1164
                'Cannot create primary key constraint declarations with getUniqueConstraintDeclarationSQL().'
1165
            );
1166
        }
1167
1168 288
        if ( ! $index->isUnique()) {
1169
            throw new \InvalidArgumentException(
1170
                'Can only create unique constraint declarations, no common index declarations with ' .
1171
                'getUniqueConstraintDeclarationSQL().'
1172
            );
1173
        }
1174
1175 288
        return $this->getTableConstraintDeclarationSQL($index, $name);
1176
    }
1177
1178
    /**
1179
     * {@inheritdoc}
1180
     */
1181 288
    public function getVarcharDefaultLength()
1182
    {
1183 288
        return 1;
1184
    }
1185
1186
    /**
1187
     * {@inheritdoc}
1188
     */
1189 936
    public function getVarcharMaxLength()
1190
    {
1191 936
        return 32767;
1192
    }
1193
1194
    /**
1195
     * {@inheritdoc}
1196
     */
1197 3240
    public function hasNativeGuidType()
1198
    {
1199 3240
        return true;
1200
    }
1201
1202
    /**
1203
     * {@inheritdoc}
1204
     */
1205 72
    public function prefersIdentityColumns()
1206
    {
1207 72
        return true;
1208
    }
1209
1210
    /**
1211
     * {@inheritdoc}
1212
     */
1213 936
    public function supportsCommentOnStatement()
1214
    {
1215 936
        return true;
1216
    }
1217
1218
    /**
1219
     * {@inheritdoc}
1220
     */
1221 72
    public function supportsIdentityColumns()
1222
    {
1223 72
        return true;
1224
    }
1225
1226
    /**
1227
     * {@inheritdoc}
1228
     */
1229 792
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
1230
    {
1231 792
        $unsigned      = ! empty($columnDef['unsigned']) ? 'UNSIGNED ' : '';
1232 792
        $autoincrement = ! empty($columnDef['autoincrement']) ? ' IDENTITY' : '';
1233
1234 792
        return $unsigned . $columnDef['integer_type'] . $autoincrement;
1235
    }
1236
1237
    /**
1238
     * {@inheritdoc}
1239
     */
1240 864
    protected function _getCreateTableSQL($tableName, array $columns, array $options = [])
1241
    {
1242 864
        $columnListSql = $this->getColumnDeclarationListSQL($columns);
1243 864
        $indexSql = [];
1244
1245 864
        if ( ! empty($options['uniqueConstraints'])) {
1246
            foreach ((array) $options['uniqueConstraints'] as $name => $definition) {
1247
                $columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($name, $definition);
1248
            }
1249
        }
1250
1251 864
        if ( ! empty($options['indexes'])) {
1252
            /** @var \Doctrine\DBAL\Schema\Index $index */
1253 288
            foreach ((array) $options['indexes'] as $index) {
1254 288
                $indexSql[] = $this->getCreateIndexSQL($index, $tableName);
1255
            }
1256
        }
1257
1258 864
        if ( ! empty($options['primary'])) {
1259 432
            $flags = '';
1260
1261 432
            if (isset($options['primary_index']) && $options['primary_index']->hasFlag('clustered')) {
1262
                $flags = ' CLUSTERED ';
1263
            }
1264
1265 432
            $columnListSql .= ', PRIMARY KEY' . $flags . ' (' . implode(', ', array_unique(array_values((array) $options['primary']))) . ')';
1266
        }
1267
1268 864
        if ( ! empty($options['foreignKeys'])) {
1269 144
            foreach ((array) $options['foreignKeys'] as $definition) {
1270 144
                $columnListSql .= ', ' . $this->getForeignKeyDeclarationSQL($definition);
1271
            }
1272
        }
1273
1274 864
        $query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql;
1275 864
        $check = $this->getCheckDeclarationSQL($columns);
1276
1277 864
        if ( ! empty($check)) {
1278 72
            $query .= ', ' . $check;
1279
        }
1280
1281 864
        $query .= ')';
1282
1283 864
        return array_merge([$query], $indexSql);
1284
    }
1285
1286
    /**
1287
     * {@inheritdoc}
1288
     */
1289 144
    protected function _getTransactionIsolationLevelSQL($level)
1290
    {
1291 8
        switch ($level) {
1292 136
            case TransactionIsolationLevel::READ_UNCOMMITTED:
1293 72
                return 0;
1294 136
            case TransactionIsolationLevel::READ_COMMITTED:
1295 72
                return 1;
1296 136
            case TransactionIsolationLevel::REPEATABLE_READ:
1297 72
                return 2;
1298 136
            case TransactionIsolationLevel::SERIALIZABLE:
1299 72
                return 3;
1300
            default:
1301 72
                throw new \InvalidArgumentException('Invalid isolation level:' . $level);
1302
        }
1303
    }
1304
1305
    /**
1306
     * {@inheritdoc}
1307
     */
1308 360
    protected function doModifyLimitQuery($query, $limit, $offset)
1309
    {
1310 360
        $limitOffsetClause = '';
1311
1312 360
        if ($limit > 0) {
1313 288
            $limitOffsetClause = 'TOP ' . $limit . ' ';
1314
        }
1315
1316 360
        if ($offset > 0) {
1317 72
            if ($limit == 0) {
1318 72
                $limitOffsetClause = 'TOP ALL ';
1319
            }
1320
1321 72
            $limitOffsetClause .= 'START AT ' . ($offset + 1) . ' ';
1322
        }
1323
1324 360
        if ($limitOffsetClause) {
1325 288
            return preg_replace('/^\s*(SELECT\s+(DISTINCT\s+)?)/i', '\1' . $limitOffsetClause, $query);
1326
        }
1327
1328 72
        return $query;
1329
    }
1330
1331
    /**
1332
     * Return the INDEX query section dealing with non-standard
1333
     * SQL Anywhere options.
1334
     *
1335
     * @param Index $index Index definition
1336
     *
1337
     * @return string
1338
     */
1339 576
    protected function getAdvancedIndexOptionsSQL(Index $index)
1340
    {
1341 576
        $sql = '';
1342
1343 576
        if ( ! $index->isPrimary() && $index->hasFlag('for_olap_workload')) {
1344 72
            $sql .= ' FOR OLAP WORKLOAD';
1345
        }
1346
1347 576
        return $sql;
1348
    }
1349
1350
    /**
1351
     * {@inheritdoc}
1352
     */
1353 72
    protected function getBinaryTypeDeclarationSQLSnippet($length, $fixed)
1354
    {
1355 72
        return $fixed
1356 72
            ? 'BINARY(' . ($length ?: $this->getBinaryDefaultLength()) . ')'
1357 72
            : 'VARBINARY(' . ($length ?: $this->getBinaryDefaultLength()) . ')';
1358
    }
1359
1360
    /**
1361
     * Returns the SQL snippet for creating a table constraint.
1362
     *
1363
     * @param Constraint  $constraint The table constraint to create the SQL snippet for.
1364
     * @param string|null $name       The table constraint name to use if any.
1365
     *
1366
     * @return string
1367
     *
1368
     * @throws \InvalidArgumentException if the given table constraint type is not supported by this method.
1369
     */
1370 756
    protected function getTableConstraintDeclarationSQL(Constraint $constraint, $name = null)
1371
    {
1372 756
        if ($constraint instanceof ForeignKeyConstraint) {
1373
            return $this->getForeignKeyDeclarationSQL($constraint);
1374
        }
1375
1376 756
        if ( ! $constraint instanceof Index) {
1377 72
            throw new \InvalidArgumentException('Unsupported constraint type: ' . get_class($constraint));
1378
        }
1379
1380 684
        if ( ! $constraint->isPrimary() && ! $constraint->isUnique()) {
1381 72
            throw new \InvalidArgumentException(
1382
                'Can only create primary, unique or foreign key constraint declarations, no common index declarations ' .
1383 72
                'with getTableConstraintDeclarationSQL().'
1384
            );
1385
        }
1386
1387 612
        $constraintColumns = $constraint->getQuotedColumns($this);
1388
1389 612
        if (empty($constraintColumns)) {
1390 144
            throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
1391
        }
1392
1393 468
        $sql   = '';
1394 468
        $flags = '';
1395
1396 468
        if ( ! empty($name)) {
1397 360
            $name = new Identifier($name);
1398 360
            $sql .= 'CONSTRAINT ' . $name->getQuotedName($this) . ' ';
1399
        }
1400
1401 468
        if ($constraint->hasFlag('clustered')) {
1402 216
            $flags = 'CLUSTERED ';
1403
        }
1404
1405 468
        if ($constraint->isPrimary()) {
1406 252
            return $sql . 'PRIMARY KEY ' . $flags . '('. $this->getIndexFieldDeclarationListSQL($constraintColumns) . ')';
1407
        }
1408
1409 288
        return $sql . 'UNIQUE ' . $flags . '('. $this->getIndexFieldDeclarationListSQL($constraintColumns) . ')';
1410
    }
1411
1412
    /**
1413
     * {@inheritdoc}
1414
     */
1415 594
    protected function getCreateIndexSQLFlags(Index $index)
1416
    {
1417 594
        $type = '';
1418 594
        if ($index->hasFlag('virtual')) {
1419 72
            $type .= 'VIRTUAL ';
1420
        }
1421
1422 594
        if ($index->isUnique()) {
1423 216
            $type .= 'UNIQUE ';
1424
        }
1425
1426 594
        if ($index->hasFlag('clustered')) {
1427 72
            $type .= 'CLUSTERED ';
1428
        }
1429
1430 594
        return $type;
1431
    }
1432
1433
    /**
1434
     * {@inheritdoc}
1435
     */
1436 360
    protected function getRenameIndexSQL($oldIndexName, Index $index, $tableName)
1437
    {
1438
        return [
1439 360
            'ALTER INDEX ' . $oldIndexName . ' ON ' . $tableName . ' RENAME TO ' . $index->getQuotedName($this)
1440
        ];
1441
    }
1442
1443
    /**
1444
     * {@inheritdoc}
1445
     */
1446 936
    protected function getReservedKeywordsClass()
1447
    {
1448 936
        return Keywords\SQLAnywhereKeywords::class;
1449
    }
1450
1451
    /**
1452
     * {@inheritdoc}
1453
     */
1454 864
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
1455
    {
1456 864
        return $fixed
1457 72
            ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(' . $this->getVarcharDefaultLength() . ')')
1458 864
            : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(' . $this->getVarcharDefaultLength() . ')');
1459
    }
1460
1461
    /**
1462
     * {@inheritdoc}
1463
     */
1464 396
    protected function initializeDoctrineTypeMappings()
1465
    {
1466 396
        $this->doctrineTypeMapping = [
1467
            'char' => 'string',
1468
            'long nvarchar' => 'text',
1469
            'long varchar' => 'text',
1470
            'nchar' => 'string',
1471
            'ntext' => 'text',
1472
            'nvarchar' => 'string',
1473
            'text' => 'text',
1474
            'uniqueidentifierstr' => 'guid',
1475
            'varchar' => 'string',
1476
            'xml' => 'text',
1477
            'bigint' => 'bigint',
1478
            'unsigned bigint' => 'bigint',
1479
            'bit' => 'boolean',
1480
            'decimal' => 'decimal',
1481
            'double' => 'float',
1482
            'float' => 'float',
1483
            'int' => 'integer',
1484
            'integer' => 'integer',
1485
            'unsigned int' => 'integer',
1486
            'numeric' => 'decimal',
1487
            'smallint' => 'smallint',
1488
            'unsigned smallint', 'smallint',
1489
            'tinyint' => 'smallint',
1490
            'unsigned tinyint', 'smallint',
1491
            'money' => 'decimal',
1492
            'smallmoney' => 'decimal',
1493
            'long varbit' => 'text',
1494
            'varbit' => 'string',
1495
            'date' => 'date',
1496
            'datetime' => 'datetime',
1497
            'smalldatetime' => 'datetime',
1498
            'time' => 'time',
1499
            'timestamp' => 'datetime',
1500
            'binary' => 'binary',
1501
            'image' => 'blob',
1502
            'long binary' => 'blob',
1503
            'uniqueidentifier' => 'guid',
1504
            'varbinary' => 'binary',
1505
        ];
1506 396
    }
1507
}
1508