Skip to content

Commit

Permalink
Remove deprecated features (filters)
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Mar 26, 2024
1 parent 8a82ae5 commit 133b914
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 289 deletions.
84 changes: 0 additions & 84 deletions src/Dumper/Config/Definition/Table/Filter.php

This file was deleted.

36 changes: 0 additions & 36 deletions src/Dumper/Config/Definition/TableConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Smile\GdprDump\Dumper\Config\Definition;

use Smile\GdprDump\Dumper\Config\Definition\Table\Filter;
use Smile\GdprDump\Dumper\Config\Definition\Table\SortOrder;
use Smile\GdprDump\Dumper\Config\Validation\ValidationException;
use Smile\GdprDump\Dumper\Config\Validation\WhereExprValidator;
Expand All @@ -18,12 +17,6 @@ class TableConfig
private array $converters = [];
private string $skipCondition = '';

/**
* @deprecated
* @var Filter[]
*/
private array $filters = [];

/**
* @var SortOrder[]
*/
Expand All @@ -43,17 +36,6 @@ public function getName(): string
return $this->name;
}

/**
* Get the filters.
*
* @deprecated
* @return Filter[]
*/
public function getFilters(): array
{
return $this->filters;
}

/**
* Get the where condition.
*/
Expand Down Expand Up @@ -88,16 +70,6 @@ public function getConverters(): array
return $this->converters;
}

/**
* Check if there is data to filter.
*
* @deprecated
*/
public function hasFilter(): bool
{
return !empty($this->filters);
}

/**
* Check if there is data to filter (with `where` param).
*/
Expand Down Expand Up @@ -149,14 +121,6 @@ private function prepareConfig(array $tableData): void
*/
private function prepareFilters(array $tableData): void
{
// Old way of declaring table filters (`filters` parameter)
if (isset($tableData['filters'])) {
foreach ($tableData['filters'] as $filter) {
$this->filters[] = new Filter((string) $filter[0], (string) $filter[1], $filter[2] ?? null);
}
}

// New way of declaring table filters (`where` parameter)
$whereCondition = (string) ($tableData['where'] ?? '');
if ($whereCondition !== '') {
$whereExprValidator = new WhereExprValidator();
Expand Down
2 changes: 1 addition & 1 deletion src/Dumper/Config/DumperConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private function prepareTableSettings(ConfigInterface $config): void
$this->tablesToSort[] = $tableConfig->getName();
}

if ($tableConfig->hasWhereCondition() || $tableConfig->hasFilter() || $tableConfig->hasLimit()) {
if ($tableConfig->hasWhereCondition() || $tableConfig->hasLimit()) {
$this->tablesToFilter[] = $tableConfig->getName();
}
}
Expand Down
91 changes: 0 additions & 91 deletions src/Dumper/Listener/TableFilterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use RuntimeException;
use Smile\GdprDump\Database\Metadata\Definition\Constraint\ForeignKey;
use Smile\GdprDump\Database\Metadata\MetadataInterface;
use Smile\GdprDump\Database\TableDependencyResolver;
use Smile\GdprDump\Dumper\Config\Definition\Table\Filter;
use Smile\GdprDump\Dumper\Config\Definition\TableConfig;
use Smile\GdprDump\Dumper\Config\DumperConfig;
use Smile\GdprDump\Dumper\Event\DumpEvent;
use UnexpectedValueException;
Expand Down Expand Up @@ -183,9 +180,6 @@ private function createQueryBuilder(string $tableName): QueryBuilder
return $queryBuilder;
}

// Apply where condition (old deprecated syntax with `filters` parameter)
$this->applyFiltersToQueryBuilder($queryBuilder, $tableConfig);

// Apply where condition (wrap the condition with brackets to prevent SQL injection)
if ($tableConfig->hasWhereCondition()) {
$queryBuilder->andWhere(sprintf('(%s)', $tableConfig->getWhereCondition()));
Expand All @@ -207,31 +201,6 @@ private function createQueryBuilder(string $tableName): QueryBuilder
return $queryBuilder;
}

