Changeset 881

Show
Ignore:
Timestamp:
08/23/07 16:26:38 (1 year ago)
Author:
mikey
Message:

stubDatabaseFinder::findByCriterion() now returns an instance of stubDatabaseFinderResult instead of an array - allows to create the entity instance when it is required, not all at once

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/experiments/people/mikey/persistence/MyNewsArticle.php

    r512 r881  
    213213     * @param   stubDatabaseFinder  $finder  the finder to use 
    214214     * @param   int                 $status  status that the instances must have 
    215      * @return  array<MyNewsArticle> 
     215     * @return  stubDatabaseFinderResult<MyNewsArticle> 
    216216     */ 
    217217    public static function findByStatus(stubDatabaseFinder $finder, $status) 
  • trunk/experiments/people/mikey/persistence/listEntries.php

    r512 r881  
    1212$finder       = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 
    1313$newsArticles = MyNewsArticle::findByStatus($finder, 5); 
    14 var_dump($newsArticles); 
     14var_dump($newsArticles->count()); 
     15foreach ($newsArticles as $newsArticle) { 
     16    var_dump($newsArticle); 
     17
    1518?> 
  • trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php

    r482 r881  
    1111                      'net.stubbles.rdbms.criteria.stubEqualCriterion', 
    1212                      'net.stubbles.rdbms.persistence.stubPersistable', 
    13                       'net.stubbles.rdbms.persistence.stubSetterMethodFactory', 
     13                      'net.stubbles.rdbms.persistence.stubSetterMethodHelper', 
    1414                      'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 
    1515                      'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', 
    1616                      'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 
    1717                      'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderException', 
     18                      'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderResult', 
    1819                      'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 
    1920                      'net.stubbles.rdbms.querybuilder.stubDatabaseSelect', 
     
    9192    { 
    9293        $refObject = $persistable->getClass(); 
    93         $select    = $this->createSelect($refObject); 
     94        $setterMethodHelper = new stubSetterMethodHelper($refObject); 
     95        $select    = $this->createSelect($refObject, $setterMethodHelper); 
    9496        try { 
    9597            $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createSelect($select)); 
     
    104106        } 
    105107         
    106         $select->fillPersistable($persistable, $data); 
     108        $setterMethodHelper->applySetterMethods($persistable, $data); 
     109        $persistable->setPersistent(true); 
    107110    } 
    108111 
     
    110113     * finds all instances of $persistableClassName by given criterion 
    111114     * 
    112      * @param   stubCriterion                $criterion 
    113      * @param   string                        $persistableClassName  non qualified classname of the persistable class to find instances of 
    114      * @param   array                        $arguments             optional  arguments for constructor 
    115      * @return  array<$persistableClassName>  list of instances of $persistableClassName found with $criterion 
     115     * @param   stubCriterion                $criterion 
     116     * @param   string                       $persistableClassName  non qualified classname of the persistable class to find instances of 
     117     * @param   array                        $arguments             optional  arguments for constructor 
     118     * @return  stubDatabaseFinderResult     list of instances of $persistableClassName found with $criterion 
    116119     * @throws  stubDatabaseFinderException 
    117120     * @throws  stubPersistenceException 
     
    129132        } 
    130133         
    131         $select = $this->createSelect($refClass); 
     134        $setterMethodHelper = new stubSetterMethodHelper($refClass); 
     135        $select             = $this->createSelect($refClass, $setterMethodHelper); 
    132136        $select->addCriterion($criterion); 
    133137        try { 
     
    140144         
    141145        if (false === $data) { 
    142             return array(); 
    143         } 
    144          
    145         $persistables = array(); 
    146         foreach ($data as $persistableData) { 
    147             try { 
    148                 if (null == $arguments) { 
    149                     $persistable = $refClass->newInstance(); 
    150                 } else { 
    151                     $persistable = $refClass->newInstanceArgs($arguments); 
    152                 } 
    153             } catch (ReflectionException $re) { 
    154                 throw new stubDatabaseFinderException('Can not create a new instance of ' . $refClass->getFullQualifiedClassName(), $re); 
    155             } 
    156              
    157             if (null == $persistable) { 
    158                 throw new stubDatabaseFinderException('Can not create a new instance of ' . $refClass->getFullQualifiedClassName()); 
    159             } 
    160              
    161             $select->fillPersistable($persistable, $persistableData); 
    162             $persistables[] = $persistable; 
    163         } 
    164          
    165         return $persistables; 
     146            $data = array(); 
     147        } 
     148         
     149        $finderResult = new stubDatabaseFinderResult($refClass, $data, $setterMethodHelper, $arguments); 
     150        return $finderResult; 
    166151    } 
    167152 
     
    170155     * 
    171156     * @param   stubBaseReflectionClass      $refBaseClass 
     157     * @param   stubSetterMethodHelper       $setterMethodHelper 
    172158     * @return  stubDatabaseSelect 
    173159     * @throws  stubDatabaseFinderException 
    174160     */ 
    175     protected function createSelect(stubBaseReflectionClass $refBaseClass
     161    protected function createSelect(stubBaseReflectionClass $refBaseClass, stubSetterMethodHelper $setterMethodHelper
    176162    { 
    177163        if ($refBaseClass->hasAnnotation('DBTable') == false) { 
     
    199185            } 
    200186             
    201            $select->addSetterMethod($dbColumn->getName(), stubSetterMethodFactory::create($dbColumn, $refBaseClass, $method->getName())); 
     187            $setterMethodHelper->addSetterMethod($dbColumn, $method->getName()); 
    202188        } 
    203189         
  • trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php

    r482 r881  
    1010                      'net.stubbles.rdbms.criteria.stubEqualCriterion', 
    1111                      'net.stubbles.rdbms.persistence.stubPersistable', 
    12                       'net.stubbles.rdbms.persistence.stubSetterMethodFactory', 
     12                      'net.stubbles.rdbms.persistence.stubSetterMethodHelper', 
    1313                      'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 
    1414                      'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', 
     
    133133            } elseif (null === $value && $persistable->isPersistent() == false) { 
    134134                $value = $dbColumn->getDefaultValue(); 
    135                 $setterMethod = stubSetterMethodFactory::create($dbColumn, $refObject, $method->getName()); 
     135                $setterMethod = stubSetterMethodHelper::create($dbColumn, $refObject, $method->getName()); 
    136136                $setterMethod->invoke($persistable, $value); 
    137137            } 
  • trunk/src/main/php/net/stubbles/rdbms/persistence/stubSetterMethodHelper.php

    r482 r881  
    11<?php 
    22/** 
    3  * Factory to get the appropriate setter method equivalent to a getter method
     3 * Helper class to work with setter methods
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    99stubClassLoader::load('net.stubbles.rdbms.querybuilder.stubDatabaseTableColumn', 
    1010                      'net.stubbles.rdbms.persistence.stubPersistenceException', 
    11                       'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 
    1211                      'net.stubbles.reflection.reflection' 
    1312); 
    1413/** 
    15  * Factory to get the appropriate setter method equivalent to a getter method
     14 * Helper class to work with setter methods
    1615 * 
    17  * @static 
    1816 * @package     stubbles 
    1917 * @subpackage  rdbms_persistence 
    2018 */ 
    21 class stubSetterMethodFactory 
     19class stubSetterMethodHelper extends stubBaseObject 
    2220{ 
    2321    /** 
    24      * creater setter method from given annotation for given class 
     22     * reflection instance for class to collect setter methods of 
    2523     * 
    26      * @param   stubDatabaseTableColumn  $dbColumn          description of column 
    27      * @param   stubBaseReflectionClass  $refBaseClass      the class that contains the method 
    28      * @param   string                   $getterMethodName  name of the method annotated with DBColumn 
    29      * @return  stubReflectionMethod     reflection instance for the setter method 
     24     * @var  stubBaseReflectionClass 
     25     */ 
     26    protected $refBaseClass; 
     27    /** 
     28     * list of setter methods 
     29     * 
     30     * @var  array<string,ReflectionMethod> 
     31     */ 
     32    protected $setterMethods = array(); 
     33 
     34    /** 
     35     * constructor 
     36     * 
     37     * @param  stubBaseReflectionClass  $refBaseClass  reflection instance for class to collect setter methods of 
     38     */ 
     39    public function __construct(stubBaseReflectionClass $refBaseClass) 
     40    { 
     41        $this->refBaseClass = $refBaseClass; 
     42    } 
     43 
     44    /** 
     45     * adds a setter method for given getter method 
     46     * 
     47     * @param   stubDatabaseTableColumn   $dbColumn          db column annotation from getter method 
     48     * @param   string                    $getterMethodName  name of getter method 
     49     * @throws  stubPersistenceException 
     50     */ 
     51    public function addSetterMethod(stubDatabaseTableColumn $dbColumn, $getterMethodName) 
     52    { 
     53        $this->setterMethods[$dbColumn->getName()] = self::create($dbColumn, $this->refBaseClass, $getterMethodName); 
     54    } 
     55 
     56    /** 
     57     * applies the setter methods with given data on given entity 
     58     * 
     59     * @param  object  $entity  entity to apply setter methods on 
     60     * @param  array   $data    data to apply setter methods with 
     61     */ 
     62    public function applySetterMethods($entity, array $data) 
     63    { 
     64        $className = $this->refBaseClass->getName(); 
     65        if (($entity instanceof $className) === false) { 
     66            throw new stubPersistenceException('Given entity must be of type ' . $this->refBaseClass->getFullQualifiedClassName()); 
     67        } 
     68         
     69        foreach ($this->setterMethods as $columnName => $setterMethod) { 
     70            if (isset($data[$columnName]) == false) { 
     71                continue; 
     72            } 
     73             
     74            $setterMethod->invoke($entity, $data[$columnName]); 
     75        } 
     76    } 
     77 
     78    /** 
     79     * create setter method from given annotation for given class 
     80     * 
     81     * @param   stubDatabaseTableColumn   $dbColumn          description of column 
     82     * @param   stubBaseReflectionClass   $refBaseClass      the class that contains the method 
     83     * @param   string                    $getterMethodName  name of the method annotated with DBColumn 
     84     * @return  stubReflectionMethod      reflection instance for the setter method 
     85     * @throws  stubPersistenceException 
    3086     */ 
    3187    public static function create(stubDatabaseTableColumn $dbColumn, stubBaseReflectionClass $refBaseClass, $getterMethodName) 
  • trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelect.php

    r536 r881  
    3333     */ 
    3434    protected $criterion; 
    35     /** 
    36      * list of setter methods 
    37      * 
    38      * @var  array<string,ReflectionMethod> 
    39      */ 
    40     protected $setterMethods = array(); 
    4135 
    4236    /** 
     
    118112        return $this->criterion; 
    119113    } 
    120  
    121     /** 
    122      * adds a setter method 
    123      * 
    124      * @param  string            $columnName    name of the column the setter method is responsible for 
    125      * @param  ReflectionMethod  $setterMethod 
    126      */ 
    127     public function addSetterMethod($columnName, ReflectionMethod $setterMethod) 
    128     { 
    129         $this->setterMethods[$columnName] = $setterMethod; 
    130     } 
    131  
    132     /** 
    133      * fills the persistable object with the given data 
    134      * 
    135      * @param  stubPersistable  $persistable 
    136      * @param  array            $data 
    137      */ 
    138     public function fillPersistable(stubPersistable $persistable, array $data) 
    139     { 
    140         foreach ($this->setterMethods as $columnName => $setterMethod) { 
    141             if (isset($data[$columnName]) == false) { 
    142                 continue; 
    143             } 
    144              
    145             $setterMethod->invoke($persistable, $data[$columnName]); 
    146         } 
    147          
    148         $persistable->setPersistent(true); 
    149     } 
    150114} 
    151115?> 
  • trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php

    r746 r881  
    4343         
    4444        // persistence 
     45        $this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php'); 
    4546        $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 
    4647        $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php

    r655 r881  
    181181        $mockResult->setReturnValueAt(0, 'fetchAll', false); 
    182182        $mockResult->setReturnValueAt(1, 'fetchAll', array(array('bar' => 'Here is bar.', 'default' => 'And this is default.'))); 
    183         $data = $this->dbFinder->findByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
    184         $this->assertEqual($data, array()); 
    185         $data = $this->dbFinder->findByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
    186         $this->assertTrue($data[0]->isPersistent()); 
    187         $this->assertEqual($data[0]->withAnnotation(), 'Here is bar.'); 
    188         $this->assertEqual($data[0]->withDefaultValue(), 'And this is default.'); 
     183        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
     184        $this->assertEqual($finderResult->count(), 0); 
     185        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockSinglePrimaryKeyPersistable'); 
     186        $this->assertEqual($finderResult->count(), 1); 
     187        $data = $finderResult->current(); 
     188        $this->assertTrue($data->isPersistent()); 
     189        $this->assertEqual($data->withAnnotation(), 'Here is bar.'); 
     190        $this->assertEqual($data->withDefaultValue(), 'And this is default.'); 
    189191        $select = $this->mockQueryBuilder->getSelect(); 
    190192        $this->assertEqual($select->getBaseTableName(), 'foo'); 
     
    213215        $this->mockConnection->setReturnValue('query', $mockResult); 
    214216        $mockResult->setReturnValue('fetchAll', array(array())); 
    215         $data = $this->dbFinder->findByCriterion($mockCriterion, 'MockInstanceArgsPersistable', array('foo', 'bar')); 
    216         $this->assertEqual($data[0]->getArg1(), 'foo'); 
    217         $this->assertEqual($data[0]->getArg2(), 'bar'); 
    218         $data = $this->dbFinder->findByCriterion($mockCriterion, 'MockInstanceArgsPersistable', array('foo')); 
    219         $this->assertEqual($data[0]->getArg1(), 'foo'); 
    220         $this->assertNull($data[0]->getArg2()); 
     217        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockInstanceArgsPersistable', array('foo', 'bar')); 
     218        $data = $finderResult->current(); 
     219        $this->assertEqual($data->getArg1(), 'foo'); 
     220        $this->assertEqual($data->getArg2(), 'bar'); 
     221        $finderResult = $this->dbFinder->findByCriterion($mockCriterion, 'MockInstanceArgsPersistable', array('foo')); 
     222        $data = $finderResult->current(); 
     223        $this->assertEqual($data->getArg1(), 'foo'); 
     224        $this->assertNull($data->getArg2()); 
    221225    } 
    222226} 
  • trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelectTestCase.php

    r482 r881  
    6767        $this->assertTrue($this->select->hasCriterion()); 
    6868    } 
    69  
    70     /** 
    71      * test that the setter methods are applied as expected 
    72      */ 
    73     public function testSetterMethods() 
    74     { 
    75         $persistable = new MockSinglePrimaryKeyPersistable(); 
    76         $this->select->addSetterMethod('id', new ReflectionMethod('MockSinglePrimaryKeyPersistable', 'setId')); 
    77         $this->select->addSetterMethod('bar', new ReflectionMethod('MockSinglePrimaryKeyPersistable', 'setBar')); 
    78         $this->select->addSetterMethod('default', new stubReflectionMethod('MockSinglePrimaryKeyPersistable', 'setDefaultValue')); 
    79         $this->select->fillPersistable($persistable, array('id' => 909, 'default' => 'foo')); 
    80         $this->assertEqual($persistable->getId(), 909); 
    81         $this->assertEqual($persistable->withAnnotation(), 'this is bar'); 
    82         $this->assertEqual($persistable->withDefaultValue(), 'foo'); 
    83         $this->assertTrue($persistable->isPersistent()); 
    84     } 
    8569} 
    8670?>