Skip to content

Commit

Permalink
feat: Add whereLike clause to query builder (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
yajra authored Aug 1, 2024
1 parent ba684a3 commit e330e12
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Oci8/Query/Grammars/OracleGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,26 @@ protected function compileUnionSelectFromDual(array $values): string
return 'select '.$values.' from dual';
})->implode(' union all ');
}

/**
* Compile a "where like" clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereLike(Builder $query, $where)
{
$where['operator'] = $where['not'] ? 'not like' : 'like';

if ($where['caseSensitive']) {
return $this->whereBasic($query, $where);
}

$value = $this->parameter($where['value']);

$operator = str_replace('?', '??', $where['operator']);

return 'upper('.$this->wrap($where['column']).') '.$operator.' upper('.$value.')';
}
}
38 changes: 38 additions & 0 deletions tests/Database/DatabaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yajra\Oci8\Tests\Database;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Orchestra\Testbench\TestCase;

abstract class DatabaseTestCase extends TestCase
{
use DatabaseMigrations;

/**
* The current database driver.
*
* @return string
*/
protected string $driver = 'oracle';

protected function setUp(): void
{
$this->beforeApplicationDestroyed(function () {
foreach (array_keys($this->app['db']->getConnections()) as $name) {
$this->app['db']->purge($name);
}
});

parent::setUp();
}

protected function defineEnvironment($app): void
{
$connection = $app['config']->get('database.default');

$this->driver = $app['config']->get("database.connections.$connection.driver");
}
}
33 changes: 33 additions & 0 deletions tests/Database/Oci8QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3136,6 +3136,39 @@ public function testRandomOrder()
$this->assertEquals([0 => 'foo'], $builder->getBindings());
}

public function testWhereLikeClause()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereLike('id', '1', true);
$this->assertSame('select * from "USERS" where "ID" like ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereLike('id', '1', false);
$this->assertSame('select * from "USERS" where upper("ID") like upper(?)', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereLike('id', '1', true);
$this->assertSame('select * from "USERS" where "ID" like ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotLike('id', '1');
$this->assertSame('select * from "USERS" where upper("ID") not like upper(?)', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotLike('id', '1', false);
$this->assertSame('select * from "USERS" where upper("ID") not like upper(?)', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotLike('id', '1', true);
$this->assertSame('select * from "USERS" where "ID" not like ?', $builder->toSql());
$this->assertEquals([0 => '1'], $builder->getBindings());
}

protected function getConnection()
{
$connection = m::mock(ConnectionInterface::class);
Expand Down
100 changes: 100 additions & 0 deletions tests/Database/QueryBuilderWhereLikeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Yajra\Oci8\Tests\Database;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class QueryBuilderWhereLikeTest extends DatabaseTestCase
{
public function testWhereLike()
{
$users = DB::table('users')->whereLike('email', 'john.doe@example.com')->get();
$this->assertCount(1, $users);
$this->assertSame('John.Doe@example.com', $users[0]->email);

$this->assertSame(4, DB::table('users')->whereNotLike('email', 'john.doe@example.com')->count());
}

public function testWhereLikeWithPercentWildcard()
{
$this->assertSame(5, DB::table('users')->whereLike('email', '%@example.com')->count());
$this->assertSame(2, DB::table('users')->whereNotLike('email', '%Doe%')->count());

$users = DB::table('users')->whereLike('email', 'john%')->get();
$this->assertCount(1, $users);
$this->assertSame('John.Doe@example.com', $users[0]->email);
}

public function testWhereLikeWithUnderscoreWildcard()
{
$users = DB::table('users')->whereLike('email', '_a_e_%@example.com')->get();
$this->assertCount(2, $users);
$this->assertSame('janedoe@example.com', $users[0]->email);
$this->assertSame('Dale.Doe@example.com', $users[1]->email);
}

public function testWhereLikeCaseSensitive()
{
$users = DB::table('users')->whereLike('email', 'john.doe@example.com', true)->get();
$this->assertCount(0, $users);

$users = DB::table('users')->whereLike('email', 'tim.smith@example.com', true)->get();
$this->assertCount(1, $users);
$this->assertSame('tim.smith@example.com', $users[0]->email);
$this->assertSame(5, DB::table('users')->whereNotLike('email', 'john.doe@example.com', true)->count());
}

public function testWhereLikeWithPercentWildcardCaseSensitive()
{
$this->assertSame(2, DB::table('users')->whereLike('email', '%Doe@example.com', true)->count());
$this->assertSame(4, DB::table('users')->whereNotLike('email', '%smith%', true)->count());

$users = DB::table('users')->whereLike('email', '%Doe@example.com', true)->get();
$this->assertCount(2, $users);
$this->assertSame('John.Doe@example.com', $users[0]->email);
$this->assertSame('Dale.Doe@example.com', $users[1]->email);
}

public function testWhereLikeWithUnderscoreWildcardCaseSensitive()
{
$users = DB::table('users')->whereLike('email', 'j__edoe@example.com', true)->get();
$this->assertCount(1, $users);
$this->assertSame('janedoe@example.com', $users[0]->email);

$users = DB::table('users')->whereNotLike('email', '%_oe@example.com', true)->get();
$this->assertCount(2, $users);
$this->assertSame('Earl.Smith@example.com', $users[0]->email);
$this->assertSame('tim.smith@example.com', $users[1]->email);
}

protected function afterRefreshingDatabase(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id('id');
$table->string('name', 200);
$table->text('email');
});
}

protected function destroyDatabaseMigrations(): void
{
Schema::drop('users');
}

protected function setUp(): void
{
parent::setUp();

DB::table('users')->insert([
['name' => 'John Doe', 'email' => 'John.Doe@example.com'],
['name' => 'Jane Doe', 'email' => 'janedoe@example.com'],
['name' => 'Dale doe', 'email' => 'Dale.Doe@example.com'],
['name' => 'Earl Smith', 'email' => 'Earl.Smith@example.com'],
['name' => 'tim smith', 'email' => 'tim.smith@example.com'],
]);
}
}

0 comments on commit e330e12

Please sign in to comment.