Changeset 907

Show
Ignore:
Timestamp:
09/11/07 23:48:38 (1 year ago)
Author:
mikey
Message:

major rework toward getting rid of stubPersistable interface

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php

    r903 r907  
    88 */ 
    99stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 
    10                       'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 
    11                       'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 
    12                       'net.stubbles.rdbms.persistence.annotations.stubEntityAnnotation', 
    13                       'net.stubbles.rdbms.persistence.annotations.stubIdAnnotation', 
    14                       'net.stubbles.rdbms.persistence.annotations.stubTransientAnnotation', 
     10                      'net.stubbles.rdbms.persistence.stubPersistenceHelper', 
    1511                      'net.stubbles.rdbms.persistence.creator.stubDatabaseCreatorException', 
    16                       'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 
    17                       'net.stubbles.reflection.reflection' 
     12                      'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory' 
    1813); 
    1914/** 
     
    2318 * @subpackage  rdbms_persistence_creator 
    2419 */ 
    25 class stubDatabaseCreator extends stubBaseObject 
     20class stubDatabaseCreator extends stubPersistenceHelper 
    2621{ 
    2722    /** 
     
    5954     * @return  stubDatabaseFinder 
    6055     */ 
    61     public function getInstance(stubDatabaseConnection $connection, $refresh = false) 
     56    public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 
    6257    { 
    6358        if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { 
     
    7974 
    8075    /** 
    81      * creates the table description from the given class 
     76     * creates the table description from the given entity class 
    8277     * 
    83      * @param   stubBaseReflectionClass  $entityClass 
     78     * @param   stubBaseReflectionClass       $entityClass 
    8479     * @throws  stubDatabaseCreatorException 
     80     * @throws  stubPersistenceException 
    8581     */ 
    8682    public function createTable(stubBaseReflectionClass $entityClass) 
    8783    { 
    8884        if ($entityClass->hasAnnotation('Entity') === false) { 
    89             throw new stubDatabaseCreatorException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 
     85            throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 
    9086        } 
    9187         
    92         if ($entityClass->hasAnnotation('DBTable') === true) { 
    93             $tableDescription = $entityClass->getAnnotation('DBTable')->getTableDescription(); 
    94         } else { 
    95             $tableDescription = new stubDatabaseTableDescription(); 
    96             $tableDescription->setName($entityClass->getName() . 's'); 
    97         } 
    98          
    99         $methods = $entityClass->getMethods(); 
     88        $tableDescription = $this->getTableDescription($entityClass);         
     89        $methods          = $entityClass->getMethods(); 
    10090        foreach ($methods as $method) { 
    101             if ($method->hasAnnotation('Transient') === true) { 
     91            $column = $this->getTableColumn($method); 
     92            if (null === $column) { 
    10293                continue; 
    103             } 
    104              
    105             if ($method->hasAnnotation('DBColumn') === true) { 
    106                 $column = $method->getAnnotation('DBColumn')->getTableColumn(); 
    107             } elseif (substr($method->getName(), 0, 3) !== 'get') { 
    108                 continue; 
    109             } elseif (in_array($method->getDeclaringClass()->getName(), array('stubBaseObject', 'stubSerializableObject'))) { 
    110                 continue; 
    111             } else { 
    112                 $column     = new stubDatabaseTableColumn(); 
    113                 $columnName = str_replace('get', '', $method->getName()); 
    114                 $column->setName(strtolower($columnName{0}) . substr($columnName, 1)); 
    115                 $returnType = $method->getReturnType(); 
    116                 if (null === $returnType) { 
    117                     // no type hint or returns null -> ignore method 
    118                     continue; 
    119                 } 
    120                  
    121                 if ($returnType instanceof stubReflectionClass) { 
    122                     // not supported yet 
    123                     throw new stubDatabaseCreatorException('Returning classes from entity getter methods is currently not supported. Sorry. :('); 
    124                 } 
    125                  
    126                 switch (strtolower($returnType)) { 
    127                     case 'int': 
    128                     case 'integer': 
    129                         $column->setType('INT'); 
    130                         $column->setSize(10); 
    131                         break; 
    132                      
    133                     case 'double': 
    134                     case 'float': 
    135                         $column->setType('FLOAT'); 
    136                         $column->setSize(10); 
    137                         break; 
    138                      
    139                     case 'bool': 
    140                     case 'boolean': 
    141                         $column->setType('TINYINT'); 
    142                         $column->setSize(1); 
    143                         break; 
    144                      
    145                     default: 
    146                         $column->setType('VARCHAR'); 
    147                         $column->setSize(255); 
    148                 } 
    149             } 
    150              
    151             if ($method->hasAnnotation('Id') === true) { 
    152                 $column->setIsPrimaryKey(true); 
    15394            } 
    15495             
  • trunk/src/main/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraser.php

    r655 r907  
    1010                      'net.stubbles.rdbms.criteria.stubCriterion', 
    1111                      'net.stubbles.rdbms.criteria.stubEqualCriterion', 
    12                       'net.stubbles.rdbms.persistence.stubPersistable', 
    13                       'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 
    14                       'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', 
    15                       'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 
     12                      'net.stubbles.rdbms.persistence.stubPersistenceHelper', 
    1613                      'net.stubbles.rdbms.persistence.eraser.stubDatabaseEraserException', 
    17                       'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 
    18                       'net.stubbles.reflection.reflection' 
     14                      'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory' 
    1915); 
    2016/** 
     
    2420 * @subpackage  rdbms_persistence_eraser 
    2521 */ 
    26 class stubDatabaseEraser extends stubBaseObject 
     22class stubDatabaseEraser extends stubPersistenceHelper 
    2723{ 
    2824    /** 
     
    6056     * @return  stubDatabaseEraser 
    6157     */ 
    62     public function getInstance(stubDatabaseConnection $connection, $refresh = false) 
     58    public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 
    6359    { 
    6460        if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { 
     
    8076 
    8177    /** 
    82      * delete a persistable object from database by its primary keys 
     78     * delete an entity from database by its primary keys 
    8379     * 
    84      * @param   stubPersistable              $persistable 
     80     * @param   stubObject                   $entity 
    8581     * @throws  stubDatabaseEraserException 
    8682     * @throws  stubPersistenceException 
    8783     */ 
    88     public function deleteByPrimaryKeys(stubPersistable $persistable
     84    public function deleteByPrimaryKeys(stubObject $entity
    8985    { 
    90         $refObject = $persistable->getClass(); 
    91         if ($refObject->hasAnnotation('DBTable') == false) { 
    92             throw new stubDatabaseEraserException('Can not delete instance of ' . $refObject->getFullQualifiedClassName() . ', class is missing the DBTable annotation.'); 
     86        $entityClass = $entity->getClass(); 
     87        if ($entityClass->hasAnnotation('Entity') === false) { 
     88            throw new stubPersistenceException('Class ' . $entity->getClassName() . ' is not an entity.'); 
    9389        } 
    9490         
    95         $table     = $refObject->getAnnotation('DBTable')->getTableDescription()->getName(); 
    96         $methods   = $refObject->getMethods(); 
     91        $table     = $this->getTableDescription($entityClass)->getName(); 
     92        $methods   = $entityClass->getMethods(); 
    9793        $criterion = new stubAndCriterion(); 
    9894        foreach ($methods as $method) { 
    99             if ($method->hasAnnotation('DBColumn') == false) { 
    100                 continue; 
    101             } elseif ($method->hasAnnotation('DBJoin') == true) { 
     95            if ($method->hasAnnotation('Id') === false) { 
    10296                continue; 
    10397            } 
    10498             
    105             $dbColumn  = $method->getAnnotation('DBColumn')->getTableColumn(); 
    106             if ($dbColumn->isPrimaryKey() == true) { 
    107                 $criterion->addCriterion(new stubEqualCriterion($dbColumn->getName(), $method->invoke($refObject->getObjectInstance()), $table))
     99            $column = $this->getTableColumn($method); 
     100            if (null === $column) { 
     101                continue
    108102            } 
     103             
     104            $criterion->addCriterion(new stubEqualCriterion($column->getName(), $method->invoke($entity), $table)); 
    109105        } 
    110106         
     
    113109            $result->free(); 
    114110        } catch (stubException $se) { 
    115             throw new stubDatabaseEraserException('Can not delete instance of ' . $refObject->getFullQualifiedClassName() . ' by its primary keys.', $se); 
     111            throw new stubDatabaseEraserException('Can not delete instance of ' . $entity->getClassName() . ' by its primary keys.', $se); 
    116112        } 
    117          
    118         $persistable->setPersistent(false); 
    119113    } 
    120114 
     
    123117     * 
    124118     * @param   stubCriterion                $criterion             the criterion that denotes all instances to delete 
    125      * @param   string                       $persistableClassName  non qualified classname of the persistable class to delete instances of 
     119     * @param   stubBaseReflectionClass      $entityClass 
    126120     * @return  int                          amount of erased instances 
    127121     * @throws  stubDatabaseEraserException 
    128122     */ 
    129     public function deleteByCriterion(stubCriterion $criterion, $persistableClassName
     123    public function deleteByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass
    130124    { 
    131         try { 
    132             $refClass = new stubReflectionClass($persistableClassName); 
    133         } catch (ReflectionException $re) { 
    134             throw new stubDatabaseEraserException('Can not delete any instance of ' . $refClass->getFullQualifiedClassName() . ' by criterion ' . $criterion, $re); 
     125        if ($entityClass->hasAnnotation('Entity') === false) { 
     126            throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 
    135127        } 
    136128         
    137         if ($refClass->implementsInterface('stubPersistable') == false) { 
    138             throw new stubDatabaseEraserException($refClass->getFullQualifiedClassName() . ' does not implement net.stubbles.rdbms.persistence.stubPersistable.'); 
    139         } 
    140          
    141         if ($refClass->hasAnnotation('DBTable') == false) { 
    142             throw new stubDatabaseEraserException('Can not delete instance of ' . $refClass->getFullQualifiedClassName() . ', class is missing the DBTable annotation.'); 
    143         } 
    144          
    145         $table = $refClass->getAnnotation('DBTable')->getTableDescription()->getName(); 
     129        $table = $this->getTableDescription($entityClass)->getName(); 
    146130        try { 
    147131            $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createDelete($table, $criterion)); 
     
    149133            $result->free(); 
    150134        } catch (stubDatabaseException $se) { 
    151             throw new stubDatabaseEraserException('Can not delete any instance of ' . $refClass->getFullQualifiedClassName() . ' by criterion ' . $criterion, $se); 
     135            throw new stubDatabaseEraserException('Can not delete any instance of ' . $entityClass->getFullQualifiedClassName() . ' by criterion ' . $criterion, $se); 
    152136        }  
    153137         
  • trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php

    r881 r907  
    6363     * @return  stubDatabaseFinder 
    6464     */ 
    65     public function getInstance(stubDatabaseConnection $connection, $refresh = false) 
     65    public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 
    6666    { 
    6767        if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { 
  • trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php

    r881 r907  
    6060     * @return  stubDatabaseFinder 
    6161     */ 
    62     public function getInstance(stubDatabaseConnection $connection, $refresh = false) 
     62    public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 
    6363    { 
    6464        if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { 
     
    8080 
    8181    /** 
    82      * takes a stubPersistable and serializes it into the database 
    83      * 
    84      * @param   stubPersistable                  $persistable 
     82     * takes an entity and serializes it into the database 
     83     * 
     84     * @param   stubObject                       $entity 
    8585     * @throws  stubDatabaseSerializerException 
    8686     * @throws  stubPersistenceException 
    87      */ 
    88     public function serialize(stubPersistable $persistable) 
    89     { 
    90         $refObject = $persistable->getClass(); 
    91         if ($refObject->hasAnnotation('DBTable') == false) { 
    92             throw new stubDatabaseSerializerException('No database table defined for ' . $refObject->getFullQualifiedClassName() . ', class is missing the DBTable annotation.'); 
     87     * @todo    isPersistent is buggy 
     88     * @todo    set default value only if not persistent 
     89     */ 
     90    public function serialize(stubObject $entity) 
     91    { 
     92        $entityClass = $entity->getClass(); 
     93        if ($entityClass->hasAnnotation('Entity') === false) { 
     94            throw new stubPersistenceException('Class ' . $entity->getClassName() . ' is not an entity.'); 
    9395        } 
    9496         
    95         $baseTableName    = $refObject->getAnnotation('DBTable')->getTableDescription()->getName(); 
     97        $baseTableName    = $this->getTableDescription($entityClass)->getName(); 
    9698        $methods          = $refObject->getMethods(); 
    9799        $tableRows        = array(); 
    98100        $singlePrimaryKey = null; 
     101        $isPersistent     = null; 
    99102        foreach ($methods as $method) { 
    100             if ($method->isPublic() == false || $method->hasAnnotation('DBColumn') == false) { 
     103            $column = $this->getTableColumn($method); 
     104            if (null === $column) { 
    101105                continue; 
    102106            } 
     
    108112             
    109113            try { 
    110                 $value = $method->invoke($persistable); 
     114                $value = $method->invoke($entity); 
    111115            } catch (ReflectionException $re) { 
    112                 throw new stubDatabaseSerializerException('Can not get return value of ' . $refObject->getFullQualifiedClassName() . '::' . $method->getName() . '(), invocation failed.', $re); 
    113             } 
    114              
    115             $dbColumn = $method->getAnnotation('DBColumn')->getTableColumn(); 
    116             if ($dbColumn->isPrimaryKey() == true) { 
     116                throw new stubDatabaseSerializerException('Can not get return value of ' . $entity->getClassName() . '::' . $method->getName() . '(), invocation failed.', $re); 
     117            } 
     118             
     119            if ($column->isPrimaryKey() === true) { 
    117120                if (null === $value) { 
    118                     if ($persistable->isPersistent() == true) { 
    119                         throw new stubDatabaseSerializerException('Data inconsistent: persistable object is already persistent, but the primary key ' . $refObject->getFullQualifiedClassName() . '::' . $method->getName() . '() is null.'); 
    120                     } 
    121                      
     121                    $isPersistent = false; 
    122122                    if (null !== $singlePrimaryKey) { 
    123123                        throw new stubDatabaseSerializerException('Persistence error: only one primary key can be null, but at least two primary keys are null: ' . $singlePrimaryKey['propertyName'] . ' and ' . $method->getName()); 
     
    129129                    continue; 
    130130                } else { 
    131                     $tableRows[$tableName]->addCriterion(new stubEqualCriterion($dbColumn->getName(), $value, $tableName)); 
     131                    $isPersistent = true; 
     132                    $tableRows[$tableName]->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableName)); 
    132133                } 
    133             } elseif (null === $value && $persistable->isPersistent() == false) { 
     134            } elseif (null === $value) { 
    134135                $value = $dbColumn->getDefaultValue(); 
    135                 $setterMethod = stubSetterMethodHelper::create($dbColumn, $refObject, $method->getName()); 
    136                 $setterMethod->invoke($persistable, $value); 
    137             } 
    138              
    139             $tableRows[$tableName]->setColumn($dbColumn->getName(), $value); 
     136                $setterMethod = stubSetterMethodHelper::create($column, $entityClass, $method->getName()); 
     137                $setterMethod->invoke($entity, $value); 
     138            } 
     139             
     140            $tableRows[$tableName]->setColumn($column->getName(), $value); 
    140141        } 
    141142         
    142143        try { 
    143             $this->processQueries($this->getQueries($tableRows, $persistable->isPersistent()), $persistable, $singlePrimaryKey); 
     144            $this->processQueries($this->getQueries($tableRows, $isPersistent), $entity, $singlePrimaryKey); 
    144145        } catch (stubDatabaseException $dbe) { 
    145             throw new stubDatabaseSerializerException('Can not persist ' . $refObject->getFullQualifiedClassName() . ': a database error occured.', $dbe); 
    146         } 
    147          
    148         $persistable->setPersistent(true); 
     146            throw new stubDatabaseSerializerException('Can not persist ' . $entity->getClassName() . ': a database error occured.', $dbe); 
     147        } 
    149148    } 
    150149 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php

    r903 r907  
    103103    public function testClassWithoutEntityAnnotation() 
    104104    { 
    105         $this->expectException('stubDatabaseCreatorException'); 
     105        $this->expectException('stubPersistenceException'); 
    106106        $this->dbCreator->createTable(new stubReflectionClass('MockNoEntityAnnotationPersistable')); 
    107107    }