|
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 RuntimeException; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Database abstraction |
|
31
|
|
|
*/ |
|
32
|
|
|
class DB extends Manager |
|
33
|
|
|
{ |
|
34
|
|
|
// Supported drivers |
|
35
|
|
|
public const MYSQL = 'mysql'; |
|
36
|
|
|
public const POSTGRES = 'pgsql'; |
|
37
|
|
|
public const SQLITE = 'sqlite'; |
|
38
|
|
|
public const SQL_SERVER = 'sqlsrv'; |
|
39
|
|
|
|
|
40
|
|
|
private const COLLATION_ASCII = [ |
|
41
|
|
|
self::MYSQL => 'ascii_bin', |
|
42
|
|
|
self::POSTGRES => 'C', |
|
43
|
|
|
self::SQLITE => 'BINARY', |
|
44
|
|
|
self::SQL_SERVER => 'Latin1_General_Bin', |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
private const COLLATION_UTF8 = [ |
|
48
|
|
|
self::MYSQL => 'utf8mb4_unicode_ci', |
|
49
|
|
|
self::POSTGRES => 'und-x-icu', |
|
50
|
|
|
self::SQLITE => 'NOCASE', |
|
51
|
|
|
self::SQL_SERVER => 'utf8_CI_AI', |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
private const REGEX_OPERATOR = [ |
|
55
|
|
|
self::MYSQL => 'REGEXP', |
|
56
|
|
|
self::POSTGRES => '~', |
|
57
|
|
|
self::SQLITE => 'REGEXP', |
|
58
|
|
|
self::SQL_SERVER => 'REGEXP', |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
private const DRIVER_INITIALIZATION = [ |
|
62
|
|
|
self::MYSQL => "SET NAMES utf8mb4, sql_mode := 'ANSI,STRICT_ALL_TABLES', TIME_ZONE := '+00:00', SQL_BIG_SELECTS := 1, GROUP_CONCAT_MAX_LEN := 1048576", |
|
63
|
|
|
self::POSTGRES => '', |
|
64
|
|
|
self::SQLITE => 'PRAGMA foreign_keys = ON', |
|
65
|
|
|
self::SQL_SERVER => 'SET language us_english', // For timestamp columns |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
public static function driverName(): string |
|
69
|
|
|
{ |
|
70
|
|
|
return self::pdo()->getAttribute(PDO::ATTR_DRIVER_NAME); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return int|false |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function exec(string $sql) |
|
77
|
|
|
{ |
|
78
|
|
|
return self::pdo()->exec($sql); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public static function lastInsertId(): int |
|
82
|
|
|
{ |
|
83
|
|
|
$return = self::pdo()->lastInsertId(); |
|
84
|
|
|
|
|
85
|
|
|
if ($return === false) { |
|
86
|
|
|
throw new RuntimeException('Unable to retrieve last insert ID'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// All IDs are integers in our schema. |
|
90
|
|
|
return (int) $return; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private static function pdo(): PDO |
|
94
|
|
|
{ |
|
95
|
|
|
return parent::connection()->getPdo(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static function prefix(string $identifier): string |
|
99
|
|
|
{ |
|
100
|
|
|
return parent::connection()->getTablePrefix() . $identifier; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* SQL-Server needs to be told that we are going to insert into an identity column. |
|
105
|
|
|
* |
|
106
|
|
|
* @param Closure(): void $callback |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function identityInsert(string $table, Closure $callback): void |
|
109
|
|
|
{ |
|
110
|
|
|
if (self::driverName() === self::SQL_SERVER) { |
|
111
|
|
|
self::exec('SET IDENTITY_INSERT [' . self::prefix($table) . '] ON'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$callback(); |
|
115
|
|
|
|
|
116
|
|
|
if (self::driverName() === self::SQL_SERVER) { |
|
117
|
|
|
self::exec('SET IDENTITY_INSERT [' . self::prefix($table) . '] OFF'); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public static function rollBack(): void |
|
122
|
|
|
{ |
|
123
|
|
|
parent::connection()->rollBack(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @internal |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function iLike(): string |
|
130
|
|
|
{ |
|
131
|
|
|
if (self::driverName() === self::POSTGRES) { |
|
132
|
|
|
return 'ILIKE'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (self::driverName() === self::SQL_SERVER) { |
|
136
|
|
|
return 'COLLATE SQL_UTF8_General_CI_AI LIKE'; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return 'LIKE'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @internal |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function groupConcat(string $column): string |
|
146
|
|
|
{ |
|
147
|
|
|
switch (self::driverName()) { |
|
148
|
|
|
case self::POSTGRES: |
|
149
|
|
|
case self::SQL_SERVER: |
|
150
|
|
|
return 'STRING_AGG(' . $column . ", ',')"; |
|
151
|
|
|
|
|
152
|
|
|
case self::MYSQL: |
|
153
|
|
|
case self::SQLITE: |
|
154
|
|
|
default: |
|
155
|
|
|
return 'GROUP_CONCAT(' . $column . ')'; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return Expression |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function binaryColumn(string $column, ?string $alias = null): Expression |
|
163
|
|
|
{ |
|
164
|
|
|
if (self::driverName() === self::MYSQL) { |
|
165
|
|
|
$sql = 'CAST(' . $column . ' AS binary)'; |
|
166
|
|
|
} else { |
|
167
|
|
|
$sql = $column; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($alias !== null) { |
|
171
|
|
|
$sql .= ' AS ' . $alias; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return new Expression($sql); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public static function regexOperator(): string |
|
178
|
|
|
{ |
|
179
|
|
|
return self::REGEX_OPERATOR[self::driverName()]; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* PHPSTAN can't detect the magic methods in the parent class. |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function query(): Builder |
|
186
|
|
|
{ |
|
187
|
|
|
return parent::connection()->query(); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|