RepositoryQuery: Pass through the query when requiring a table or column

This allows now to adjust the query in custom repository implementations.

refs #8826
This commit is contained in:
Johannes Meyer 2015-05-28 13:25:26 +02:00
parent 23b7ab0764
commit 647dd9d425
3 changed files with 42 additions and 32 deletions

View File

@ -323,13 +323,15 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
/**
* Validate that the requested table exists
*
* @param string $table
* @param string $table The table to validate
* @param RepositoryQuery $query An optional query to pass as context
* (unused by the base implementation)
*
* @return string The table's name, with the table prefix being prepended
*
* @throws ProgrammingError In case the given table does not exist
* @throws ProgrammingError In case the given table does not exist
*/
public function requireTable($table)
public function requireTable($table, RepositoryQuery $query = null)
{
$statementColumns = $this->getStatementColumns();
if (! isset($statementColumns[$table])) {
@ -345,12 +347,14 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
* In case of a PostgreSQL connection, this applies LOWER() on the column and strtolower()
* on the value if a COLLATE SQL-instruction is part of the resolved column.
*
* @param string $table
* @param Filter $filter
* @param string $table The table being filtered
* @param Filter $filter The filter to recurse
* @param RepositoryQuery $query An optional query to pass as context
* (Directly passed through to $this->requireFilterColumn)
*/
public function requireFilter($table, Filter $filter)
public function requireFilter($table, Filter $filter, RepositoryQuery $query = null)
{
parent::requireFilter($table, $filter);
parent::requireFilter($table, $filter, $query);
if ($filter->isExpression()) {
$column = $filter->getColumn();

View File

@ -592,13 +592,15 @@ abstract class Repository implements Selectable
/**
* Validate that the requested table exists
*
* @param string $table
* @param string $table The table to validate
* @param RepositoryQuery $query An optional query to pass as context
* (unused by the base implementation)
*
* @return string The table's name, may differ from the given one
* @return string The table's name, may differ from the given one
*
* @throws ProgrammingError In case the given table does not exist
* @throws ProgrammingError In case the given table does not exist
*/
public function requireTable($table)
public function requireTable($table, RepositoryQuery $query = null)
{
$queryColumns = $this->getQueryColumns();
if (! isset($queryColumns[$table])) {
@ -611,18 +613,20 @@ abstract class Repository implements Selectable
/**
* Recurse the given filter, require each column for the given table and convert all values
*
* @param string $table
* @param Filter $filter
* @param string $table The table being filtered
* @param Filter $filter The filter to recurse
* @param RepositoryQuery $query An optional query to pass as context
* (Directly passed through to $this->requireFilterColumn)
*/
public function requireFilter($table, Filter $filter)
public function requireFilter($table, Filter $filter, RepositoryQuery $query = null)
{
if ($filter->isExpression()) {
$column = $filter->getColumn();
$filter->setColumn($this->requireFilterColumn($table, $column));
$filter->setColumn($this->requireFilterColumn($table, $column, $query));
$filter->setExpression($this->persistColumn($column, $filter->getExpression()));
} elseif ($filter->isChain()) {
foreach ($filter->filters() as $chainOrExpression) {
$this->requireFilter($table, $chainOrExpression);
$this->requireFilter($table, $chainOrExpression, $query);
}
}
}
@ -707,14 +711,15 @@ abstract class Repository implements Selectable
/**
* Validate that the given column is a valid query target and return it or the actual name if it's an alias
*
* @param string $table The table where to look for the column or alias
* @param string $name The name or alias of the column to validate
* @param string $table The table where to look for the column or alias
* @param string $name The name or alias of the column to validate
* @param RepositoryQuery $query An optional query to pass as context (unused by the base implementation)
*
* @return string The given column's name
* @return string The given column's name
*
* @throws QueryException In case the given column is not a valid query column
* @throws QueryException In case the given column is not a valid query column
*/
public function requireQueryColumn($table, $name)
public function requireQueryColumn($table, $name, RepositoryQuery $query = null)
{
if (in_array($name, $this->getFilterColumns())) {
throw new QueryException(t('Filter column "%s" cannot be queried'), $name);
@ -748,14 +753,15 @@ abstract class Repository implements Selectable
/**
* Validate that the given column is a valid filter target and return it or the actual name if it's an alias
*
* @param string $table The table where to look for the column or alias
* @param string $name The name or alias of the column to validate
* @param string $table The table where to look for the column or alias
* @param string $name The name or alias of the column to validate
* @param RepositoryQuery $query An optional query to pass as context (unused by the base implementation)
*
* @return string The given column's name
* @return string The given column's name
*
* @throws QueryException In case the given column is not a valid filter column
* @throws QueryException In case the given column is not a valid filter column
*/
public function requireFilterColumn($table, $name)
public function requireFilterColumn($table, $name, RepositoryQuery $query = null)
{
if (($column = $this->resolveQueryColumnAlias($table, $name)) === null) {
throw new QueryException(t('Filter column "%s" not found'), $name);

View File

@ -76,7 +76,7 @@ class RepositoryQuery implements QueryInterface, Iterator
*/
public function from($target, array $columns = null)
{
$target = $this->repository->requireTable($target);
$target = $this->repository->requireTable($target, $this);
$this->query = $this->repository
->getDataSource()
->select()
@ -127,7 +127,7 @@ class RepositoryQuery implements QueryInterface, Iterator
} else {
$columns = array();
foreach ($desiredColumns as $customAlias => $columnAlias) {
$resolvedColumn = $this->repository->requireQueryColumn($target, $columnAlias);
$resolvedColumn = $this->repository->requireQueryColumn($target, $columnAlias, $this);
if ($resolvedColumn !== $columnAlias) {
$columns[is_string($customAlias) ? $customAlias : $columnAlias] = $resolvedColumn;
} elseif (is_string($customAlias)) {
@ -154,7 +154,7 @@ class RepositoryQuery implements QueryInterface, Iterator
public function where($column, $value = null)
{
$this->query->where(
$this->repository->requireFilterColumn($this->target, $column),
$this->repository->requireFilterColumn($this->target, $column, $this),
$this->repository->persistColumn($column, $value)
);
return $this;
@ -186,7 +186,7 @@ class RepositoryQuery implements QueryInterface, Iterator
public function setFilter(Filter $filter)
{
$filter = clone $filter;
$this->repository->requireFilter($this->target, $filter);
$this->repository->requireFilter($this->target, $filter, $this);
$this->query->setFilter($filter);
return $this;
}
@ -203,7 +203,7 @@ class RepositoryQuery implements QueryInterface, Iterator
public function addFilter(Filter $filter)
{
$filter = clone $filter;
$this->repository->requireFilter($this->target, $filter);
$this->repository->requireFilter($this->target, $filter, $this);
$this->query->addFilter($filter);
return $this;
}
@ -268,7 +268,7 @@ class RepositoryQuery implements QueryInterface, Iterator
try {
$this->query->order(
$this->repository->requireFilterColumn($this->target, $column),
$this->repository->requireFilterColumn($this->target, $column, $this),
$specificDirection ?: $baseDirection
// I would have liked the following solution, but hey, a coder should be allowed to produce crap...
// $specificDirection && (! $direction || $column !== $field) ? $specificDirection : $baseDirection