33namespace Grimzy \LaravelMysqlSpatial \Eloquent ;
44
55use Grimzy \LaravelMysqlSpatial \Exceptions \SpatialFieldsNotDefinedException ;
6+ use Grimzy \LaravelMysqlSpatial \Exceptions \UnknownSpatialRelationFunction ;
67use Grimzy \LaravelMysqlSpatial \Types \Geometry ;
78use Grimzy \LaravelMysqlSpatial \Types \GeometryInterface ;
89use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
@@ -37,6 +38,17 @@ trait SpatialTrait
3738
3839 public $ geometries = [];
3940
41+ protected $ stRelations = [
42+ 'within ' ,
43+ 'crosses ' ,
44+ 'contains ' ,
45+ 'disjoint ' ,
46+ 'equals ' ,
47+ 'intersects ' ,
48+ 'overlaps ' ,
49+ 'touches ' ,
50+ ];
51+
4052 /**
4153 * Create a new Eloquent query builder for the model.
4254 *
@@ -49,12 +61,21 @@ public function newEloquentBuilder($query)
4961 return new Builder ($ query );
5062 }
5163
64+ protected function newBaseQueryBuilder ()
65+ {
66+ $ connection = $ this ->getConnection ();
67+
68+ return new BaseBuilder (
69+ $ connection , $ connection ->getQueryGrammar (), $ connection ->getPostProcessor ()
70+ );
71+ }
72+
5273 protected function performInsert (EloquentBuilder $ query , array $ options = [])
5374 {
5475 foreach ($ this ->attributes as $ key => $ value ) {
5576 if ($ value instanceof GeometryInterface) {
5677 $ this ->geometries [$ key ] = $ value ; //Preserve the geometry objects prior to the insert
57- $ this ->attributes [$ key ] = $ this -> getConnection ()-> raw ( sprintf ( " GeomFromText('%s') " , $ value-> toWKT ()) );
78+ $ this ->attributes [$ key ] = new SpatialExpression ( $ value );
5879 }
5980 }
6081
@@ -89,30 +110,58 @@ public function getSpatialFields()
89110 }
90111 }
91112
113+ public function isColumnAllowed ($ geometryColumn )
114+ {
115+ if (!in_array ($ geometryColumn , $ this ->getSpatialFields ())) {
116+ throw new SpatialFieldsNotDefinedException ();
117+ }
118+ return true ;
119+ }
120+
92121 public function scopeDistance ($ query , $ geometryColumn , $ geometry , $ distance , $ exclude_self = false )
93122 {
94- $ query ->whereRaw ("st_distance(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) <= {$ distance }" );
123+ $ this ->isColumnAllowed ($ geometryColumn );
124+
125+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) <= ? " , [
126+ $ geometry ->toWkt (),
127+ $ distance ,
128+ ]);
95129
96130 if ($ exclude_self ) {
97- $ query ->whereRaw ("st_distance(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) != 0 " );
131+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) != 0 " , [
132+ $ geometry ->toWkt (),
133+ ]);
98134 }
99135
100136 return $ query ;
101137 }
102138
103139 public function scopeDistanceValue ($ query , $ geometryColumn , $ geometry )
104140 {
141+ $ this ->isColumnAllowed ($ geometryColumn );
142+
105143 $ columns = $ query ->getQuery ()->columns ;
106144
107145 if (!$ columns ) {
108146 $ query ->select ('* ' );
109147 }
110- $ query ->selectRaw ("st_distance(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) as distance " );
148+
149+ $ query ->selectRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?)) as distance " , [
150+ $ geometry ->toWkt (),
151+ ]);
111152 }
112153
113154 public function scopeComparison ($ query , $ geometryColumn , $ geometry , $ relationship )
114155 {
115- $ query ->whereRaw ("st_ {$ relationship }(` {$ geometryColumn }`, GeomFromText(' {$ geometry ->toWkt ()}')) " );
156+ $ this ->isColumnAllowed ($ geometryColumn );
157+
158+ if (!in_array ($ relationship , $ this ->stRelations )) {
159+ throw new UnknownSpatialRelationFunction ($ relationship );
160+ }
161+
162+ $ query ->whereRaw ("st_ {$ relationship }(` $ geometryColumn`, ST_GeomFromText(?)) " , [
163+ $ geometry ->toWkt (),
164+ ]);
116165
117166 return $ query ;
118167 }
0 commit comments