Commit fa2da84b authored by zYne's avatar zYne

--no commit message

--no commit message
parent 86fa70d6
...@@ -151,6 +151,10 @@ class Doctrine_Hydrate2 ...@@ -151,6 +151,10 @@ class Doctrine_Hydrate2
{ {
$this->tableAliases = $aliases; $this->tableAliases = $aliases;
} }
public function getTableAlias($componentAlias)
{
return $this->aliasHandler->getShortAlias($componentAlias);
}
/** /**
* copyAliases * copyAliases
* *
...@@ -195,7 +199,7 @@ class Doctrine_Hydrate2 ...@@ -195,7 +199,7 @@ class Doctrine_Hydrate2
if ( ! isset($this->parts[$part])) { if ( ! isset($this->parts[$part])) {
throw new Doctrine_Hydrate_Exception('Unknown query part ' . $part); throw new Doctrine_Hydrate_Exception('Unknown query part ' . $part);
} }
return $this->parts[$part]; return $this->parts[$part];
} }
/** /**
......
...@@ -140,12 +140,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -140,12 +140,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
public function processPendingFields($componentAlias) public function processPendingFields($componentAlias)
{ {
$tableAlias = $this->getTableAlias($componentAlias); $tableAlias = $this->getTableAlias($componentAlias);
$table = $this->_aliasMap[$componentAlias]['table'];
if ( ! isset($this->tables[$tableAlias])) {
throw new Doctrine_Query_Exception('Unknown component alias ' . $componentAlias);
}
$table = $this->tables[$tableAlias];
if (isset($this->pendingFields[$componentAlias])) { if (isset($this->pendingFields[$componentAlias])) {
$fields = $this->pendingFields[$componentAlias]; $fields = $this->pendingFields[$componentAlias];
...@@ -642,13 +637,14 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -642,13 +637,14 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
*/ */
public function getQuery($params = array()) public function getQuery($params = array())
{ {
if(empty($this->parts["select"]) || empty($this->parts["from"])) if (empty($this->parts['select']) || empty($this->parts['from'])) {
return false; return false;
}
$needsSubQuery = false; $needsSubQuery = false;
$subquery = ''; $subquery = '';
$k = array_keys($this->tables); $k = array_keys($this->_aliasMap);
$table = $this->tables[$k[0]]; $table = $this->_aliasMap[$k[0]]['table'];
if( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) { if( ! empty($this->parts['limit']) && $this->needsSubquery && $table->getAttribute(Doctrine::ATTR_QUERY_LIMIT) == Doctrine::LIMIT_RECORDS) {
$needsSubQuery = true; $needsSubQuery = true;
...@@ -661,63 +657,63 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -661,63 +657,63 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
// build the basic query // build the basic query
$str = ''; $str = '';
if($this->isDistinct()) if($this->isDistinct()) {
$str = 'DISTINCT '; $str = 'DISTINCT ';
}
$q = $this->getQueryBase(); $q = $this->getQueryBase();
$q .= array_shift($this->parts['from']);
print $q;
foreach ($this->parts['from'] as $part) {
$q .= $this->parts['from']; // preserve LEFT JOINs only if needed
foreach($this->parts['join'] as $parts) { if (substr($part, 0, 9) === 'LEFT JOIN') {
foreach($parts as $part) { $e = explode(' ', $part);
// preserve LEFT JOINs only if needed
if(substr($part, 0,9) === 'LEFT JOIN') { $aliases = array_merge($this->subqueryAliases,
$e = explode(' ', $part); array_keys($this->neededTables));
$aliases = array_merge($this->subqueryAliases,
array_keys($this->neededTables));
if( ! in_array($e[3], $aliases) &&
! in_array($e[2], $aliases) &&
if( ! in_array($e[3], $aliases) && ! empty($this->pendingFields)) {
! in_array($e[2], $aliases) && continue;
}
! empty($this->pendingFields)) { }
continue;
}
} $e = explode(' ON ', $part);
// we can always be sure that the first join condition exists
$e2 = explode(' AND ', $e[1]);
$e = explode(' ON ', $part); $part = $e[0] . ' ON ' . array_shift($e2);
// we can always be sure that the first join condition exists
$e2 = explode(' AND ', $e[1]);
$part = $e[0] . ' ON '
. array_shift($e2);
if( ! empty($e2)) {
$parser = new Doctrine_Query_JoinCondition($this);
$part .= ' AND ' . $parser->parse(implode(' AND ', $e2));
}
$q .= ' ' . $part; if ( ! empty($e2)) {
$parser = new Doctrine_Query_JoinCondition($this);
$part .= ' AND ' . $parser->parse(implode(' AND ', $e2));
} }
$q .= ' ' . $part;
} }
if( ! empty($this->parts['set'])) {
if ( ! empty($this->parts['set'])) {
$q .= ' SET ' . implode(', ', $this->parts['set']); $q .= ' SET ' . implode(', ', $this->parts['set']);
} }
$string = $this->applyInheritance(); $string = $this->applyInheritance();
if( ! empty($string)) if ( ! empty($string)) {
$this->parts['where'][] = '('.$string.')'; $this->parts['where'][] = '(' . $string . ')';
}
$modifyLimit = true; $modifyLimit = true;
if( ! empty($this->parts["limit"]) || ! empty($this->parts["offset"])) { if ( ! empty($this->parts["limit"]) || ! empty($this->parts["offset"])) {
if($needsSubQuery) { if($needsSubQuery) {
$subquery = $this->getLimitSubquery(); $subquery = $this->getLimitSubquery();
...@@ -750,15 +746,17 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -750,15 +746,17 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']):''; $q .= ( ! empty($this->parts['having']))? ' HAVING ' . implode(' AND ', $this->parts['having']):'';
$q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']):''; $q .= ( ! empty($this->parts['orderby']))? ' ORDER BY ' . implode(', ', $this->parts['orderby']):'';
if($modifyLimit) if ($modifyLimit) {
$q = $this->conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']); $q = $this->conn->modifyLimitQuery($q, $this->parts['limit'], $this->parts['offset']);
}
// return to the previous state // return to the previous state
if( ! empty($string)) if ( ! empty($string)) {
array_pop($this->parts['where']); array_pop($this->parts['where']);
if($needsSubQuery) }
if ($needsSubQuery) {
array_shift($this->parts['where']); array_shift($this->parts['where']);
}
return $q; return $q;
} }
/** /**
...@@ -1047,22 +1045,40 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1047,22 +1045,40 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$tmp = explode(' ', $path); $tmp = explode(' ', $path);
$componentAlias = (count($tmp) > 1) ? end($tmp) : false; $componentAlias = (count($tmp) > 1) ? end($tmp) : false;
$e = preg_split("/[.:]/", $tmp[0], -1); $e = preg_split("/[.:]/", $tmp[0], -1);
$fullPath = $tmp[0];
$prevPath = '';
if (isset($this->_aliasMap[$e[0]])) { if (isset($this->_aliasMap[$e[0]])) {
$table = $this->_aliasMap[$e[0]]['table']; $table = $this->_aliasMap[$e[0]]['table'];
$parent = array_shift($e); $prevPath = $parent = array_shift($e);
} }
foreach ($e as $key => $name) { foreach ($e as $key => $name) {
// build the current component path
$prevPath = ($prevPath) ? $prevPath . '.' . $name : $name;
// if an alias is not given use the current path as an alias identifier
if ($prevPath !== $fullPath || ! $componentAlias) {
$componentAlias = $prevPath;
}
// load fields if necessary
if ($loadFields) {
$this->pendingFields[$componentAlias] = array('*');
}
if ( ! isset($table)) { if ( ! isset($table)) {
// process the root of the path // process the root of the path
$table = $this->loadRoot($name, $componentAlias); $table = $this->loadRoot($name, $componentAlias);
} else { } else {
$relation = $table->getRelation($name); $relation = $table->getRelation($name);
$this->_aliasMap[$componentAlias] = array('parent' => $parent, $this->_aliasMap[$componentAlias] = array('table' => $relation->getTable(),
'parent' => $parent,
'relation' => $relation); 'relation' => $relation);
if( ! $relation->isOneToOne()) { if( ! $relation->isOneToOne()) {
$this->needsSubquery = true; $this->needsSubquery = true;
...@@ -1070,15 +1086,15 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1070,15 +1086,15 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$localAlias = $this->getShortAlias($parent, $table->getTableName()); $localAlias = $this->getShortAlias($parent, $table->getTableName());
$foreignAlias = $this->getShortAlias($componentAlias, $relation->getTable()->getTableName()); $foreignAlias = $this->getShortAlias($componentAlias, $relation->getTable()->getTableName());
$aliasString = $this->conn->quoteIdentifier($table->getTableName()) . ' AS ' . $localAlias; $localSql = $this->conn->quoteIdentifier($table->getTableName()) . ' ' . $localAlias;
$foreignSql = $this->conn->quoteIdentifier($relation->getTable()->getTableName()) . ' ' . $foreignAlias;
$map = $relation->getTable()->inheritanceMap; $map = $relation->getTable()->inheritanceMap;
if( ! $loadFields || ! empty($map) || $joinCondition) { if ( ! $loadFields || ! empty($map) || $joinCondition) {
$this->subqueryAliases[] = $foreignAlias; $this->subqueryAliases[] = $foreignAlias;
} }
if ($relation instanceof Doctrine_Relation_Association) { if ($relation instanceof Doctrine_Relation_Association) {
$asf = $relation->getAssociationFactory(); $asf = $relation->getAssociationFactory();
...@@ -1087,36 +1103,34 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1087,36 +1103,34 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
if( ! $loadFields || ! empty($map) || $joinCondition) { if( ! $loadFields || ! empty($map) || $joinCondition) {
$this->subqueryAliases[] = $assocTableName; $this->subqueryAliases[] = $assocTableName;
} }
$assocPath = $prevPath . '.' . $asf->getComponentName(); $assocPath = $prevPath . '.' . $asf->getComponentName();
if (isset($this->tableAliases[$assocPath])) { $assocAlias = $this->getShortAlias($assocPath, $asf->getTableName());
$assocAlias = $this->tableAliases[$assocPath];
} else { $queryPart = 'LEFT JOIN ' . $assocTableName . ' ' . $assocAlias . ' ON ' . $localAlias . '.'
$assocAlias = $this->aliasHandler->generateShortAlias($assocTableName);
}
$queryPart = 'LEFT JOIN ' . $assocTableName . ' ' . $assocAlias . ' ON ' . $foreignAlias . '.'
. $table->getIdentifier() . ' = ' . $table->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getLocal(); . $assocAlias . '.' . $relation->getLocal();
if ($relation instanceof Doctrine_Relation_Association_Self) { if ($relation instanceof Doctrine_Relation_Association_Self) {
$queryPart .= ' OR ' . $localAlias . '.' . $table->getIdentifier() . ' = ' $queryPart .= ' OR ' . $localAlias . '.' . $table->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getForeign(); . $assocAlias . '.' . $relation->getForeign();
} }
$queryPart = 'LEFT JOIN ' . $aliasString . ' ON ' . $foreignAlias . '.' $this->parts['from'][] = $queryPart;
. $relation->getTable()->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getForeign() $queryPart = 'LEFT JOIN ' . $foreignSql . ' ON ' . $foreignAlias . '.'
. $joinCondition; . $relation->getTable()->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getForeign()
. $joinCondition;
if ($relation instanceof Doctrine_Relation_Association_Self) { if ($relation instanceof Doctrine_Relation_Association_Self) {
$queryPart .= ' OR ' . $foreignTable . '.' . $table->getIdentifier() . ' = ' $queryPart .= ' OR ' . $foreignTable . '.' . $table->getIdentifier() . ' = '
. $assocAlias . '.' . $relation->getLocal(); . $assocAlias . '.' . $relation->getLocal();
} }
} else { } else {
$queryPart = 'LEFT JOIN ' . $aliasString $queryPart = 'LEFT JOIN ' . $localSql
. ' ON ' . $localAlias . '.' . ' ON ' . $localAlias . '.'
. $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign() . $relation->getLocal() . ' = ' . $foreignAlias . '.' . $relation->getForeign()
. $joinCondition; . $joinCondition;
...@@ -1133,6 +1147,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1133,6 +1147,12 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
} }
} }
} }
/**
* loadRoot
*
* @param string $name
* @param string $componentAlias
*/
public function loadRoot($name, $componentAlias) public function loadRoot($name, $componentAlias)
{ {
// get the connection for the component // get the connection for the component
...@@ -1143,7 +1163,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1143,7 +1163,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
$tableName = $table->getTableName(); $tableName = $table->getTableName();
// get the short alias for this table // get the short alias for this table
$tableAlias = $this->aliasHandler->getShortAlias($tableName); $tableAlias = $this->aliasHandler->getShortAlias($componentAlias, $tableName);
// quote table name // quote table name
$queryPart = $this->conn->quoteIdentifier($tableName); $queryPart = $this->conn->quoteIdentifier($tableName);
...@@ -1171,11 +1191,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable { ...@@ -1171,11 +1191,7 @@ class Doctrine_Query2 extends Doctrine_Hydrate2 implements Countable {
*/ */
public function getShortAlias($componentAlias, $tableName) public function getShortAlias($componentAlias, $tableName)
{ {
if (isset($this->tableAliases[$componentAlias])) { return $this->aliasHandler->getShortAlias($componentAlias, $tableName);
return $this->tableAliases[$componentAlias];
}
return $this->aliasHandler->getShortAlias($tableName);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment