Repository: Handle column name ambiguousness automatically

refs #8826
This commit is contained in:
Johannes Meyer 2015-05-13 13:27:08 +02:00
parent 0a387573f3
commit e9fee2dad6
2 changed files with 153 additions and 40 deletions

View File

@ -252,19 +252,66 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
*/ */
protected function initializeStatementMaps() protected function initializeStatementMaps()
{ {
$this->statementTableMap = array();
$this->statementColumnMap = array();
foreach ($this->getStatementColumns() as $table => $columns) { foreach ($this->getStatementColumns() as $table => $columns) {
foreach ($columns as $alias => $column) { foreach ($columns as $alias => $column) {
if (! is_string($alias)) { $key = is_string($alias) ? $alias : $column;
$this->statementTableMap[$column] = $table; if (array_key_exists($key, $this->statementTableMap)) {
$this->statementColumnMap[$column] = $column; if ($this->statementTableMap[$key] !== null) {
$existingTable = $this->statementTableMap[$key];
$existingColumn = $this->statementColumnMap[$key];
$this->statementTableMap[$existingTable . '.' . $key] = $existingTable;
$this->statementColumnMap[$existingTable . '.' . $key] = $existingColumn;
$this->statementTableMap[$key] = null;
$this->statementColumnMap[$key] = null;
}
$this->statementTableMap[$table . '.' . $key] = $table;
$this->statementColumnMap[$table . '.' . $key] = $column;
} else { } else {
$this->statementTableMap[$alias] = $table; $this->statementTableMap[$key] = $table;
$this->statementColumnMap[$alias] = $column; $this->statementColumnMap[$key] = $column;
} }
} }
} }
} }
/**
* Return this repository's query columns of the given table mapped to their respective aliases
*
* @param mixed $table
*
* @return array
*
* @throws ProgrammingError In case $table does not exist
*/
public function requireAllQueryColumns($table)
{
if (is_array($table)) {
$table = array_shift($table);
}
return parent::requireAllQueryColumns($this->removeTablePrefix($table));
}
/**
* Return the query column name for the given alias or null in case the alias does not exist
*
* @param mixed $table
* @param string $alias
*
* @return string|null
*/
public function resolveQueryColumnAlias($table, $alias)
{
if (is_array($table)) {
$table = array_shift($table);
}
return parent::resolveQueryColumnAlias($this->removeTablePrefix($table), $alias);
}
/** /**
* Return whether the given query column name or alias is available in the given table * Return whether the given query column name or alias is available in the given table
* *
@ -283,21 +330,51 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
} }
/** /**
* Return whether the given statement column name or alias is available in the given table * Return the statement column name for the given alias or null in case the alias does not exist
* *
* @param mixed $table * @param mixed $table
* @param string $column * @param string $alias
*
* @return string|null
*/
public function resolveStatementColumnAlias($table, $alias)
{
if (is_array($table)) {
$table = array_shift($table);
}
$statementColumnMap = $this->getStatementColumnMap();
if (isset($statementColumnMap[$alias])) {
return $statementColumnMap[$alias];
}
$prefixedAlias = $table . '.' . $alias;
if (isset($statementColumnMap[$prefixedAlias])) {
return $statementColumnMap[$prefixedAlias];
}
}
/**
* Return whether the given alias or statement column name is available in the given table
*
* @param mixed $table
* @param string $alias
* *
* @return bool * @return bool
*/ */
public function validateStatementColumnAssociation($table, $column) public function validateStatementColumnAssociation($table, $alias)
{ {
if (is_array($table)) { if (is_array($table)) {
$table = array_shift($table); $table = array_shift($table);
} }
$statementTableMap = $this->getStatementTableMap(); $statementTableMap = $this->getStatementTableMap();
return $statementTableMap[$column] === $this->removeTablePrefix($table); if (isset($statementTableMap[$alias])) {
return $statementTableMap[$alias] === $this->removeTablePrefix($table);
}
$prefixedAlias = $this->removeTablePrefix($table) . '.' . $alias;
return isset($statementTableMap[$prefixedAlias]);
} }
/** /**
@ -310,9 +387,8 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
*/ */
public function hasStatementColumn($table, $name) public function hasStatementColumn($table, $name)
{ {
$statementColumnMap = $this->getStatementColumnMap();
if ( if (
! array_key_exists($name, $statementColumnMap) $this->resolveStatementColumnAlias($table, $name) === null
|| !$this->validateStatementColumnAssociation($table, $name) || !$this->validateStatementColumnAssociation($table, $name)
) { ) {
return parent::hasStatementColumn($table, $name); return parent::hasStatementColumn($table, $name);
@ -333,8 +409,7 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
*/ */
public function requireStatementColumn($table, $name) public function requireStatementColumn($table, $name)
{ {
$statementColumnMap = $this->getStatementColumnMap(); if (($column = $this->resolveStatementColumnAlias($table, $name)) === null) {
if (! array_key_exists($name, $statementColumnMap)) {
return parent::requireStatementColumn($table, $name); return parent::requireStatementColumn($table, $name);
} }
@ -342,6 +417,6 @@ abstract class DbRepository extends Repository implements Extensible, Updatable,
throw new StatementException('Statement column "%s" not found in table "%s"', $name, $table); throw new StatementException('Statement column "%s" not found in table "%s"', $name, $table);
} }
return $statementColumnMap[$name]; return $column;
} }
} }

