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:
parent
23b7ab0764
commit
647dd9d425
|
@ -323,13 +323,15 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
|
||||||
/**
|
/**
|
||||||
* Validate that the requested table exists
|
* 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
|
* @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();
|
$statementColumns = $this->getStatementColumns();
|
||||||
if (! isset($statementColumns[$table])) {
|
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()
|
* 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.
|
* on the value if a COLLATE SQL-instruction is part of the resolved column.
|
||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table The table being filtered
|
||||||
* @param Filter $filter
|
* @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()) {
|
if ($filter->isExpression()) {
|
||||||
$column = $filter->getColumn();
|
$column = $filter->getColumn();
|
||||||
|
|
|
@ -592,13 +592,15 @@ abstract class Repository implements Selectable
|
||||||
/**
|
/**
|
||||||
* Validate that the requested table exists
|
* 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();
|
$queryColumns = $this->getQueryColumns();
|
||||||
if (! isset($queryColumns[$table])) {
|
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
|
* Recurse the given filter, require each column for the given table and convert all values
|
||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table The table being filtered
|
||||||
* @param Filter $filter
|
* @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()) {
|
if ($filter->isExpression()) {
|
||||||
$column = $filter->getColumn();
|
$column = $filter->getColumn();
|
||||||
$filter->setColumn($this->requireFilterColumn($table, $column));
|
$filter->setColumn($this->requireFilterColumn($table, $column, $query));
|
||||||
$filter->setExpression($this->persistColumn($column, $filter->getExpression()));
|
$filter->setExpression($this->persistColumn($column, $filter->getExpression()));
|
||||||
} elseif ($filter->isChain()) {
|
} elseif ($filter->isChain()) {
|
||||||
foreach ($filter->filters() as $chainOrExpression) {
|
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
|
* 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 $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 $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())) {
|
if (in_array($name, $this->getFilterColumns())) {
|
||||||
throw new QueryException(t('Filter column "%s" cannot be queried'), $name);
|
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
|
* 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 $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 $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) {
|
if (($column = $this->resolveQueryColumnAlias($table, $name)) === null) {
|
||||||
throw new QueryException(t('Filter column "%s" not found'), $name);
|
throw new QueryException(t('Filter column "%s" not found'), $name);
|
||||||
|
|
|
@ -76,7 +76,7 @@ class RepositoryQuery implements QueryInterface, Iterator
|
||||||
*/
|
*/
|
||||||
public function from($target, array $columns = null)
|
public function from($target, array $columns = null)
|
||||||
{
|
{
|
||||||
$target = $this->repository->requireTable($target);
|
$target = $this->repository->requireTable($target, $this);
|
||||||
$this->query = $this->repository
|
$this->query = $this->repository
|
||||||
->getDataSource()
|
->getDataSource()
|
||||||
->select()
|
->select()
|
||||||
|
@ -127,7 +127,7 @@ class RepositoryQuery implements QueryInterface, Iterator
|
||||||
} else {
|
} else {
|
||||||
$columns = array();
|
$columns = array();
|
||||||
foreach ($desiredColumns as $customAlias => $columnAlias) {
|
foreach ($desiredColumns as $customAlias => $columnAlias) {
|
||||||
$resolvedColumn = $this->repository->requireQueryColumn($target, $columnAlias);
|
$resolvedColumn = $this->repository->requireQueryColumn($target, $columnAlias, $this);
|
||||||
if ($resolvedColumn !== $columnAlias) {
|
if ($resolvedColumn !== $columnAlias) {
|
||||||
$columns[is_string($customAlias) ? $customAlias : $columnAlias] = $resolvedColumn;
|
$columns[is_string($customAlias) ? $customAlias : $columnAlias] = $resolvedColumn;
|
||||||
} elseif (is_string($customAlias)) {
|
} elseif (is_string($customAlias)) {
|
||||||
|
@ -154,7 +154,7 @@ class RepositoryQuery implements QueryInterface, Iterator
|
||||||
public function where($column, $value = null)
|
public function where($column, $value = null)
|
||||||
{
|
{
|
||||||
$this->query->where(
|
$this->query->where(
|
||||||
$this->repository->requireFilterColumn($this->target, $column),
|
$this->repository->requireFilterColumn($this->target, $column, $this),
|
||||||
$this->repository->persistColumn($column, $value)
|
$this->repository->persistColumn($column, $value)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
@ -186,7 +186,7 @@ class RepositoryQuery implements QueryInterface, Iterator
|
||||||
public function setFilter(Filter $filter)
|
public function setFilter(Filter $filter)
|
||||||
{
|
{
|
||||||
$filter = clone $filter;
|
$filter = clone $filter;
|
||||||
$this->repository->requireFilter($this->target, $filter);
|
$this->repository->requireFilter($this->target, $filter, $this);
|
||||||
$this->query->setFilter($filter);
|
$this->query->setFilter($filter);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -203,7 +203,7 @@ class RepositoryQuery implements QueryInterface, Iterator
|
||||||
public function addFilter(Filter $filter)
|
public function addFilter(Filter $filter)
|
||||||
{
|
{
|
||||||
$filter = clone $filter;
|
$filter = clone $filter;
|
||||||
$this->repository->requireFilter($this->target, $filter);
|
$this->repository->requireFilter($this->target, $filter, $this);
|
||||||
$this->query->addFilter($filter);
|
$this->query->addFilter($filter);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -268,7 +268,7 @@ class RepositoryQuery implements QueryInterface, Iterator
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->query->order(
|
$this->query->order(
|
||||||
$this->repository->requireFilterColumn($this->target, $column),
|
$this->repository->requireFilterColumn($this->target, $column, $this),
|
||||||
$specificDirection ?: $baseDirection
|
$specificDirection ?: $baseDirection
|
||||||
// I would have liked the following solution, but hey, a coder should be allowed to produce crap...
|
// I would have liked the following solution, but hey, a coder should be allowed to produce crap...
|
||||||
// $specificDirection && (! $direction || $column !== $field) ? $specificDirection : $baseDirection
|
// $specificDirection && (! $direction || $column !== $field) ? $specificDirection : $baseDirection
|
||||||
|
|
Loading…
Reference in New Issue