3
3
namespace Grimzy \LaravelMysqlSpatial \Eloquent ;
4
4
5
5
use Grimzy \LaravelMysqlSpatial \Exceptions \SpatialFieldsNotDefinedException ;
6
+ use Grimzy \LaravelMysqlSpatial \Exceptions \UnknownSpatialRelationFunction ;
6
7
use Grimzy \LaravelMysqlSpatial \Types \Geometry ;
7
8
use Grimzy \LaravelMysqlSpatial \Types \GeometryInterface ;
8
9
use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
@@ -37,6 +38,17 @@ trait SpatialTrait
37
38
38
39
public $ geometries = [];
39
40
41
+ protected $ stRelations = [
42
+ 'within ' ,
43
+ 'crosses ' ,
44
+ 'contains ' ,
45
+ 'disjoint ' ,
46
+ 'equals ' ,
47
+ 'intersects ' ,
48
+ 'overlaps ' ,
49
+ 'touches ' ,
50
+ ];
51
+
40
52
/**
41
53
* Create a new Eloquent query builder for the model.
42
54
*
@@ -49,12 +61,21 @@ public function newEloquentBuilder($query)
49
61
return new Builder ($ query );
50
62
}
51
63
64
+ protected function newBaseQueryBuilder ()
65
+ {
66
+ $ connection = $ this ->getConnection ();
67
+
68
+ return new BaseBuilder (
69
+ $ connection , $ connection ->getQueryGrammar (), $ connection ->getPostProcessor ()
70
+ );
71
+ }
72
+
52
73
protected function performInsert (EloquentBuilder $ query , array $ options = [])
53
74
{
54
75
foreach ($ this ->attributes as $ key => $ value ) {
55
76
if ($ value instanceof GeometryInterface) {
56
77
$ this ->geometries [$ key ] = $ value ; //Preserve the geometry objects prior to the insert
57
- $ this ->attributes [$ key ] = $ this -> getConnection ()-> raw ( sprintf ( " ST_GeomFromText('%s') " , $ value-> toWKT ()) );
78
+ $ this ->attributes [$ key ] = new SpatialExpression ( $ value );
58
79
}
59
80
}
60
81
@@ -89,61 +110,105 @@ public function getSpatialFields()
89
110
}
90
111
}
91
112
113
+ public function isColumnAllowed ($ geometryColumn )
114
+ {
115
+ if (!in_array ($ geometryColumn , $ this ->getSpatialFields ())) {
116
+ throw new SpatialFieldsNotDefinedException ();
117
+ }
118
+
119
+ return true ;
120
+ }
121
+
92
122
public function scopeDistance ($ query , $ geometryColumn , $ geometry , $ distance )
93
123
{
94
- $ query ->whereRaw ("st_distance(` {$ geometryColumn }`, ST_GeomFromText(' {$ geometry ->toWkt ()}')) <= {$ distance }" );
124
+ $ this ->isColumnAllowed ($ geometryColumn );
125
+
126
+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) <= ? " , [
127
+ $ geometry ->toWkt (),
128
+ $ distance ,
129
+ ]);
95
130
96
131
return $ query ;
97
132
}
98
133
99
134
public function scopeDistanceExcludingSelf ($ query , $ geometryColumn , $ geometry , $ distance )
100
135
{
136
+ $ this ->isColumnAllowed ($ geometryColumn );
137
+
101
138
$ query = $ this ->scopeDistance ($ query , $ geometryColumn , $ geometry , $ distance );
102
139
103
- $ query ->whereRaw ("st_distance(` {$ geometryColumn }`, ST_GeomFromText(' {$ geometry ->toWkt ()}')) != 0 " );
140
+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) != 0 " , [
141
+ $ geometry ->toWkt (),
142
+ ]);
104
143
105
144
return $ query ;
106
145
}
107
146
108
147
public function scopeDistanceValue ($ query , $ geometryColumn , $ geometry )
109
148
{
149
+ $ this ->isColumnAllowed ($ geometryColumn );
150
+
110
151
$ columns = $ query ->getQuery ()->columns ;
111
152
112
153
if (!$ columns ) {
113
154
$ query ->select ('* ' );
114
155
}
115
- $ query ->selectRaw ("st_distance(` {$ geometryColumn }`, ST_GeomFromText(' {$ geometry ->toWkt ()}')) as distance " );
156
+
157
+ $ query ->selectRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) as distance " , [
158
+ $ geometry ->toWkt (),
159
+ ]);
116
160
}
117
161
118
162
public function scopeDistanceSphere ($ query , $ geometryColumn , $ geometry , $ distance )
119
163
{
120
- $ query ->whereRaw ("st_distance_sphere(` {$ geometryColumn }`, ST_GeomFromText(' {$ geometry ->toWkt ()}')) <= {$ distance }" );
164
+ $ this ->isColumnAllowed ($ geometryColumn );
165
+
166
+ $ query ->whereRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?)) <= ? " , [
167
+ $ geometry ->toWkt (),
168
+ $ distance ,
169
+ ]);
121
170
122
171
return $ query ;
123
172
}
124
173
125
174
public function scopeDistanceSphereExcludingSelf ($ query , $ geometryColumn , $ geometry , $ distance )
126
175
{
176
+ $ this ->isColumnAllowed ($ geometryColumn );
177
+
127
178
$ query = $ this ->scopeDistanceSphere ($ query , $ geometryColumn , $ geometry , $ distance );
128
179
129
- $ query ->whereRaw ("st_distance_sphere(` {$ geometryColumn }`, ST_GeomFromText(' {$ geometry ->toWkt ()}')) != 0 " );
180
+ $ query ->whereRaw ("st_distance_sphere( $ geometryColumn, ST_GeomFromText(?)) != 0 " , [
181
+ $ geometry ->toWkt (),
182
+ ]);
130
183
131
184
return $ query ;
132
185
}
133
186
134
187
public function scopeDistanceSphereValue ($ query , $ geometryColumn , $ geometry )
135
188
{
189
+ $ this ->isColumnAllowed ($ geometryColumn );
190
+
136
191
$ columns = $ query ->getQuery ()->columns ;
137
192
138
193
if (!$ columns ) {
139
194
$ query ->select ('* ' );
140
195
}
141
- $ query ->selectRaw ("st_distance_sphere(` {$ geometryColumn }`, ST_GeomFromText(' {$ geometry ->toWkt ()}')) as distance " );
196
+ $ query ->selectRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?)) as distance " , [
197
+ $ geometry ->toWkt (),
198
+ ]);
142
199
}
143
200
144
201
public function scopeComparison ($ query , $ geometryColumn , $ geometry , $ relationship )
145
202
{
146
- $ query ->whereRaw ("st_ {$ relationship }(` {$ geometryColumn }`, ST_GeomFromText(' {$ geometry ->toWkt ()}')) " );
203
+ $ this ->isColumnAllowed ($ geometryColumn );
204
+
205
+ if (!in_array ($ relationship , $ this ->stRelations )) {
206
+ throw new UnknownSpatialRelationFunction ($ relationship );
207
+ }
208
+
209
+ $ query ->whereRaw ("st_ {$ relationship }(` $ geometryColumn`, ST_GeomFromText(?)) " , [
210
+ $ geometry ->toWkt (),
211
+ ]);
147
212
148
213
return $ query ;
149
214
}
0 commit comments