Skip to content

Commit

Permalink
167 support wherelike orwherelike wherenotlike orwherenotlike (#169)
Browse files Browse the repository at this point in the history
* feature: Added support for whereLike/orWhereLike/whereNotLike/orWhereNotLike

* docs: added whereLike / orWhereLike / whereNotLike / orWhereNotLike
  • Loading branch information
LaravelFreelancerNL authored Nov 10, 2024
1 parent 9e1b965 commit 8b405eb
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/compatibility-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ rightJoin / rightJoinSub / joinWhere?
where / orWhere / whereNot / orWhereNot / whereColumn / whereExists
whereBetween / whereNotBetween / whereBetweenColumns / whereNotBetweenColumns /
whereJsonContains / whereJsonLength /
whereIn / whereNotIn / whereNone / orWhereNone / whereNot / orWhereNot /
whereIn / whereNotIn /
whereLike / orWhereLike / whereNotLike / orWhereNotLike /
whereNone / orWhereNone / whereNot / orWhereNot /
whereNull / whereNotNull /
whereDate / whereMonth / whereDay / whereYear / whereTime /
whereRaw (use AQL) /
Expand Down
21 changes: 21 additions & 0 deletions src/Query/Concerns/BuildsWheres.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ public function whereJsonLength($column, $operator, $value = null, $boolean = 'a
return $this;
}

/**
* Add a "where like" clause to the query.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $column
* @param string $value
* @param bool $caseSensitive
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereLike($column, $value, $caseSensitive = false, $boolean = 'and', $not = false)
{
$type = 'Like';

$value = $this->bindValue($value);

$this->wheres[] = compact('type', 'column', 'value', 'caseSensitive', 'boolean', 'not');

return $this;
}

/**
* Add a "where null" clause to the query.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Query/Concerns/CompilesFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ protected function filterYear(IlluminateQueryBuilder $query, $filter)
return implode(' ', $predicate);
}

/**
* Compile a filter month clause.
*
* @param IlluminateQueryBuilder $query
* @param array<mixed> $filter
* @return string
* @throws \Exception
*/
protected function filterLike(IlluminateQueryBuilder $query, $filter)
{
$column = $this->normalizeColumn($query, $filter['column']);
$value = $this->parameter($filter['value']);

$predicate = [];

$filter = $this->normalizeOperator($filter);

$predicate[0] = ($filter['caseSensitive']) ? $column : 'LOWER(' . $column . ')';
$predicate[1] = 'LIKE';
$predicate[2] = ($filter['caseSensitive']) ? $value : 'LOWER(' . $value . ')';

$result = implode(' ', $predicate);

if ($filter['not']) {
$result = 'NOT (' . $result . ')';
}

return $result;
}

/**
* Compile a filter month clause.
*
Expand Down
78 changes: 76 additions & 2 deletions tests/Query/WheresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,6 @@
->orWhereNot('surname', 'Lannister')
->get();

ray($results);

expect($results->count())->toBe(27);
});

Expand All @@ -661,3 +659,79 @@

expect($results->count())->toBe(27);
});

test('whereLike', function () {
$query = \DB::table('houses')
->whereLike('en.coat-of-arms', '%dragon%');

$binds = $query->getBindings();
$bindKeys = array_keys($binds);

$this->assertSame(
'FOR houseDoc IN houses FILTER LOWER(`houseDoc`.`en`.`coat-of-arms`) LIKE LOWER(@' . $bindKeys[0]
. ') RETURN houseDoc',
$query->toSql(),
);

$results = $query->get();
expect($results->count())->toBe(1);
expect(($results->first())->name)->toBe('Targaryen');
});

test('orWhereLike', function () {
$query = \DB::table('houses')
->where('name', 'Stark')
->orWhereLike('en.coat-of-arms', '%dragon%');

$binds = $query->getBindings();
$bindKeys = array_keys($binds);

$this->assertSame(
'FOR houseDoc IN houses FILTER `houseDoc`.`name` == @' . $bindKeys[0]
. ' or LOWER(`houseDoc`.`en`.`coat-of-arms`) LIKE LOWER(@' . $bindKeys[1]
. ') RETURN houseDoc',
$query->toSql(),
);

$results = $query->get();
expect($results->count())->toBe(2);
expect(($results->first())->name)->toBe('Stark');
});

test('whereNotLike', function () {
$query = \DB::table('houses')
->whereNotLike('en.coat-of-arms', '%dragon%');

$binds = $query->getBindings();
$bindKeys = array_keys($binds);

$this->assertSame(
'FOR houseDoc IN houses FILTER NOT (LOWER(`houseDoc`.`en`.`coat-of-arms`) LIKE LOWER(@' . $bindKeys[0]
. ')) RETURN houseDoc',
$query->toSql(),
);

$results = $query->get();
expect($results->count())->toBe(2);
expect(($results->first())->name)->toBe('Lannister');
});

test('orWhereNotLike', function () {
$query = \DB::table('houses')
->where('name', 'Stark')
->orWhereNotLike('en.coat-of-arms', '%dragon%');

$binds = $query->getBindings();
$bindKeys = array_keys($binds);

$this->assertSame(
'FOR houseDoc IN houses FILTER `houseDoc`.`name` == @' . $bindKeys[0]
. ' or NOT (LOWER(`houseDoc`.`en`.`coat-of-arms`) LIKE LOWER(@' . $bindKeys[1]
. ')) RETURN houseDoc',
$query->toSql(),
);

$results = $query->get();
expect($results->count())->toBe(2);
expect(($results->first())->name)->toBe('Lannister');
});

0 comments on commit 8b405eb

Please sign in to comment.