Skip to content

Commit

Permalink
Add MariaDB support
Browse files Browse the repository at this point in the history
  • Loading branch information
tontonsb committed May 26, 2024
1 parent d41b8c6 commit e12f831
Show file tree
Hide file tree
Showing 18 changed files with 506 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changelog
- PHP 8.2 support
- MySQL support
- MariaDB support

## [0.1.0] - 2022-07-22
### Added
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<directory suffix="Test.php">./tests/Geometry</directory>
<directory suffix="Test.php">./tests/Feature</directory>
<directory suffix="Test.php">./tests/Constructors</directory>
<directory suffix="Test.php">./tests/MariaDB</directory>
<directory suffix="Test.php">./tests/MySQL</directory>
<directory suffix="Test.php">./tests/PostGIS</directory>
<directory suffix="Test.php">./tests/SpatiaLite</directory>
Expand All @@ -27,6 +28,9 @@
<testsuite name="Contructor">
<directory suffix="Test.php">./tests/Constructors</directory>
</testsuite>
<testsuite name="MariaDB">
<directory suffix="Test.php">./tests/MariaDB</directory>
</testsuite>
<testsuite name="MySQL">
<directory suffix="Test.php">./tests/MySQL</directory>
</testsuite>
Expand Down
13 changes: 13 additions & 0 deletions src/MariaDB/Curve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\Curve as OGCCurve;

/**
* Curve model with MariaDB-specific functions.
*/
class Curve extends Geometry implements OGCCurve
{
use \GlaivePro\SF\OGC\Traits\Curve;
}
53 changes: 53 additions & 0 deletions src/MariaDB/Geometry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Contracts\Geometry as GeometryInterface;
use GlaivePro\SF\OGC\Geometry as OGCGeometry;

/**
* Geometry model with MariaDB-specific functions.
*/
class Geometry extends OGCGeometry
{
protected const sfc = Sfc::class;

/**
* There is no native SetSRID in MariaDB, but it's important enough to support it.
*/
public function setSRID(int $srid): static
{
return static::fromMethod(
'ST_GeomFromWKB',
static::fromMethod('ST_AsBinary', $this),
$srid,
);
}

public function coordinateDimension(): Expression // Integer-valued expression
{
throw new MethodNotImplemented;
}

public function is3D(): Expression // Boolean-valued expression
{
throw new MethodNotImplemented;
}

public function isMeasured(): Expression // Boolean-valued expression
{
throw new MethodNotImplemented;
}

public function locateAlong(float $mValue): GeometryInterface
{
throw new MethodNotImplemented;
}

public function locateBetween(float $mStart, float $mEnd): GeometryInterface
{
throw new MethodNotImplemented;
}
}
13 changes: 13 additions & 0 deletions src/MariaDB/GeometryCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\GeometryCollection as OGCGeometryCollection;

/**
* GeometryCollection model with MariaDB-specific functions.
*/
class GeometryCollection extends Geometry implements OGCGeometryCollection
{
use \GlaivePro\SF\OGC\Traits\GeometryCollection;
}
13 changes: 13 additions & 0 deletions src/MariaDB/LineString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\LineString as OGCLineString;

/**
* LineString model with MariaDB-specific functions.
*/
class LineString extends Curve implements OGCLineString
{
use \GlaivePro\SF\OGC\Traits\LineString;
}
13 changes: 13 additions & 0 deletions src/MariaDB/MultiCurve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\MultiCurve as OGCMultiCurve;

/**
* MultiCurve model with MariaDB-specific functions.
*/
class MultiCurve extends GeometryCollection implements OGCMultiCurve
{
use \GlaivePro\SF\OGC\Traits\CurveBasic;
}
10 changes: 10 additions & 0 deletions src/MariaDB/MultiLineString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\MultiLineString as OGCMultiLineString;

/**
* MultiLineString model with MariaDB-specific functions.
*/
class MultiLineString extends MultiCurve implements OGCMultiLineString {}
10 changes: 10 additions & 0 deletions src/MariaDB/MultiPoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\MultiPoint as OGCMultiPoint;

/**
* MultiPoint model with MariaDB-specific functions.
*/
class MultiPoint extends GeometryCollection implements OGCMultiPoint {}
10 changes: 10 additions & 0 deletions src/MariaDB/MultiPolygon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\MultiPolygon as OGCMultiPolygon;

/**
* MultiPolygon model with MariaDB-specific functions.
*/
class MultiPolygon extends MultiSurface implements OGCMultiPolygon {}
13 changes: 13 additions & 0 deletions src/MariaDB/MultiSurface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\MultiSurface as OGCMultiSurface;

/**
* MultiSurface model with MariaDB-specific functions.
*/
class MultiSurface extends GeometryCollection implements OGCMultiSurface
{
use \GlaivePro\SF\OGC\Traits\SurfaceBasic;
}
25 changes: 25 additions & 0 deletions src/MariaDB/Point.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Contracts\Point as OGCPoint;

/**
* Point model with MariaDB-specific functions.
*/
class Point extends Geometry implements OGCPoint
{
use \GlaivePro\SF\OGC\Traits\Point;

public function Z(): Expression
{
throw new MethodNotImplemented;
}

public function M(): Expression
{
throw new MethodNotImplemented();
}
}
19 changes: 19 additions & 0 deletions src/MariaDB/Polygon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\Expression;
use GlaivePro\SF\OGC\Contracts\Polygon as OGCPolygon;

/**
* Polygon model with MariaDB-specific functions.
*/
class Polygon extends Surface implements OGCPolygon
{
use \GlaivePro\SF\OGC\Traits\Polygon;

public function numInteriorRing(): Expression // Integer-valued expression
{
return $this->wrap('ST_NumInteriorRings');
}
}
83 changes: 83 additions & 0 deletions src/MariaDB/Sfc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\Exceptions\MethodNotImplemented;
use GlaivePro\SF\OGC\Sfc as OGCSfc;

/**
* Implements constructors available in MariaDB.
*/
class Sfc extends OGCSfc
{
/**
* Calls `fromMethod` on the desired class.
*
* We have to specify PostGIS-specific classes here.
*/
protected static function callFromMethod(string $method, array $args): Geometry
{
$class = match ($method) {
'geometry' => Geometry::class,
'geometryCollection' => GeometryCollection::class,
'lineString' => LineString::class,
'multiCurve' => MultiCurve::class,
'multiLineString' => MultiLineString::class,
'multiPoint' => MultiPoint::class,
'multiPolygon' => MultiPolygon::class,
'point' => Point::class,
'polygon' => Polygon::class,
};

return $class::fromMethod(...$args);
}

/**
* Create a Point.
*
* Unlike POINT in MariaDB, this also accepts POINT(x, y, srid)
* and will set the SRID on the created POINT in such case.
*/
public static function point(float $x, float $y, int $srid = null): Point
{
$point = Point::fromMethod('Point', $x, $y);

if (\is_null($srid))
return $point;

return $point->setSRID($srid);
}

/**
* Not implemented in MariaDB.
*/
public static function bdPolyFromWKB(string $WKBMultiLineString, int $SRID = null): Polygon
{
throw new MethodNotImplemented;
}

/**
* Not implemented in MariaDB.
*/
public static function bdPolyFromText(string $multiLineStringTaggedText, int $SRID = null): Polygon
{
throw new MethodNotImplemented;
}

/**
* Not implemented in MariaDB.
*/
public static function bdMPolyFromWKB(string $WKBMultiLineString, int $SRID = null): MultiPolygon
{
throw new MethodNotImplemented;
}

/**
* Not implemented in MariaDB.
*/
public static function bdMPolyFromText(string $multiLineStringTaggedText, int $SRID = null): MultiPolygon
{
throw new MethodNotImplemented;
}

}
13 changes: 13 additions & 0 deletions src/MariaDB/Surface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace GlaivePro\SF\MariaDB;

use GlaivePro\SF\OGC\Contracts\Surface as OGCSurface;

/**
* Surface model with MariaDB-specific functions.
*/
class Surface extends Geometry implements OGCSurface
{
use \GlaivePro\SF\OGC\Traits\Surface;
}
26 changes: 26 additions & 0 deletions tests/MariaDB/ConstructorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace GlaivePro\SF\Tests\MariaDB;

use PHPUnit\Framework\TestCase;
use GlaivePro\SF\MariaDB\Sfc;

class ConstructorTest extends TestCase
{
public function testPointConstructors(): void
{
$point = Sfc::point(1, 3);
$this->assertSame(
'Point(?, ?)',
(string) $point,
);
$this->assertEquals([1, 3], $point->bindings);

$pointWithSrid = Sfc::point(1, 3, 4326);
$this->assertSame(
'ST_GeomFromWKB(ST_AsBinary(Point(?, ?)), ?)',
(string) $pointWithSrid,
);
$this->assertEquals([1, 3, 4326], $pointWithSrid->bindings);
}
}
Loading

0 comments on commit e12f831

Please sign in to comment.