Changeset 1896

Show
Ignore:
Timestamp:
10/23/08 19:37:46 (3 months ago)
Author:
mikey
Message:

implement enhancement #180: persistence API should support date instances

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • framework/trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php

    r1763 r1896  
    77 * @subpackage  rdbms_persistence_serializer 
    88 */ 
    9 stubClassLoader::load('net::stubbles::rdbms::stubDatabaseConnection', 
     9stubClassLoader::load('net::stubbles::lang::types::stubDate', 
     10                      'net::stubbles::rdbms::stubDatabaseConnection', 
    1011                      'net::stubbles::rdbms::criteria::stubEqualCriterion', 
    1112                      'net::stubbles::rdbms::persistence::stubPersistenceHelper', 
     
    213214            } catch (ReflectionException $re) { 
    214215                throw new stubDatabaseSerializerException('Can not get return value of ' . $entityClass->getFullQualifiedClassName() . '::' . $method->getName() . '(), invocation failed.', $re); 
     216            } 
     217             
     218            if ($value instanceof stubDate) { 
     219                $value = $value->format('Y-m-d H:i:s'); 
    215220            } 
    216221             
     
    264269            // fill default values into entity and table row 
    265270            foreach ($defaultValues as $defaultValue) { 
    266                 $defaultValue['setterMethod']->invokeArgs($entity, array($defaultValue['defaultValue'])); 
     271                // only reset entity with default value if default value is not null 
     272                if (null !== $defaultValue['defaultValue']) { 
     273                    $defaultValue['setterMethod']->invokeArgs($entity, array($defaultValue['defaultValue'])); 
     274                } 
     275                 
    267276                $tableRow->setColumn($defaultValue['column'], $defaultValue['defaultValue']); 
    268277            } 
  • framework/trunk/src/main/php/net/stubbles/rdbms/persistence/stubPersistenceHelper.php

    r1763 r1896  
    7777            return null; 
    7878        } else { 
    79             $column     = new stubDatabaseTableColumn(); 
     79            $column = new stubDatabaseTableColumn(); 
    8080            $column->setName($this->getPropertyName($method->getName())); 
    8181            $returnType = $method->getReturnType(); 
     
    8686             
    8787            if ($returnType instanceof stubReflectionClass) { 
    88                 // not supported yet 
    89                 throw new stubPersistenceException('Returning classes from entity getter methods is currently not supported. Sorry. :('); 
    90             } 
    91              
    92             switch ($returnType->value()) { 
    93                 case 'int': 
    94                     $column->setType('INT'); 
    95                     $column->setSize(10); 
    96                     break; 
     88                if ($returnType->getName() !== 'stubDate') { 
     89                    // not supported yet 
     90                    throw new stubPersistenceException('Returning classes from entity getter methods is currently not supported, except for net::stubbles::lang::types::stubDate. Sorry. :('); 
     91                } 
    9792                 
    98                 case 'float': 
    99                     $column->setType('FLOAT'); 
    100                     $column->setSize(10); 
    101                     break; 
    102                  
    103                 case 'bool': 
    104                     $column->setType('TINYINT'); 
    105                     $column->setSize(1); 
    106                     break; 
    107                  
    108                 default: 
    109                     $column->setType('VARCHAR'); 
    110                     $column->setSize(255); 
     93                $column->setType('DATETIME'); 
     94            } else { 
     95                switch ($returnType->value()) { 
     96                    case 'int': 
     97                        $column->setType('INT'); 
     98                        $column->setSize(10); 
     99                        break; 
     100                     
     101                    case 'float': 
     102                        $column->setType('FLOAT'); 
     103                        $column->setSize(10); 
     104                        break; 
     105                     
     106                    case 'bool': 
     107                        $column->setType('TINYINT'); 
     108                        $column->setSize(1); 
     109                        break; 
     110                     
     111                    default: 
     112                        $column->setType('VARCHAR'); 
     113                        $column->setSize(255); 
     114                } 
    111115            } 
    112116        } 
  • framework/trunk/src/main/php/net/stubbles/rdbms/persistence/stubSetterMethodHelper.php

    r1763 r1896  
    77 * @subpackage  rdbms_persistence 
    88 */ 
    9 stubClassLoader::load('net::stubbles::rdbms::querybuilder::stubDatabaseTableColumn', 
     9stubClassLoader::load('net::stubbles::lang::types::stubDate', 
     10                      'net::stubbles::rdbms::querybuilder::stubDatabaseTableColumn', 
    1011                      'net::stubbles::rdbms::persistence::stubPersistenceException', 
    1112                      'net::stubbles::reflection::reflection' 
     
    2829     * list of setter methods 
    2930     * 
    30      * @var  array<string,ReflectionMethod
     31     * @var  array<string,array<string,mixed>
    3132     */ 
    3233    protected $setterMethods = array(); 
     
    5152    public function addSetterMethod(stubDatabaseTableColumn $dbColumn, $getterMethodName) 
    5253    { 
    53         $this->setterMethods[$dbColumn->getName()] = self::create($dbColumn, $this->refBaseClass, $getterMethodName); 
     54        $this->setterMethods[$dbColumn->getName()] = array('setterMethod' => self::create($dbColumn, $this->refBaseClass, $getterMethodName), 
     55                                                           'type'         => $dbColumn->getType() 
     56                                                     ); 
    5457    } 
    5558 
     
    6871        } 
    6972         
    70         foreach ($this->setterMethods as $columnName => $setterMethod) { 
     73        foreach ($this->setterMethods as $columnName => $info) { 
    7174            if (isset($data[$columnName]) == false) { 
    7275                continue; 
    7376            } 
    7477             
    75             $setterMethod->invoke($entity, $data[$columnName]); 
     78            if ('DATETIME' === $info['type']) { 
     79                $data[$columnName] = new stubDate($data[$columnName]); 
     80            } 
     81             
     82            $info['setterMethod']->invoke($entity, $data[$columnName]); 
    7683        } 
    7784    } 
  • framework/trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyEntity.php

    r1763 r1896  
    66 * @subpackage  rdbms_persistence_test 
    77 */ 
     8stubClassLoader::load('net::stubbles::lang::types::stubDate'); 
    89/** 
    910 * This is a mocked entity that has some annotations. 
     
    3435     */ 
    3536    protected $id; 
     37    /** 
     38     * a date instance 
     39     * 
     40     * @var  stubDate 
     41     */ 
     42    protected $date; 
    3643 
    3744    /** 
     
    101108        return $this->defaultValue; 
    102109    } 
     110 
     111    /** 
     112     * sets the date instance 
     113     * 
     114     * @param  stubDate  $date 
     115     */ 
     116    public function setDate(stubDate $date) 
     117    { 
     118        $this->date = $date; 
     119    } 
     120 
     121    /** 
     122     * method that returns a date instance 
     123     * 
     124     * @return  stubDate 
     125     * @DBColumn(name='date', type='DATETIME', setterMethod='setDate') 
     126     */ 
     127    public function withDate() 
     128    { 
     129        return $this->date; 
     130    } 
    103131} 
    104132?> 
  • framework/trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php

    r1763 r1896  
    167167        $this->assertEquals($tableDescription->getName(), 'foo'); 
    168168        $columns = $tableDescription->getColumns(); 
    169         $this->assertEquals(3, count($columns)); 
     169        $this->assertEquals(4, count($columns)); 
    170170        $this->assertEquals('id', $columns[1]->getName()); 
    171171        $this->assertTrue($columns[1]->isPrimaryKey()); 
     
    174174        $this->assertEquals('default', $columns[3]->getName()); 
    175175        $this->assertFalse($columns[3]->isPrimaryKey()); 
     176        $this->assertEquals('date', $columns[4]->getName()); 
     177        $this->assertEquals('DATETIME', $columns[4]->getType()); 
     178        $this->assertFalse($columns[3]->isPrimaryKey()); 
    176179    } 
    177180} 
  • framework/trunk/src/test/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializerTestCase.php

    r1763 r1896  
    190190        $this->assertTrue(isset($tableRows['foo'])); 
    191191        $this->assertEquals('mockId', $singlePrimaryKeyEntity->getId()); 
    192         $this->assertEquals(array('bar' => 'this is bar', 'default' => 'example'), $tableRows['foo']->getColumns()); 
     192        $this->assertEquals(array('bar' => 'this is bar', 'default' => 'example', 'date' => null), $tableRows['foo']->getColumns()); 
    193193        $this->assertFalse($tableRows['foo']->hasCriterion()); 
    194194    } 
     
    213213        $this->assertTrue(isset($tableRows['foo'])); 
    214214        $this->assertEquals('mockId', $singlePrimaryKeyEntity->getId()); 
    215         $this->assertEquals(array('id' => 'mockId', 'bar' => 'this is bar', 'default' => 'example'), $tableRows['foo']->getColumns()); 
     215        $this->assertEquals(array('id' => 'mockId', 'bar' => 'this is bar', 'default' => 'example', 'date' => null), $tableRows['foo']->getColumns()); 
    216216        $this->assertFalse($tableRows['foo']->hasCriterion()); 
    217217    } 
     
    227227        $singlePrimaryKeyEntity->setId('mockId'); 
    228228        $singlePrimaryKeyEntity->setDefaultValue('anotherExample'); 
     229        $singlePrimaryKeyEntity->setDate(new stubDate('2008-10-23 19:27:22')); 
    229230        $this->mockConnection->expects($this->never())->method('getLastInsertId'); 
    230231        $this->mockConnection->expects($this->once())->method('exec'); 
     
    237238        $this->assertTrue(isset($tableRows['foo'])); 
    238239        $this->assertEquals('mockId', $singlePrimaryKeyEntity->getId()); 
    239         $this->assertEquals(array('bar' => 'this is bar', 'default' => 'anotherExample'), $tableRows['foo']->getColumns()); 
     240        $this->assertEquals(array('bar' => 'this is bar', 'default' => 'anotherExample', 'date' => '2008-10-23 19:27:22'), $tableRows['foo']->getColumns()); 
    240241        $this->assertTrue($tableRows['foo']->hasCriterion()); 
    241242        $this->assertEquals("(`foo`.`id` = 'mockId')", $tableRows['foo']->getCriterion()->toSQL()); 
     
    260261        $this->assertTrue(isset($tableRows['foo'])); 
    261262        $this->assertEquals('mockId', $singlePrimaryKeyEntity->getId()); 
    262         $this->assertEquals(array('bar' => 'this is bar', 'default' => 'example'), $tableRows['foo']->getColumns()); 
     263        $this->assertEquals(array('bar' => 'this is bar', 'default' => 'example', 'date' => null), $tableRows['foo']->getColumns()); 
    263264        $this->assertFalse($tableRows['foo']->hasCriterion()); 
    264265    } 
     
    274275        $singlePrimaryKeyEntity->setId('mockId'); 
    275276        $singlePrimaryKeyEntity->setDefaultValue('anotherExample'); 
     277        $singlePrimaryKeyEntity->setDate(new stubDate('2008-10-23 19:27:22')); 
    276278        $this->mockConnection->expects($this->never())->method('getLastInsertId'); 
    277279        $this->mockConnection->expects($this->once())->method('exec'); 
     
    284286        $this->assertTrue(isset($tableRows['foo'])); 
    285287        $this->assertEquals('mockId', $singlePrimaryKeyEntity->getId()); 
    286         $this->assertEquals(array('bar' => 'this is bar', 'default' => 'anotherExample'), $tableRows['foo']->getColumns()); 
     288        $this->assertEquals(array('bar' => 'this is bar', 'default' => 'anotherExample', 'date' => '2008-10-23 19:27:22'), $tableRows['foo']->getColumns()); 
    287289        $this->assertTrue($tableRows['foo']->hasCriterion()); 
    288290        $this->assertEquals("(`foo`.`id` = 'mockId')", $tableRows['foo']->getCriterion()->toSQL()); 
  • framework/trunk/src/test/php/net/stubbles/rdbms/persistence/stubSetterMethodHelperTestCase.php

    r1763 r1896  
    5151        $refMethod   = new stubReflectionMethod('MockSinglePrimaryKeyEntity', 'withDefaultValue'); 
    5252        $this->setterMethodHelper->addSetterMethod($refMethod->getAnnotation('DBColumn')->getTableColumn(), $refMethod->getName()); 
    53         $this->setterMethodHelper->applySetterMethods($entity, array('id' => 909, 'default' => 'foo', 'ignored' => 'ignored')); 
     53        $refMethod   = new stubReflectionMethod('MockSinglePrimaryKeyEntity', 'withDate'); 
     54        $this->setterMethodHelper->addSetterMethod($refMethod->getAnnotation('DBColumn')->getTableColumn(), $refMethod->getName()); 
     55        $this->setterMethodHelper->applySetterMethods($entity, array('id' => 909, 'default' => 'foo', 'ignored' => 'ignored', 'date' => null)); 
    5456        $this->assertEquals(909, $entity->getId()); 
    5557        $this->assertEquals('this is bar', $entity->withAnnotation()); 
    5658        $this->assertEquals('foo', $entity->withDefaultValue()); 
     59        $this->assertNull($entity->withDate()); 
     60    } 
     61 
     62    /** 
     63     * test that the setter methods are applied as expected 
     64     * 
     65     * @test 
     66     */ 
     67    public function setterMethodsWithDate() 
     68    { 
     69        $entity = new MockSinglePrimaryKeyEntity(); 
     70        $refMethod   = new stubReflectionMethod('MockSinglePrimaryKeyEntity', 'getId'); 
     71        $this->setterMethodHelper->addSetterMethod($refMethod->getAnnotation('DBColumn')->getTableColumn(), $refMethod->getName()); 
     72        $refMethod   = new stubReflectionMethod('MockSinglePrimaryKeyEntity', 'withAnnotation'); 
     73        $this->setterMethodHelper->addSetterMethod($refMethod->getAnnotation('DBColumn')->getTableColumn(), $refMethod->getName()); 
     74        $refMethod   = new stubReflectionMethod('MockSinglePrimaryKeyEntity', 'withDefaultValue'); 
     75        $this->setterMethodHelper->addSetterMethod($refMethod->getAnnotation('DBColumn')->getTableColumn(), $refMethod->getName()); 
     76        $refMethod   = new stubReflectionMethod('MockSinglePrimaryKeyEntity', 'withDate'); 
     77        $this->setterMethodHelper->addSetterMethod($refMethod->getAnnotation('DBColumn')->getTableColumn(), $refMethod->getName()); 
     78        $this->setterMethodHelper->applySetterMethods($entity, array('id' => 909, 'default' => 'foo', 'ignored' => 'ignored', 'date' => '2008-10-23 19:18:09')); 
     79        $this->assertEquals(909, $entity->getId()); 
     80        $this->assertEquals('this is bar', $entity->withAnnotation()); 
     81        $this->assertEquals('foo', $entity->withDefaultValue()); 
     82        $this->assertEquals('2008-10-23 19:18:09', $entity->withDate()->format('Y-m-d H:i:s')); 
    5783    } 
    5884