This commit is contained in:
parent
7e953aa240
commit
df773520d6
@ -122,6 +122,10 @@ class Doctrine_Hydrate
|
||||
* @see Doctrine_Query::* constants
|
||||
*/
|
||||
protected $type = self::SELECT;
|
||||
|
||||
protected $shortAliases = array();
|
||||
|
||||
protected $shortAliasIndexes = array();
|
||||
/**
|
||||
* constructor
|
||||
*
|
||||
@ -133,12 +137,101 @@ class Doctrine_Hydrate
|
||||
$connection = Doctrine_Manager::getInstance()->getCurrentConnection();
|
||||
}
|
||||
$this->conn = $connection;
|
||||
$this->aliasHandler = new Doctrine_Hydrate_Alias();
|
||||
}
|
||||
public function generateNewAlias($alias)
|
||||
{
|
||||
if (isset($this->shortAliases[$alias])) {
|
||||
// generate a new alias
|
||||
$name = substr($alias, 0, 1);
|
||||
$i = ((int) substr($alias, 1));
|
||||
|
||||
if ($i == 0) {
|
||||
$i = 1;
|
||||
}
|
||||
|
||||
$newIndex = ($this->shortAliasIndexes[$name] + $i);
|
||||
|
||||
return $name . $newIndex;
|
||||
}
|
||||
|
||||
return $alias;
|
||||
}
|
||||
|
||||
public function hasAlias($tableName)
|
||||
{
|
||||
return (isset($this->shortAliases[$tableName]));
|
||||
}
|
||||
|
||||
public function getComponentAlias($tableAlias)
|
||||
{
|
||||
if ( ! isset($this->shortAliases[$tableAlias])) {
|
||||
throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias);
|
||||
}
|
||||
return $this->shortAliases[$tableAlias];
|
||||
}
|
||||
|
||||
public function getShortAliasIndex($alias)
|
||||
{
|
||||
if ( ! isset($this->shortAliasIndexes[$alias])) {
|
||||
return 0;
|
||||
}
|
||||
return $this->shortAliasIndexes[$alias];
|
||||
}
|
||||
public function generateShortAlias($componentAlias, $tableName)
|
||||
{
|
||||
$char = strtolower(substr($tableName, 0, 1));
|
||||
|
||||
$alias = $char;
|
||||
|
||||
if ( ! isset($this->shortAliasIndexes[$alias])) {
|
||||
$this->shortAliasIndexes[$alias] = 1;
|
||||
}
|
||||
while (isset($this->shortAliases[$alias])) {
|
||||
$alias = $char . ++$this->shortAliasIndexes[$alias];
|
||||
}
|
||||
|
||||
$this->shortAliases[$alias] = $componentAlias;
|
||||
|
||||
return $alias;
|
||||
}
|
||||
public function getAliases()
|
||||
{
|
||||
return $this->shortAliases;
|
||||
}
|
||||
public function addAlias($tableAlias, $componentAlias)
|
||||
{
|
||||
$this->shortAliases[$tableAlias] = $componentAlias;
|
||||
}
|
||||
/**
|
||||
* getShortAlias
|
||||
* some database such as Oracle need the identifier lengths to be < ~30 chars
|
||||
* hence Doctrine creates as short identifier aliases as possible
|
||||
*
|
||||
* this method is used for the creation of short table aliases, its also
|
||||
* smart enough to check if an alias already exists for given component (componentAlias)
|
||||
*
|
||||
* @param string $componentAlias the alias for the query component to search table alias for
|
||||
* @param string $tableName the table name from which the table alias is being created
|
||||
* @return string the generated / fetched short alias
|
||||
*/
|
||||
public function getShortAlias($componentAlias, $tableName = null)
|
||||
{
|
||||
$alias = array_search($componentAlias, $this->shortAliases);
|
||||
|
||||
if ($alias !== false) {
|
||||
return $alias;
|
||||
}
|
||||
|
||||
if ($tableName === null) {
|
||||
throw new Doctrine_Hydrate_Exception("Couldn't get short alias for " . $componentAlias);
|
||||
}
|
||||
|
||||
return $this->generateShortAlias($componentAlias, $tableName);
|
||||
}
|
||||
|
||||
public function getTableAlias($componentAlias)
|
||||
{
|
||||
return $this->aliasHandler->getShortAlias($componentAlias);
|
||||
return $this->getShortAlias($componentAlias);
|
||||
}
|
||||
public function addQueryPart($name, $part)
|
||||
{
|
||||
@ -174,7 +267,7 @@ class Doctrine_Hydrate
|
||||
*/
|
||||
public function copyAliases($query)
|
||||
{
|
||||
$this->aliasHandler = $query->aliasHandler;
|
||||
$this->shortAliases = $query->shortAliases;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -252,7 +345,6 @@ class Doctrine_Hydrate
|
||||
'offset' => false,
|
||||
);
|
||||
$this->inheritanceApplied = false;
|
||||
$this->aliasHandler->clear();
|
||||
}
|
||||
/**
|
||||
* getConnection
|
||||
@ -399,7 +491,7 @@ class Doctrine_Hydrate
|
||||
if (empty($row)) {
|
||||
continue;
|
||||
}
|
||||
$alias = $this->aliasHandler->getComponentAlias($tableAlias);
|
||||
$alias = $this->getComponentAlias($tableAlias);
|
||||
$map = $this->_aliasMap[$alias];
|
||||
|
||||
// initialize previous row array if not set
|
||||
|
@ -1,136 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* $Id$
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.phpdoctrine.com>.
|
||||
*/
|
||||
/**
|
||||
* Doctrine_Hydrate_Alias
|
||||
* This class handles the creation of aliases for components in DQL query
|
||||
*
|
||||
* @package Doctrine
|
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
||||
* @category Object Relational Mapping
|
||||
* @link www.phpdoctrine.com
|
||||
* @since 1.0
|
||||
* @version $Revision$
|
||||
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
||||
*/
|
||||
class Doctrine_Hydrate_Alias
|
||||
{
|
||||
|
||||
protected $shortAliases = array();
|
||||
|
||||
protected $shortAliasIndexes = array();
|
||||
|
||||
public function clear()
|
||||
{
|
||||
$this->shortAliases = array();
|
||||
$this->shortAliasIndexes = array();
|
||||
}
|
||||
|
||||
public function generateNewAlias($alias)
|
||||
{
|
||||
if (isset($this->shortAliases[$alias])) {
|
||||
// generate a new alias
|
||||
$name = substr($alias, 0, 1);
|
||||
$i = ((int) substr($alias, 1));
|
||||
|
||||
if ($i == 0) {
|
||||
$i = 1;
|
||||
}
|
||||
|
||||
$newIndex = ($this->shortAliasIndexes[$name] + $i);
|
||||
|
||||
return $name . $newIndex;
|
||||
}
|
||||
|
||||
return $alias;
|
||||
}
|
||||
|
||||
public function hasAlias($tableName)
|
||||
{
|
||||
return (isset($this->shortAliases[$tableName]));
|
||||
}
|
||||
|
||||
public function getComponentAlias($tableAlias)
|
||||
{
|
||||
if ( ! isset($this->shortAliases[$tableAlias])) {
|
||||
throw new Doctrine_Hydrate_Exception('Unknown table alias ' . $tableAlias);
|
||||
}
|
||||
return $this->shortAliases[$tableAlias];
|
||||
}
|
||||
|
||||
public function getShortAliasIndex($alias)
|
||||
{
|
||||
if ( ! isset($this->shortAliasIndexes[$alias])) {
|
||||
return 0;
|
||||
}
|
||||
return $this->shortAliasIndexes[$alias];
|
||||
}
|
||||
public function generateShortAlias($componentAlias, $tableName)
|
||||
{
|
||||
$char = strtolower(substr($tableName, 0, 1));
|
||||
|
||||
$alias = $char;
|
||||
|
||||
if ( ! isset($this->shortAliasIndexes[$alias])) {
|
||||
$this->shortAliasIndexes[$alias] = 1;
|
||||
}
|
||||
while (isset($this->shortAliases[$alias])) {
|
||||
$alias = $char . ++$this->shortAliasIndexes[$alias];
|
||||
}
|
||||
|
||||
$this->shortAliases[$alias] = $componentAlias;
|
||||
|
||||
return $alias;
|
||||
}
|
||||
public function getAliases()
|
||||
{
|
||||
return $this->shortAliases;
|
||||
}
|
||||
public function addAlias($tableAlias, $componentAlias)
|
||||
{
|
||||
$this->shortAliases[$tableAlias] = $componentAlias;
|
||||
}
|
||||
/**
|
||||
* getShortAlias
|
||||
* some database such as Oracle need the identifier lengths to be < ~30 chars
|
||||
* hence Doctrine creates as short identifier aliases as possible
|
||||
*
|
||||
* this method is used for the creation of short table aliases, its also
|
||||
* smart enough to check if an alias already exists for given component (componentAlias)
|
||||
*
|
||||
* @param string $componentAlias the alias for the query component to search table alias for
|
||||
* @param string $tableName the table name from which the table alias is being created
|
||||
* @return string the generated / fetched short alias
|
||||
*/
|
||||
public function getShortAlias($componentAlias, $tableName = null)
|
||||
{
|
||||
$alias = array_search($componentAlias, $this->shortAliases);
|
||||
|
||||
if ($alias !== false) {
|
||||
return $alias;
|
||||
}
|
||||
|
||||
if ($tableName === null) {
|
||||
throw new Doctrine_Hydrate_Exception("Couldn't get short alias for " . $componentAlias);
|
||||
}
|
||||
|
||||
return $this->generateShortAlias($componentAlias, $tableName);
|
||||
}
|
||||
}
|
@ -653,7 +653,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
break;
|
||||
}
|
||||
|
||||
$field = $this->aliasHandler->getShortAlias($rootAlias) . '.' . $table->getIdentifier();
|
||||
$field = $this->getShortAlias($rootAlias) . '.' . $table->getIdentifier();
|
||||
|
||||
// only append the subquery if it actually contains something
|
||||
if ($subquery !== '') {
|
||||
@ -704,7 +704,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
$componentAlias = key($this->_aliasMap);
|
||||
|
||||
// get short alias
|
||||
$alias = $this->aliasHandler->getShortAlias($componentAlias);
|
||||
$alias = $this->getShortAlias($componentAlias);
|
||||
$primaryKey = $alias . '.' . $table->getIdentifier();
|
||||
|
||||
// initialize the base of the subquery
|
||||
@ -727,7 +727,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
|
||||
foreach ($this->parts['from'] as $part) {
|
||||
// preserve LEFT JOINs only if needed
|
||||
if (substr($part,0,9) === 'LEFT JOIN') {
|
||||
if (substr($part, 0, 9) === 'LEFT JOIN') {
|
||||
$e = explode(' ', $part);
|
||||
|
||||
if ( ! in_array($e[3], $this->subqueryAliases) &&
|
||||
@ -755,8 +755,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
continue;
|
||||
}
|
||||
|
||||
if($this->aliasHandler->hasAlias($part)) {
|
||||
$parts[$k] = $this->aliasHandler->generateNewAlias($part);
|
||||
if($this->hasAlias($part)) {
|
||||
$parts[$k] = $this->generateNewAlias($part);
|
||||
}
|
||||
|
||||
if(strpos($part, '.') !== false) {
|
||||
@ -765,7 +765,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
$trimmed = ltrim($e[0], '( ');
|
||||
$pos = strpos($e[0], $trimmed);
|
||||
|
||||
$e[0] = substr($e[0], 0, $pos) . $this->aliasHandler->generateNewAlias($trimmed);
|
||||
$e[0] = substr($e[0], 0, $pos) . $this->generateNewAlias($trimmed);
|
||||
$parts[$k] = implode('.', $e);
|
||||
}
|
||||
}
|
||||
@ -1070,7 +1070,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
$tableName = $table->getTableName();
|
||||
|
||||
// get the short alias for this table
|
||||
$tableAlias = $this->aliasHandler->getShortAlias($componentAlias, $tableName);
|
||||
$tableAlias = $this->getShortAlias($componentAlias, $tableName);
|
||||
// quote table name
|
||||
$queryPart = $this->conn->quoteIdentifier($tableName);
|
||||
|
||||
@ -1115,7 +1115,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
$table = $map['table'];
|
||||
|
||||
// build the query base
|
||||
$q = 'SELECT COUNT(DISTINCT ' . $this->aliasHandler->getShortAlias($table->getTableName())
|
||||
$q = 'SELECT COUNT(DISTINCT ' . $this->getShortAlias($table->getTableName())
|
||||
. '.' . $table->getIdentifier()
|
||||
. ') FROM ' . $this->buildFromPart();
|
||||
|
||||
@ -1153,20 +1153,4 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
|
||||
|
||||
return $this->execute($params);
|
||||
}
|
||||
/**
|
||||
* getShortAlias
|
||||
* some database such as Oracle need the identifier lengths to be < ~30 chars
|
||||
* hence Doctrine creates as short identifier aliases as possible
|
||||
*
|
||||
* this method is used for the creation of short table aliases, its also
|
||||
* smart enough to check if an alias already exists for given component (componentAlias)
|
||||
*
|
||||
* @param string $componentAlias the alias for the query component to search table alias for
|
||||
* @param string $tableName the table name from which the table alias is being created
|
||||
* @return string the generated / fetched short alias
|
||||
*/
|
||||
public function getShortAlias($componentAlias, $tableName)
|
||||
{
|
||||
return $this->aliasHandler->getShortAlias($componentAlias, $tableName);
|
||||
}
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
|
||||
throw new Doctrine_RawSql_Exception('All selected fields in Sql query must be in format tableAlias.fieldName');
|
||||
}
|
||||
// try to auto-add component
|
||||
if ( ! $this->aliasHandler->hasAlias($e[0])) {
|
||||
if ( ! $this->hasAlias($e[0])) {
|
||||
try {
|
||||
$this->addComponent($e[0], ucwords($e[0]));
|
||||
} catch(Doctrine_Exception $exception) {
|
||||
@ -144,7 +144,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
|
||||
}
|
||||
|
||||
if ($e[1] == '*') {
|
||||
$componentAlias = $this->aliasHandler->getComponentAlias($e[0]);
|
||||
$componentAlias = $this->getComponentAlias($e[0]);
|
||||
|
||||
foreach ($this->_aliasMap[$componentAlias]['table']->getColumnNames() as $name) {
|
||||
$field = $e[0] . '.' . $name;
|
||||
@ -158,7 +158,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
|
||||
|
||||
// force-add all primary key fields
|
||||
|
||||
foreach ($this->aliasHandler->getAliases() as $tableAlias => $componentAlias) {
|
||||
foreach ($this->getAliases() as $tableAlias => $componentAlias) {
|
||||
$map = $this->_aliasMap[$componentAlias];
|
||||
|
||||
foreach ($map['table']->getPrimaryKeys() as $key) {
|
||||
@ -257,7 +257,7 @@ class Doctrine_RawSql extends Doctrine_Query_Abstract
|
||||
'parent' => $parent,
|
||||
'relation' => $relation);
|
||||
}
|
||||
$this->aliasHandler->addAlias($tableAlias, $componentAlias);
|
||||
$this->addAlias($tableAlias, $componentAlias);
|
||||
|
||||
$parent = $currPath;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user