Skip to content

Commit

Permalink
Allow expressions in filter columns
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayet committed Feb 8, 2024
1 parent ea038b0 commit 745b178
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/config/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ tables:
filters:
- ['created_at', 'gt', 'expr: date_sub(now(), interval 60 day)']

# Dump only CMS pages without "FAQ" in their meta title
cms_page:
filters:
- ["expr: COALESCE(meta_title, '')", 'notLike', '%FAQ%']

# Anonymize a table named "my_custom_table"
my_custom_table:
converters:
Expand Down
16 changes: 15 additions & 1 deletion src/Dumper/Mysql/TableFilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private function applyTableConfigToQueryBuilder(QueryBuilder $queryBuilder, Tabl

$whereExpr = call_user_func_array(
$callable,
[$this->connection->quoteIdentifier($filter->getColumn()), $value]
[$this->getFilterColumn($filter), $value]
);

$queryBuilder->andWhere($whereExpr);
Expand Down Expand Up @@ -303,4 +303,18 @@ private function quoteValue(mixed $value): mixed

return $value;
}

/**
* Get a filter column so it can be safely injected in SQL query
* Extracts a possible expression to use instead of a real column name.
*
* @param Filter $filter Table filter
*/
private function getFilterColumn(Filter $filter): mixed
{
$column = $filter->getColumn();

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

0 comments on commit 745b178

Please sign in to comment.