Author: ts Date: Fri Jan 11 13:43:52 2008 New Revision: 7126 Log: - Further refactoring steps for ezcPersistentSession (#12287). - Added common base class handlers (ezcPersistentSessionHandler). - Stored database and definition manager locally in handler to avoid multiple calls to ezcPersistentSession->__get().
Added: trunk/PersistentObject/src/interfaces/handler.php (with props) Modified: trunk/PersistentObject/design/class_diagram.png trunk/PersistentObject/src/handlers/delete_handler.php trunk/PersistentObject/src/handlers/load_handler.php trunk/PersistentObject/src/handlers/save_handler.php trunk/PersistentObject/src/persistent_autoload.php trunk/PersistentObject/src/persistent_session.php Modified: trunk/PersistentObject/design/class_diagram.png ============================================================================== Binary files - no diff available. Modified: trunk/PersistentObject/src/handlers/delete_handler.php ============================================================================== --- trunk/PersistentObject/src/handlers/delete_handler.php [iso-8859-1] (original) +++ trunk/PersistentObject/src/handlers/delete_handler.php [iso-8859-1] Fri Jan 11 13:43:52 2008 @@ -1,4 +1,12 @@ <?php +/** + * File containing the ezcPersistentDeleteHandler class. + * + * @package PersistentObject + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ /** * Helper class for ezcPersistentSession to handle object deleting. @@ -10,25 +18,8 @@ * @version //autogen// * @access private */ -class ezcPersistentDeleteHandler +class ezcPersistentDeleteHandler extends ezcPersistentSessionHandler { - /** - * Session object this instance belongs to. - * - * @var ezcPersistentSession - */ - private $session; - - /** - * Creates a new delete handler. - * - * @param ezcPersistentSession $session - */ - public function __construct( ezcPersistentSession $session ) - { - $this->session = $session; - } - /** * Deletes the persistent object $object. * @@ -59,7 +50,7 @@ public function delete( $object ) { $class = get_class( $object ); - $def = $this->session->definitionManager->fetchDefinition( $class ); // propagate exception + $def = $this->definitionManager->fetchDefinition( $class ); // propagate exception $state = $this->session->getObjectState( $object ); $idValue = $state[$def->idProperty->propertyName]; @@ -71,7 +62,7 @@ } // Transaction savety for exceptions thrown while cascading - $this->session->database->beginTransaction(); + $this->database->beginTransaction(); try { @@ -84,14 +75,14 @@ catch ( Exception $e ) { // Roll back the current transaction on any exception - $this->session->database->rollback(); + $this->database->rollback(); throw $e; } // create and execute query - $q = $this->session->database->createDeleteQuery(); - $q->deleteFrom( $this->session->database->quoteIdentifier( $def->table ) ) - ->where( $q->expr->eq( $this->session->database->quoteIdentifier( $def->idProperty->columnName ), + $q = $this->database->createDeleteQuery(); + $q->deleteFrom( $this->database->quoteIdentifier( $def->table ) ) + ->where( $q->expr->eq( $this->database->quoteIdentifier( $def->idProperty->columnName ), $q->bindValue( $idValue ) ) ); try @@ -100,13 +91,13 @@ } catch ( Exception $e ) { - $this->session->database->rollback(); + $this->database->rollback(); throw $e; } // After recursion of cascades everything should be fine here, or this // final commit call should perform the rollback ordered by a deeper level - $this->session->database->commit(); + $this->database->commit(); } /** @@ -129,7 +120,7 @@ public function removeRelatedObject( $object, $relatedObject ) { $class = get_class( $object ); - $def = $this->session->definitionManager->fetchDefinition( ( $class = get_class( $object ) ) ); + $def = $this->definitionManager->fetchDefinition( ( $class = get_class( $object ) ) ); $relatedClass = get_class( $relatedObject ); @@ -150,7 +141,7 @@ $objectState = $this->session->getObjectState( $object ); $relatedObjectState = $this->session->getObjectState( $relatedObject ); - $relatedDef = $this->session->definitionManager->fetchDefinition( get_class( $relatedObject ) ); + $relatedDef = $this->definitionManager->fetchDefinition( get_class( $relatedObject ) ); switch ( get_class( ( $relation = $def->relations[get_class( $relatedObject )] ) ) ) { case "ezcPersistentOneToManyRelation": @@ -161,17 +152,17 @@ } break; case "ezcPersistentManyToManyRelation": - $q = $this->session->database->createDeleteQuery(); - $q->deleteFrom( $this->session->database->quoteIdentifier( $relation->relationTable ) ); + $q = $this->database->createDeleteQuery(); + $q->deleteFrom( $this->database->quoteIdentifier( $relation->relationTable ) ); foreach ( $relation->columnMap as $map ) { $q->where( $q->expr->eq( - $this->session->database->quoteIdentifier( $map->relationSourceColumn ), + $this->database->quoteIdentifier( $map->relationSourceColumn ), $q->bindValue( $objectState[$def->columns[$map->sourceColumn]->propertyName] ) ), $q->expr->eq( - $this->session->database->quoteIdentifier( $map->relationDestinationColumn ), + $this->database->quoteIdentifier( $map->relationDestinationColumn ), $q->bindValue( $relatedObjectState[$relatedDef->columns[$map->destinationColumn]->propertyName] ) ) ); @@ -227,12 +218,12 @@ */ public function createDeleteQuery( $class ) { - $def = $this->session->definitionManager->fetchDefinition( $class ); // propagate exception + $def = $this->definitionManager->fetchDefinition( $class ); // propagate exception // init query - $q = $this->session->database->createDeleteQuery(); + $q = $this->database->createDeleteQuery(); $q->setAliases( $this->session->generateAliasMap( $def, false ) ); - $q->deleteFrom( $this->session->database->quoteIdentifier( $def->table ) ); + $q->deleteFrom( $this->database->quoteIdentifier( $def->table ) ); return $q; } Modified: trunk/PersistentObject/src/handlers/load_handler.php ============================================================================== --- trunk/PersistentObject/src/handlers/load_handler.php [iso-8859-1] (original) +++ trunk/PersistentObject/src/handlers/load_handler.php [iso-8859-1] Fri Jan 11 13:43:52 2008 @@ -1,4 +1,12 @@ <?php +/** + * File containing the ezcPersistentLoadHandler class + * + * @package PersistentObject + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ /** * Helper class for ezcPersistentSession to handle object loading. @@ -10,25 +18,8 @@ * @version //autogen// * @access private */ -class ezcPersistentLoadHandler +class ezcPersistentLoadHandler extends ezcPersistentSessionHandler { - /** - * Session object this instance belongs to. - * - * @var ezcPersistentSession - */ - private $session; - - /** - * Creates a new load handler. - * - * @param ezcPersistentSession $session - */ - public function __construct( ezcPersistentSession $session ) - { - $this->session = $session; - } - /** * Returns the persistent object of class $class with id $id. * @@ -44,7 +35,7 @@ */ public function load( $class, $id ) { - $def = $this->session->definitionManager->fetchDefinition( $class ); // propagate exception + $def = $this->definitionManager->fetchDefinition( $class ); // propagate exception $object = new $def->class; $this->loadIntoObject( $object, $id ); return $object; @@ -93,11 +84,11 @@ */ public function loadIntoObject( $object, $id ) { - $def = $this->session->definitionManager->fetchDefinition( get_class( $object ) ); // propagate exception - $q = $this->session->database->createSelectQuery(); + $def = $this->definitionManager->fetchDefinition( get_class( $object ) ); // propagate exception + $q = $this->database->createSelectQuery(); $q->select( $this->session->getColumnsFromDefinition( $def ) ) - ->from( $this->session->database->quoteIdentifier( $def->table ) ) - ->where( $q->expr->eq( $this->session->database->quoteIdentifier( $def->idProperty->columnName ), + ->from( $this->database->quoteIdentifier( $def->table ) ) + ->where( $q->expr->eq( $this->database->quoteIdentifier( $def->idProperty->columnName ), $q->bindValue( $id ) ) ); $stmt = $this->session->performQuery( $q ); @@ -150,7 +141,7 @@ */ public function refresh( $object ) { - $def = $this->session->definitionManager->fetchDefinition( get_class( $object ) ); // propagate exception + $def = $this->definitionManager->fetchDefinition( get_class( $object ) ); // propagate exception $state = $this->session->getObjectState( $object ); $idValue = $state[$def->idProperty->propertyName]; if ( $idValue !== null ) @@ -203,7 +194,7 @@ */ public function find( ezcQuerySelect $query, $class ) { - $def = $this->session->definitionManager->fetchDefinition( $class ); // propagate exception + $def = $this->definitionManager->fetchDefinition( $class ); // propagate exception $rows = $this->session->performQuery( $query )->fetchAll( PDO::FETCH_ASSOC ); @@ -247,7 +238,7 @@ */ public function findIterator( ezcQuerySelect $query, $class ) { - $def = $this->session->definitionManager->fetchDefinition( $class ); // propagate exception + $def = $this->definitionManager->fetchDefinition( $class ); // propagate exception $stmt = $this->session->performQuery( $query ); return new ezcPersistentFindIterator( $stmt, $def ); } @@ -357,13 +348,13 @@ */ public function createFindQuery( $class ) { - $def = $this->session->definitionManager->fetchDefinition( $class ); // propagate exception + $def = $this->definitionManager->fetchDefinition( $class ); // propagate exception // init query - $q = $this->session->database->createSelectQuery(); + $q = $this->database->createSelectQuery(); $q->setAliases( $this->session->generateAliasMap( $def ) ); $q->select( $this->session->getColumnsFromDefinition( $def ) ) - ->from( $this->session->database->quoteIdentifier( $def->table ) ); + ->from( $this->database->quoteIdentifier( $def->table ) ); return $q; } @@ -386,7 +377,7 @@ */ public function createRelationFindQuery( $object, $relatedClass ) { - $def = $this->session->definitionManager->fetchDefinition( ( $class = get_class( $object ) ) ); + $def = $this->definitionManager->fetchDefinition( ( $class = get_class( $object ) ) ); if ( !isset( $def->relations[$relatedClass] ) ) { throw new ezcPersistentRelationNotFoundException( $class, $relatedClass ); @@ -406,24 +397,24 @@ { $query->where( $query->expr->eq( - $this->session->database->quoteIdentifier( "{$map->destinationColumn}" ), + $this->database->quoteIdentifier( "{$map->destinationColumn}" ), $query->bindValue( $objectState[$def->columns[$map->sourceColumn]->propertyName] ) ) ); } break; case "ezcPersistentManyToManyRelation": - $query->from( $this->session->database->quoteIdentifier( $relation->relationTable ) ); + $query->from( $this->database->quoteIdentifier( $relation->relationTable ) ); foreach ( $relation->columnMap as $map ) { $query->where( $query->expr->eq( - $this->session->database->quoteIdentifier( $relation->relationTable ) . "." . $this->session->database->quoteIdentifier( $map->relationSourceColumn ), + $this->database->quoteIdentifier( $relation->relationTable ) . "." . $this->database->quoteIdentifier( $map->relationSourceColumn ), $query->bindValue( $objectState[$def->columns[$map->sourceColumn]->propertyName] ) ), $query->expr->eq( - $this->session->database->quoteIdentifier( $relation->relationTable ) . "." . $this->session->database->quoteIdentifier( $map->relationDestinationColumn ), - $this->session->database->quoteIdentifier( $relation->destinationTable ) . "." . $this->session->database->quoteIdentifier( $map->destinationColumn ) + $this->database->quoteIdentifier( $relation->relationTable ) . "." . $this->database->quoteIdentifier( $map->relationDestinationColumn ), + $this->database->quoteIdentifier( $relation->destinationTable ) . "." . $this->database->quoteIdentifier( $map->destinationColumn ) ) ); } Modified: trunk/PersistentObject/src/handlers/save_handler.php ============================================================================== --- trunk/PersistentObject/src/handlers/save_handler.php [iso-8859-1] (original) +++ trunk/PersistentObject/src/handlers/save_handler.php [iso-8859-1] Fri Jan 11 13:43:52 2008 @@ -1,4 +1,12 @@ <?php +/** + * File containing the ezcPersistentDeleteHandler class. + * + * @package PersistentObject + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ /** * Helper class for ezcPersistentSession to handle object saving. @@ -10,25 +18,8 @@ * @version //autogen// * @access private */ -class ezcPersistentSaveHandler +class ezcPersistentSaveHandler extends ezcPersistentSessionHandler { - /** - * Session object this instance belongs to. - * - * @var ezcPersistentSession - */ - private $session; - - /** - * Creates a new save handler. - * - * @param ezcPersistentSession $session - */ - public function __construct( ezcPersistentSession $session ) - { - $this->session = $session; - } - /** * Saves the new persistent object $object to the database using an INSERT INTO query. * @@ -85,7 +76,7 @@ */ public function saveOrUpdate( $object ) { - $def = $this->session->definitionManager->fetchDefinition( get_class( $object ) );// propagate exception + $def = $this->definitionManager->fetchDefinition( get_class( $object ) );// propagate exception $state = $this->session->getObjectState( $object ); // fetch the id generator @@ -100,7 +91,7 @@ } } - if ( !$idGenerator->checkPersistence( $def, $this->session->database, $state ) ) + if ( !$idGenerator->checkPersistence( $def, $this->database, $state ) ) { $this->saveInternal( $object, false, $idGenerator ); } @@ -132,7 +123,7 @@ public function addRelatedObject( $object, $relatedObject ) { $class = get_class( $object ); - $def = $this->session->definitionManager->fetchDefinition( ( $class = get_class( $object ) ) ); + $def = $this->definitionManager->fetchDefinition( ( $class = get_class( $object ) ) ); $relatedClass = get_class( $relatedObject ); @@ -153,7 +144,7 @@ ); } - $relatedDef = $this->session->definitionManager->fetchDefinition( get_class( $relatedObject ) ); + $relatedDef = $this->definitionManager->fetchDefinition( get_class( $relatedObject ) ); switch ( get_class( ( $relation = $def->relations[get_class( $relatedObject )] ) ) ) { case "ezcPersistentOneToManyRelation": @@ -165,15 +156,15 @@ } break; case "ezcPersistentManyToManyRelation": - $q = $this->session->database->createInsertQuery(); - $q->insertInto( $this->session->database->quoteIdentifier( $relation->relationTable ) ); + $q = $this->database->createInsertQuery(); + $q->insertInto( $this->database->quoteIdentifier( $relation->relationTable ) ); $insertColumns = array(); foreach ( $relation->columnMap as $map ) { if ( in_array( $map->relationSourceColumn, $insertColumns ) === false ) { $q->set( - $this->session->database->quoteIdentifier( $map->relationSourceColumn ), + $this->database->quoteIdentifier( $map->relationSourceColumn ), $q->bindValue( $objectState[$def->columns[$map->sourceColumn]->propertyName] ) ); $insertColumns[] = $map->relationSourceColumn; @@ -181,7 +172,7 @@ if ( in_array( $map->relationDestinationColumn, $insertColumns ) === false ) { $q->set( - $this->session->database->quoteIdentifier( $map->relationDestinationColumn ), + $this->database->quoteIdentifier( $map->relationDestinationColumn ), $q->bindValue( $relatedObjectState[$relatedDef->columns[$map->destinationColumn]->propertyName] ) ); $insertColumns[] = $map->relationDestinationColumn; @@ -209,12 +200,12 @@ */ public function createUpdateQuery( $class ) { - $def = $this->session->definitionManager->fetchDefinition( $class ); // propagate exception + $def = $this->definitionManager->fetchDefinition( $class ); // propagate exception // init query - $q = $this->session->database->createUpdateQuery(); + $q = $this->database->createUpdateQuery(); $q->setAliases( $this->session->generateAliasMap( $def, false ) ); - $q->update( $this->session->database->quoteIdentifier( $def->table ) ); + $q->update( $this->database->quoteIdentifier( $def->table ) ); return $q; } @@ -264,7 +255,7 @@ private function saveInternal( $object, $doPersistenceCheck = true, ezcPersistentIdentifierGenerator $idGenerator = null ) { - $def = $this->session->definitionManager->fetchDefinition( get_class( $object ) );// propagate exception + $def = $this->definitionManager->fetchDefinition( get_class( $object ) );// propagate exception $state = $this->filterAndCastState( $this->session->getObjectState( $object ), $def ); $idValue = $state[$def->idProperty->propertyName]; @@ -279,7 +270,7 @@ } } - if ( $doPersistenceCheck == true && $idGenerator->checkPersistence( $def, $this->session->database, $state ) ) + if ( $doPersistenceCheck == true && $idGenerator->checkPersistence( $def, $this->database, $state ) ) { $class = get_class( $object ); throw new ezcPersistentObjectAlreadyPersistentException( $class ); @@ -287,20 +278,20 @@ // set up and execute the query - $q = $this->session->database->createInsertQuery(); - $q->insertInto( $this->session->database->quoteIdentifier( $def->table ) ); + $q = $this->database->createInsertQuery(); + $q->insertInto( $this->database->quoteIdentifier( $def->table ) ); foreach ( $state as $name => $value ) { if ( $name != $def->idProperty->propertyName ) // skip the id field { // set each of the properties - $q->set( $this->session->database->quoteIdentifier( $def->properties[$name]->columnName ), $q->bindValue( $value ) ); - } - } - - $this->session->database->beginTransaction(); + $q->set( $this->database->quoteIdentifier( $def->properties[$name]->columnName ), $q->bindValue( $value ) ); + } + } + + $this->database->beginTransaction(); // let presave id generator do its work - $idGenerator->preSave( $def, $this->session->database, $q ); + $idGenerator->preSave( $def, $this->database, $q ); // execute the insert query try @@ -309,21 +300,21 @@ } catch ( Exception $e ) { - $this->session->database->rollback(); + $this->database->rollback(); throw $e; } // fetch the newly created id, and set it to the object - $id = $idGenerator->postSave( $def, $this->session->database ); + $id = $idGenerator->postSave( $def, $this->database ); if ( $id === null ) { - $this->session->database->rollback(); + $this->database->rollback(); throw new ezcPersistentIdentifierGenerationException( $def->class ); } // everything seems to be fine, lets commit the queries to the database // and update the object with its newly created id. - $this->session->database->commit(); + $this->database->commit(); $state[$def->idProperty->propertyName] = $id; $object->setState( $state ); @@ -344,7 +335,7 @@ */ private function updateInternal( $object, $doPersistenceCheck = true ) { - $def = $this->session->definitionManager->fetchDefinition( get_class( $object ) ); // propagate exception + $def = $this->definitionManager->fetchDefinition( get_class( $object ) ); // propagate exception $state = $this->filterAndCastState( $this->session->getObjectState( $object ), $def ); $idValue = $state[$def->idProperty->propertyName]; @@ -360,24 +351,24 @@ } } - if ( $doPersistenceCheck == true && !$idGenerator->checkPersistence( $def, $this->session->database, $state ) ) + if ( $doPersistenceCheck == true && !$idGenerator->checkPersistence( $def, $this->database, $state ) ) { $class = get_class( $object ); throw new ezcPersistentObjectNotPersistentException( get_class( $object ) ); } // set up and execute the query - $q = $this->session->database->createUpdateQuery(); - $q->update( $this->session->database->quoteIdentifier( $def->table ) ); + $q = $this->database->createUpdateQuery(); + $q->update( $this->database->quoteIdentifier( $def->table ) ); foreach ( $state as $name => $value ) { if ( $name != $def->idProperty->propertyName ) // skip the id field { // set each of the properties - $q->set( $this->session->database->quoteIdentifier( $def->properties[$name]->columnName ), $q->bindValue( $value ) ); - } - } - $q->where( $q->expr->eq( $this->session->database->quoteIdentifier( $def->idProperty->columnName ), + $q->set( $this->database->quoteIdentifier( $def->properties[$name]->columnName ), $q->bindValue( $value ) ); + } + } + $q->where( $q->expr->eq( $this->database->quoteIdentifier( $def->idProperty->columnName ), $q->bindValue( $idValue ) ) ); $this->session->performQuery( $q ); Added: trunk/PersistentObject/src/interfaces/handler.php ============================================================================== --- trunk/PersistentObject/src/interfaces/handler.php (added) +++ trunk/PersistentObject/src/interfaces/handler.php [iso-8859-1] Fri Jan 11 13:43:52 2008 @@ -1,0 +1,64 @@ +<?php +/** + * File containing the ezcPersistentSessionHandler abstract base class + * + * @package PersistentObject + * @version //autogen// + * @copyright Copyright (C) 2005-2008 eZ systems as. All rights reserved. + * @license http://ez.no/licenses/new_bsd New BSD License + */ + +/** + * Base class for handler classes of ezcPersistentSession. + * + * This base class should be used to realized handler classes for [EMAIL PROTECTED] + * ezcPersistentSession}, which are used to structure the methods provided by + * [EMAIL PROTECTED] ezcPersistentSession}. + * + * @package PersistentObject + * @version //autogen// + * @access private + */ +abstract class ezcPersistentSessionHandler +{ + /** + * Session object this instance belongs to. + * + * @var ezcPersistentSession + */ + protected $session; + + /** + * Database connection from [EMAIL PROTECTED] $session}. + * + * Kept to avoid a call to [EMAIL PROTECTED] ezcPersistentSession->__get()} whenever + * the database connection is used. + * + * @var ezcDbHandler + */ + protected $database; + + /** + * Definition manager from [EMAIL PROTECTED] $session}. + * + * Kept to avoid a call to [EMAIL PROTECTED] ezcPersistentSession->__get()} whenever + * the definition manager is used. + * + * @var ezcPersistentDefinitionManager + */ + protected $definitionManager; + + /** + * Creates a new load handler. + * + * @param ezcPersistentSession $session + */ + public function __construct( ezcPersistentSession $session ) + { + $this->session = $session; + $this->database = $session->database; + $this->definitionManager = $session->definitionManager; + } +} + +?> Propchange: trunk/PersistentObject/src/interfaces/handler.php ------------------------------------------------------------------------------ svn:eol-style = native Modified: trunk/PersistentObject/src/persistent_autoload.php ============================================================================== --- trunk/PersistentObject/src/persistent_autoload.php [iso-8859-1] (original) +++ trunk/PersistentObject/src/persistent_autoload.php [iso-8859-1] Fri Jan 11 13:43:52 2008 @@ -26,6 +26,7 @@ 'ezcPersistentIdentifierGenerator' => 'PersistentObject/interfaces/identifier_generator.php', 'ezcPersistentPropertyConverter' => 'PersistentObject/interfaces/property_converter.php', 'ezcPersistentRelation' => 'PersistentObject/interfaces/relation.php', + 'ezcPersistentSessionHandler' => 'PersistentObject/interfaces/handler.php', 'ezcPersistentCacheManager' => 'PersistentObject/managers/cache_manager.php', 'ezcPersistentCodeManager' => 'PersistentObject/managers/code_manager.php', 'ezcPersistentDeleteHandler' => 'PersistentObject/handlers/delete_handler.php', Modified: trunk/PersistentObject/src/persistent_session.php ============================================================================== --- trunk/PersistentObject/src/persistent_session.php [iso-8859-1] (original) +++ trunk/PersistentObject/src/persistent_session.php [iso-8859-1] Fri Jan 11 13:43:52 2008 @@ -1,6 +1,6 @@ <?php /** - * File containing the ezcPersistentSession class + * File containing the ezcPersistentSession class. * * @package PersistentObject * @version //autogen// -- svn-components mailing list svn-components@lists.ez.no http://lists.ez.no/mailman/listinfo/svn-components