1 | <?php |
||
2 | |||
3 | /** |
||
4 | * webtrees: online genealogy |
||
5 | * Copyright (C) 2025 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; |
||
21 | |||
22 | use Closure; |
||
23 | use Illuminate\Database\Capsule\Manager; |
||
24 | use Illuminate\Database\Query\Builder; |
||
25 | use Illuminate\Database\Query\Expression; |
||
26 | use PDO; |
||
27 | use PDOException; |
||
28 | use RuntimeException; |
||
29 | use SensitiveParameter; |
||
30 | |||
31 | /** |
||
32 | * Database abstraction |
||
33 | */ |
||
34 | class DB extends Manager |
||
35 | { |
||
36 | // Supported drivers |
||
37 | public const string MYSQL = 'mysql'; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
38 | public const string POSTGRES = 'pgsql'; |
||
39 | public const string SQLITE = 'sqlite'; |
||
40 | public const string SQL_SERVER = 'sqlsrv'; |
||
41 | |||
42 | private const array COLLATION_ASCII = [ |
||
43 | self::MYSQL => 'ascii_bin', |
||
44 | self::POSTGRES => 'C', |
||
45 | self::SQLITE => 'BINARY', |
||
46 | self::SQL_SERVER => 'Latin1_General_Bin', |
||
47 | ]; |
||
48 | |||
49 | private const array COLLATION_UTF8 = [ |
||
50 | self::MYSQL => 'utf8mb4_unicode_ci', |
||
51 | self::POSTGRES => 'und-x-icu', |
||
52 | self::SQLITE => 'NOCASE', |
||
53 | self::SQL_SERVER => 'utf8_CI_AI', |
||
54 | ]; |
||
55 | |||
56 | private const array REGEX_OPERATOR = [ |
||
57 | self::MYSQL => 'REGEXP', |
||
58 | self::POSTGRES => '~', |
||
59 | self::SQLITE => 'REGEXP', |
||
60 | self::SQL_SERVER => 'REGEXP', |
||
61 | ]; |
||
62 | |||
63 | private const array DRIVER_INITIALIZATION = [ |
||
64 | self::MYSQL => "SET NAMES utf8mb4, sql_mode := 'ANSI,STRICT_ALL_TABLES', TIME_ZONE := '+00:00', SQL_BIG_SELECTS := 1, GROUP_CONCAT_MAX_LEN := 1048576", |
||
65 | self::POSTGRES => '', |
||
66 | self::SQLITE => 'PRAGMA foreign_keys = ON', |
||
67 | self::SQL_SERVER => 'SET language us_english', // For timestamp columns |
||
68 | ]; |
||
69 | |||
70 | public static function connect( |
||
71 | #[SensitiveParameter] |
||
72 | string $driver, |
||
73 | #[SensitiveParameter] |
||
74 | string $host, |
||
75 | #[SensitiveParameter] |
||
76 | string $port, |
||
77 | #[SensitiveParameter] |
||
78 | string $database, |
||
79 | #[SensitiveParameter] |
||
80 | string $username, |
||
81 | #[SensitiveParameter] |
||
82 | string $password, |
||
83 | #[SensitiveParameter] |
||
84 | string $prefix, |
||
85 | #[SensitiveParameter] |
||
86 | string $key, |
||
87 | #[SensitiveParameter] |
||
88 | string $certificate, |
||
89 | #[SensitiveParameter] |
||
90 | string $ca, |
||
91 | #[SensitiveParameter] |
||
92 | bool $verify_certificate, |
||
93 | ): void { |
||
94 | $options = [ |
||
95 | // Some drivers do this and some don't. Make them consistent. |
||
96 | PDO::ATTR_STRINGIFY_FETCHES => true, |
||
97 | ]; |
||
98 | |||
99 | // MySQL/MariaDB support encrypted connections |
||
100 | if ($driver === self::MYSQL && $key !== '' && $certificate !== '' && $ca !== '') { |
||
101 | $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = $verify_certificate; |
||
102 | $options[PDO::MYSQL_ATTR_SSL_KEY] = Webtrees::ROOT_DIR . 'data/' . $key; |
||
103 | $options[PDO::MYSQL_ATTR_SSL_CERT] = Webtrees::ROOT_DIR . 'data/' . $certificate; |
||
104 | $options[PDO::MYSQL_ATTR_SSL_CA] = Webtrees::ROOT_DIR . 'data/' . $ca; |
||
105 | } |
||
106 | |||
107 | if ($driver === self::SQLITE && $database !== ':memory:') { |
||
108 | $database = Webtrees::ROOT_DIR . 'data/' . $database . '.sqlite'; |
||
109 | } |
||
110 | |||
111 | $capsule = new self(); |
||
112 | $capsule->addConnection([ |
||
113 | 'driver' => $driver, |
||
114 | 'host' => $host, |
||
115 | 'port' => $port, |
||
116 | 'database' => $database, |
||
117 | 'username' => $username, |
||
118 | 'password' => $password, |
||
119 | 'prefix' => $prefix, |
||
120 | 'prefix_indexes' => true, |
||
121 | 'options' => $options, |
||
122 | ]); |
||
123 | $capsule->setAsGlobal(); |
||
124 | |||
125 | // Eager-load the connection, to prevent database credentials appearing in error logs. |
||
126 | try { |
||
127 | self::pdo(); |
||
128 | } catch (PDOException $exception) { |
||
129 | throw new RuntimeException($exception->getMessage()); |
||
130 | } |
||
131 | |||
132 | $sql = self::DRIVER_INITIALIZATION[$driver]; |
||
133 | |||
134 | if ($sql !== '') { |
||
135 | self::exec($sql); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public static function driverName(): string |
||
140 | { |
||
141 | return self::pdo()->getAttribute(PDO::ATTR_DRIVER_NAME); |
||
142 | } |
||
143 | |||
144 | public static function exec(string $sql): int|false |
||
145 | { |
||
146 | return self::pdo()->exec($sql); |
||
147 | } |
||
148 | |||
149 | public static function lastInsertId(): int |
||
150 | { |
||
151 | $return = self::pdo()->lastInsertId(); |
||
152 | |||
153 | if ($return === false) { |
||
154 | throw new RuntimeException('Unable to retrieve last insert ID'); |
||
155 | } |
||
156 | |||
157 | // All IDs are integers in our schema. |
||
158 | return (int) $return; |
||
159 | } |
||
160 | |||
161 | private static function pdo(): PDO |
||
162 | { |
||
163 | return parent::connection()->getPdo(); |
||
164 | } |
||
165 | |||
166 | public static function prefix(string $identifier): string |
||
167 | { |
||
168 | return parent::connection()->getTablePrefix() . $identifier; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * SQL-Server needs to be told that we are going to insert into an identity column. |
||
173 | * |
||
174 | * @param Closure(): void $callback |
||
175 | */ |
||
176 | public static function identityInsert(string $table, Closure $callback): void |
||
177 | { |
||
178 | if (self::driverName() === self::SQL_SERVER) { |
||
179 | self::exec('SET IDENTITY_INSERT [' . self::prefix(identifier: $table) . '] ON'); |
||
180 | } |
||
181 | |||
182 | $callback(); |
||
183 | |||
184 | if (self::driverName() === self::SQL_SERVER) { |
||
185 | self::exec('SET IDENTITY_INSERT [' . self::prefix(identifier: $table) . '] OFF'); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | public static function rollBack(): void |
||
190 | { |
||
191 | parent::connection()->rollBack(); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @internal |
||
196 | */ |
||
197 | public static function iLike(): string |
||
198 | { |
||
199 | if (self::driverName() === self::POSTGRES) { |
||
200 | return 'ILIKE'; |
||
201 | } |
||
202 | |||
203 | if (self::driverName() === self::SQL_SERVER) { |
||
204 | return 'COLLATE SQL_UTF8_General_CI_AI LIKE'; |
||
205 | } |
||
206 | |||
207 | return 'LIKE'; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @internal |
||
212 | */ |
||
213 | public static function groupConcat(string $column): string |
||
214 | { |
||
215 | switch (self::driverName()) { |
||
216 | case self::POSTGRES: |
||
217 | case self::SQL_SERVER: |
||
218 | return 'STRING_AGG(' . $column . ", ',')"; |
||
219 | |||
220 | case self::MYSQL: |
||
221 | case self::SQLITE: |
||
222 | default: |
||
223 | return 'GROUP_CONCAT(' . $column . ')'; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return Expression<string> |
||
229 | */ |
||
230 | public static function binaryColumn(string $column, string|null $alias = null): Expression |
||
231 | { |
||
232 | if (self::driverName() === self::MYSQL) { |
||
233 | $sql = 'CAST(' . $column . ' AS binary)'; |
||
234 | } else { |
||
235 | $sql = $column; |
||
236 | } |
||
237 | |||
238 | if ($alias !== null) { |
||
239 | $sql .= ' AS ' . $alias; |
||
240 | } |
||
241 | |||
242 | return new Expression($sql); |
||
243 | } |
||
244 | |||
245 | public static function regexOperator(): string |
||
246 | { |
||
247 | return self::REGEX_OPERATOR[self::driverName()]; |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * PHPSTAN can't detect the magic methods in the parent class. |
||
252 | */ |
||
253 | public static function query(): Builder |
||
254 | { |
||
255 | return parent::connection()->query(); |
||
256 | } |
||
257 | } |
||
258 |