Started refactoring of the default hydrator, including some minor speed tweaks.
This commit is contained in:
parent
ea3935bc44
commit
a4a1a3f577
@ -47,15 +47,31 @@ class Doctrine_Hydrator_Default extends Doctrine_Hydrator_Abstract
|
|||||||
* @todo: Detailed documentation. Refactor (too long & nesting level).
|
* @todo: Detailed documentation. Refactor (too long & nesting level).
|
||||||
*
|
*
|
||||||
* @param mixed $stmt
|
* @param mixed $stmt
|
||||||
|
* @param array $tableAliases Array that maps table aliases (SQL alias => DQL alias)
|
||||||
|
* @param array $aliasMap Array that maps DQL aliases to their components
|
||||||
|
* (DQL alias => array('table' => Table object,
|
||||||
|
* 'parent' => Parent DQL alias (if any),
|
||||||
|
* 'relation' => Relation object (if any),
|
||||||
|
* 'map' => ??? (if any)
|
||||||
|
* )
|
||||||
|
* )
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function hydrateResultSet($stmt, $aliasMap, $tableAliases, $hydrationMode = null)
|
public function hydrateResultSet($stmt, $aliasMap, $tableAliases, $hydrationMode = null)
|
||||||
{
|
{
|
||||||
|
$s = microtime(true);
|
||||||
|
|
||||||
$this->_aliasMap = $aliasMap;
|
$this->_aliasMap = $aliasMap;
|
||||||
//echo "aliasmap:<br />";
|
$this->_tableAliases = $tableAliases;
|
||||||
/*foreach ($this->_aliasMap as $map) {
|
/*echo "aliasmap:<br />";
|
||||||
|
foreach ($this->_aliasMap as $map) {
|
||||||
|
if ( ! empty($map['map'])) {
|
||||||
Doctrine::dump($map['map']);
|
Doctrine::dump($map['map']);
|
||||||
|
}
|
||||||
}*/
|
}*/
|
||||||
|
//Doctrine::dump($this->_aliasMap);
|
||||||
|
//echo "<br />";
|
||||||
|
//echo "tableAliases:<br />";
|
||||||
//Doctrine::dump($tableAliases);
|
//Doctrine::dump($tableAliases);
|
||||||
//echo "<br /><br />";
|
//echo "<br /><br />";
|
||||||
|
|
||||||
@ -81,19 +97,17 @@ class Doctrine_Hydrator_Default extends Doctrine_Hydrator_Abstract
|
|||||||
$rootAlias = key($this->_aliasMap);
|
$rootAlias = key($this->_aliasMap);
|
||||||
$componentName = $rootMap['table']->getComponentName();
|
$componentName = $rootMap['table']->getComponentName();
|
||||||
$isSimpleQuery = count($this->_aliasMap) <= 1;
|
$isSimpleQuery = count($this->_aliasMap) <= 1;
|
||||||
$array = array(); // Holds the resulting hydrated data structure
|
$result = array(); // Holds the resulting hydrated data structure
|
||||||
$cache = array(); // Temporarily holds results of some operations to improve performance
|
|
||||||
$listeners = array(); // Holds hydration listeners that get called during hydration
|
$listeners = array(); // Holds hydration listeners that get called during hydration
|
||||||
$identifierMap = array(); // ???
|
$identifierMap = array(); // Lookup map to quickly discover/lookup existing records in the result
|
||||||
$prev = array(); // ???
|
$prev = array(); // Holds for each component the last previously seen element in the result set
|
||||||
$id = array(); // ???
|
$id = array(); // holds the values of the identifier/primary key columns of components,
|
||||||
$currData = array(); // ???
|
// separated by a pipe '|' and grouped by component alias (r, u, i, ... whatever)
|
||||||
$identifiable = array(); // ???
|
|
||||||
|
|
||||||
$array = $driver->getElementCollection($componentName);
|
$result = $driver->getElementCollection($componentName);
|
||||||
|
|
||||||
if ($stmt === false || $stmt === 0) {
|
if ($stmt === false || $stmt === 0) {
|
||||||
return $array;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the variables
|
// Initialize the variables
|
||||||
@ -101,93 +115,65 @@ class Doctrine_Hydrator_Default extends Doctrine_Hydrator_Abstract
|
|||||||
$componentName = $data['table']->getComponentName();
|
$componentName = $data['table']->getComponentName();
|
||||||
$listeners[$componentName] = $data['table']->getRecordListener();
|
$listeners[$componentName] = $data['table']->getRecordListener();
|
||||||
$identifierMap[$alias] = array();
|
$identifierMap[$alias] = array();
|
||||||
$currData[$alias] = array();
|
|
||||||
$prev[$alias] = array();
|
$prev[$alias] = array();
|
||||||
$id[$alias] = '';
|
$id[$alias] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hydrate
|
// Process result set
|
||||||
|
$cache = array();
|
||||||
while ($data = $stmt->fetch(Doctrine::FETCH_ASSOC)) {
|
while ($data = $stmt->fetch(Doctrine::FETCH_ASSOC)) {
|
||||||
$currData = array();
|
|
||||||
$identifiable = array();
|
$identifiable = array();
|
||||||
|
|
||||||
foreach ($data as $key => $value) {
|
$rowData = $this->_gatherRowData($data, $cache, $id, $identifiable);
|
||||||
|
|
||||||
if ( ! isset($cache[$key])) {
|
//echo "rowData of row:<br />";
|
||||||
$e = explode('__', $key);
|
//Doctrine::dump($rowData);
|
||||||
$last = strtolower(array_pop($e));
|
|
||||||
$cache[$key]['alias'] = $tableAliases[strtolower(implode('__', $e))];
|
|
||||||
$fieldName = $this->_aliasMap[$cache[$key]['alias']]['table']->getFieldName($last);
|
|
||||||
$cache[$key]['fieldName'] = $fieldName;
|
|
||||||
}
|
|
||||||
|
|
||||||
$map = $this->_aliasMap[$cache[$key]['alias']];
|
|
||||||
$table = $map['table'];
|
|
||||||
$alias = $cache[$key]['alias'];
|
|
||||||
$fieldName = $cache[$key]['fieldName'];
|
|
||||||
|
|
||||||
if (isset($this->_aliasMap[$alias]['agg'][$fieldName])) {
|
|
||||||
$fieldName = $this->_aliasMap[$alias]['agg'][$fieldName];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($table->isIdentifier($fieldName)) {
|
|
||||||
$id[$alias] .= '|' . $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
$currData[$alias][$fieldName] = $table->prepareValue($fieldName, $value);
|
|
||||||
|
|
||||||
if ($value !== null) {
|
|
||||||
$identifiable[$alias] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//echo "currdata of row:<br />";
|
|
||||||
//Doctrine::dump($currData);
|
|
||||||
//echo "<br /><br />";
|
//echo "<br /><br />";
|
||||||
|
|
||||||
// dealing with root component
|
//
|
||||||
|
// hydrate the data of the root component from the current row
|
||||||
|
//
|
||||||
$table = $this->_aliasMap[$rootAlias]['table'];
|
$table = $this->_aliasMap[$rootAlias]['table'];
|
||||||
$componentName = $table->getComponentName();
|
$componentName = $table->getComponentName();
|
||||||
$event->set('data', $currData[$rootAlias]);
|
$event->set('data', $rowData[$rootAlias]);
|
||||||
$listeners[$componentName]->preHydrate($event);
|
$listeners[$componentName]->preHydrate($event);
|
||||||
$element = $driver->getElement($currData[$rootAlias], $componentName);
|
$element = $driver->getElement($rowData[$rootAlias], $componentName);
|
||||||
|
|
||||||
$oneToOne = false;
|
|
||||||
|
|
||||||
if ($isSimpleQuery) {
|
|
||||||
$index = false;
|
$index = false;
|
||||||
} else {
|
|
||||||
$index = isset($identifierMap[$rootAlias][$id[$rootAlias]]) ?
|
|
||||||
$identifierMap[$rootAlias][$id[$rootAlias]] : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($index === false) {
|
// Check for an existing element
|
||||||
|
if ($isSimpleQuery || ! isset($identifierMap[$rootAlias][$id[$rootAlias]])) {
|
||||||
$event->set('data', $element);
|
$event->set('data', $element);
|
||||||
$listeners[$componentName]->postHydrate($event);
|
$listeners[$componentName]->postHydrate($event);
|
||||||
|
|
||||||
if (isset($this->_aliasMap[$rootAlias]['map'])) {
|
// do we need to index by a custom field?
|
||||||
$key = $this->_aliasMap[$rootAlias]['map'];
|
if ($field = $this->_getCustomIndexField($rootAlias)) {
|
||||||
|
if (isset($result[$field])) {
|
||||||
if (isset($array[$key])) {
|
|
||||||
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found non-unique key mapping.");
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found non-unique key mapping.");
|
||||||
}
|
} else if ( ! isset($element[$field])) {
|
||||||
|
|
||||||
if ( ! isset($element[$key])) {
|
|
||||||
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found a non-existent key.");
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found a non-existent key.");
|
||||||
}
|
}
|
||||||
|
$result[$element[$field]] = $element;
|
||||||
$array[$element[$key]] = $element;
|
|
||||||
} else {
|
} else {
|
||||||
$array[] = $element;
|
$result[] = $element;
|
||||||
}
|
}
|
||||||
|
|
||||||
$identifierMap[$rootAlias][$id[$rootAlias]] = $driver->getLastKey($array);
|
$identifierMap[$rootAlias][$id[$rootAlias]] = $driver->getLastKey($result);
|
||||||
|
} else {
|
||||||
|
$index = $identifierMap[$rootAlias][$id[$rootAlias]];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_setLastElement($prev, $array, $index, $rootAlias, $oneToOne);
|
$this->_setLastElement($prev, $result, $index, $rootAlias, false);
|
||||||
unset($currData[$rootAlias]);
|
unset($rowData[$rootAlias]);
|
||||||
|
|
||||||
foreach ($currData as $alias => $data) {
|
// end hydrate data of the root component for the current row
|
||||||
|
//echo "\$result after root element hydration:<br />";
|
||||||
|
//Doctrine::dump($result);
|
||||||
|
//echo "<br /><br />";
|
||||||
|
|
||||||
|
// now hydrate the rest of the data found in the current row, that belongs to other
|
||||||
|
// (related) components
|
||||||
|
$oneToOne = false;
|
||||||
|
foreach ($rowData as $alias => $data) {
|
||||||
$index = false;
|
$index = false;
|
||||||
$map = $this->_aliasMap[$alias];
|
$map = $this->_aliasMap[$alias];
|
||||||
$table = $this->_aliasMap[$alias]['table'];
|
$table = $this->_aliasMap[$alias]['table'];
|
||||||
@ -208,43 +194,32 @@ class Doctrine_Hydrator_Default extends Doctrine_Hydrator_Abstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check the type of the relation
|
// check the type of the relation
|
||||||
if ( ! $relation->isOneToOne()) {
|
if ( ! $relation->isOneToOne() && $driver->initRelated($prev[$parent], $componentAlias)) {
|
||||||
// initialize the collection
|
|
||||||
|
|
||||||
if ($driver->initRelated($prev[$parent], $componentAlias)) {
|
|
||||||
|
|
||||||
// append element
|
// append element
|
||||||
if (isset($identifiable[$alias])) {
|
if (isset($identifiable[$alias])) {
|
||||||
if ($isSimpleQuery) {
|
if ($isSimpleQuery || ! isset($identifierMap[$path][$id[$parent]][$id[$alias]])) {
|
||||||
$index = false;
|
//$index = false;
|
||||||
} else {
|
|
||||||
$index = isset($identifierMap[$path][$id[$parent]][$id[$alias]]) ?
|
|
||||||
$identifierMap[$path][$id[$parent]][$id[$alias]] : false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($index === false) {
|
|
||||||
$event->set('data', $element);
|
$event->set('data', $element);
|
||||||
$listeners[$componentName]->postHydrate($event);
|
$listeners[$componentName]->postHydrate($event);
|
||||||
|
|
||||||
if (isset($map['map'])) {
|
if ($field = $this->_getCustomIndexField($alias)) {
|
||||||
$key = $map['map'];
|
if (isset($prev[$parent][$componentAlias][$field])) {
|
||||||
if (isset($prev[$parent][$componentAlias][$key])) {
|
|
||||||
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found non-unique key mapping.");
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found non-unique key mapping.");
|
||||||
}
|
} else if ( ! isset($element[$field])) {
|
||||||
if ( ! isset($element[$key])) {
|
|
||||||
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found a non-existent key.");
|
throw new Doctrine_Hydrate_Exception("Couldn't hydrate. Found a non-existent key.");
|
||||||
}
|
}
|
||||||
$prev[$parent][$componentAlias][$element[$key]] = $element;
|
$prev[$parent][$componentAlias][$element[$field]] = $element;
|
||||||
} else {
|
} else {
|
||||||
$prev[$parent][$componentAlias][] = $element;
|
$prev[$parent][$componentAlias][] = $element;
|
||||||
}
|
}
|
||||||
|
|
||||||
$identifierMap[$path][$id[$parent]][$id[$alias]] = $driver->getLastKey($prev[$parent][$componentAlias]);
|
$identifierMap[$path][$id[$parent]][$id[$alias]] = $driver->getLastKey($prev[$parent][$componentAlias]);
|
||||||
|
} else {
|
||||||
|
$index = $identifierMap[$path][$id[$parent]][$id[$alias]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// register collection for later snapshots
|
// register collection for later snapshots
|
||||||
$driver->registerCollection($prev[$parent][$componentAlias]);
|
$driver->registerCollection($prev[$parent][$componentAlias]);
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if ( ! isset($identifiable[$alias])) {
|
if ( ! isset($identifiable[$alias])) {
|
||||||
$prev[$parent][$componentAlias] = $driver->getNullPointer();
|
$prev[$parent][$componentAlias] = $driver->getNullPointer();
|
||||||
@ -257,6 +232,9 @@ class Doctrine_Hydrator_Default extends Doctrine_Hydrator_Abstract
|
|||||||
$this->_setLastElement($prev, $coll, $index, $alias, $oneToOne);
|
$this->_setLastElement($prev, $coll, $index, $alias, $oneToOne);
|
||||||
$id[$alias] = '';
|
$id[$alias] = '';
|
||||||
}
|
}
|
||||||
|
//echo "\$result after related element hydration:<br />";
|
||||||
|
//Doctrine::dump($result);
|
||||||
|
//echo "<br /><br />";
|
||||||
$id[$rootAlias] = '';
|
$id[$rootAlias] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -264,7 +242,11 @@ class Doctrine_Hydrator_Default extends Doctrine_Hydrator_Abstract
|
|||||||
|
|
||||||
$stmt->closeCursor();
|
$stmt->closeCursor();
|
||||||
|
|
||||||
return $array;
|
$e = microtime(true);
|
||||||
|
|
||||||
|
//echo 'Hydration took: ' . ($e - $s) . ' for '.count($result).' records<br />';
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -282,28 +264,82 @@ class Doctrine_Hydrator_Default extends Doctrine_Hydrator_Abstract
|
|||||||
if ($coll === self::$_null) {
|
if ($coll === self::$_null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($index !== false) {
|
if ($index !== false) {
|
||||||
|
// Set lement at $index as previous element for the component
|
||||||
|
// identified by the DQL alias $alias
|
||||||
$prev[$alias] =& $coll[$index];
|
$prev[$alias] =& $coll[$index];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// first check the count (we do not want to get the last element
|
|
||||||
// of an empty collection/array)
|
if (is_array($coll) && $coll) {
|
||||||
if (count($coll) > 0) {
|
|
||||||
if (is_array($coll)) {
|
|
||||||
if ($oneToOne) {
|
if ($oneToOne) {
|
||||||
$prev[$alias] =& $coll;
|
$prev[$alias] =& $coll;
|
||||||
} else {
|
} else {
|
||||||
end($coll);
|
end($coll);
|
||||||
$prev[$alias] =& $coll[key($coll)];
|
$prev[$alias] =& $coll[key($coll)];
|
||||||
}
|
}
|
||||||
} else {
|
} else if (count($coll) > 0) {
|
||||||
$prev[$alias] = $coll->getLast();
|
$prev[$alias] = $coll->getLast();
|
||||||
}
|
} else if (isset($prev[$alias])) {
|
||||||
} else {
|
|
||||||
if (isset($prev[$alias])) {
|
|
||||||
unset($prev[$alias]);
|
unset($prev[$alias]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Puts the fields of a data row into a new array, grouped by the component
|
||||||
|
* they belong to. The column names in the result set are mapped to their
|
||||||
|
* field names during this procedure.
|
||||||
|
*
|
||||||
|
* @return array An array with all the fields (name => value) of the data row,
|
||||||
|
* grouped by their component (alias).
|
||||||
|
*/
|
||||||
|
protected function _gatherRowData(&$data, &$cache, &$id, &$identifiable)
|
||||||
|
{
|
||||||
|
$rowData = array();
|
||||||
|
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
// Parse each column name only once. Cache the results.
|
||||||
|
if ( ! isset($cache[$key])) {
|
||||||
|
$e = explode('__', $key);
|
||||||
|
$last = strtolower(array_pop($e));
|
||||||
|
$cache[$key]['alias'] = $this->_tableAliases[strtolower(implode('__', $e))];
|
||||||
|
$fieldName = $this->_aliasMap[$cache[$key]['alias']]['table']->getFieldName($last);
|
||||||
|
$cache[$key]['fieldName'] = $fieldName;
|
||||||
|
}
|
||||||
|
|
||||||
|
$map = $this->_aliasMap[$cache[$key]['alias']];
|
||||||
|
$table = $map['table'];
|
||||||
|
$alias = $cache[$key]['alias'];
|
||||||
|
$fieldName = $cache[$key]['fieldName'];
|
||||||
|
|
||||||
|
if (isset($this->_aliasMap[$alias]['agg'][$fieldName])) {
|
||||||
|
$fieldName = $this->_aliasMap[$alias]['agg'][$fieldName];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($table->isIdentifier($fieldName)) {
|
||||||
|
$id[$alias] .= '|' . $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rowData[$alias][$fieldName] = $table->prepareValue($fieldName, $value);
|
||||||
|
|
||||||
|
if ($value !== null) {
|
||||||
|
$identifiable[$alias] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rowData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the custom field used for indexing for the specified component alias.
|
||||||
|
*
|
||||||
|
* @return string The field name of the field used for indexing or NULL
|
||||||
|
* if the component does not use any custom field indices.
|
||||||
|
*/
|
||||||
|
protected function _getCustomIndexField($alias)
|
||||||
|
{
|
||||||
|
return isset($this->_aliasMap[$alias]['map']) ? $this->_aliasMap[$alias]['map'] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1612,7 +1612,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable
|
|||||||
{
|
{
|
||||||
if ($value === self::$_null) {
|
if ($value === self::$_null) {
|
||||||
return self::$_null;
|
return self::$_null;
|
||||||
} elseif ($value === null) {
|
} else if ($value === null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
$type = $this->getTypeOf($fieldName);
|
$type = $this->getTypeOf($fieldName);
|
||||||
|
Loading…
Reference in New Issue
Block a user