/**
* Apply filters to a query builder.
*
* @deprecated
* @throws RuntimeException
*/
private function applyFiltersToQueryBuilder(QueryBuilder $queryBuilder, TableConfig $tableConfig): void
{
// Apply filters (deprecated)
foreach ($tableConfig->getFilters() as $filter) {
$value = $this->getFilterValue($filter);
$callable = [$queryBuilder->expr(), $filter->getOperator()];

// Filter operators must match the method names of the Doctrine expression builder
if (!is_callable($callable)) {
throw new RuntimeException(
sprintf('The doctrine expression builder does not implement "%s".', $filter->getOperator())
);
}

$whereExpr = call_user_func_array($callable, [$this->getFilterColumn($filter), $value]);
$queryBuilder->andWhere($whereExpr);
}
}

/**
* Get the SQL query that represents a list of columns.
*/
Expand Down Expand Up @@ -265,64 +234,4 @@ private function getWhereSql(QueryBuilder $queryBuilder): string

return substr($sql, strpos($sql, ' WHERE ') + 7);
}

/**
* Get a filter column.
* If it was prefixed with `expr:`, returns the raw SQL statement instead of a quoted identifier.
*
* @deprecated
*/
private function getFilterColumn(Filter $filter): string
{
$column = $filter->getColumn();

return str_starts_with($column, 'expr:') ?
ltrim(substr($column, 5)) : $this->connection->quoteIdentifier($column);
}

/**
* Get a filter value.
* If it was prefixed with `expr:`, returns the raw SQL statement instead of a quoted value.
*
* @deprecated
* @throws UnexpectedValueException
*/
private function getFilterValue(Filter $filter): mixed
{
$value = $filter->getValue();

if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->quoteValue($v);
}

return $value;
}

return $this->quoteValue($value);
}

/**
* Quote a value so that it can be safely injected in a SQL query
* (we can't use query params because Mysqldump library doesn't allow it).
*
* @deprecated
* @throws UnexpectedValueException
*/
private function quoteValue(mixed $value): mixed
{
if ($value !== null && !is_scalar($value)) {
throw new UnexpectedValueException('Non-scalar values can not be used in filters.');
}

if (is_bool($value)) {
return (int) $value;
}

if (is_string($value)) {
return str_starts_with($value, 'expr:') ? ltrim(substr($value, 5)) : $this->connection->quote($value);
}

return $value;
}
}
3 changes: 1 addition & 2 deletions tests/functional/Resources/config/templates/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ tables_blacklist:
tables:
stores:
order_by: 'code'
filters:
- ['store_id', 'in', [1, 2]]
where: 'store_id in (1,2)'

customers:
where: 'email like "%@test.org" and created_at > date_sub(now(), interval 55 day)'
Expand Down
54 changes: 0 additions & 54 deletions tests/unit/Dumper/Config/Definition/Table/FilterTest.php

This file was deleted.

21 changes: 0 additions & 21 deletions tests/unit/Dumper/Config/Definition/TableConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Smile\GdprDump\Tests\Unit\Dumper\Config\Definition;

use Smile\GdprDump\Dumper\Config\Definition\Table\Filter;
use Smile\GdprDump\Dumper\Config\Definition\TableConfig;
use Smile\GdprDump\Dumper\Config\Validation\ValidationException;
use Smile\GdprDump\Tests\Unit\TestCase;
Expand All @@ -23,11 +22,9 @@ public function testEmptyData(): void
$this->assertEmpty($config->getConverters());

$this->assertFalse($config->hasLimit());
$this->assertFalse($config->hasFilter());
$this->assertFalse($config->hasSortOrder());

$this->assertNull($config->getLimit());
$this->assertEmpty($config->getFilters());
$this->assertEmpty($config->getSortOrders());
}

Expand Down Expand Up @@ -111,24 +108,6 @@ public function testWhereConditionWithUnmatchedClosingBracket(): void
]);
}

/**
* Test the "filters" parameter.
*
* @deprecated
*/
public function testFilter(): void
{
$config = new TableConfig('table1', [
'filters' => [
['column1', Filter::OPERATOR_IS_NULL],
['column2', Filter::OPERATOR_EQ, 'value'],
],
]);

$this->assertCount(2, $config->getFilters());
$this->assertTrue($config->hasFilter());
}

/**
* Test the "limit" parameter.
*/
Expand Down

0 comments on commit 133b914

Please sign in to comment.