Commit a770f83d authored by zYne's avatar zYne

DQL-to-SQL lazy conversion

parent 4346c9f8
...@@ -71,7 +71,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -71,7 +71,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
*/ */
protected $_dqlParts = array( protected $_dqlParts = array(
'select' => array(), 'select' => array(),
'distinct' => false,
'forUpdate' => false, 'forUpdate' => false,
'from' => array(), 'from' => array(),
'set' => array(), 'set' => array(),
...@@ -99,6 +98,17 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -99,6 +98,17 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
{ {
return new Doctrine_Query($conn); return new Doctrine_Query($conn);
} }
public function reset()
{
$this->_enumParams = array();
$this->_pendingJoinConditions = array();
$this->pendingSubqueries = array();
$this->pendingFields = array();
$this->_neededTables = array();
$this->subqueryAliases = array();
$this->needsSubquery = false;
$this->isLimitSubqueryUsed = false;
}
/** /**
* setOption * setOption
* *
...@@ -266,20 +276,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -266,20 +276,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$this->_dqlParts[$queryPartName] = array($queryPart); $this->_dqlParts[$queryPartName] = array($queryPart);
} }
// check for cache
if ( ! $this->_options['resultSetCache']) {
$parser = $this->getParser($queryPartName);
$sql = $parser->parse($queryPart);
if (isset($sql)) {
if ($append) {
$this->addQueryPart($queryPartName, $sql);
} else {
$this->setQueryPart($queryPartName, $sql);
}
}
}
return $this; return $this;
} }
...@@ -639,16 +635,36 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -639,16 +635,36 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
public function getQuery($params = array()) public function getQuery($params = array())
{ {
// check if parser cache is on // check if parser cache is on
if ($this->_cache) {
$this->_aliasMap = array();
$this->pendingAggregates = array();
$this->aggregateMap = array();
$this->reset();
foreach ($this->_dqlParts as $queryPartName => $queryParts) { foreach ($this->_dqlParts as $queryPartName => $queryParts) {
$this->parts[$queryPartName] = array();
if (is_array($queryParts) && ! empty($queryParts)) { if (is_array($queryParts) && ! empty($queryParts)) {
foreach ($queryParts as $queryPart) { foreach ($queryParts as $queryPart) {
$this->getParser($queryPartName)->parse($queryPart); $parser = $this->getParser($queryPartName);
$sql = $parser->parse($queryPart);
if (isset($sql)) {
if ($queryPartName == 'limit' ||
$queryPartName == 'offset') {
$this->setQueryPart($queryPartName, $sql);
} else {
$this->addQueryPart($queryPartName, $sql);
}
} }
} }
} }
} }
if (empty($this->parts['from'])) { if (empty($this->parts['from'])) {
return false; return false;
} }
...@@ -687,7 +703,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -687,7 +703,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$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();
...@@ -922,8 +938,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -922,8 +938,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
break; break;
case 'update': case 'update':
$this->type = self::UPDATE; $this->type = self::UPDATE;
$k = 'FROM'; $k = 'from';
case 'from': case 'from':
$this->parseQueryPart($k, $part); $this->parseQueryPart($k, $part);
break; break;
...@@ -1155,6 +1170,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1155,6 +1170,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
*/ */
public function count($params = array()) public function count($params = array())
{ {
$this->getQuery();
// initialize temporary variables // initialize temporary variables
$where = $this->parts['where']; $where = $this->parts['where'];
$having = $this->parts['having']; $having = $this->parts['having'];
...@@ -1162,6 +1179,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable ...@@ -1162,6 +1179,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable
$componentAlias = key($this->_aliasMap); $componentAlias = key($this->_aliasMap);
$table = $map['table']; $table = $map['table'];
// build the query base // build the query base
$q = 'SELECT COUNT(DISTINCT ' . $this->getTableAlias($componentAlias) $q = 'SELECT COUNT(DISTINCT ' . $this->getTableAlias($componentAlias)
. '.' . $table->getIdentifier() . '.' . $table->getIdentifier()
......
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