1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees: online genealogy |
5
|
|
|
* Copyright (C) 2024 webtrees development team |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* You should have received a copy of the GNU General Public License |
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\DB; |
21
|
|
|
|
22
|
|
|
use Fisharebest\Webtrees\Arr; |
23
|
|
|
use Fisharebest\Webtrees\DB; |
24
|
|
|
use LogicException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Simplify constructor arguments for doctrine/dbal. |
28
|
|
|
* |
29
|
|
|
* @internal - use DB::select(), DB::update(), DB::insertInto(), DB::deleteFrom() |
30
|
|
|
*/ |
31
|
|
|
final class Query |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @param array<int|Expression,string|Expression> $columns |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
private readonly array $columns = [], |
38
|
|
|
private readonly bool $distinct = false, |
39
|
|
|
private readonly string $table = '', |
40
|
|
|
private readonly int $offset = 0, |
41
|
|
|
private readonly int $limit = 0, |
42
|
|
|
) { |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function distinct(): self |
46
|
|
|
{ |
47
|
|
|
return new self( |
48
|
|
|
columns: $this->columns, |
49
|
|
|
distinct: true, |
50
|
|
|
table: $this->table, |
51
|
|
|
offset: $this->offset, |
52
|
|
|
limit: $this->limit, |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function from(string $table): self |
57
|
|
|
{ |
58
|
|
|
return new self( |
59
|
|
|
columns: $this->columns, |
60
|
|
|
distinct: $this->distinct, |
61
|
|
|
table: DB::prefix($table), |
62
|
|
|
offset: $this->offset, |
63
|
|
|
limit: $this->limit, |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* This is an update query. Return the count of updated rows. |
69
|
|
|
* |
70
|
|
|
* @param array<string,string|Expression> $updates |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public function set(array $updates): int |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if ( |
77
|
|
|
$this->columns !== [] || |
78
|
|
|
$this->distinct === true || |
79
|
|
|
$this->table === '' || |
80
|
|
|
$this->offset !== 0 || |
81
|
|
|
$this->limit !== 0 |
82
|
|
|
) { |
83
|
|
|
throw new LogicException('Invalid SQL query definition'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Arr<int,object> |
91
|
|
|
*/ |
92
|
|
|
public function rows(): Arr |
93
|
|
|
{ |
94
|
|
|
return new Arr(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Arr<int|string,int|string> |
99
|
|
|
*/ |
100
|
|
|
public function pluck(): Arr |
101
|
|
|
{ |
102
|
|
|
return new Arr(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function firstRow(): object |
106
|
|
|
{ |
107
|
|
|
return (object) []; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function first(): string|int|float|null |
111
|
|
|
{ |
112
|
|
|
return 0; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.