Commit 929273a0 authored by romanb's avatar romanb

More refactorings and small speed improvements on the default hydrator.

parent 205c50ea
<?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_Hydrate_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_Hydrate_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);
}
}
}
......@@ -47,7 +47,7 @@ abstract class Doctrine_Hydrator_Abstract extends Doctrine_Locator_Injectable
* map the name of the column / aggregate value this
* component is mapped to a collection
*/
protected $_aliasMap = array();
protected $_queryComponents = array();
/**
* The current hydration mode.
......@@ -59,10 +59,7 @@ abstract class Doctrine_Hydrator_Abstract extends Doctrine_Locator_Injectable
*
* @param Doctrine_Connection|null $connection
*/
public function __construct()
{
}
public function __construct() {}
/**
* Sets the fetchmode.
......@@ -81,10 +78,9 @@ abstract class Doctrine_Hydrator_Abstract extends Doctrine_Locator_Injectable
* @param array $map alias map
* @return Doctrine_Hydrate this object
*/
public function setAliasMap(array $map)
public function setQueryComponents(array $queryComponents)
{
$this->_aliasMap = $map;
return $this;
$this->_queryComponents = $queryComponents;
}
/**
......@@ -93,9 +89,9 @@ abstract class Doctrine_Hydrator_Abstract extends Doctrine_Locator_Injectable
*
* @return array component alias map
*/
public function getAliasMap()
public function getQueryComponents()
{
return $this->_aliasMap;
return $this->_queryComponents;
}
/**
......@@ -114,6 +110,6 @@ abstract class Doctrine_Hydrator_Abstract extends Doctrine_Locator_Injectable
* @param mixed $stmt
* @return array
*/
abstract public function hydrateResultSet($stmt, $aliasMap, $tableAliases, $hydrationMode = null);
abstract public function hydrateResultSet($stmt, $tableAliases, $hydrationMode = null);
}
This diff is collapsed.
......@@ -31,7 +31,7 @@
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Hydrator_Default_FetchModeDriver_Array
class Doctrine_Hydrator_Default_ArrayDriver
{
public function getElementCollection($component)
{
......
......@@ -31,7 +31,7 @@
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Hydrator_Default_FetchModeDriver_Record extends Doctrine_Locator_Injectable
class Doctrine_Hydrator_Default_RecordDriver extends Doctrine_Locator_Injectable
{
protected $_collections = array();
......@@ -56,12 +56,14 @@ class Doctrine_Hydrator_Default_FetchModeDriver_Record extends Doctrine_Locator_
public function initRelated($record, $name)
{
return true;
/*
if ( ! is_array($record)) {
$record[$name];
return true;
}
return false;
*/
}
public function registerCollection(Doctrine_Collection $coll)
......
<?php
/*
* $Id: Exception.php 1080 2007-02-10 18:17:08Z 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::autoload('Doctrine_Exception');
/**
* Doctrine_Hydrate_Exception
*
* @package Doctrine
* @subpackage Hydrate
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1080 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Hydrate_Exception extends Doctrine_Exception
<?php
/*
* $Id: Exception.php 1080 2007-02-10 18:17:08Z 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::autoload('Doctrine_Exception');
/**
* Doctrine_Hydrator_Exception
*
* @package Doctrine
* @subpackage Hydrate
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision: 1080 $
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Hydrator_Exception extends Doctrine_Exception
{ }
\ No newline at end of file
......@@ -208,31 +208,6 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
$this->_params = $params;
}
/**
* getCachedForm
* returns the cached form of this query for given resultSet
*
* @param array $resultSet
* @return string serialized string representation of this query
*/
public function getCachedForm(array $resultSet)
{
$map = '';
foreach ($this->getAliasMap() as $k => $v) {
if ( ! isset($v['parent'])) {
$map[$k][] = $v['table']->getComponentName();
} else {
$map[$k][] = $v['parent'] . '.' . $v['relation']->getAlias();
}
if (isset($v['agg'])) {
$map[$k][] = $v['agg'];
}
}
return serialize(array($resultSet, $map, $this->getTableAliases()));
}
/**
* fetchArray
* Convenience method to execute using array fetching as hydration mode.
......@@ -950,8 +925,8 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
}
if (count($tableAliases) !== 1) {
$componentAlias = reset($this->tableAliases);
$tableAlias = key($this->tableAliases);
$componentAlias = reset($this->_tableAliases);
$tableAlias = key($this->_tableAliases);
}
$index = count($this->aggregateMap);
......@@ -1405,7 +1380,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
{
$e = Doctrine_Tokenizer::sqlExplode($query, ' ');
foreach ($e as $k=>$part) {
foreach ($e as $k => $part) {
$part = trim($part);
switch (strtolower($part)) {
case 'delete':
......@@ -1752,7 +1727,7 @@ class Doctrine_Query extends Doctrine_Query_Abstract implements Countable, Seria
$queryPart .= ' ' . $this->_conn->quoteIdentifier($tableAlias);
}
$this->tableAliases[$tableAlias] = $componentAlias;
$this->_tableAliases[$tableAlias] = $componentAlias;
$queryPart .= $this->buildInheritanceJoinSql($name, $componentAlias);
......
......@@ -752,8 +752,9 @@ abstract class Doctrine_Query_Abstract
if ($cached === false) {
// cache miss
$stmt = $this->_execute($params);
$array = $this->_hydrator->hydrateResultSet($stmt, $this->_aliasMap,
$this->_tableAliases, Doctrine::HYDRATE_ARRAY);
$this->_hydrator->setQueryComponents($this->_aliasMap);
$array = $this->_hydrator->hydrateResultSet($stmt, $this->_tableAliases,
Doctrine::HYDRATE_ARRAY);
$cached = $this->getCachedForm($array);
......@@ -786,12 +787,36 @@ abstract class Doctrine_Query_Abstract
return $stmt;
}
$array = $this->_hydrator->hydrateResultSet($stmt, $this->_aliasMap,
$this->_tableAliases, $hydrationMode);
$this->_hydrator->setQueryComponents($this->_aliasMap);
$array = $this->_hydrator->hydrateResultSet($stmt, $this->_tableAliases, $hydrationMode);
}
return $array;
}
/**
* getCachedForm
* returns the cached form of this query for given resultSet
*
* @param array $resultSet
* @return string serialized string representation of this query
*/
public function getCachedForm(array $resultSet)
{
$map = array();
foreach ($this->getAliasMap() as $k => $v) {
if ( ! isset($v['parent'])) {
$map[$k][] = $v['table']->getComponentName();
} else {
$map[$k][] = $v['parent'] . '.' . $v['relation']->getAlias();
}
if (isset($v['agg'])) {
$map[$k][] = $v['agg'];
}
}
return serialize(array($resultSet, $map, $this->getTableAliases()));
}
/**
* addSelect
......
......@@ -98,7 +98,7 @@ class Doctrine_Hydrate_Mock extends Doctrine_Hydrator_Abstract
$this->data = $data;
}
public function hydrateResultSet($stmt, $aliasMap, $tableAliases, $hydrationMode = null)
public function hydrateResultSet($stmt, $tableAliases, $hydrationMode = null)
{
return true;
}
......
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