Skip to content

Commit 89160a5

Browse files
committed
[graphql] SearchBy: Will always use table alias for Eloquent Builder (this is required to avoid conflicts with SortBy).
1 parent d69c964 commit 89160a5

File tree

3 files changed

+169
-48
lines changed

3 files changed

+169
-48
lines changed

src/SearchBy/Operators/Complex/RelationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static function (): EloquentBuilder {
123123
[
124124
'sql' => 'select * from "table_a" where exists ('.
125125
'select * from "table_b" where '.
126-
'"table_a"."id" = "table_b"."table_a_id" and "property" = ?'.
126+
'"table_a"."id" = "table_b"."table_a_id" and "table_b"."property" = ?'.
127127
')',
128128
'bindings' => [123],
129129
],

src/SearchBy/SearchBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ public function processComparison(
254254
// Table Alias?
255255
if ($tableAlias) {
256256
$property = "{$tableAlias}.{$property}";
257+
} elseif ($builder instanceof EloquentBuilder) {
258+
$property = $builder->qualifyColumn($property);
259+
} else {
260+
// empty
257261
}
258262

259263
// Apply

src/SearchBy/SearchBuilderTest.php

Lines changed: 164 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Exception;
77
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8+
use Illuminate\Database\Eloquent\Model;
89
use Illuminate\Database\Query\Builder as QueryBuilder;
910
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Contracts\Operator;
1011
use LastDragon_ru\LaraASP\GraphQL\SearchBy\Operators\Comparison\Equal;
@@ -33,6 +34,8 @@ class SearchBuilderTest extends TestCase {
3334
* @covers ::process
3435
*
3536
* @dataProvider dataProviderProcess
37+
* @dataProvider dataProviderProcessQuery
38+
* @dataProvider dataProviderProcessEloquent
3639
*
3740
* @param array<mixed> $conditions
3841
*/
@@ -103,6 +106,8 @@ static function (
103106
* @covers ::processComparison
104107
*
105108
* @dataProvider dataProviderProcessComparison
109+
* @dataProvider dataProviderProcessComparisonQuery
110+
* @dataProvider dataProviderProcessComparisonEloquent
106111
*
107112
* @param array<mixed> $conditions
108113
*/
@@ -291,9 +296,11 @@ public function dataProviderProcess(): array {
291296
],
292297
null,
293298
],
294-
'valid condition' => [
299+
'valid condition with table alias' => [
295300
[
296-
'sql' => 'select * from "tmp" where (not ((("a" != ?) and ((("a" = ?) or ("b" != ?))))))',
301+
'sql' => 'select * from "tmp" where ('.
302+
'not ((("alias"."a" != ?) and ((("alias"."a" = ?) or ("alias"."b" != ?)))))'.
303+
')',
297304
'bindings' => [
298305
1,
299306
2,
@@ -326,49 +333,117 @@ public function dataProviderProcess(): array {
326333
],
327334
],
328335
],
329-
null,
336+
'alias',
330337
],
331-
'valid condition with table alias' => [
332-
[
333-
'sql' => 'select * from "tmp" where ('.
334-
'not ((("alias"."a" != ?) and ((("alias"."a" = ?) or ("alias"."b" != ?)))))'.
335-
')',
336-
'bindings' => [
337-
1,
338-
2,
339-
3,
340-
],
338+
]),
339+
))->getData();
340+
}
341+
342+
/**
343+
* @return array<mixed>
344+
*/
345+
public function dataProviderProcessQuery(): array {
346+
return [
347+
'query: valid condition' => [
348+
[
349+
'sql' => 'select * from "tmp" where ('.
350+
'not ((("a" != ?) and ((("a" = ?) or ("b" != ?)))))'.
351+
')',
352+
'bindings' => [
353+
1,
354+
2,
355+
3,
341356
],
342-
[
343-
'not' => 'yes',
344-
'allOf' => [
345-
[
346-
'a' => [
347-
'eq' => 1,
348-
'not' => 'yes',
357+
],
358+
static function (TestCase $test): QueryBuilder {
359+
return $test->app->make('db')->table('tmp');
360+
},
361+
[
362+
'not' => 'yes',
363+
'allOf' => [
364+
[
365+
'a' => [
366+
'eq' => 1,
367+
'not' => 'yes',
368+
],
369+
],
370+
[
371+
'anyOf' => [
372+
[
373+
'a' => [
374+
'eq' => 2,
375+
],
376+
],
377+
[
378+
'b' => [
379+
'eq' => 3,
380+
'not' => 'yes',
381+
],
349382
],
350383
],
351-
[
352-
'anyOf' => [
353-
[
354-
'a' => [
355-
'eq' => 2,
356-
],
384+
],
385+
],
386+
],
387+
null,
388+
],
389+
];
390+
}
391+
392+
/**
393+
* @return array<mixed>
394+
*/
395+
public function dataProviderProcessEloquent(): array {
396+
return [
397+
'eloquent: valid condition' => [
398+
[
399+
'sql' => 'select * from "tmp" where ('.
400+
'not ((("tmp"."a" != ?) and ((("tmp"."a" = ?) or ("tmp"."b" != ?)))))'.
401+
')',
402+
'bindings' => [
403+
1,
404+
2,
405+
3,
406+
],
407+
],
408+
static function (TestCase $test): EloquentBuilder {
409+
return (new class() extends Model {
410+
/**
411+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
412+
*
413+
* @var string
414+
*/
415+
public $table = 'tmp';
416+
})->query();
417+
},
418+
[
419+
'not' => 'yes',
420+
'allOf' => [
421+
[
422+
'a' => [
423+
'eq' => 1,
424+
'not' => 'yes',
425+
],
426+
],
427+
[
428+
'anyOf' => [
429+
[
430+
'a' => [
431+
'eq' => 2,
357432
],
358-
[
359-
'b' => [
360-
'eq' => 3,
361-
'not' => 'yes',
362-
],
433+
],
434+
[
435+
'b' => [
436+
'eq' => 3,
437+
'not' => 'yes',
363438
],
364439
],
365440
],
366441
],
367442
],
368-
'alias',
369443
],
370-
]),
371-
))->getData();
444+
null,
445+
],
446+
];
372447
}
373448

374449
/**
@@ -479,19 +554,6 @@ public function dataProviderProcessComparison(): array {
479554
],
480555
null,
481556
],
482-
'valid condition' => [
483-
[
484-
'sql' => 'select * from "tmp" where "property" = ?',
485-
'bindings' => [
486-
123,
487-
],
488-
],
489-
'property',
490-
[
491-
'eq' => 123,
492-
],
493-
null,
494-
],
495557
'valid condition with table alias' => [
496558
[
497559
'sql' => 'select * from "tmp" where "alias"."property" = ?',
@@ -508,5 +570,60 @@ public function dataProviderProcessComparison(): array {
508570
]),
509571
))->getData();
510572
}
573+
574+
/**
575+
* @return array<mixed>
576+
*/
577+
public function dataProviderProcessComparisonQuery(): array {
578+
return [
579+
'query: valid condition' => [
580+
[
581+
'sql' => 'select * from "tmp" where "property" = ?',
582+
'bindings' => [
583+
123,
584+
],
585+
],
586+
static function (TestCase $test): QueryBuilder {
587+
return $test->app->make('db')->table('tmp');
588+
},
589+
'property',
590+
[
591+
'eq' => 123,
592+
],
593+
null,
594+
],
595+
];
596+
}
597+
598+
/**
599+
* @return array<mixed>
600+
*/
601+
public function dataProviderProcessComparisonEloquent(): array {
602+
return [
603+
'eloquent: valid condition' => [
604+
[
605+
'sql' => 'select * from "tmp" where "tmp"."property" = ?',
606+
'bindings' => [
607+
123,
608+
],
609+
],
610+
static function (TestCase $test): EloquentBuilder {
611+
return (new class() extends Model {
612+
/**
613+
* @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
614+
*
615+
* @var string
616+
*/
617+
public $table = 'tmp';
618+
})->query();
619+
},
620+
'property',
621+
[
622+
'eq' => 123,
623+
],
624+
null,
625+
],
626+
];
627+
}
511628
// </editor-fold>
512629
}

0 commit comments

Comments
 (0)