Test Failed
Pull Request — dev (#105)
by Def
32:07 queued 20:37
created

DDLQueryBuilder::truncateTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\InvalidArgumentException;
9
use Yiisoft\Db\Exception\NotSupportedException;
10
use Yiisoft\Db\Query\DDLQueryBuilder as AbstractDDLQueryBuilder;
11
use Yiisoft\Db\Query\QueryBuilderInterface;
12
use Yiisoft\Db\Schema\ColumnSchemaBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Sqlite\ColumnSchemaBuilder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use Yiisoft\Db\Schema\QuoterInterface;
14
use Yiisoft\Db\Schema\SchemaInterface;
15
16
use function count;
17
18 336
final class DDLQueryBuilder extends AbstractDDLQueryBuilder
19
{
20 336
    public function __construct(
21
        private QueryBuilderInterface $queryBuilder,
22
        private QuoterInterface $quoter,
23
        SchemaInterface $schema
24
    ) {
25
        parent::__construct($queryBuilder, $quoter, $schema);
26
    }
27
28
    /**
29
     * @throws NotSupportedException
30
     */
31
    public function addCheck(string $name, string $table, string $expression): string
32
    {
33
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
34
    }
35
36
    /**
37
     * @throws NotSupportedException
38
     */
39
    public function addCommentOnColumn(string $table, string $column, string $comment): string
40
    {
41
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
42
    }
43
44
    /**
45
     * @throws NotSupportedException
46
     */
47
    public function addCommentOnTable(string $table, string $comment): string
48
    {
49
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
50 3
    }
51
52
    /**
53
     * @throws NotSupportedException
54
     */
55
    public function addForeignKey(
56
        string $name,
57
        string $table,
58
        array|string $columns,
59 3
        string $refTable,
60
        array|string $refColumns,
61
        ?string $delete = null,
62
        ?string $update = null
63
    ): string {
64
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
65
    }
66
67
    /**
68
     * @throws NotSupportedException
69
     */
70
    public function addPrimaryKey(string $name, string $table, array|string $columns): string
71
    {
72
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
73 1
    }
74
75 1
    /**
76
     * @throws NotSupportedException
77
     */
78
    public function addUnique(string $name, string $table, array|string $columns): string
79
    {
80
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
81
    }
82
83
    /**
84
     * @throws NotSupportedException
85
     */
86
    public function alterColumn(string $table, string $column, ColumnSchemaBuilder|string $type): string
87
    {
88
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
89
    }
90
91
    public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
92
    {
93
        return 'PRAGMA foreign_keys=' . (int) $check;
94 7
    }
95
96 7
    /**
97
     * @throws Exception|InvalidArgumentException
98 7
     */
99 7
    public function createIndex(string $name, string $table, array|string $columns, ?string $indexType = null, ?string $indexMethod = null): string
100 1
    {
101
        $tableParts = explode('.', $table);
102
103 7
        $schema = null;
104 7
        if (count($tableParts) === 2) {
105
            [$schema, $table] = $tableParts;
106 7
        }
107 7
108
        return 'CREATE ' . ($indexType ? ($indexType . ' ') : '') . 'INDEX '
109
            . $this->quoter->quoteTableName(($schema ? $schema . '.' : '') . $name)
110
            . ' ON '
111
            . $this->quoter->quoteTableName($table)
112
            . ' (' . $this->queryBuilder->buildColumns($columns) . ')';
113
    }
114
115
    /**
116
     * @throws NotSupportedException
117
     */
118
    public function dropCheck(string $name, string $table): string
119
    {
120
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
121
    }
122
123
    /**
124
     * @throws NotSupportedException
125
     */
126
    public function dropColumn(string $table, string $column): string
127
    {
128
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
129
    }
130
131
    /**
132
     * @throws NotSupportedException
133
     */
134
    public function dropCommentFromColumn(string $table, string $column): string
135
    {
136
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
137
    }
138
139
    /**
140
     * @throws NotSupportedException
141
     */
142
    public function dropCommentFromTable(string $table): string
143
    {
144
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
145 2
    }
146
147 2
    /**
148
     * @throws NotSupportedException
149
     */
150 2
    public function dropForeignKey(string $name, string $table): string
151
    {
152 2
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
153
    }
154
155
    public function dropIndex(string $name, string $table): string
156
    {
157
        return 'DROP INDEX ' . $this->quoter->quoteTableName($name);
158
    }
159
160
    /**
161
     * @throws NotSupportedException
162
     */
163
    public function dropPrimaryKey(string $name, string $table): string
164
    {
165
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
166 1
    }
167
168 1
    /**
169
     * @throws NotSupportedException
170
     */
171
    public function dropUnique(string $name, string $table): string
172
    {
173
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
174
    }
175
176
    /**
177
     * @throws NotSupportedException
178
     */
179 3
    public function renameColumn(string $table, string $oldName, string $newName): string
180
    {
181
        throw new NotSupportedException(__METHOD__ . ' is not supported by SQLite.');
182 3
    }
183
184 3
    public function renameTable(string $oldName, string $newName): string
185
    {
186
        return 'ALTER TABLE '
187 1
            . $this->quoter->quoteTableName($oldName)
188
            . ' RENAME TO '
189 1
            . $this->quoter->quoteTableName($newName);
190
    }
191
}
192