Skip to content

Commit 1fc63eb

Browse files
committed
Added support to MySQL. Updated tests.
1 parent b196d3e commit 1fc63eb

File tree

7 files changed

+56
-40
lines changed

7 files changed

+56
-40
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "astrogin/laravel-mysql-spatial",
2+
"name": "grimzy/laravel-mysql-spatial",
33
"description": "MySQL spatial data types extension for Laravel.",
44
"type": "library",
55
"license": "MIT",
66
"authors": [
77
{
8-
"name": "Andrey",
9-
"email": "astrogin24@gmail.com"
8+
"name": "Joseph Estefane",
9+
"email": "estefanejoe@gmail.com"
1010
}
1111
],
1212
"require": {
@@ -35,7 +35,7 @@
3535
},
3636
"extra": {
3737
"branch-alias": {
38-
"dev-master": "1.0.x-dev"
38+
"dev-master": "1.0.x-mysql-8-dev"
3939
}
4040
}
4141
}

src/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function update(array $values)
2020

2121
protected function asWKT(GeometryInterface $geometry)
2222
{
23-
return $this->getQuery()->raw("GeomFromText('".$geometry->toWKT()."')");
23+
return $this->getQuery()->raw("ST_GeomFromText('".$geometry->toWKT()."')");
2424
}
2525
}

src/Eloquent/SpatialTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,21 @@ public function getSpatialFields()
7474

7575
public function scopeDistance($query, $distance, $geometry, $column_name, $exclude_self = false)
7676
{
77-
$query->whereRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
77+
$query->whereRaw("st_distance(`{$column_name}`, ST_GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
7878

7979
if ($exclude_self) {
80-
$query->whereRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) != 0");
80+
$query->whereRaw("st_distance(`{$column_name}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0");
8181
}
8282

8383
return $query;
8484
}
8585

8686
public function scopeDistanceSphere($query, $distance, $geometry, $column_name, $exclude_self = false)
8787
{
88-
$query->whereRaw("st_distance_sphere(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
88+
$query->whereRaw("st_distance_sphere(`{$column_name}`, ST_GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
8989

9090
if ($exclude_self) {
91-
$query->whereRaw("st_distance_sphere(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) != 0");
91+
$query->whereRaw("st_distance_sphere(`{$column_name}`, ST_GeomFromText('{$geometry->toWkt()}')) != 0");
9292
}
9393

9494
return $query;
@@ -101,7 +101,7 @@ public function scopeDistanceValue($query, $geometry, $column_name)
101101
if (! $columns) {
102102
$query->select('*');
103103
}
104-
$query->selectRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) as distance");
104+
$query->selectRaw("st_distance(`{$column_name}`, ST_GeomFromText('{$geometry->toWkt()}')) as distance");
105105
}
106106

107107
public function scopeDistanceSphereValue($query, $geometry, $column_name)
@@ -111,11 +111,11 @@ public function scopeDistanceSphereValue($query, $geometry, $column_name)
111111
if (! $columns) {
112112
$query->select('*');
113113
}
114-
$query->selectRaw("st_distance_sphere(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) as distance");
114+
$query->selectRaw("st_distance_sphere(`{$column_name}`, ST_GeomFromText('{$geometry->toWkt()}')) as distance");
115115
}
116116

117117
public function scopeBounding($query, Geometry $bounds, $column_name)
118118
{
119-
return $query->whereRaw("st_intersects(GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
119+
return $query->whereRaw("st_intersects(ST_GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
120120
}
121121
}

start_db-5.7.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
docker run -d --name spatial-mysql-7 \
4+
-p 3306:3306 \
5+
-v $(pwd)/db:/var/lib/mysql \
6+
-e MYSQL_DATABASE=test \
7+
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
8+
mysql:latest

start_db-8.0.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
docker run -d --name spatial-mysql-8 \
4+
-p 3306:3306 \
5+
-v $(pwd)/db:/var/lib/mysql \
6+
-e MYSQL_DATABASE=test \
7+
-e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
8+
mysql:8.0 --character-set-server=utf8 --collation-server=utf8_general_ci

tests/Unit/Eloquent/BuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function testUpdatePoint()
3636
{
3737
$this->queryBuilder
3838
->shouldReceive('raw')
39-
->with("GeomFromText('POINT(2 1)')")
39+
->with("ST_GeomFromText('POINT(2 1)')")
4040
->once();
4141

4242
$this->queryBuilder
@@ -50,7 +50,7 @@ public function testUpdateLinestring()
5050
{
5151
$this->queryBuilder
5252
->shouldReceive('raw')
53-
->with("GeomFromText('LINESTRING(0 0,1 1,2 2)')")
53+
->with("ST_GeomFromText('LINESTRING(0 0,1 1,2 2)')")
5454
->once();
5555

5656
$this->queryBuilder
@@ -66,7 +66,7 @@ public function testUpdatePolygon()
6666
{
6767
$this->queryBuilder
6868
->shouldReceive('raw')
69-
->with("GeomFromText('POLYGON((0 0,1 0),(1 0,1 1),(1 1,0 0))')")
69+
->with("ST_GeomFromText('POLYGON((0 0,1 0),(1 0,1 1),(1 1,0 0))')")
7070
->once();
7171

7272
$this->queryBuilder

tests/Unit/Eloquent/SpatialTraitTest.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function testInsertUpdatePointHasCorrectSql()
3636
$this->model->save();
3737

3838
$this->assertStringStartsWith('insert', $this->queries[0]);
39-
$this->assertContains("GeomFromText('POINT(2 1)')", $this->queries[0]);
39+
$this->assertContains("ST_GeomFromText('POINT(2 1)')", $this->queries[0]);
4040
$this->assertTrue($this->model->exists);
4141

4242
$this->model->point = new Point(1, 2);
4343
$this->model->save();
4444

4545
$this->assertStringStartsWith('update', $this->queries[1]);
46-
$this->assertContains("GeomFromText('POINT(2 1)')", $this->queries[1]);
46+
$this->assertContains("ST_GeomFromText('POINT(2 1)')", $this->queries[1]);
4747
}
4848

4949
public function testInsertUpdateLineStringHasCorrectSql()
@@ -57,14 +57,14 @@ public function testInsertUpdateLineStringHasCorrectSql()
5757
$this->model->save();
5858

5959
$this->assertStringStartsWith('insert', $this->queries[0]);
60-
$this->assertContains("GeomFromText('LINESTRING(2 1,3 2)')", $this->queries[0]);
60+
$this->assertContains("ST_GeomFromText('LINESTRING(2 1,3 2)')", $this->queries[0]);
6161
$this->assertTrue($this->model->exists);
6262

6363
$this->model->linestring = new \Grimzy\LaravelMysqlSpatial\Types\LineString([$point1, $point2]);
6464
$this->model->save();
6565

6666
$this->assertStringStartsWith('update', $this->queries[1]);
67-
$this->assertContains("GeomFromText('LINESTRING(2 1,3 2)')", $this->queries[1]);
67+
$this->assertContains("ST_GeomFromText('LINESTRING(2 1,3 2)')", $this->queries[1]);
6868
}
6969

7070
public function testInsertUpdatePolygonHasCorrectSql()
@@ -82,13 +82,13 @@ public function testInsertUpdatePolygonHasCorrectSql()
8282
$this->model->save();
8383

8484
$this->assertStringStartsWith('insert', $this->queries[0]);
85-
$this->assertContains("GeomFromText('POLYGON((2 1,3 2),(2 3,1 2))')", $this->queries[0]);
85+
$this->assertContains("ST_GeomFromText('POLYGON((2 1,3 2),(2 3,1 2))')", $this->queries[0]);
8686
$this->assertTrue($this->model->exists);
8787

8888
$this->model->polygon = new \Grimzy\LaravelMysqlSpatial\Types\Polygon([$linestring1, $linestring2]);
8989
$this->model->save();
9090
$this->assertStringStartsWith('update', $this->queries[1]);
91-
$this->assertContains("GeomFromText('POLYGON((2 1,3 2),(2 3,1 2))')", $this->queries[1]);
91+
$this->assertContains("ST_GeomFromText('POLYGON((2 1,3 2),(2 3,1 2))')", $this->queries[1]);
9292
}
9393

9494
public function testInsertUpdateMultiPointHasCorrectSql()
@@ -102,14 +102,14 @@ public function testInsertUpdateMultiPointHasCorrectSql()
102102
$this->model->save();
103103

104104
$this->assertStringStartsWith('insert', $this->queries[0]);
105-
$this->assertContains("GeomFromText('MULTIPOINT((2 1),(3 2))')", $this->queries[0]);
105+
$this->assertContains("ST_GeomFromText('MULTIPOINT((2 1),(3 2))')", $this->queries[0]);
106106
$this->assertTrue($this->model->exists);
107107

108108
$this->model->multipoint = new \Grimzy\LaravelMysqlSpatial\Types\MultiPoint([$point1, $point2]);
109109
$this->model->save();
110110

111111
$this->assertStringStartsWith('update', $this->queries[1]);
112-
$this->assertContains("GeomFromText('MULTIPOINT((2 1),(3 2))')", $this->queries[1]);
112+
$this->assertContains("ST_GeomFromText('MULTIPOINT((2 1),(3 2))')", $this->queries[1]);
113113
}
114114

