-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\Exception; | ||
|
||
use InvalidArgumentException; | ||
use Throwable; | ||
|
||
final class NotSupportedFilterOptionException extends InvalidArgumentException | ||
{ | ||
/** | ||
* @param string $optionName Option name in filter. | ||
* @param string $driverName Driver name of database. | ||
*/ | ||
public function __construct(string $optionName, string $driverName, int $code = 0, ?Throwable $previous = null) | ||
{ | ||
parent::__construct("\$$optionName option is not supported when using $driverName driver.", $code, $previous); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\FilterHandler\LikeFilterHandler; | ||
|
||
use Yiisoft\Data\Reader\Filter\Like; | ||
use Yiisoft\Data\Reader\FilterHandlerInterface; | ||
|
||
abstract class BaseLikeFilterHandler implements FilterHandlerInterface | ||
{ | ||
protected array $escapingReplacements = [ | ||
'%' => '\%', | ||
'_' => '\_', | ||
'\\' => '\\\\', | ||
]; | ||
|
||
public function getFilterClass(): string | ||
{ | ||
return Like::class; | ||
} | ||
|
||
protected function prepareValue(string $value, bool $escape = false): string | ||
{ | ||
return '%' . strtr($value, $this->escapingReplacements) . '%'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\FilterHandler\LikeFilterHandler; | ||
|
||
use RuntimeException; | ||
use Yiisoft\Data\Reader\FilterHandlerInterface; | ||
|
||
class LikeFilterHandlerFactory | ||
{ | ||
public static function getLikeHandler(string $driverName): FilterHandlerInterface | ||
{ | ||
// default - ignored due to the complexity of testing and preventing splitting of databaseDriver argument. | ||
// @codeCoverageIgnoreStart | ||
return match ($driverName) { | ||
'sqlite' => new SqliteLikeFilterHandler(), | ||
'mysql' => new MysqlLikeFilterHandler(), | ||
'pgsql' => new PostgresLikeFilterHandler(), | ||
'sqlsrv' => new MssqlLikeFilterHandler(), | ||
'oci' => new OracleLikeFilterHandler(), | ||
default => throw new RuntimeException("$driverName database driver is not supported."), | ||
}; | ||
// @codeCoverageIgnoreEnd | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\FilterHandler\LikeFilterHandler; | ||
|
||
use Yiisoft\Data\Db\Exception\NotSupportedFilterOptionException; | ||
use Yiisoft\Data\Db\FilterHandler\Context; | ||
use Yiisoft\Data\Db\FilterHandler\Criteria; | ||
use Yiisoft\Data\Db\FilterHandler\QueryFilterHandlerInterface; | ||
use Yiisoft\Data\Reader\FilterInterface; | ||
|
||
final class MssqlLikeFilterHandler extends BaseLikeFilterHandler implements QueryFilterHandlerInterface | ||
{ | ||
public function __construct() | ||
{ | ||
unset($this->escapingReplacements['\\']); | ||
} | ||
|
||
public function getCriteria(FilterInterface $filter, Context $context): ?Criteria | ||
{ | ||
if ($filter->isCaseSensitive() === true) { | ||
Check failure on line 22 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 22 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 22 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestUndefinedInterfaceMethod
|
||
throw new NotSupportedFilterOptionException(optionName: 'caseSensitive', driverName: 'sqlsrv'); | ||
} | ||
|
||
return new Criteria(['LIKE', $filter->getField(), $this->prepareValue($filter->getValue())]); | ||
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestMixedArgument
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedArgument
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedArgument
Check failure on line 26 in src/FilterHandler/LikeFilterHandler/MssqlLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestUndefinedInterfaceMethod
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\FilterHandler\LikeFilterHandler; | ||
|
||
use Yiisoft\Data\Db\FilterHandler\Context; | ||
use Yiisoft\Data\Db\FilterHandler\Criteria; | ||
use Yiisoft\Data\Db\FilterHandler\QueryFilterHandlerInterface; | ||
use Yiisoft\Data\Reader\Filter\Like; | ||
use Yiisoft\Data\Reader\FilterInterface; | ||
|
||
final class MysqlLikeFilterHandler extends BaseLikeFilterHandler implements QueryFilterHandlerInterface | ||
{ | ||
public function getCriteria(FilterInterface $filter, Context $context): ?Criteria | ||
{ | ||
/** @var Like $filter */ | ||
|
||
if ($filter->isCaseSensitive() !== true) { | ||
return new Criteria(['LIKE', $filter->getField(), $this->prepareValue($filter->getValue())]); | ||
} | ||
|
||
return new Criteria([ | ||
'LIKE BINARY', | ||
$filter->getField(), | ||
$this->prepareValue($filter->getValue(), escape: true), | ||
]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\FilterHandler\LikeFilterHandler; | ||
|
||
use Yiisoft\Data\Db\FilterHandler\Context; | ||
use Yiisoft\Data\Db\FilterHandler\Criteria; | ||
use Yiisoft\Data\Db\FilterHandler\QueryFilterHandlerInterface; | ||
use Yiisoft\Data\Reader\FilterInterface; | ||
|
||
final class OracleLikeFilterHandler extends BaseLikeFilterHandler implements QueryFilterHandlerInterface | ||
{ | ||
public function getCriteria(FilterInterface $filter, Context $context): ?Criteria | ||
{ | ||
return new Criteria(['LIKE', $filter->getField(), $this->prepareValue($filter->getValue())]); | ||
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestMixedArgument
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestMixedArgument
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestMixedArgument
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/OracleLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestUndefinedInterfaceMethod
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\FilterHandler\LikeFilterHandler; | ||
|
||
use Yiisoft\Data\Db\FilterHandler\Context; | ||
use Yiisoft\Data\Db\FilterHandler\Criteria; | ||
use Yiisoft\Data\Db\FilterHandler\QueryFilterHandlerInterface; | ||
use Yiisoft\Data\Reader\FilterInterface; | ||
|
||
final class PostgresLikeFilterHandler extends BaseLikeFilterHandler implements QueryFilterHandlerInterface | ||
{ | ||
public function getCriteria(FilterInterface $filter, Context $context): ?Criteria | ||
{ | ||
if ($filter->isCaseSensitive() !== true) { | ||
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/PostgresLikeFilterHandler.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/PostgresLikeFilterHandler.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestUndefinedInterfaceMethod
Check failure on line 16 in src/FilterHandler/LikeFilterHandler/PostgresLikeFilterHandler.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestUndefinedInterfaceMethod
|
||
return new Criteria(['ILIKE', $filter->getField(), $this->prepareValue($filter->getValue(), escape: true)]); | ||
} | ||
|
||
return new Criteria(['LIKE', $filter->getField(), $this->prepareValue($filter->getValue())]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Data\Db\FilterHandler\LikeFilterHandler; | ||
|
||
use Yiisoft\Data\Db\Exception\NotSupportedFilterOptionException; | ||
use Yiisoft\Data\Db\FilterHandler\Context; | ||
use Yiisoft\Data\Db\FilterHandler\Criteria; | ||
use Yiisoft\Data\Db\FilterHandler\QueryFilterHandlerInterface; | ||
use Yiisoft\Data\Reader\FilterInterface; | ||
|
||
final class SqliteLikeFilterHandler extends BaseLikeFilterHandler implements QueryFilterHandlerInterface | ||
{ | ||
public function __construct() | ||
{ | ||
unset($this->escapingReplacements['\\']); | ||
} | ||
|
||
public function getCriteria(FilterInterface $filter, Context $context): ?Criteria | ||
{ | ||
if ($filter->isCaseSensitive() === true) { | ||
throw new NotSupportedFilterOptionException(optionName: 'caseSensitive', driverName: 'sqlite'); | ||
} | ||
|
||
return new Criteria(['LIKE', $filter->getField(), $this->prepareValue($filter->getValue())]); | ||
} | ||
} |