• 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/PdoPgsqlDriver.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

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

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

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

NEW
39
                if (is_int($queryPart->value)) {
×
NEW
40
                    $queryString .= '::int'; // PARAM_INT seems to have no effect on pdo_pgsql
×
NEW
41
                    $queryParams[] = [$queryPart->value, PDO::PARAM_INT];
×
NEW
42
                } elseif (is_float($queryPart->value)) {
×
NEW
43
                    $queryString .= '::float';
×
NEW
44
                    $queryParams[] = [$queryPart->value, PDO::PARAM_STR];
×
NEW
45
                } elseif (is_bool($queryPart->value)) {
×
NEW
46
                    $queryParams[] = [$queryPart->value, PDO::PARAM_BOOL];
×
47
                } else {
NEW
48
                    $queryParams[] = [$queryPart->value, PDO::PARAM_STR];
×
49
                }
50
            } else {
NEW
51
                $queryString .= $queryPart;
×
52
            }
53
        }
54

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

59
        try {
NEW
60
            $statement = $this->pdo->prepare($queryString);
×
61

NEW
62
            $position = 1;
×
63

NEW
64
            foreach ($queryParams as [$value, $type]) {
×
NEW
65
                $statement->bindValue($position++, $value, $type);
×
66
            }
67

NEW
68
            $statement->execute();
×
69

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

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

NEW
82
        return new Row($this, $result[0]);
×
83
    }
84

85
    public function convertBinaryResult(mixed $value) : string
86
    {
NEW
87
        if (is_resource($value)) {
×
NEW
88
            return stream_get_contents($value);
×
89
        }
90

NEW
91
        throw GeometryEngineException::unexpectedDatabaseReturnType('resource', $value);
×
92
    }
93

94
    public function convertStringResult(mixed $value) : string
95
    {
NEW
96
        if (is_string($value)) {
×
NEW
97
            return $value;
×
98
        }
99

NEW
100
        throw GeometryEngineException::unexpectedDatabaseReturnType('string', $value);
×
101
    }
102

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

NEW
108
        if (is_int($value)) {
×
NEW
109
            return $value;
×
110
        }
111

NEW
112
        throw GeometryEngineException::unexpectedDatabaseReturnType('int', $value);
×
113
    }
114

115
    public function convertFloatResult(mixed $value) : float
116
    {
NEW
117
        if (is_numeric($value)) {
×
NEW
118
            return (float) $value;
×
119
        }
120

NEW
121
        throw GeometryEngineException::unexpectedDatabaseReturnType('number or numeric string', $value);
×
122
    }
123

124
    public function convertBoolResult(mixed $value) : bool
125
    {
NEW
126
        if (is_bool($value)) {
×
NEW
127
            return $value;
×
128
        }
129

NEW
130
        throw GeometryEngineException::unexpectedDatabaseReturnType('bool', $value);
×
131
    }
132
}
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