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

brick / geo / 14072842519

26 Mar 2025 12:34AM UTC coverage: 62.112% (-0.02%) from 62.13%
14072842519

push

github

BenMorel
Implement PgsqlDriver (pgsql extension)

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

36 existing lines in 4 files now uncovered.

1882 of 3030 relevant lines covered (62.11%)

1695.42 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
    #[Override]
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
    #[Override]
97
    public function convertStringResult(mixed $value) : string
98
    {
NEW
99
        if (is_string($value)) {
×
NEW
100
            return $value;
×
101
        }
102

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

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

NEW
112
        if (is_int($value)) {
×
NEW
113
            return $value;
×
114
        }
115

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

119
    #[Override]
120
    public function convertFloatResult(mixed $value) : float
121
    {
NEW
122
        if (is_numeric($value)) {
×
NEW
123
            return (float) $value;
×
124
        }
125

NEW
126
        throw GeometryEngineException::unexpectedDatabaseReturnType('number or numeric string', $value);
×
127
    }
128

129
    #[Override]
130
    public function convertBoolResult(mixed $value) : bool
131
    {
NEW
132
        return match ($value) {
×
NEW
133
            't' => true,
×
NEW
134
            'f' => false,
×
NEW
135
            default => throw GeometryEngineException::unexpectedDatabaseReturnType('t or f', $value),
×
NEW
136
        };
×
137
    }
138
}
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