Author: Frederik Holljen
Date: 2006-06-22 14:26:44 +0200 (Thu, 22 Jun 2006)
New Revision: 3150

Log:
- Implemented suggestion #8526: retreiving the dbHandler from the 
persistentObject for transaction support


Modified:
   trunk/PersistentObject/ChangeLog
   trunk/PersistentObject/src/persistent_session.php
   trunk/PersistentObject/tests/persistent_session_test.php

Modified: trunk/PersistentObject/ChangeLog
===================================================================
--- trunk/PersistentObject/ChangeLog    2006-06-20 11:16:48 UTC (rev 3149)
+++ trunk/PersistentObject/ChangeLog    2006-06-22 12:26:44 UTC (rev 3150)
@@ -1,3 +1,8 @@
+1.1.1 - [RELEASEDATE]
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+       
+- Implemented suggestion #8526: retreiving the dbHandler from the 
persistentObject for transaction support
+       
 1.1 - Monday 12 June 2006
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

Modified: trunk/PersistentObject/src/persistent_session.php
===================================================================
--- trunk/PersistentObject/src/persistent_session.php   2006-06-20 11:16:48 UTC 
(rev 3149)
+++ trunk/PersistentObject/src/persistent_session.php   2006-06-22 12:26:44 UTC 
(rev 3150)
@@ -15,29 +15,22 @@
  * the object is already persistent you can store it using update() which 
results in
  * an UPDATE query. If you want to query persistent objects you can use the 
find methods.
  *
- * @todo if there is a high probability that an exception was caused by
- *       a bad definition or by a bad set/getState method we should run a 
checker or some sort.
- * @todo - remove required and default value fields in definition for now
+ * The ezcPersistentSession class has the following properties:
+ * - <b>database</b> <i>ezcDbHandler</i>, the database handler set in the 
constructor.
+ * - <b>definitionManager</b> <i>ezcPersistentDefinitionManager</i>, the 
persistent definition manager set in the constructor.
  *
  * @package PersistentObject
  */
 class ezcPersistentSession
 {
     /**
-     * The database instance this session works on.
+     * Holds the properties of this class.
      *
-     * @var PDO
+     * @var array(string=>mixed)
      */
-    private $db = null;
+    private $properties = array();
 
     /**
-     * The persistent object definition manager.
-     *
-     * @var ezcPersistentDefinitionManager
-     */
-    private $manager = null;
-
-    /**
      * Constructs a new persistent session that works on the database $db.
      *
      * The $manager provides valid persistent object definitions to the
@@ -48,11 +41,56 @@
      */
     public function __construct( PDO $db, ezcPersistentDefinitionManager 
$manager )
     {
-        $this->db = $db;
-        $this->manager = $manager;
+        $this->properties['database'] = $db;
+        $this->properties['definitionManager'] = $manager;
     }
 
     /**
+     * Sets the property $name to $value.
+     *
+     * @throws ezcBasePropertyNotFoundException if the property does not exist.
+     * @param string $name
+     * @param mixed $value
+     * @return void
+     */
+    public function __set( $name, $value )
+    {
+        switch ( $name )
+        {
+            case 'database':
+            case 'definitionManager':
+                throw new ezcBasePropertyPermissionException( $name, 
ezcBasePropertyPermissionException::READ );
+                break;
+            default:
+                throw new ezcBasePropertyNotFoundException( $name );
+                break;
+        }
+
+    }
+
+    /**
+     * Returns the property $name.
+     *
+     * @throws ezcBasePropertyNotFoundException if the property does not exist.
+     * @param string $name
+     * @return mixed
+     */
+    public function __get( $name )
+    {
+        switch ( $name )
+        {
+            case 'database':
+            case 'definitionManager':
+                return isset( $this->properties[$name] ) ? 
$this->properties[$name] : null;
+                break;
+
+            default:
+                throw new ezcBasePropertyNotFoundException( $name );
+                break;
+        }
+    }
+
+    /**
      * Deletes the persistent object $pObject.
      *
      * This method will perform a DELETE query based on the identifier
@@ -69,7 +107,7 @@
      */
     public function delete( $pObject )
     {
-        $def = $this->manager->fetchDefinition( get_class( $pObject ) ); // 
propagate exception
+        $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) ); // propagate exception
         $state = $pObject->getState();
         $idValue = $state[$def->idProperty->propertyName];
 
