Passed
Push — dbal ( 2391b4...52c0e6 )
by Greg
08:07
created

AbstractDriver::escapeLike()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 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 PDO;
23
use PDOException;
24
use RuntimeException;
25
26
use function is_bool;
27
use function is_int;
28
29
/**
30
 * Common functionality for all drivers.
31
 */
32
abstract class AbstractDriver
33
{
34
    /**
35
     * @param PDO    $pdo
36
     * @param string $prefix
37
     */
38
    public function __construct(protected readonly PDO $pdo, protected readonly string $prefix)
39
    {
40
    }
41
42
    /**
43
     * Prepare, bind and execute a select query.
44
     *
45
     * @param string                            $sql
46
     * @param array<null|bool|int|float|string> $bindings
47
     *
48
     * @return array<object>
49
     */
50
    public function query(string $sql, array $bindings = []): array
51
    {
52
        try {
53
            $statement = $this->pdo->prepare($sql);
54
        } catch (PDOException) {
55
            $statement = false;
56
        }
57
58
        if ($statement === false) {
59
            throw new RuntimeException('Failed to prepare statement: ' . $sql);
60
        }
61
62
        foreach ($bindings as $param => $value) {
63
            $type = match (true) {
64
                $value === null => PDO::PARAM_NULL,
65
                is_bool($value) => PDO::PARAM_BOOL,
66
                is_int($value)  => PDO::PARAM_INT,
67
                default         => PDO::PARAM_STR,
68
            };
69
70
            if (is_int($param)) {
71
                // Positional parameters are numeric, starting at 1.
72
                $statement->bindValue($param + 1, $value, $type);
73
            } else {
74
                // Named parameters are (optionally) prefixed with a colon.
75
                $statement->bindValue(':' . $param, $value, $type);
76
            }
77
        }
78
79
        if ($statement->execute()) {
80
            return $statement->fetchAll(PDO::FETCH_OBJ);
81
        }
82
83
        throw new RuntimeException('Failed to execute statement: ' . $sql);
84
    }
85
86
    /**
87
     * @param string $string
88
     *
89
     * @return string
90
     */
91
    public function escapeLike(string $string): string
92
    {
93
        return strtr($string, [
94
            '%'  => '\\%',
95
            '_'  => '\\_',
96
            '\\' => '\\\\',
97
        ]);
98
    }
99
}
100