• 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

93.33
/src/Engine/Database/Internal/AbstractDatabaseWkbEngine.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Engine\Database\Internal;
6

7
use Brick\Geo\Engine\Database\Driver\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 Brick\Geo\Geometry;
13
use Brick\Geo\Io\WkbReader;
14
use Brick\Geo\Io\WkbWriter;
15
use Brick\Geo\Point;
16
use Override;
17

18
/**
19
 * Base class for database engines with standard support for WKB, but no support for EWKB.
20
 *
21
 * @internal
22
 */
23
abstract readonly class AbstractDatabaseWkbEngine extends AbstractDatabaseEngine
24
{
25
    private WkbReader $wkbReader;
26
    private WkbWriter $wkbWriter;
27

28
    final public function __construct(
29
        protected DatabaseDriver $driver,
30
    ) {
31
        $this->wkbReader = new WkbReader();
×
32
        $this->wkbWriter = new WkbWriter();
×
33
    }
34

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

48
        if ($returnsGeometry) {
1,466✔
49
            $query[] = 'ST_AsBinary(g), ST_SRID(g) FROM (SELECT ';
603✔
50
        }
51

52
        $query[] = $function . '(';
1,466✔
53

54
        $first = true;
1,466✔
55
        foreach ($parameters as $parameter) {
1,466✔
56
            if ($first) {
1,466✔
57
                $first = false;
1,466✔
58
            } else {
59
                $query[] = ',';
872✔
60
            }
61

62
            if ($parameter instanceof Geometry) {
1,466✔
63
                if ($parameter instanceof Point && $parameter->isEmpty()) {
1,466✔
64
                    // WKB does not support empty points, and MySQL does not support them either.
UNCOV
65
                    throw new GeometryEngineException('MySQL does not support empty points');
8✔
66
                }
67
                $query[] = 'ST_GeomFromWKB(';
1,458✔
68
                $query[] = new BinaryValue($this->wkbWriter->write($parameter));
1,458✔
69
                $query[] = ',';
1,458✔
70
                $query[] = new ScalarValue($parameter->srid());
1,458✔
71
                $query[] = ')';
1,458✔
72
            } else {
73
                $query[] = new ScalarValue($parameter);
399✔
74
            }
75
        }
76

77
        $query[] = ')';
1,458✔
78

79
        if ($returnsGeometry) {
1,458✔
80
            $query[] = ' AS g) AS q';
595✔
81
        }
82

83
        return $this->driver->executeQuery(...$query);
1,458✔
84
    }
85

86
    /**
87
     * Queries a GIS function returning a boolean value.
88
     *
89
     * @param string          $function      The SQL GIS function to execute.
90
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
91
     *
92
     * @throws GeometryEngineException
93
     */
94
    #[Override]
95
    final protected function queryBool(string $function, Geometry|string|float|int|bool ...$parameters) : bool
96
    {
97
        return $this->query($function, $parameters, false)->get(0)->asBool();
755✔
98
    }
99

100
    /**
101
     * Queries a GIS function returning a floating point value.
102
     *
103
     * @param string          $function      The SQL GIS function to execute.
104
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
105
     *
106
     * @throws GeometryEngineException
107
     */
108
    #[Override]
109
    final protected function queryFloat(string $function, Geometry|string|float|int|bool ...$parameters) : float
110
    {
111
        return $this->query($function, $parameters, false)->get(0)->asFloat();
204✔
112
    }
113

114
    /**
115
     * Queries a GIS function returning a Geometry object.
116
     *
117
     * @param string             $function   The SQL GIS function to execute.
118
     * @param Geometry|scalar ...$parameters The Geometry objects or scalar values to pass as parameters.
119
     *
120
     * @throws GeometryEngineException
121
     */
122
    #[Override]
123
    final protected function queryGeometry(string $function, Geometry|string|float|int|bool ...$parameters) : Geometry
124
    {
125
        $row = $this->query($function, $parameters, true);
603✔
126

127
        $wkb = $row->get(0)->asBinary();
326✔
128
        $srid = $row->get(1)->asInt();
311✔
129

130
        return $this->wkbReader->read($wkb, $srid);
311✔
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