• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / geo / 14057076899

25 Mar 2025 10:14AM UTC coverage: 52.154% (-13.7%) from 65.828%
14057076899

push

github

BenMorel
Wip

0 of 383 new or added lines in 14 files covered. (0.0%)

141 existing lines in 12 files now uncovered.

1634 of 3133 relevant lines covered (52.15%)

396.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Engine/Database/Driver/PdoMysqlDriver.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Engine\Database\Driver;
6

7
use Brick\Geo\Engine\Database\DatabaseDriver;
8
use Brick\Geo\Engine\Database\Query\BinaryValue;
9
use Brick\Geo\Engine\Database\Query\ScalarValue;
10
use Brick\Geo\Engine\Database\Result\Row;
11
use Brick\Geo\Exception\GeometryEngineException;
12
use Override;
13
use PDO;
14
use PDOException;
15
use PDOStatement;
16

17
/**
18
 * Database driver using a pdo_mysql connection.
19
 */
20
final class PdoMysqlDriver implements DatabaseDriver
21
{
22
    public function __construct(
23
        private readonly PDO $pdo,
24
    ) {
NEW
25
    }
×
26

27
    #[Override]
28
    public function executeQuery(string|BinaryValue|ScalarValue ...$query) : Row
29
    {
NEW
30
        $queryString = '';
×
NEW
31
        $queryParams = [];
×
32

NEW
33
        foreach ($query as $queryPart) {
×
NEW
34
            if ($queryPart instanceof BinaryValue) {
×
NEW
35
                $queryString .= 'BINARY ?';
×
NEW
36
                $queryParams[] = [$queryPart->value, PDO::PARAM_LOB];
×
NEW
37
            } elseif ($queryPart instanceof ScalarValue) {
×
NEW
38
                $queryString .= '?';
×
39

NEW
40
                if (is_int($queryPart->value)) {
×
NEW
41
                    $queryParams[] = [$queryPart->value, PDO::PARAM_INT];
×
NEW
42
                } elseif (is_bool($queryPart->value)) {
×
NEW
43
                    $queryParams[] = [$queryPart->value, PDO::PARAM_BOOL];
×
44
                } else {
NEW
45
                    $queryParams[] = [$queryPart->value, PDO::PARAM_STR];
×
46
                }
47
            } else {
NEW
48
                $queryString .= $queryPart;
×
49
            }
50
        }
51

52
        /** @var int $errMode */
NEW
53
        $errMode = $this->pdo->getAttribute(PDO::ATTR_ERRMODE);
×
NEW
54
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
×
55

56
        try {
NEW
57
            $statement = $this->pdo->prepare($queryString);
×
58

NEW
59
            $position = 1;
×
60

NEW
61
            foreach ($queryParams as [$value, $type]) {
×
NEW
62
                $statement->bindValue($position++, $value, $type);
×
63
            }
64

NEW
65
            $statement->execute();
×
66

67
            /** @var list<list<mixed>> $result */
NEW
68
            $result = $statement->fetchAll(PDO::FETCH_NUM);
×
NEW
69
        } catch (PDOException $e) {
×
NEW
70
            throw GeometryEngineException::wrap($e);
×
71
        } finally {
NEW
72
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, $errMode);
×
73
        }
74

NEW
75
        if (count($result) !== 1) {
×
NEW
76
            throw new GeometryEngineException(sprintf('Expected exactly one row, got %d.', count($result)));
×
77
        }
78

NEW
79
        return new Row($this, $result[0]);
×
80
    }
81

82
    public function convertBinaryResult(mixed $value) : string
83
    {
NEW
84
        if (is_string($value)) {
×
NEW
85
            return $value;
×
86
        }
87

NEW
88
        throw GeometryEngineException::unexpectedDatabaseReturnType('string', $value);
×
89
    }
90

91
    public function convertStringResult(mixed $value) : string
92
    {
NEW
93
        if (is_string($value)) {
×
NEW
94
            return $value;
×
95
        }
96

NEW
97
        throw GeometryEngineException::unexpectedDatabaseReturnType('string', $value);
×
98
    }
99

100
    public function convertIntResult(mixed $value) : int
101
    {
102
        // TODO check that actually returned as int;
103
        //      maybe checks for all types sent & received for each driver?
104

NEW
105
        if (is_int($value)) {
×
NEW
106
            return $value;
×
107
        }
108

NEW
109
        throw GeometryEngineException::unexpectedDatabaseReturnType('int', $value);
×
110
    }
111

112
    public function convertFloatResult(mixed $value) : float
113
    {
NEW
114
        if (is_numeric($value)) {
×
NEW
115
            return (float) $value;
×
116
        }
117

NEW
118
        throw GeometryEngineException::unexpectedDatabaseReturnType('number or numeric string', $value);
×
119
    }
120

121
    public function convertBoolResult(mixed $value) : bool
122
    {
NEW
123
        return match ($value) {
×
NEW
124
            0 => false,
×
NEW
125
            1 => true,
×
NEW
126
            default => throw GeometryEngineException::unexpectedDatabaseReturnType('0 or 1', $value),
×
NEW
127
        };
×
128
    }
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc