Skip to content

Commit 9ce4f4c

Browse files
MatanYadaevMatan Yadaev
andauthored
Query Builder - Support column to have table name (#61)
* Add test * Fix Co-authored-by: Matan Yadaev <[email protected]>
1 parent e8b08ea commit 9ce4f4c

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/SpatialBuilder.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function withDistance(
3131
$this->selectRaw(
3232
sprintf(
3333
'ST_DISTANCE(%s, %s) AS %s',
34-
"`{$column}`",
34+
$this->getQuery()->getGrammar()->wrap($column),
3535
$this->toExpression($geometryOrColumn),
3636
$alias,
3737
)
@@ -50,7 +50,7 @@ public function whereDistance(
5050
$this->whereRaw(
5151
sprintf(
5252
'ST_DISTANCE(%s, %s) %s %s',
53-
"`{$column}`",
53+
$this->getQuery()->getGrammar()->wrap($column),
5454
$this->toExpression($geometryOrColumn),
5555
$operator,
5656
$value,
@@ -69,7 +69,7 @@ public function orderByDistance(
6969
$this->orderByRaw(
7070
sprintf(
7171
'ST_DISTANCE(%s, %s) %s',
72-
"`{$column}`",
72+
$this->getQuery()->getGrammar()->wrap($column),
7373
$this->toExpression($geometryOrColumn),
7474
$direction,
7575
)
@@ -91,7 +91,7 @@ public function withDistanceSphere(
9191
$this->selectRaw(
9292
sprintf(
9393
'ST_DISTANCE_SPHERE(%s, %s) AS %s',
94-
"`{$column}`",
94+
$this->getQuery()->getGrammar()->wrap($column),
9595
$this->toExpression($geometryOrColumn),
9696
$alias,
9797
)
@@ -110,7 +110,7 @@ public function whereDistanceSphere(
110110
$this->whereRaw(
111111
sprintf(
112112
'ST_DISTANCE_SPHERE(%s, %s) %s %s',
113-
"`{$column}`",
113+
$this->getQuery()->getGrammar()->wrap($column),
114114
$this->toExpression($geometryOrColumn),
115115
$operator,
116116
$value
@@ -129,7 +129,7 @@ public function orderByDistanceSphere(
129129
$this->orderByRaw(
130130
sprintf(
131131
'ST_DISTANCE_SPHERE(%s, %s) %s',
132-
"`{$column}`",
132+
$this->getQuery()->getGrammar()->wrap($column),
133133
$this->toExpression($geometryOrColumn),
134134
$direction
135135
)
@@ -143,7 +143,7 @@ public function whereWithin(string $column, Geometry|string $geometryOrColumn):
143143
$this->whereRaw(
144144
sprintf(
145145
'ST_WITHIN(%s, %s)',
146-
"`{$column}`",
146+
$this->getQuery()->getGrammar()->wrap($column),
147147
$this->toExpression($geometryOrColumn),
148148
)
149149
);
@@ -156,7 +156,7 @@ public function whereNotWithin(string $column, Geometry|string $geometryOrColumn
156156
$this->whereRaw(
157157
sprintf(
158158
'ST_WITHIN(%s, %s) = 0',
159-
"`{$column}`",
159+
$this->getQuery()->getGrammar()->wrap($column),
160160
$this->toExpression($geometryOrColumn),
161161
)
162162
);
@@ -169,7 +169,7 @@ public function whereContains(string $column, Geometry|string $geometryOrColumn)
169169
$this->whereRaw(
170170
sprintf(
171171
'ST_CONTAINS(%s, %s)',
172-
"`{$column}`",
172+
$this->getQuery()->getGrammar()->wrap($column),
173173
$this->toExpression($geometryOrColumn),
174174
)
175175
);
@@ -182,7 +182,7 @@ public function whereNotContains(string $column, Geometry|string $geometryOrColu
182182
$this->whereRaw(
183183
sprintf(
184184
'ST_CONTAINS(%s, %s) = 0',
185-
"`{$column}`",
185+
$this->getQuery()->getGrammar()->wrap($column),
186186
$this->toExpression($geometryOrColumn),
187187
)
188188
);
@@ -195,7 +195,7 @@ public function whereTouches(string $column, Geometry|string $geometryOrColumn):
195195
$this->whereRaw(
196196
sprintf(
197197
'ST_TOUCHES(%s, %s)',
198-
"`{$column}`",
198+
$this->getQuery()->getGrammar()->wrap($column),
199199
$this->toExpression($geometryOrColumn),
200200
)
201201
);
@@ -208,7 +208,7 @@ public function whereIntersects(string $column, Geometry|string $geometryOrColum
208208
$this->whereRaw(
209209
sprintf(
210210
'ST_INTERSECTS(%s, %s)',
211-
"`{$column}`",
211+
$this->getQuery()->getGrammar()->wrap($column),
212212
$this->toExpression($geometryOrColumn),
213213
)
214214
);
@@ -221,7 +221,7 @@ public function whereCrosses(string $column, Geometry|string $geometryOrColumn):
221221
$this->whereRaw(
222222
sprintf(
223223
'ST_CROSSES(%s, %s)',
224-
"`{$column}`",
224+
$this->getQuery()->getGrammar()->wrap($column),
225225
$this->toExpression($geometryOrColumn),
226226
)
227227
);
@@ -234,7 +234,7 @@ public function whereDisjoint(string $column, Geometry|string $geometryOrColumn)
234234
$this->whereRaw(
235235
sprintf(
236236
'ST_DISJOINT(%s, %s)',
237-
"`{$column}`",
237+
$this->getQuery()->getGrammar()->wrap($column),
238238
$this->toExpression($geometryOrColumn),
239239
)
240240
);
@@ -247,7 +247,7 @@ public function whereOverlaps(string $column, Geometry|string $geometryOrColumn)
247247
$this->whereRaw(
248248
sprintf(
249249
'ST_OVERLAPS(%s, %s)',
250-
"`{$column}`",
250+
$this->getQuery()->getGrammar()->wrap($column),
251251
$this->toExpression($geometryOrColumn),
252252
)
253253
);
@@ -260,7 +260,7 @@ public function whereEquals(string $column, Geometry|string $geometryOrColumn):
260260
$this->whereRaw(
261261
sprintf(
262262
'ST_EQUALS(%s, %s)',
263-
"`{$column}`",
263+
$this->getQuery()->getGrammar()->wrap($column),
264264
$this->toExpression($geometryOrColumn),
265265
)
266266
);
@@ -277,7 +277,7 @@ public function whereSrid(
277277
$this->whereRaw(
278278
sprintf(
279279
'ST_SRID(%s) %s %s',
280-
"`{$column}`",
280+
$this->getQuery()->getGrammar()->wrap($column),
281281
$operator,
282282
$value,
283283
)

tests/SpatialBuilderTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,14 @@
392392
expect($testPlaces)->toHaveCount(1);
393393
expect($testPlaces[0]->point)->toEqual($point1);
394394
});
395+
396+
it('uses spatial function on column that contains its table name', function (): void {
397+
TestPlace::factory()->create(['point' => new Point(0, 0, 4326)]);
398+
399+
/** @var TestPlace $testPlaceWithDistance */
400+
$testPlaceWithDistance = TestPlace::query()
401+
->withDistance('test_places.point', new Point(0, 0, 4326))
402+
->firstOrFail();
403+
404+
expect($testPlaceWithDistance->distance)->toBe(0.0);
405+
});

0 commit comments

Comments
 (0)