• 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/MariadbEngine.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo\Engine;
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 Brick\Geo\Geometry;
13
use Brick\Geo\Io\WkbReader;
14
use Brick\Geo\Io\WkbWriter;
15
use Override;
16

17
/**
18
 * Database engine based on MariaDB.
19
 */
20
final readonly class MariadbEngine extends DatabaseEngine
21
{
22
    private WkbReader $wkbReader;
23
    private WkbWriter $wkbWriter;
24

25
    public function __construct(
26
        // TODO private
27
        public DatabaseDriver $driver,
28
        // TODO
29
        private bool $useProxy = true,
30
    ) {
NEW
31
        $this->wkbReader = new WkbReader();
×
NEW
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 list<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
    {
NEW
46
        $query = ['SELECT '];
×
47

NEW
48
        if ($returnsGeometry) {
×
NEW
49
            $query[] = 'ST_AsWKB(g), ST_SRID(g) FROM (SELECT ';
×
50
        }
51

NEW
52
        $query[] = $function . '(';
×
53

NEW
54
        foreach ($parameters as $key => $parameter) {
×
NEW
55
            if ($key !== 0) {
×
NEW
56
                $query[] = ',';
×
57
            }
58

NEW
59
            if ($parameter instanceof Geometry) {
×
NEW
60
                if ($parameter->isEmpty() && $parameter->geometryType() !== 'GeometryCollection') {
×
NEW
61
                    throw new GeometryEngineException('TODO'); // TODO Error message
×
62
                }
NEW
63
                $query[] = 'ST_GeomFromWKB(';
×
NEW
64
                $query[] = new BinaryValue($this->wkbWriter->write($parameter));
×
NEW
65
                $query[] = ',';
×
NEW
66
                $query[] = new ScalarValue($parameter->srid());
×
NEW
67
                $query[] = ')';
×
68
            } else {
NEW
69
                $query[] = new ScalarValue($parameter);
×
70
            }
71
        }
72

NEW
73
        $query[] = ')';
×
74

NEW
75
        if ($returnsGeometry) {
×
NEW
76
            $query[] = ' AS g) AS q';
×
77
        }
78

NEW
79
        return $this->driver->executeQuery(...$query);
×
80
    }
81

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

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

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

NEW
123
        $wkb = $row->get(0)->asBinary();
×
NEW
124
        $srid = $row->get(1)->asInt();
×
125

NEW
126
        return $this->wkbReader->read($wkb, $srid);
×
127
    }
128

129
//    /**
130
//     * @return class-string<Proxy\ProxyInterface&Geometry>
131
//     *
132
//     * @throws GeometryEngineException
133
//     */
134
//    private function getProxyClassName(string $geometryType) : string
135
//    {
136
//        $proxyClasses = [
137
//            'CIRCULARSTRING'     => Proxy\CircularStringProxy::class,
138
//            'COMPOUNDCURVE'      => Proxy\CompoundCurveProxy::class,
139
//            'CURVE'              => Proxy\CurveProxy::class,
140
//            'CURVEPOLYGON'       => Proxy\CurvePolygonProxy::class,
141
//            'GEOMCOLLECTION'     => Proxy\GeometryCollectionProxy::class, /* MySQL 8 - https://github.com/brick/geo/pull/33 */
142
//            'GEOMETRY'           => Proxy\GeometryProxy::class,
143
//            'GEOMETRYCOLLECTION' => Proxy\GeometryCollectionProxy::class,
144
//            'LINESTRING'         => Proxy\LineStringProxy::class,
145
//            'MULTICURVE'         => Proxy\MultiCurveProxy::class,
146
//            'MULTILINESTRING'    => Proxy\MultiLineStringProxy::class,
147
//            'MULTIPOINT'         => Proxy\MultiPointProxy::class,
148
//            'MULTIPOLYGON'       => Proxy\MultiPolygonProxy::class,
149
//            'MULTISURFACE'       => Proxy\MultiSurfaceProxy::class,
150
//            'POINT'              => Proxy\PointProxy::class,
151
//            'POLYGON'            => Proxy\PolygonProxy::class,
152
//            'POLYHEDRALSURFACE'  => Proxy\PolyhedralSurfaceProxy::class,
153
//            'SURFACE'            => Proxy\SurfaceProxy::class,
154
//            'TIN'                => Proxy\TinProxy::class,
155
//            'TRIANGLE'           => Proxy\TriangleProxy::class
156
//        ];
157
//
158
//        $geometryType = strtoupper($geometryType);
159
//        $geometryType = preg_replace('/^ST_/', '', $geometryType);
160
//        assert($geometryType !== null);
161
//        $geometryType = preg_replace('/ .*/', '', $geometryType);
162
//        assert($geometryType !== null);
163
//
164
//        if (! isset($proxyClasses[$geometryType])) {
165
//            throw new GeometryEngineException('Unknown geometry type: ' . $geometryType);
166
//        }
167
//
168
//        return $proxyClasses[$geometryType];
169
//    }
170
}
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