Passed
Pull Request — master (#160)
by Dmitriy
02:29
created

CommandInterfaceProxy::addDefaultValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Database;
6
7
use Closure;
8
use Throwable;
9
use Yiisoft\Db\Command\CommandInterface;
10
use Yiisoft\Db\Profiler\ProfilerInterface;
11
use Yiisoft\Db\Query\Data\DataReaderInterface;
12
use Yiisoft\Db\Query\QueryInterface;
13
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
14
15
final class CommandInterfaceProxy implements CommandInterface
16
{
17
    public function __construct(
18
        private CommandInterface $decorated,
19
        private DatabaseCollector $collector
20
    ) {
21
    }
22
23
    public function addCheck(string $name, string $table, string $expression): static
24
    {
25
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
26
    }
27
28
    public function addColumn(string $table, string $column, string $type): static
29
    {
30
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
31
    }
32
33
    public function addCommentOnColumn(string $table, string $column, string $comment): static
34
    {
35
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
36
    }
37
38
    public function addCommentOnTable(string $table, string $comment): static
39
    {
40
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
41
    }
42
43
    public function addDefaultValue(string $name, string $table, string $column, mixed $value): static
44
    {
45
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
46
    }
47
48
    public function addForeignKey(
49
        string $name,
50
        string $table,
51
        array|string $columns,
52
        string $refTable,
53
        array|string $refColumns,
54
        string $delete = null,
55
        string $update = null
56
    ): static {
57
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
58
    }
59
60
    public function addPrimaryKey(string $name, string $table, array|string $columns): static
61
    {
62
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
63
    }
64
65
    public function addUnique(string $name, string $table, array|string $columns): static
66
    {
67
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
68
    }
69
70
    public function alterColumn(string $table, string $column, string $type): static
71
    {
72
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
73
    }
74
75
    public function batchInsert(string $table, array $columns, iterable $rows): static
76
    {
77
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
78
    }
79
80
    public function bindParam(
81
        int|string $name,
82
        mixed &$value,
83
        int $dataType = null,
84
        int $length = null,
85
        mixed $driverOptions = null
86
    ): static {
87
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
88
    }
89
90
    public function bindValue(int|string $name, mixed $value, int $dataType = null): static
91
    {
92
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
93
    }
94
95
    public function bindValues(array $values): static
96
    {
97
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
98
    }
99
100
    public function cancel(): void
101
    {
102
        $this->decorated->{__FUNCTION__}(...func_get_args());
103
    }
104
105
    public function checkIntegrity(string $schema, string $table, bool $check = true): static
106
    {
107
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
108
    }
109
110
    public function createIndex(
111
        string $name,
112
        string $table,
113
        array|string $columns,
114
        string $indexType = null,
115
        string $indexMethod = null
116
    ): static {
117
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
118
    }
119
120
    public function createTable(string $table, array $columns, string $options = null): static
121
    {
122
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
123
    }
124
125
    public function createView(string $viewName, QueryInterface|string $subQuery): static
126
    {
127
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
128
    }
129
130
    public function delete(string $table, array|string $condition = '', array $params = []): static
131
    {
132
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
133
    }
134
135
    public function dropCheck(string $name, string $table): static
136
    {
137
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
138
    }
139
140
    public function dropColumn(string $table, string $column): static
141
    {
142
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
143
    }
144
145
    public function dropCommentFromColumn(string $table, string $column): static
146
    {
147
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
148
    }
149
150
    public function dropCommentFromTable(string $table): static
151
    {
152
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
153
    }
154
155
    public function dropDefaultValue(string $name, string $table): static
156
    {
157
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
158
    }
159
160
    public function dropForeignKey(string $name, string $table): static
161
    {
162
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
163
    }
164
165
    public function dropIndex(string $name, string $table): static
166
    {
167
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
168
    }
169
170
    public function dropPrimaryKey(string $name, string $table): static
171
    {
172
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
173
    }
174
175
    public function dropTable(string $table): static
176
    {
177
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
178
    }
179
180
    public function dropUnique(string $name, string $table): static
181
    {
182
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
183
    }
184
185
    public function dropView(string $viewName): static
186
    {
187
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
188
    }
189
190
    public function execute(): int
191
    {
192
        [$callStack] = debug_backtrace();
193
194
        $id = random_bytes(36);
195
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
196
        try {
197
            $result = $this->decorated->execute();
198
        } catch (Throwable $e) {
199
            $this->collectQueryError($id, $e);
200
            throw $e;
201
        }
202
        $this->collectQueryEnd($id, $result);
203
        return $result;
204
    }
205
206
    public function getParams(bool $asValues = true): array
207
    {
208
        return $this->decorated->getParams($asValues);
209
    }
210
211
    public function getRawSql(): string
212
    {
213
        return $this->decorated->getRawSql();
214
    }
215
216
    public function getSql(): string
217
    {
218
        return $this->decorated->getSql();
219
    }
220
221
    public function insert(string $table, QueryInterface|array $columns): static
222
    {
223
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
224
    }
225
226
    public function insertWithReturningPks(string $table, array $columns): bool|array
227
    {
228
        return $this->decorated->{__FUNCTION__}(...func_get_args());
229
    }
230
231
    public function prepare(bool $forRead = null): void
232
    {
233
        $this->decorated->{__FUNCTION__}(...func_get_args());
234
    }
235
236
    public function query(): DataReaderInterface
237
    {
238
        [$callStack] = debug_backtrace();
239
240
        $id = random_bytes(36);
241
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
242
        try {
243
            $result = $this->decorated->query();
244
        } catch (Throwable $e) {
245
            $this->collectQueryError($id, $e);
246
            throw $e;
247
        }
248
        $rowsNumber = $result->count();
249
        $this->collectQueryEnd($id, $rowsNumber);
250
        return $result;
251
    }
252
253
    public function queryAll(): array
254
    {
255
        [$callStack] = debug_backtrace();
256
257
        $id = random_bytes(36);
258
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
259
        try {
260
            $result = $this->decorated->queryAll();
261
        } catch (Throwable $e) {
262
            $this->collectQueryError($id, $e);
263
            throw $e;
264
        }
265
        $this->collectQueryEnd($id, count($result));
266
        return $result;
267
    }
268
269
    public function queryBuilder(): QueryBuilderInterface
270
    {
271
        return $this->decorated->{__FUNCTION__}(...func_get_args());
272
    }
273
274
    public function queryColumn(): array
275
    {
276
        [$callStack] = debug_backtrace();
277
278
        $id = random_bytes(36);
279
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
280
        try {
281
            $result = $this->decorated->queryColumn();
282
        } catch (Throwable $e) {
283
            $this->collectQueryError($id, $e);
284
            throw $e;
285
        }
286
        $this->collectQueryEnd($id, count($result));
287
        return $result;
288
    }
289
290
    public function queryOne(): array|null
291
    {
292
        [$callStack] = debug_backtrace();
293
294
        $id = random_bytes(36);
295
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
296
        try {
297
            $result = $this->decorated->queryOne();
298
        } catch (Throwable $e) {
299
            $this->collectQueryError($id, $e);
300
            throw $e;
301
        }
302
        $this->collectQueryEnd($id, $result === null ? 0 : 1);
303
        return $result;
304
    }
305
306
    public function queryScalar(): bool|string|null|int|float
307
    {
308
        [$callStack] = debug_backtrace();
309
310
        $id = random_bytes(36);
311
        $this->collectQueryStart($id, $callStack['file'] . ':' . $callStack['line']);
312
        try {
313
            $result = $this->decorated->queryScalar();
314
        } catch (Throwable $e) {
315
            $this->collectQueryError($id, $e);
316
            throw $e;
317
        }
318
        $this->collectQueryEnd($id, $result === null ? 0 : 1);
319
        return $result;
320
    }
321
322
    public function renameColumn(string $table, string $oldName, string $newName): static
323
    {
324
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
325
    }
326
327
    public function renameTable(string $table, string $newName): static
328
    {
329
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
330
    }
331
332
    public function resetSequence(string $table, int|string $value = null): static
333
    {
334
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
335
    }
336
337
    public function setProfiler(?ProfilerInterface $profiler): void
338
    {
339
        $this->decorated->{__FUNCTION__}(...func_get_args());
340
    }
341
342
    public function setRawSql(string $sql): static
343
    {
344
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
345
    }
346
347
    public function setRetryHandler(?Closure $handler): static
348
    {
349
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
350
    }
351
352
    public function setSql(string $sql): static
353
    {
354
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
355
    }
356
357
    public function truncateTable(string $table): static
358
    {
359
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
360
    }
361
362
    public function update(string $table, array $columns, array|string $condition = '', array $params = []): static
363
    {
364
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
365
    }
366
367
    public function upsert(
368
        string $table,
369
        QueryInterface|array $insertColumns,
370
        bool|array $updateColumns = true,
371
        array $params = []
372
    ): static {
373
        return new self($this->decorated->{__FUNCTION__}(...func_get_args()), $this->collector);
374
    }
375
376
    private function collectQueryStart(string $id, string $line): void
377
    {
378
        $this->collector->collectQueryStart(
379
            id: $id,
380
            sql: $this->decorated->getSql(),
381
            rawSql: $this->decorated->getRawSql(),
382
            params: $this->decorated->getParams(),
383
            line: $line,
384
        );
385
    }
386
387
    private function collectQueryError(string $id, Throwable $exception): void
388
    {
389
        $this->collector->collectQueryError($id, $exception);
390
    }
391
392
    private function collectQueryEnd(string $id, int $rowsNumber): void
393
    {
394
        $this->collector->collectQueryEnd($id, $rowsNumber);
395
    }
396
}
397