Skip to content

Commit 68c363b

Browse files
committed
wip
1 parent daf46fc commit 68c363b

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/ModelToSearchThrough.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,11 @@ public function getModel(): Model
138138
*/
139139
public function getModelKey($suffix = 'key'): string
140140
{
141-
$parts = [
141+
return implode('_', [
142142
$this->key,
143143
Str::snake(class_basename($this->getModel())),
144144
$suffix,
145-
];
146-
147-
// SQLite doesn't allow column aliases starting with numbers
148-
if ($this->getModel()->getConnection()->getDriverName() === 'sqlite') {
149-
array_unshift($parts, 'model');
150-
}
151-
152-
return implode('_', $parts);
145+
]);
153146
}
154147

155148
/**

src/Searcher.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ private function addRelevanceQueryToBuilder($builder, $modelToSearchThrough)
556556
*/
557557
protected function makeSelects(ModelToSearchThrough $currentModel): array
558558
{
559-
$selects = $this->modelsToSearchThrough->flatMap(function (ModelToSearchThrough $modelToSearchThrough) use ($currentModel) {
559+
$grammar = $this->getSearchGrammar();
560+
561+
$selects = $this->modelsToSearchThrough->flatMap(function (ModelToSearchThrough $modelToSearchThrough) use ($currentModel, $grammar) {
560562
$qualifiedKeyName = $qualifiedOrderByColumnName = $modelOrderKey = 'null';
561563

562564
if ($modelToSearchThrough === $currentModel) {
@@ -578,9 +580,9 @@ protected function makeSelects(ModelToSearchThrough $currentModel): array
578580
}
579581

580582
return array_filter([
581-
DB::raw("{$qualifiedKeyName} as {$modelToSearchThrough->getModelKey()}"),
582-
DB::raw("{$qualifiedOrderByColumnName} as {$modelToSearchThrough->getModelKey('order')}"),
583-
$this->orderByModel ? DB::raw("{$modelOrderKey} as {$modelToSearchThrough->getModelKey('model_order')}") : null,
583+
DB::raw("{$qualifiedKeyName} as {$grammar->wrap($modelToSearchThrough->getModelKey())}"),
584+
DB::raw("{$qualifiedOrderByColumnName} as {$grammar->wrap($modelToSearchThrough->getModelKey('order'))}"),
585+
$this->orderByModel ? DB::raw("{$modelOrderKey} as {$grammar->wrap($modelToSearchThrough->getModelKey('model_order'))}") : null,
584586
]);
585587
})->all();
586588

@@ -596,7 +598,9 @@ protected function makeSelects(ModelToSearchThrough $currentModel): array
596598
protected function makeOrderBy(): string
597599
{
598600
$grammar = $this->getSearchGrammar();
599-
$modelOrderKeys = $this->modelsToSearchThrough->map->getModelKey('order')->toArray();
601+
$modelOrderKeys = $this->modelsToSearchThrough->map(function($modelToSearchThrough) use ($grammar) {
602+
return $grammar->wrap($modelToSearchThrough->getModelKey('order'));
603+
})->toArray();
600604

601605
return $grammar->coalesce($modelOrderKeys);
602606
}
@@ -610,7 +614,9 @@ protected function makeOrderBy(): string
610614
protected function makeOrderByModel(): string
611615
{
612616
$grammar = $this->getSearchGrammar();
613-
$modelOrderKeys = $this->modelsToSearchThrough->map->getModelKey('model_order')->toArray();
617+
$modelOrderKeys = $this->modelsToSearchThrough->map(function($modelToSearchThrough) use ($grammar) {
618+
return $grammar->wrap($modelToSearchThrough->getModelKey('model_order'));
619+
})->toArray();
614620

615621
return $grammar->coalesce($modelOrderKeys);
616622
}
@@ -742,7 +748,9 @@ protected function applyModelOrdering($query): void
742748
}
743749

744750
$grammar = $this->getSearchGrammar();
745-
$modelOrderKeys = $this->modelsToSearchThrough->map->getModelKey('model_order')->toArray();
751+
$modelOrderKeys = $this->modelsToSearchThrough->map(function($modelToSearchThrough) use ($grammar) {
752+
return $grammar->wrap($modelToSearchThrough->getModelKey('model_order'));
753+
})->toArray();
746754
$modelCoalesceExpr = $grammar->coalesce($modelOrderKeys);
747755

748756
$query->orderByRaw($modelCoalesceExpr . ' ' . $this->getOrderDirection());
@@ -774,7 +782,9 @@ protected function applyStandardOrdering($query): void
774782
}
775783

776784
$grammar = $this->getSearchGrammar();
777-
$orderKeys = $this->modelsToSearchThrough->map->getModelKey('order')->toArray();
785+
$orderKeys = $this->modelsToSearchThrough->map(function($modelToSearchThrough) use ($grammar) {
786+
return $grammar->wrap($modelToSearchThrough->getModelKey('order'));
787+
})->toArray();
778788
$coalesceExpr = $grammar->coalesce($orderKeys);
779789

780790
$query->orderByRaw($coalesceExpr . ' ' . $this->getOrderDirection());

tests/SearchTest.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ public function it_can_count_the_results()
7777
/** @test */
7878
public function it_respects_table_prefixes()
7979
{
80-
// Temporarily set table prefix
80+
// Create a new in-memory database with prefix for testing
8181
$connection = DB::connection();
8282
$originalPrefix = $connection->getTablePrefix();
83-
$connection->setTablePrefix('prefix_');
8483

85-
// Recreate tables with prefix
86-
$this->artisan('migrate:fresh');
84+
// Create tables first, then set prefix and recreate
85+
$connection->setTablePrefix('prefix_');
86+
87+
// Create the prefixed tables manually
8788
include_once __DIR__ . '/create_tables.php';
8889
(new \CreateTables)->up();
8990

@@ -98,7 +99,7 @@ public function it_respects_table_prefixes()
9899

99100
$this->assertEquals(3, $count);
100101

101-
// Reset prefix
102+
// Reset prefix and clean up
102103
$connection->setTablePrefix($originalPrefix);
103104
}
104105

@@ -235,6 +236,11 @@ public function it_can_search_on_the_left_side_of_the_term()
235236
/** @test */
236237
public function it_can_use_the_sounds_like_operator()
237238
{
239+
// Skip on SQLite since it doesn't support SOUNDS LIKE
240+
if (env('DB_CONNECTION') === 'sqlite') {
241+
$this->markTestSkipped('SOUNDS LIKE operator not supported on SQLite');
242+
}
243+
238244
Video::create(['title' => 'laravel']);
239245

240246
$this->assertCount(0, Search::add(Video::class, 'title')->search('larafel'));
@@ -632,6 +638,10 @@ public function it_includes_a_custom_model_identifier_to_search_results()
632638
/** @test */
633639
public function it_supports_full_text_search()
634640
{
641+
// Skip on SQLite since Laravel doesn't support full-text search on SQLite
642+
if (env('DB_CONNECTION') === 'sqlite') {
643+
$this->markTestSkipped('Full-text search not supported on SQLite');
644+
}
635645
$postA = Post::create(['title' => 'Laravel Framework']);
636646
$postB = Post::create(['title' => 'Tailwind Framework']);
637647

@@ -659,6 +669,10 @@ public function it_supports_full_text_search()
659669
/** @test */
660670
public function it_supports_full_text_search_on_relations()
661671
{
672+
// Skip on SQLite since Laravel doesn't support full-text search on SQLite
673+
if (env('DB_CONNECTION') === 'sqlite') {
674+
$this->markTestSkipped('Full-text search not supported on SQLite');
675+
}
662676
$videoA = Video::create(['title' => 'Page A']);
663677
$videoB = Video::create(['title' => 'Page B']);
664678
$videoC = Video::create(['title' => 'Page C']);

0 commit comments

Comments
 (0)