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

brick / geo / 14062212560

25 Mar 2025 02:26PM UTC coverage: 53.902% (+1.7%) from 52.154%
14062212560

push

github

BenMorel
Implement PgsqlDriver (pgsql extension)

0 of 52 new or added lines in 1 file covered. (0.0%)

110 existing lines in 8 files now uncovered.

1637 of 3037 relevant lines covered (53.9%)

409.17 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\Query\BinaryValue;
8
use Brick\Geo\Engine\Database\Query\ScalarValue;
9
use Brick\Geo\Engine\Database\Result\Row;
10
use Brick\Geo\Exception\GeometryEngineException;
11
use Override;
12
use PgSql\Connection;
13

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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