Passed
Push — dbal ( 23f922...9cad68 )
by Greg
15:01
created

DriverTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A quoteIdentifier() 0 5 1
A quoteValue() 0 3 1
A query() 0 34 6
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 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 Fisharebest\Webtrees\DB\Expression;
23
use PDO;
24
use PDOException;
25
use RuntimeException;
26
27
use function is_bool;
28
use function is_int;
29
30
/**
31
 * Common functionality for all drivers.
32
 */
33
trait DriverTrait
34
{
35
    protected const IDENTIFIER_OPEN_QUOTE  = '"';
36
    protected const IDENTIFIER_CLOSE_QUOTE = '"';
37
38
    /**
39
     * @param string $identifier
40
     *
41
     * @return Expression
42
     */
43
    public function quoteIdentifier(string $identifier): Expression
44
    {
45
        $escaped = strtr($identifier, [static::IDENTIFIER_CLOSE_QUOTE => static::IDENTIFIER_CLOSE_QUOTE . static::IDENTIFIER_CLOSE_QUOTE]);
0 ignored issues
show
Bug introduced by
The constant Fisharebest\Webtrees\DB\...:IDENTIFIER_CLOSE_QUOTE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
46
47
        return new Expression(static::IDENTIFIER_OPEN_QUOTE . $escaped . static::IDENTIFIER_CLOSE_QUOTE);
0 ignored issues
show
Bug introduced by
The constant Fisharebest\Webtrees\DB\...::IDENTIFIER_OPEN_QUOTE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
48
    }
49
50
    /**
51
     * For quoting strings in DDL statements which cannot use placeholders. e.g. COMMENT 'foo' and DEFAULT 'bar'.
52
     *
53
     * @param string $value
54
     *
55
     * @return Expression
56
     */
57
    public function quoteValue(string $value): Expression
58
    {
59
        return new Expression($this->pdo->quote($value));
60
    }
61
62
    /**
63
     * Prepare, bind and execute a select query.
64
     *
65
     * @param string                            $sql
66
     * @param array<bool|int|float|string|null> $bindings
67
     *
68
     * @return array<object>
69
     */
70
    public function query(string $sql, array $bindings = []): array
71
    {
72
        try {
73
            $statement = $this->pdo->prepare($sql);
74
        } catch (PDOException) {
75
            $statement = false;
76
        }
77
78
        if ($statement === false) {
79
            throw new RuntimeException('Failed to prepare statement: ' . $sql);
80
        }
81
82
        foreach ($bindings as $param => $value) {
83
            $type = match (true) {
84
                $value === null => PDO::PARAM_NULL,
85
                is_bool($value) => PDO::PARAM_BOOL,
86
                is_int($value)  => PDO::PARAM_INT,
87
                default         => PDO::PARAM_STR,
88
            };
89
90
            if (is_int($param)) {
91
                // Positional parameters are numeric, starting at 1.
92
                $statement->bindValue($param + 1, $value, $type);
93
            } else {
94
                // Named parameters are (optionally) prefixed with a colon.
95
                $statement->bindValue(':' . $param, $value, $type);
96
            }
97
        }
98
99
        if ($statement->execute()) {
100
            return $statement->fetchAll(PDO::FETCH_OBJ);
101
        }
102
103
        throw new RuntimeException('Failed to execute statement: ' . $sql);
104
    }
105
}
106