Commit 156cfd4a authored by romanb's avatar romanb

Introduced a separation of the 2 concerns: 1) Query construction 2) Result hydration.

Makes it easier to see the dependencies between these two concerns, cache the results of the query parsing process later on, implement & use alternative hydrators to experiment with different algorithms etc. etc.
Other small refactorings.
parent 8195898f
......@@ -278,7 +278,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
if ( ! $event->skipOperation) {
$record->state(Doctrine_Record::STATE_TDIRTY);
if (count($table->getOption('joinedParents')) > 0) {
if ($table->getOption('joinedParents')) {
foreach ($table->getOption('joinedParents') as $parent) {
$parentTable = $table->getConnection()->getTable($parent);
......@@ -413,6 +413,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
// Protection against infinite function recursion before attempting to save
if ($obj instanceof Doctrine_Record && $obj->isModified()) {
$obj->save($this->conn);
/** Can this be removed?
$id = array_values($obj->identifier());
......@@ -461,8 +462,8 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
foreach ($v->getInsertDiff() as $r) {
$assocRecord = $assocTable->create();
$assocRecord->set($rel->getForeign(), $r);
$assocRecord->set($rel->getLocal(), $record);
$assocRecord->set($assocTable->getFieldName($rel->getForeign()), $r);
$assocRecord->set($assocTable->getFieldName($rel->getLocal()), $record);
$this->saveGraph($assocRecord);
}
......@@ -540,7 +541,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
if ( ! $event->skipOperation) {
$identifier = $record->identifier();
if (count($table->getOption('joinedParents')) > 0) {
if ($table->getOption('joinedParents')) {
$dataSet = $this->formatDataSet($record);
$component = $table->getComponentName();
......@@ -595,7 +596,7 @@ class Doctrine_Connection_UnitOfWork extends Doctrine_Connection_Module
$table->getRecordListener()->preInsert($event);
if ( ! $event->skipOperation) {
if (count($table->getOption('joinedParents')) > 0) {
if ($table->getOption('joinedParents')) {
$dataSet = $this->formatDataSet($record);
$component = $table->getComponentName();
......
This diff is collapsed.
<?php
/*
* $Id: Hydrate.php 3192 2007-11-19 17:55:23Z romanb $
*
* 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_Hydrator_Abstract
*
* @package Doctrine
* @subpackage Hydrate
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 3192 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
abstract class Doctrine_Hydrator_Abstract extends Doctrine_Locator_Injectable
{
/**
* @var array $_aliasMap two dimensional array containing the map for query aliases
* Main keys are component aliases
*
* table table object associated with given alias
*
* relation the relation object owned by the parent
*
* parent the alias of the parent
*
* agg the aggregates of this component
*
* map the name of the column / aggregate value this
* component is mapped to a collection
*/
protected $_aliasMap = array();
/**
* The current hydration mode.
*/
protected $_hydrationMode = Doctrine::HYDRATE_RECORD;
/**
* constructor
*
* @param Doctrine_Connection|null $connection
*/
public function __construct()
{
}
/**
* Sets the fetchmode.
*
* @param integer $fetchmode One of the Doctrine::HYDRATE_* constants.
*/
public function setHydrationMode($hydrationMode)
{
$this->_hydrationMode = $hydrationMode;
}
/**
* setAliasMap
* sets the whole component alias map
*
* @param array $map alias map
* @return Doctrine_Hydrate this object
*/
public function setAliasMap(array $map)
{
$this->_aliasMap = $map;
return $this;
}
/**
* getAliasMap
* returns the component alias map
*
* @return array component alias map
*/
public function getAliasMap()
{
return $this->_aliasMap;
}
/**
* parseData
* parses the data returned by statement object
*
* This is method defines the core of Doctrine object population algorithm
* hence this method strives to be as fast as possible
*
* The key idea is the loop over the rowset only once doing all the needed operations
* within this massive loop.
*
* @todo: Can we refactor this function so that it is not so long and
* nested?
*
* @param mixed $stmt
* @return array
*/
abstract public function hydrateResultSet($stmt, $aliasMap, $tableAliases, $hydrationMode = null);
}
This diff is collapsed.
<?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_Array
* defines an array fetching strategy for Doctrine_Hydrate
*
* @package Doctrine
* @subpackage Hydrate
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Hydrator_Default_FetchModeDriver_Array
{
public function getElementCollection($component)
{
return array();
}
public function getElement(array $data, $component)
{
return $data;
}
public function isIdentifiable(array $data, Doctrine_Table $table)
{
return ( ! empty($data));
}
public function registerCollection($coll)
{
}
public function initRelated(array &$data, $name)
{
if ( ! isset($data[$name])) {
$data[$name] = array();
}
return true;
}
public function getNullPointer()
{
return null;
}
public function getLastKey(&$data)
{
end($data);
return key($data);
}
public function flush()
{
}
}
<?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_Record
* defines a record fetching strategy for Doctrine_Hydrate
*
* @package Doctrine
* @subpackage Hydrate
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Hydrator_Default_FetchModeDriver_Record extends Doctrine_Locator_Injectable
{
protected $_collections = array();
protected $_records = array();
protected $_tables = array();
public function getElementCollection($component)
{
$coll = new Doctrine_Collection($component);
$this->_collections[] = $coll;
return $coll;
}
public function getLastKey($coll)
{
$coll->end();
return $coll->key();
}
public function initRelated($record, $name)
{
if ( ! is_array($record)) {
$record[$name];
return true;
}
return false;
}
public function registerCollection(Doctrine_Collection $coll)
{
$this->_collections[] = $coll;
}
/**
* isIdentifiable
* returns whether or not a given data row is identifiable (it contains
* all primary key fields specified in the second argument)
*
* @param array $row
* @param Doctrine_Table $table
* @return boolean
*/
public function isIdentifiable(array $row, Doctrine_Table $table)
{
$primaryKeys = $table->getIdentifierColumnNames();
if (is_array($primaryKeys)) {
foreach ($primaryKeys as $id) {
if ( ! isset($row[$id])) {
return false;
}
}
} else {
if ( ! isset($row[$primaryKeys])) {
return false;
}
}
return true;
}
public function getNullPointer()
{
return self::$_null;
}
public function getElement(array $data, $component)
{
if ( ! isset($this->_tables[$component])) {
$this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
$this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
}
$this->_tables[$component]->setData($data);
$record = $this->_tables[$component]->getRecord();
if ( ! isset($this->_records[$record->getOid()]) ) {
$record->clearRelated();
$this->_records[$record->getOid()] = $record;
}
return $record;
}
public function flush()
{
// take snapshots from all initialized collections
foreach ($this->_collections as $key => $coll) {
$coll->takeSnapshot();
}
foreach ($this->_tables as $table) {
$table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
}
}
}
This diff is collapsed.
This diff is collapsed.
......@@ -35,7 +35,7 @@ class Doctrine_Query_From extends Doctrine_Query_Part
/**
* DQL FROM PARSER
* parses the from part of the query string
*
* @param string $str
* @return void
*/
......
......@@ -89,7 +89,7 @@ class HydrationListener extends Doctrine_Record_Listener
}
}
}
class Doctrine_Hydrate_Mock extends Doctrine_Hydrate
class Doctrine_Hydrate_Mock extends Doctrine_Hydrator_Abstract
{
protected $data;
......@@ -97,12 +97,9 @@ class Doctrine_Hydrate_Mock extends Doctrine_Hydrate
{
$this->data = $data;
}
public function getQuery($params = array())
{
}
public function execute($params = array(), $hydrationMode = null)
public function hydrateResultSet($stmt, $aliasMap, $tableAliases, $hydrationMode = null)
{
return $this->data;
return true;
}
}
......@@ -281,3 +281,4 @@ $test->addTestCase($data);
$test->run();
echo memory_get_peak_usage() / 1024;
\ No newline at end of file
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