• 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/PgsqlDriver.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 PgSql\Connection;
14

15
/**
16
 * Database driver using the pgsql extension for PostgreSQL.
17
 *
18
 * TODO error handling!
19
 */
20
final class PgsqlDriver implements DatabaseDriver
21
{
22
    public function __construct(
23
        private readonly Connection $connection,
24
    ) {
NEW
25
    }
×
26

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

NEW
32
        $queryString = '';
×
NEW
33
        $queryParams = [];
×
34

NEW
35
        foreach ($query as $queryPart) {
×
NEW
36
            if ($queryPart instanceof BinaryValue) {
×
NEW
37
                $queryString .= '$' . $position++ . '::bytea';
×
NEW
38
                $queryParams[] = pg_escape_bytea($this->connection, $queryPart->value);
×
NEW
39
            } elseif ($queryPart instanceof ScalarValue) {
×
NEW
40
                $queryString .= '$' . $position++;
×
41

NEW
42
                if (is_int($queryPart->value)) {
×
NEW
43
                    $queryString .= '::int';
×
NEW
44
                    $queryParams[] = $queryPart->value;
×
NEW
45
                } elseif (is_float($queryPart->value)) {
×
NEW
46
                    $queryString .= '::float';
×
NEW
47
                    $queryParams[] = $queryPart->value;
×
NEW
48
                } elseif (is_bool($queryPart->value)) {
×
NEW
49
                    $queryString .= '::bool';
×
NEW
50
                    $queryParams[] = $queryPart->value ? 't' : 'f';
×
51
                } else {
NEW
52
                    $queryParams[] = $queryPart->value;
×
53
                }
54
            } else {
NEW
55
                $queryString .= $queryPart;
×
56
            }
57
        }
58

NEW
59
        $value = pg_prepare($this->connection, '', $queryString);
×
60

NEW
61
        if ($value === false) {
×
NEW
62
            die(pg_last_error());
×
63
        }
64

NEW
65
        $result = pg_execute($this->connection, '', $queryParams);
×
66

NEW
67
        if ($result === false) {
×
NEW
68
            var_dump($queryParams);
×
NEW
69
            var_dump(pg_last_error($this->connection));
×
NEW
70
            die;
×
71
        }
72

73
        /** @var list<list<mixed>> $rows */
NEW
74
        $rows = pg_fetch_all($result, PGSQL_NUM);
×
75

NEW
76
        if ($rows === false) {
×
NEW
77
            die(pg_last_error());
×
78
        }
79

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

NEW
84
        return new Row($this, $rows[0]);
×
85
    }
86

87
    public function convertBinaryResult(mixed $value) : string
88
    {
NEW
89
        if (is_string($value)) {
×
NEW
90
            return pg_unescape_bytea($value);
×
91
        }
92

NEW
93
        throw GeometryEngineException::unexpectedDatabaseReturnType('string', $value);
×
94
    }
95

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

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

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

NEW
110
        if (is_int($value)) {
×
NEW
111
            return $value;
×
112
        }
113

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

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

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

126
    public function convertBoolResult(mixed $value) : bool
127
    {
NEW
128
        return match ($value) {
×
NEW
129
            't' => true,
×
NEW
130
            'f' => false,
×
NEW
131
            default => throw GeometryEngineException::unexpectedDatabaseReturnType('t or f', $value),
×
NEW
132
        };
×
133
    }
134
}
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