Skip to content

Commit

Permalink
Automatic update
Browse files Browse the repository at this point in the history
  • Loading branch information
xosofox committed Apr 6, 2011
1 parent 47d94b9 commit 21fe17a
Show file tree
Hide file tree
Showing 4 changed files with 365 additions and 15 deletions.
73 changes: 72 additions & 1 deletion runtime/lib/query/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author Eric Dobbs <eric@dobbse.net> (Torque)
* @author Henning P. Schmiedehausen <hps@intermeta.de> (Torque)
* @author Sam Joseph <sam@neurogrid.com> (Torque)
* @version $Revision: 2200 $
* @version $Revision: 2246 $
* @package propel.runtime.query
*/
class Criteria implements IteratorAggregate
Expand Down Expand Up @@ -182,6 +182,7 @@ class Criteria implements IteratorAggregate
* @var array
*/
protected $joins = array();
protected $selectQueries = array();

/**
* The name of the database.
Expand Down Expand Up @@ -284,6 +285,7 @@ public function clear()
$this->having = null;
$this->asColumns = array();
$this->joins = array();
$this->selectQueries = array();
$this->dbName = $this->originalDbName;
$this->offset = 0;
$this->limit = -1;
Expand Down Expand Up @@ -948,6 +950,75 @@ public function getJoins()
{
return $this->joins;
}

/**
* Adds a Criteria as subQuery in the From Clause.
*
* @param Criteria $subQueryCriteria Criteria to build the subquery from
* @param string $alias alias for the subQuery
*
* @return Criteria this modified Criteria object (Fluid API)
*/
public function addSelectQuery(Criteria $subQueryCriteria, $alias = null)
{
if (null === $alias) {
$alias = 'alias_' . ($subQueryCriteria->forgeSelectQueryAlias() + count($this->selectQueries));
}
$this->selectQueries[$alias] = $subQueryCriteria;

return $this;
}

/**
* Checks whether this Criteria has a subquery.
*
* @return Boolean
*/
public function hasSelectQueries()
{
return (bool) $this->selectQueries;
}

/**
* Get the associative array of Criteria for the subQueries per alias.
*
* @return array Criteria[]
*/
public function getSelectQueries()
{
return $this->selectQueries;
}

/**
* Get the Criteria for a specific subQuery.
*
* @param string $alias alias for the subQuery
* @return Criteria
*/
public function getSelectQuery($alias)
{
return $this->selectQueries[$alias];
}

/**
* checks if the Criteria for a specific subQuery is set.
*
* @param string $alias alias for the subQuery
* @return boolean
*/
public function hasSelectQuery($alias)
{
return isset($this->selectQueries[$alias]);
}

public function forgeSelectQueryAlias()
{
$aliasNumber = 0;
foreach ($this->getSelectQueries() as $c1) {
$aliasNumber += $c1->forgeSelectQueryAlias();
}
return ++$aliasNumber;
}

/**
* Adds "ALL" modifier to the SQL statement.
Expand Down
75 changes: 63 additions & 12 deletions runtime/lib/query/ModelCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @method ModelCriteria innerJoin($relation) Adds a INNER JOIN clause to the query
*
* @author François Zaninotto
* @version $Revision: 2200 $
* @version $Revision: 2245 $
* @package propel.runtime.query
*/
class ModelCriteria extends Criteria
Expand Down Expand Up @@ -950,7 +950,37 @@ public function getPrimaryCriteria()
{
return $this->primaryCriteria;
}


/**
* Adds a Criteria as subQuery in the From Clause.
*
* @see Criteria::addSelectQuery()
*
* @param Criteria $subQueryCriteria Criteria to build the subquery from
* @param string $alias alias for the subQuery
* @param boolean $addAliasAndSelectColumns Set to false if you want to manually add the aliased select columns
*
* @return ModelCriteria The current object, for fluid interface
*/
public function addSelectQuery(Criteria $subQueryCriteria, $alias = null, $addAliasAndSelectColumns = true)
{
if (!$subQueryCriteria->hasSelectClause()) {
$subQueryCriteria->addSelfSelectColumns();
}
parent::addSelectQuery($subQueryCriteria, $alias);
if ($addAliasAndSelectColumns) {
// give this query-model same alias as subquery
if (null === $alias) {
end($this->selectQueries);
$alias = key($this->selectQueries);
}
$this->setModelAlias($alias, true);
// so we can add selfSelectColumns
$this->addSelfSelectColumns();
}
return $this;
}