@@ -81,7 +119,7 @@
         }
 
         // create and execute query
-        $q = $this->db->createDeleteQuery();
+        $q = $this->database->createDeleteQuery();
         $q->deleteFrom( $def->table )
             ->where( $q->expr->eq( $def->idProperty->columnName, 
$q->bindValue( $idValue ) ) );
 
@@ -119,10 +157,10 @@
      */
     public function createDeleteQuery( $class )
     {
-        $def = $this->manager->fetchDefinition( $class ); // propagate 
exception
+        $def = $this->definitionManager->fetchDefinition( $class ); // 
propagate exception
 
         // init query
-        $q = $this->db->createDeleteQuery();
+        $q = $this->database->createDeleteQuery();
         $q->setAliases( $this->generateAliasMap( $def ) );
         $q->deleteFrom( $def->table );
 
@@ -174,10 +212,10 @@
      */
     public function createUpdateQuery( $class )
     {
-        $def = $this->manager->fetchDefinition( $class ); // propagate 
exception
+        $def = $this->definitionManager->fetchDefinition( $class ); // 
propagate exception
 
         // init query
-        $q = $this->db->createUpdateQuery();
+        $q = $this->database->createUpdateQuery();
         $q->setAliases( $this->generateAliasMap( $def ) );
         $q->update( $def->table );
 
@@ -230,10 +268,10 @@
      */
     public function createFindQuery( $class )
     {
-        $def = $this->manager->fetchDefinition( $class ); // propagate 
exception
+        $def = $this->definitionManager->fetchDefinition( $class ); // 
propagate exception
 
         // init query
-        $q = $this->db->createSelectQuery();
+        $q = $this->database->createSelectQuery();
         $q->setAliases( $this->generateAliasMap( $def ) );
         $q->select( $this->getColumnsFromDefinition( $def ) )->from( 
$def->table );
 
@@ -260,7 +298,7 @@
      */
     public function find( ezcQuerySelect $query, $class )
     {
-        $def = $this->manager->fetchDefinition( $class ); // propagate 
exception
+        $def = $this->definitionManager->fetchDefinition( $class ); // 
propagate exception
 
         try
         {
@@ -299,7 +337,7 @@
      */
     public function findIterator( ezcQuerySelect $query, $class )
     {
-        $def = $this->manager->fetchDefinition( $class ); // propagate 
exception
+        $def = $this->definitionManager->fetchDefinition( $class ); // 
propagate exception
         try
         {
             $stmt = $query->prepare();
@@ -323,7 +361,7 @@
      */
     public function load( $class, $id )
     {
-        $def = $this->manager->fetchDefinition( $class ); // propagate 
exception
+        $def = $this->definitionManager->fetchDefinition( $class ); // 
propagate exception
         $object = new $def->class;
         $this->loadIntoObject( $object, $id );
         return $object;
@@ -374,8 +412,8 @@
             throw new ezcPersistentQueryException( "The parameter 'id' was not 
a valid integer." );
         }
 
-        $def = $this->manager->fetchDefinition( get_class( $pObject ) ); // 
propagate exception
-        $q = $this->db->createSelectQuery();
+        $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) ); // propagate exception
+        $q = $this->database->createSelectQuery();
         $q->select( $this->getColumnsFromDefinition( $def ) )->from( 
$def->table )
             ->where( $q->expr->eq( $def->idProperty->columnName,
                                    $q->bindValue( $id ) ) );
@@ -429,7 +467,7 @@
      */
     public function refresh( $pObject )
     {
-        $def = $this->manager->fetchDefinition( get_class( $pObject ) ); // 
propagate exception
+        $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) ); // propagate exception
         $state = $pObject->getState();
         $idValue = $state[$def->idProperty->propertyName];
         if ( $idValue !== null )
@@ -457,7 +495,7 @@
      */
     public function save( $pObject )
     {
-        $def = $this->manager->fetchDefinition( get_class( $pObject ) );// 
propagate exception
+        $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) );// propagate exception
         $state = $pObject->getState();
         $idValue = $state[$def->idProperty->propertyName];
 
@@ -473,7 +511,7 @@
             }
         }
 
-        if ( $idGenerator->checkPersistence( $def, $this->db, $state ) )
+        if ( $idGenerator->checkPersistence( $def, $this->database, $state ) )
         {
             $class = get_class( $pObject );
             throw new ezcPersistentObjectAlreadyPersistentException( $class );
@@ -481,7 +519,7 @@
 
 
         // set up and execute the query
-        $q = $this->db->createInsertQuery();
+        $q = $this->database->createInsertQuery();
         $q->insertInto( $def->table );
         foreach ( $state as $name => $value )
         {
@@ -492,9 +530,9 @@
             }
         }
 
-        $this->db->beginTransaction();
+        $this->database->beginTransaction();
         // let presave id generator do its work
-        $idGenerator->preSave( $def, $this->db, $q );
+        $idGenerator->preSave( $def, $this->database, $q );
 
         // execute the insert query
         try
@@ -504,21 +542,21 @@
         }
         catch ( PDOException $e )
         {
-            $this->db->rollback();
+            $this->database->rollback();
             throw new ezcPersistentObjectException( "The insert query 
failed.", $e->getMessage() );
         }
 
         // fetch the newly created id, and set it to the object
-        $id = $idGenerator->postSave( $def, $this->db );
+        $id = $idGenerator->postSave( $def, $this->database );
         if ( $id === null )
         {
-            $this->db->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->db->commit();
+        $this->database->commit();
 
         $state[$def->idProperty->propertyName] = $id;
         $pObject->setState( $state );
@@ -538,7 +576,7 @@
      */
     public function saveOrUpdate( $pObject )
     {
-        $def = $this->manager->fetchDefinition( get_class( $pObject ) );// 
propagate exception
+        $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) );// propagate exception
         $state = $pObject->getState();
         $idValue = $state[$def->idProperty->propertyName];
         if ( $idValue === null )
@@ -562,7 +600,7 @@
      */
     public function update( $pObject )
     {
-        $def = $this->manager->fetchDefinition( get_class( $pObject ) ); // 
propagate exception
+        $def = $this->definitionManager->fetchDefinition( get_class( $pObject 
) ); // propagate exception
         $state = $pObject->getState();
         $idValue = $state[$def->idProperty->propertyName];
 
@@ -573,7 +611,7 @@
         }
 
         // set up and execute the query
-        $q = $this->db->createUpdateQuery();
+        $q = $this->database->createUpdateQuery();
         $q->update( $def->table );
         foreach ( $state as $name => $value )
         {

Modified: trunk/PersistentObject/tests/persistent_session_test.php
===================================================================
--- trunk/PersistentObject/tests/persistent_session_test.php    2006-06-20 
11:16:48 UTC (rev 3149)
+++ trunk/PersistentObject/tests/persistent_session_test.php    2006-06-22 
12:26:44 UTC (rev 3150)
@@ -493,6 +493,34 @@
         $this->assertEquals( 603.70, (float)$objects[0]->decimal );
         $this->assertEquals( 'Ukraine has a long coastline to the black see.', 
$objects[0]->text );
     }
+
+    public function testDatabaseProperty()
+    {
+        $db = ezcDbInstance::get();
+        $session = new ezcPersistentSession( $db,
+                                             new ezcPersistentCodeManager( 
dirname( __FILE__ ) . "/data/" ) );
+        $this->assertEquals( (string)$db, (string)$session->database );
+        try
+        {
+            $session->database = $db;
+            $this->fail( "Did not get exception when expected" );
+        }catch( ezcBasePropertyPermissionException $e ){
+        }
+    }
+
+    public function testDefinitionManagerProperty()
+    {
+        $db = ezcDbInstance::get();
+        $manager = new ezcPersistentCodeManager( dirname( __FILE__ ) . 
"/data/" );
+        $session = new ezcPersistentSession( $db, $manager );
+        $this->assertEquals( (string)$manager, 
(string)$session->definitionManager );
+        try
+        {
+            $session->definitionManager = $manager;
+            $this->fail( "Did not get exception when expected" );
+        }catch( ezcBasePropertyPermissionException $e ){
+        }
+    }
 }
 
 ?>

-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to