DbQuery: Initialize self::$select as early as possible

I'd like to use Zend's implementation instead of re-inventing the wheel just
because someone decided to only work with a copy of it in the frameworks
query but do exactly the opposite in the monitoring module's IDO query...
This commit is contained in:
Johannes Meyer 2015-05-28 13:49:36 +02:00
parent 5326ce6bca
commit 58d78f59f3
2 changed files with 13 additions and 20 deletions

View File

@ -8,7 +8,7 @@ use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterOr; use Icinga\Data\Filter\FilterOr;
use Icinga\Data\Filter\FilterAnd; use Icinga\Data\Filter\FilterAnd;
use Icinga\Data\Filter\FilterNot; use Icinga\Data\Filter\FilterNot;
use Icinga\Exception\IcingaException; use Icinga\Exception\QueryException;
use Zend_Db_Select; use Zend_Db_Select;
/** /**
@ -66,6 +66,7 @@ class DbQuery extends SimpleQuery
protected function init() protected function init()
{ {
$this->db = $this->ds->getDbAdapter(); $this->db = $this->ds->getDbAdapter();
$this->select = $this->db->select();
parent::init(); parent::init();
} }
@ -75,6 +76,13 @@ class DbQuery extends SimpleQuery
return $this; return $this;
} }
public function from($target, array $fields = null)
{
parent::from($target, $fields);
$this->select->from($this->target, array());
return $this;
}
public function where($condition, $value = null) public function where($condition, $value = null)
{ {
// $this->count = $this->select = null; // $this->count = $this->select = null;
@ -83,9 +91,6 @@ class DbQuery extends SimpleQuery
protected function dbSelect() protected function dbSelect()
{ {
if ($this->select === null) {
$this->select = $this->db->select()->from($this->target, array());
}
return clone $this->select; return clone $this->select;
} }
@ -151,7 +156,7 @@ class DbQuery extends SimpleQuery
$op = ' AND '; $op = ' AND ';
$str .= ' NOT '; $str .= ' NOT ';
} else { } else {
throw new IcingaException( throw new QueryException(
'Cannot render filter: %s', 'Cannot render filter: %s',
$filter $filter
); );
@ -212,7 +217,7 @@ class DbQuery extends SimpleQuery
if (! $value) { if (! $value) {
/* /*
NOTE: It's too late to throw exceptions, we might finish in __toString NOTE: It's too late to throw exceptions, we might finish in __toString
throw new IcingaException(sprintf( throw new QueryException(sprintf(
'"%s" is not a valid time expression', '"%s" is not a valid time expression',
$value $value
)); ));
@ -318,9 +323,7 @@ class DbQuery extends SimpleQuery
public function __clone() public function __clone()
{ {
if ($this->select) { $this->select = clone $this->select;
$this->select = clone $this->select;
}
} }
/** /**

View File

@ -415,21 +415,11 @@ abstract class IdoQuery extends DbQuery
} elseif ($dbType === 'pgsql') { } elseif ($dbType === 'pgsql') {
$this->initializeForPostgres(); $this->initializeForPostgres();
} }
$this->dbSelect(); $this->joinBaseTables();
$this->select->columns($this->columns); $this->select->columns($this->columns);
//$this->joinBaseTables();
$this->prepareAliasIndexes(); $this->prepareAliasIndexes();
} }
protected function dbSelect()
{
if ($this->select === null) {
$this->select = $this->db->select();
$this->joinBaseTables();
}
return clone $this->select;
}
/** /**
* Join the base tables for this query * Join the base tables for this query
*/ */