Passed
Pull Request — master (#140)
by Alexander
07:36 queued 03:38
created

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