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

brick / geo / 14062713921

25 Mar 2025 02:49PM UTC coverage: 62.13% (+0.2%) from 61.936%
14062713921

push

github

BenMorel
Implement PgsqlDriver (pgsql extension)

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

59 existing lines in 8 files now uncovered.

1890 of 3042 relevant lines covered (62.13%)

1687.76 hits per line

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

90.91
/src/Engine/PostgisEngine.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Engine;
6

7
use Brick\Geo\Engine\Database\Driver\DatabaseDriver;
8
use Brick\Geo\Engine\Database\Internal\AbstractDatabaseEngine;
9
use Brick\Geo\Engine\Database\Query\BinaryValue;
10
use Brick\Geo\Engine\Database\Query\ScalarValue;
11
use Brick\Geo\Engine\Database\Result\Row;
12
use Brick\Geo\Exception\GeometryEngineException;
13
use Brick\Geo\Geometry;
14
use Brick\Geo\Io\EwkbReader;
15
use Brick\Geo\Io\EwkbWriter;
16
use Override;
17

18
/**
19
 * Database engine based on PostgreSQL with the PostGIS extension.
20
 */
21
final readonly class PostgisEngine extends AbstractDatabaseEngine
22
{
23
    private EwkbReader $ewkbReader;
24
    private EwkbWriter $ewkbWriter;
25

26
    public function __construct(
27
        private DatabaseDriver $driver,
28
    ) {
29
        $this->ewkbReader = new EwkbReader();
×
UNCOV
30
        $this->ewkbWriter = new EwkbWriter();
×
31
    }
32

33
    /**
34
     * Builds and executes a SQL query for a GIS function.
35
     *
36
     * @param string              $function        The SQL GIS function to execute.
37
     * @param (Geometry|scalar)[] $parameters      The Geometry objects or scalar values to pass as parameters.
38
     * @param bool                $returnsGeometry Whether the GIS function returns a Geometry.
39
     *
40
     * @throws GeometryEngineException
41
     */
42
    private function query(string $function, array $parameters, bool $returnsGeometry) : Row
43
    {
UNCOV
44
        $query = ['SELECT '];
300✔
45

46
        if ($returnsGeometry) {
300✔
UNCOV
47
            $query[] = 'ST_AsEWKB(';
123✔
48
        }
49

UNCOV
50
        $query[] = $function . '(';
300✔
51

52
        foreach ($parameters as $key => $parameter) {
300✔
53
            if ($key !== 0) {
300✔
UNCOV
54
                $query[] = ',';
177✔
55
            }
56

57
            if ($parameter instanceof Geometry) {
300✔
58
                $query[] = 'ST_GeomFromEWKB(';
300✔
59
                $query[] = new BinaryValue($this->ewkbWriter->write($parameter));
300✔
UNCOV
60
                $query[] = ')';
300✔
61
            } else {
UNCOV
62
                $query[] = new ScalarValue($parameter);
80✔
63
            }
64
        }
65

UNCOV
66
        $query[] = ')';
300✔
67

68
        if ($returnsGeometry) {
300✔
UNCOV
69
            $query[] = ')';
123✔
70
        }
71

UNCOV
72
        return $this->driver->executeQuery(...$query);
300✔
73
    }
74

75
    /**
76
     * Queries a GIS function returning a boolean value.
77
     *
78
     * @param string          $function      The SQL GIS function to execute.
79
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
80
     *
81
     * @throws GeometryEngineException
82
     */
83
    #[Override]
84
    public function queryBool(string $function, Geometry|string|float|int|bool ...$parameters) : bool
85
    {
UNCOV
86
        return $this->query($function, $parameters, false)->get(0)->asBool();
161✔
87
    }
88

89
    /**
90
     * Queries a GIS function returning a floating point value.
91
     *
92
     * @param string          $function      The SQL GIS function to execute.
93
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
94
     *
95
     * @throws GeometryEngineException
96
     */
97
    #[Override]
98
    public function queryFloat(string $function, Geometry|string|float|int|bool ...$parameters) : float
99
    {
UNCOV
100
        return $this->query($function, $parameters, false)->get(0)->asFloat();
41✔
101
    }
102

103
    /**
104
     * Queries a GIS function returning a Geometry object.
105
     *
106
     * @param string             $function   The SQL GIS function to execute.
107
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
108
     *
109
     * @throws GeometryEngineException
110
     */
111
    #[Override]
112
    public function queryGeometry(string $function, Geometry|string|float|int|bool ...$parameters) : Geometry
113
    {
UNCOV
114
        $ewkb = $this->query($function, $parameters, true)->get(0)->asBinary();
123✔
115

UNCOV
116
        return $this->ewkbReader->read($ewkb);
123✔
117
    }
118
}
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