/**
* Adds the select columns for a the current table
*
Expand Down Expand Up @@ -1846,12 +1876,12 @@ protected function getColumnFromName($phpName, $failSilently = true)
} elseif (isset($this->joins[$class])) {
// column of a relations's model
$tableMap = $this->joins[$class]->getTableMap();
} elseif ($this->hasSelectQuery($class)) {
return $this->getColumnFromSubQuery($class, $phpName, $failSilently);
} elseif ($failSilently) {
return array(null, null);
} else {
if ($failSilently) {
return array(null, null);
} else {
throw new PropelException('Unknown model or alias ' . $class);
}
throw new PropelException(sprintf('Unknown model or alias "%"', $class));
}

if ($tableMap->hasColumnByPhpName($phpName)) {
Expand All @@ -1866,15 +1896,36 @@ protected function getColumnFromName($phpName, $failSilently = true)
} elseif (isset($this->asColumns[$phpName])) {
// aliased column
return array(null, $phpName);
} elseif ($failSilently) {
return array(null, null);
} else {
if ($failSilently) {
return array(null, null);
} else {
throw new PropelException('Unknown column ' . $phpName . ' on model or alias ' . $class);
}
throw new PropelException(sprintf('Unknown column "%s" on model or alias "%s"', $phpName, $class));
}
}

/**
* Special case for subquery columns
*
* @return array List($columnMap, $realColumnName)
*/
protected function getColumnFromSubQuery($class, $phpName, $failSilently = true)
{
$subQueryCriteria = $this->getSelectQuery($class);
$tableMap = $subQueryCriteria->getTableMap();
if ($tableMap->hasColumnByPhpName($phpName)) {
$column = $tableMap->getColumnByPhpName($phpName);
$realColumnName = $class . '.' . $column->getName();
return array($column, $realColumnName);
} elseif (isset($subQueryCriteria->asColumns[$phpName])) {
// aliased column
return array(null, $class . '.' . $phpName);
} elseif ($failSilently) {
return array(null, null);
} else {
throw new PropelException(sprintf('Unknown column "%s" in the subQuery with alias "%s"', $phpName, $class));
}
}

/**
* Return a fully qualified column name corresponding to a simple column phpName
* Uses model alias if it exists
Expand Down
23 changes: 21 additions & 2 deletions runtime/lib/util/BasePeer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @author John D. McNally <jmcnally@collab.net> (Torque)
* @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
* @author Stephen Haberman <stephenh@chase3000.com> (Torque)
* @version $Revision: 2078 $
* @version $Revision: 2244 $
* @package propel.runtime.util
*/
class BasePeer
Expand Down Expand Up @@ -779,12 +779,31 @@ public static function createSelectSql(Criteria $criteria, &$params)
$fromClause[] = $criteria->getPrimaryTableName();
}

// tables should not exist as alias of subQuery
if ($criteria->hasSelectQueries()) {
foreach ($fromClause as $key => $ftable) {
if (strpos($ftable, ' ') !== false) {
list($realtable, $tableName) = explode(' ', $ftable);
} else {
$tableName = $ftable;
}
if ($criteria->hasSelectQuery($tableName)) {
unset($fromClause[$key]);
}
}
}

// from / join tables quoted if it is necessary
if ($db->useQuoteIdentifier()) {
$fromClause = array_map(array($db, 'quoteIdentifierTable'), $fromClause);
$joinClause = $joinClause ? $joinClause : array_map(array($db, 'quoteIdentifierTable'), $joinClause);
}


// add subQuery to From after adding quotes
foreach ($criteria->getSelectQueries() as $subQueryAlias => $subQueryCriteria) {
$fromClause[] = '(' . BasePeer::createSelectSql($subQueryCriteria, $params) . ') AS ' . $subQueryAlias;
}

// build from-clause
$from = '';
if (!empty($joinClause) && count($fromClause) > 1) {
Expand Down
Loading

0 comments on commit 21fe17a

Please sign in to comment.