115115
public function testInsertUpdateMultiLineStringHasCorrectSql()
@@ -127,13 +127,13 @@ public function testInsertUpdateMultiLineStringHasCorrectSql()
127127
$this->model->save();
128128

129129
$this->assertStringStartsWith('insert', $this->queries[0]);
130-
$this->assertContains("GeomFromText('MULTILINESTRING((2 1,3 2),(2 3,1 2))')", $this->queries[0]);
130+
$this->assertContains("ST_GeomFromText('MULTILINESTRING((2 1,3 2),(2 3,1 2))')", $this->queries[0]);
131131
$this->assertTrue($this->model->exists);
132132

133133
$this->model->multilinestring = new \Grimzy\LaravelMysqlSpatial\Types\MultiLineString([$linestring1, $linestring2]);
134134
$this->model->save();
135135
$this->assertStringStartsWith('update', $this->queries[1]);
136-
$this->assertContains("GeomFromText('MULTILINESTRING((2 1,3 2),(2 3,1 2))')", $this->queries[1]);
136+
$this->assertContains("ST_GeomFromText('MULTILINESTRING((2 1,3 2),(2 3,1 2))')", $this->queries[1]);
137137
}
138138

139139
public function testInsertUpdateMultiPolygonHasCorrectSql()
@@ -160,13 +160,13 @@ public function testInsertUpdateMultiPolygonHasCorrectSql()
160160
$this->model->save();
161161

162162
$this->assertStringStartsWith('insert', $this->queries[0]);
163-
$this->assertContains("GeomFromText('MULTIPOLYGON(((2 1,3 2),(2 3,1 2)),((5 4,6 5),(5 6,4 5)))')", $this->queries[0]);
163+
$this->assertContains("ST_GeomFromText('MULTIPOLYGON(((2 1,3 2),(2 3,1 2)),((5 4,6 5),(5 6,4 5)))')", $this->queries[0]);
164164
$this->assertTrue($this->model->exists);
165165

166166
$this->model->multipolygon = new \Grimzy\LaravelMysqlSpatial\Types\MultiPolygon([$polygon1, $polygon2]);
167167
$this->model->save();
168168
$this->assertStringStartsWith('update', $this->queries[1]);
169-
$this->assertContains("GeomFromText('MULTIPOLYGON(((2 1,3 2),(2 3,1 2)),((5 4,6 5),(5 6,4 5)))')", $this->queries[1]);
169+
$this->assertContains("ST_GeomFromText('MULTIPOLYGON(((2 1,3 2),(2 3,1 2)),((5 4,6 5),(5 6,4 5)))')", $this->queries[1]);
170170
}
171171

172172
public function testInsertUpdateGeometryCollectionHasCorrectSql()
@@ -182,13 +182,13 @@ public function testInsertUpdateGeometryCollectionHasCorrectSql()
182182
$this->model->save();
183183

184184
$this->assertStringStartsWith('insert', $this->queries[0]);
185-
$this->assertContains("GeomFromText('GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(3 2,3 3))')", $this->queries[0]);
185+
$this->assertContains("ST_GeomFromText('GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(3 2,3 3))')", $this->queries[0]);
186186
$this->assertTrue($this->model->exists);
187187

188188
$this->model->geometrycollection = new \Grimzy\LaravelMysqlSpatial\Types\GeometryCollection([$point1, $linestring1]);
189189
$this->model->save();
190190
$this->assertStringStartsWith('update', $this->queries[1]);
191-
$this->assertContains("GeomFromText('GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(3 2,3 3))')", $this->queries[1]);
191+
$this->assertContains("ST_GeomFromText('GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(3 2,3 3))')", $this->queries[1]);
192192
}
193193

194194
public function testScopeDistance()
@@ -199,7 +199,7 @@ public function testScopeDistance()
199199
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
200200
$q = $query->getQuery();
201201
$this->assertNotEmpty($q->wheres);
202-
$this->assertContains("st_distance(`point`, GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
202+
$this->assertContains("st_distance(`point`, ST_GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
203203
}
204204

205205
public function testScopeDistanceExcludingSelf()
@@ -210,8 +210,8 @@ public function testScopeDistanceExcludingSelf()
210210
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
211211
$q = $query->getQuery();
212212
$this->assertNotEmpty($q->wheres);
213-
$this->assertContains("st_distance(`point`, GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
214-
$this->assertContains("st_distance(`point`, GeomFromText('POINT(2 1)')) != 0", $q->wheres[1]['sql']);
213+
$this->assertContains("st_distance(`point`, ST_GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
214+
$this->assertContains("st_distance(`point`, ST_GeomFromText('POINT(2 1)')) != 0", $q->wheres[1]['sql']);
215215
}
216216

217217
public function testScopeDistanceSphere()
@@ -222,7 +222,7 @@ public function testScopeDistanceSphere()
222222
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
223223
$q = $query->getQuery();
224224
$this->assertNotEmpty($q->wheres);
225-
$this->assertContains("st_distance_sphere(`point`, GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
225+
$this->assertContains("st_distance_sphere(`point`, ST_GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
226226
}
227227

228228
public function testScopeDistanceSphereExcludingSelf()
@@ -233,8 +233,8 @@ public function testScopeDistanceSphereExcludingSelf()
233233
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
234234
$q = $query->getQuery();
235235
$this->assertNotEmpty($q->wheres);
236-
$this->assertContains("st_distance_sphere(`point`, GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
237-
$this->assertContains("st_distance_sphere(`point`, GeomFromText('POINT(2 1)')) != 0", $q->wheres[1]['sql']);
236+
$this->assertContains("st_distance_sphere(`point`, ST_GeomFromText('POINT(2 1)')) <= 10", $q->wheres[0]['sql']);
237+
$this->assertContains("st_distance_sphere(`point`, ST_GeomFromText('POINT(2 1)')) != 0", $q->wheres[1]['sql']);
238238
}
239239

240240
public function testScopeDistanceValue()
@@ -247,7 +247,7 @@ public function testScopeDistanceValue()
247247
$this->assertNotEmpty($q->columns);
248248
$this->assertContains("*", $q->columns[0]);
249249
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
250-
$this->assertContains("st_distance(`point`, GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
250+
$this->assertContains("st_distance(`point`, ST_GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
251251
}
252252

253253
public function testScopeDistanceValueWithSelect()
@@ -260,7 +260,7 @@ public function testScopeDistanceValueWithSelect()
260260
$this->assertNotEmpty($q->columns);
261261
$this->assertContains("some_column", $q->columns[0]);
262262
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
263-
$this->assertContains("st_distance(`point`, GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
263+
$this->assertContains("st_distance(`point`, ST_GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
264264
}
265265

266266
public function testScopeDistanceSphereValue()
@@ -273,7 +273,7 @@ public function testScopeDistanceSphereValue()
273273
$this->assertNotEmpty($q->columns);
274274
$this->assertContains("*", $q->columns[0]);
275275
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
276-
$this->assertContains("st_distance_sphere(`point`, GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
276+
$this->assertContains("st_distance_sphere(`point`, ST_GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
277277
}
278278

279279
public function testScopeDistanceSphereValueWithSelect()
@@ -286,7 +286,7 @@ public function testScopeDistanceSphereValueWithSelect()
286286
$this->assertNotEmpty($q->columns);
287287
$this->assertContains("some_column", $q->columns[0]);
288288
$this->assertInstanceOf(\Illuminate\Database\Query\Expression::class, $q->columns[1]);
289-
$this->assertContains("st_distance_sphere(`point`, GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
289+
$this->assertContains("st_distance_sphere(`point`, ST_GeomFromText('POINT(2 1)')) as distance", $q->columns[1]->getValue());
290290
}
291291

292292
public function testScopeBounding()
@@ -307,7 +307,7 @@ public function testScopeBounding()
307307
$this->assertInstanceOf(\Grimzy\LaravelMysqlSpatial\Eloquent\Builder::class, $query);
308308
$q = $query->getQuery();
309309
$this->assertNotEmpty($q->wheres);
310-
$this->assertContains("st_intersects(GeomFromText('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))'), `point`)", $q->wheres[0]['sql']);
310+
$this->assertContains("st_intersects(ST_GeomFromText('POLYGON((1 1,2 1),(2 1,2 2),(2 2,1 1))'), `point`)", $q->wheres[0]['sql']);
311311
}
312312
}
313313

0 commit comments

Comments
 (0)