View File

@ -362,11 +362,27 @@ abstract class Repository implements Selectable
foreach ($queryColumns as $table => $columns) { foreach ($queryColumns as $table => $columns) {
foreach ($columns as $alias => $column) { foreach ($columns as $alias => $column) {
if (! is_string($alias)) { if (! is_string($alias)) {
$this->aliasTableMap[$column] = $table; $key = $column;
$this->aliasColumnMap[$column] = $column;
} else { } else {
$this->aliasTableMap[$alias] = $table; $key = $alias;
$this->aliasColumnMap[$alias] = preg_replace('~\n\s*~', ' ', $column); $column = preg_replace('~\n\s*~', ' ', $column);
}
if (array_key_exists($key, $this->aliasTableMap)) {
if ($this->aliasTableMap[$key] !== null) {
$existingTable = $this->aliasTableMap[$key];
$existingColumn = $this->aliasColumnMap[$key];
$this->aliasTableMap[$existingTable . '.' . $key] = $existingTable;
$this->aliasColumnMap[$existingTable . '.' . $key] = $existingColumn;
$this->aliasTableMap[$key] = null;
$this->aliasColumnMap[$key] = null;
}
$this->aliasTableMap[$table . '.' . $key] = $table;
$this->aliasColumnMap[$table . '.' . $key] = $column;
} else {
$this->aliasTableMap[$key] = $table;
$this->aliasColumnMap[$key] = $column;
} }
} }
} }
@ -598,32 +614,57 @@ abstract class Repository implements Selectable
* @param string $table * @param string $table
* *
* @return array * @return array
*
* @throws ProgrammingError In case $table does not exist
*/ */
public function requireAllQueryColumns($table) public function requireAllQueryColumns($table)
{ {
$map = array(); $queryColumns = $this->getQueryColumns();
foreach ($this->getAliasColumnMap() as $alias => $_) { if (! array_key_exists($table, $queryColumns)) {
if ($this->hasQueryColumn($table, $alias)) { throw new ProgrammingError('Table name "%s" not found', $table);
// Just in case $this->requireQueryColumn has been overwritten and there is some magic going on
$map[$alias] = $this->requireQueryColumn($table, $alias);
}
} }
return $map; return $queryColumns[$table];
} }
/** /**
* Return whether the given query column name or alias is available in the given table * Return the query column name for the given alias or null in case the alias does not exist
* *
* @param string $table * @param string $table
* @param string $column * @param string $alias
*
* @return string|null
*/
public function resolveQueryColumnAlias($table, $alias)
{
$aliasColumnMap = $this->getAliasColumnMap();
if (isset($aliasColumnMap[$alias])) {
return $aliasColumnMap[$alias];
}
$prefixedAlias = $table . '.' . $alias;
if (isset($aliasColumnMap[$prefixedAlias])) {
return $aliasColumnMap[$prefixedAlias];
}
}
/**
* Return whether the given alias or query column name is available in the given table
*
* @param string $table
* @param string $alias
* *
* @return bool * @return bool
*/ */
public function validateQueryColumnAssociation($table, $column) public function validateQueryColumnAssociation($table, $alias)
{ {
$aliasTableMap = $this->getAliasTableMap(); $aliasTableMap = $this->getAliasTableMap();
return $aliasTableMap[$column] === $table; if (isset($aliasTableMap[$alias])) {
return $aliasTableMap[$alias] === $table;
}
$prefixedAlias = $table . '.' . $alias;
return isset($aliasTableMap[$prefixedAlias]);
} }
/** /**
@ -640,7 +681,7 @@ abstract class Repository implements Selectable
return false; return false;
} }
return array_key_exists($name, $this->getAliasColumnMap()) return $this->resolveQueryColumnAlias($table, $name) !== null
&& $this->validateQueryColumnAssociation($table, $name); && $this->validateQueryColumnAssociation($table, $name);
} }
@ -660,8 +701,7 @@ abstract class Repository implements Selectable
throw new QueryException(t('Filter column "%s" cannot be queried'), $name); throw new QueryException(t('Filter column "%s" cannot be queried'), $name);
} }
$aliasColumnMap = $this->getAliasColumnMap(); if (($column = $this->resolveQueryColumnAlias($table, $name)) === null) {
if (! array_key_exists($name, $aliasColumnMap)) {
throw new QueryException(t('Query column "%s" not found'), $name); throw new QueryException(t('Query column "%s" not found'), $name);
} }
@ -669,7 +709,7 @@ abstract class Repository implements Selectable
throw new QueryException(t('Query column "%s" not found in table "%s"'), $name, $table); throw new QueryException(t('Query column "%s" not found in table "%s"'), $name, $table);
} }
return $aliasColumnMap[$name]; return $column;
} }
/** /**
@ -682,7 +722,7 @@ abstract class Repository implements Selectable
*/ */
public function hasFilterColumn($table, $name) public function hasFilterColumn($table, $name)
{ {
return array_key_exists($name, $this->getAliasColumnMap()) return $this->resolveQueryColumnAlias($table, $name) !== null
&& $this->validateQueryColumnAssociation($table, $name); && $this->validateQueryColumnAssociation($table, $name);
} }
@ -698,8 +738,7 @@ abstract class Repository implements Selectable
*/ */
public function requireFilterColumn($table, $name) public function requireFilterColumn($table, $name)
{ {
$aliasColumnMap = $this->getAliasColumnMap(); if (($column = $this->resolveQueryColumnAlias($table, $name)) === null) {
if (! array_key_exists($name, $aliasColumnMap)) {
throw new QueryException(t('Filter column "%s" not found'), $name); throw new QueryException(t('Filter column "%s" not found'), $name);
} }
@ -707,7 +746,7 @@ abstract class Repository implements Selectable
throw new QueryException(t('Filter column "%s" not found in table "%s"'), $name, $table); throw new QueryException(t('Filter column "%s" not found in table "%s"'), $name, $table);
} }
return $aliasColumnMap[$name]; return $column;
} }
/** /**
@ -739,8 +778,7 @@ abstract class Repository implements Selectable
throw new StatementException('Filter column "%s" cannot be referenced in a statement', $name); throw new StatementException('Filter column "%s" cannot be referenced in a statement', $name);
} }
$aliasColumnMap = $this->getAliasColumnMap(); if (($column = $this->resolveQueryColumnAlias($table, $name)) === null) {
if (! array_key_exists($name, $aliasColumnMap)) {
throw new StatementException('Statement column "%s" not found', $name); throw new StatementException('Statement column "%s" not found', $name);
} }
@ -748,7 +786,7 @@ abstract class Repository implements Selectable
throw new StatementException('Statement column "%s" not found in table "%s"', $name, $table); throw new StatementException('Statement column "%s" not found in table "%s"', $name, $table);
} }
return $aliasColumnMap[$name]; return $column;
} }
/** /**