|
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\Drivers; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Driver\PDO\Connection; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
use PDOException; |
|
25
|
|
|
use RuntimeException; |
|
26
|
|
|
use SensitiveParameter; |
|
27
|
|
|
|
|
28
|
|
|
use function is_bool; |
|
29
|
|
|
use function is_int; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Common functionality for all drivers. |
|
33
|
|
|
*/ |
|
34
|
|
|
trait DriverTrait |
|
35
|
|
|
{ |
|
36
|
|
|
public function __construct(private readonly PDO $pdo) |
|
37
|
|
|
{ |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function connect( |
|
41
|
|
|
#[SensitiveParameter] |
|
|
|
|
|
|
42
|
|
|
array $params, |
|
43
|
|
|
): Connection { |
|
44
|
|
|
return new Connection($this->pdo); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Prepare, bind and execute a select query. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $sql |
|
51
|
|
|
* @param array<bool|int|float|string|null> $bindings |
|
52
|
|
|
* |
|
53
|
|
|
* @return array<object> |
|
54
|
|
|
*/ |
|
55
|
|
|
public function query(string $sql, array $bindings = []): array |
|
56
|
|
|
{ |
|
57
|
|
|
try { |
|
58
|
|
|
$statement = $this->pdo->prepare($sql); |
|
59
|
|
|
} catch (PDOException) { |
|
60
|
|
|
$statement = false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($statement === false) { |
|
64
|
|
|
throw new RuntimeException('Failed to prepare statement: ' . $sql); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
foreach ($bindings as $param => $value) { |
|
68
|
|
|
$type = match (true) { |
|
69
|
|
|
$value === null => PDO::PARAM_NULL, |
|
70
|
|
|
is_bool(value: $value) => PDO::PARAM_BOOL, |
|
71
|
|
|
is_int(value: $value) => PDO::PARAM_INT, |
|
72
|
|
|
default => PDO::PARAM_STR, |
|
73
|
|
|
}; |
|
74
|
|
|
|
|
75
|
|
|
if (is_int(value: $param)) { |
|
76
|
|
|
// Positional parameters are numeric, starting at 1. |
|
77
|
|
|
$statement->bindValue(param: $param + 1, value: $value, type: $type); |
|
78
|
|
|
} else { |
|
79
|
|
|
// Named parameters are (optionally) prefixed with a colon. |
|
80
|
|
|
$statement->bindValue(param: ':' . $param, value: $value, type: $type); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($statement->execute()) { |
|
85
|
|
|
return $statement->fetchAll(PDO::FETCH_OBJ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
throw new RuntimeException('Failed to execute statement: ' . $sql); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.