Skip to content

Commit

Permalink
Merge pull request #110 from netpok/master
Browse files Browse the repository at this point in the history
Handle columns with reserved names
  • Loading branch information
freekmurze authored May 3, 2021
2 parents a246b78 + 0bf7417 commit ab81713
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
11 changes: 2 additions & 9 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [7.2, 7.3, 7.4, 8.0]
laravel: [8.*, 6.*, 7.*]
php: [7.3, 7.4, 8.0]
laravel: [8.*]
dependency-version: [prefer-lowest, prefer-stable]
include:
- laravel: 8.*
testbench: 6.*
- laravel: 7.*
testbench: 5.*
- laravel: 6.*
testbench: 4.*
exclude:
- laravel: 8.*
php: 7.2

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
}
],
"require": {
"php": "^7.2|^8.0",
"laravel/framework": "^6.0|^7.0|^8.0"
"php": "^7.3|^8.0",
"laravel/framework": "^8.0"
},
"require-dev": {
"orchestra/testbench": "~3.8.0|^4.0|^5.0|^6.0",
"orchestra/testbench": "^6.0",
"larapack/dd": "^1.0",
"phpunit/phpunit": "^7.5|^8.0|^9.3"
"phpunit/phpunit": "^9.3"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion src/ModelSearchAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ protected function addSearchConditions(Builder $query, string $term)

$query->where(function (Builder $query) use ($attributes, $term, $searchTerms) {
foreach (Arr::wrap($attributes) as $attribute) {
$sql = "LOWER({$query->getGrammar()->wrap($attribute->getAttribute())}) LIKE ?";

foreach ($searchTerms as $searchTerm) {
$sql = "LOWER({$attribute->getAttribute()}) LIKE ?";
$searchTerm = mb_strtolower($searchTerm, 'UTF8');

$attribute->isPartial()
Expand Down
20 changes: 17 additions & 3 deletions tests/ModelSearchAspectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function it_can_perform_a_search_on_multiple_columns()
$this->assertInstanceOf(TestModel::class, $results[0]);
}

/** @test */
public function it_can_perform_a_search_on_columns_with_reserved_name()
{
TestModel::createWithNameAndLastName('jane', 'doe');
TestModel::createWithNameAndLastName('Taylor', 'Otwell');

$searchAspect = ModelSearchAspect::forModel(TestModel::class, 'name', 'where');

$results = $searchAspect->getResults('Taylor Otwell');

$this->assertCount(1, $results);
$this->assertInstanceOf(TestModel::class, $results[0]);
}

/** @test */
public function it_can_add_searchable_attributes()
{
Expand Down Expand Up @@ -71,7 +85,7 @@ public function it_can_build_an_eloquent_query()

$searchAspect->getResults('john');

$expectedQuery = 'select * from "test_models" where (LOWER(name) LIKE ? or "email" = ?)';
$expectedQuery = 'select * from "test_models" where (LOWER("name") LIKE ? or "email" = ?)';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');

Expand Down Expand Up @@ -110,7 +124,7 @@ public function it_can_build_an_eloquent_query_applying_scopes()

$searchAspect->getResults('john');

$expectedQuery = 'select * from "test_models" where "active" = ? and (LOWER(name) LIKE ?)';
$expectedQuery = 'select * from "test_models" where "active" = ? and (LOWER("name") LIKE ?)';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');
$firstBinding = Arr::get(DB::getQueryLog(), '0.bindings.0');
Expand Down Expand Up @@ -173,7 +187,7 @@ public function it_can_build_an_eloquent_query_by_many_same_methods()

$searchAspect->getResults('taylor');

$expectedQuery = 'select * from "test_models" where "gender" = ? and "status" = ? and (LOWER(name) LIKE ?)';
$expectedQuery = 'select * from "test_models" where "gender" = ? and "status" = ? and (LOWER("name") LIKE ?)';

$executedQuery = Arr::get(DB::getQueryLog(), '0.query');

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function setUpDatabase(Application $app)
$table->timestamps();
$table->string('name');
$table->string('last_name')->nullable();
$table->string('where')->nullable();
$table->boolean('active')->default(false);
$table->string('gender')->nullable();
});
Expand Down

0 comments on commit ab81713

Please sign in to comment.