Changeset 899

Show
Ignore:
Timestamp:
09/11/07 17:03:58 (1 year ago)
Author:
mikey
Message:

added entity support to table creation

Files:

Legend:

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

    r482 r899  
    99stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 
    1010                      'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 
    11                       'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', 
    1211                      'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 
     12                      'net.stubbles.rdbms.persistence.annotations.stubEntityAnnotation', 
     13                      'net.stubbles.rdbms.persistence.annotations.stubTransientAnnotation', 
    1314                      'net.stubbles.rdbms.persistence.creator.stubDatabaseCreatorException', 
    1415                      'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 
     
    7980     * creates the table description from the given class 
    8081     * 
    81      * @param   string  $fqClassName  the full qualified classname of the class to create the table for 
     82     * @param   stubBaseReflectionClass  $entityClass 
    8283     * @throws  stubDatabaseCreatorException 
    8384     */ 
    84     public function createTable($fqClassName
     85    public function createTable(stubBaseReflectionClass $entityClass
    8586    { 
    86         $refClass = new stubReflectionClass($fqClassName); 
    87         if ($refClass->hasAnnotation('DBTable') == false) { 
    88             throw new stubDatabaseCreatorException('Class ' . $refClass->getName() . ' is missing the DBTable annotation.'); 
     87        if ($entityClass->hasAnnotation('Entity') === false) { 
     88            throw new stubDatabaseCreatorException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 
    8989        } 
    9090         
    91         $tableDescription = $refClass->getAnnotation('DBTable')->getTableDescription(); 
    92         $methods          = $refClass->getMethods(); 
     91        if ($entityClass->hasAnnotation('DBTable') === true) { 
     92            $tableDescription = $entityClass->getAnnotation('DBTable')->getTableDescription(); 
     93        } else { 
     94            $tableDescription = new stubDatabaseTableDescription(); 
     95            $tableDescription->setName($entityClass->getName() . 's'); 
     96        } 
     97         
     98        $methods = $entityClass->getMethods(); 
    9399        foreach ($methods as $method) { 
    94             if ($method->hasAnnotation('DBColumn') == false) { 
    95                 continue; 
    96             } elseif ($method->hasAnnotation('DBJoin') == true && $method->getAnnotation('DBJoin')->getTableJoin()->getName() != $tableDescription->getName()) { 
     100            if ($method->hasAnnotation('Transient') === true) { 
    97101                continue; 
    98102            } 
    99103             
    100             $tableDescription->addColumn($method->getAnnotation('DBColumn')->getTableColumn()); 
     104            if ($method->hasAnnotation('DBColumn') === true) { 
     105                $column = $method->getAnnotation('DBColumn')->getTableColumn(); 
     106            } elseif (substr($method->getName(), 0, 3) !== 'get') { 
     107                continue; 
     108            } elseif (in_array($method->getDeclaringClass()->getName(), array('stubBaseObject', 'stubSerializableObject'))) { 
     109                continue; 
     110            } else { 
     111                // well, if we could check the return value this could be customized a bit... 
     112                $column     = new stubDatabaseTableColumn(); 
     113                $columnName = str_replace('get', '', $method->getName()); 
     114                $column->setName(strtolower($columnName{0}) . substr($columnName, 1)); 
     115                $column->setType('VARCHAR'); 
     116                $column->setSize(255); 
     117            } 
     118             
     119            $tableDescription->addColumn($column); 
    101120        } 
    102121         
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockMultiplePrimaryKeyPersistable.php

    r492 r899  
    1212 * @package     stubbles 
    1313 * @subpackage  rdbms_persistence_test 
     14 * @Entity 
    1415 * @DBTable(name='foo') 
    1516 */ 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php

    r482 r899  
    1212 * @package     stubbles 
    1313 * @subpackage  rdbms_persistence_test 
     14 * @Entity 
    1415 */ 
    1516class MockNoTableAnnotationPersistable extends stubAbstractPersistable 
    1617{ 
    17     // intentionally empty 
     18    /** 
     19     * returns the primary key 
     20     * 
     21     * @return  string 
     22     * @DBColumn(name='id', type='INT', size=10, isUnsigned=true, isPrimaryKey=true) 
     23     */ 
     24    public function getId() 
     25    { 
     26        return $this->id; 
     27    } 
     28 
     29    /** 
     30     * method that has no annotation 
     31     */ 
     32    public function noAnnotation() { } 
     33 
     34    /** 
     35     * sets bar 
     36     * 
     37     * @param  string  $bar 
     38     */ 
     39    public function setBar($bar) 
     40    { 
     41        $this->bar = $bar; 
     42    } 
     43 
     44    /** 
     45     * method that has a DBColumn annotation 
     46     * 
     47     * @DBColumn(name='bar', type='VARCHAR', size=10, isNullable=true, setterMethod='setBar') 
     48     */ 
     49    public function withAnnotation() 
     50    { 
     51        return $this->bar; 
     52    } 
     53 
     54    /** 
     55     * set the default value 
     56     * 
     57     * @param  string  $defaultValue 
     58     */ 
     59    public function setDefaultValue($defaultValue) 
     60    { 
     61        $this->defaultValue = $defaultValue; 
     62    } 
     63 
     64    /** 
     65     * method that has a no annotation 
     66     */ 
     67    public function getDefaultValue() 
     68    { 
     69        return $this->defaultValue; 
     70    } 
     71 
     72    /** 
     73     * this method is ignored 
     74     * 
     75     * @return  string 
     76     * @Transient 
     77     */ 
     78    public function getIgnored() 
     79    { 
     80        return 'ignored'; 
     81    } 
    1882} 
    1983?> 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php

    r492 r899  
    1212 * @package     stubbles 
    1313 * @subpackage  rdbms_persistence_test 
     14 * @Entity 
    1415 * @DBTable(name='foo') 
    1516 */ 
     
    9192     * @DBColumn(name='baz', type='VARCHAR', size=10) 
    9293     * @DBJoin(name='blub', type='INNER', conditionType='ON') 
     94     * @Transient 
    9395     */ 
    9496    public function otherTable() 
    9597    { 
     98        // FIXME: this method should be removed as DBJoin will not be supported for the next time 
    9699        return 'this is baz'; 
    97100    } 
  • trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php

    r482 r899  
    1010Mock::generate('stubDatabaseConnection'); 
    1111require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 
     12require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 
    1213require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 
    1314require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 
     
    100101     * check that a class that is missing the DBTable annotation throws an exception 
    101102     */ 
     103    public function testClassWithoutEntityAnnotation() 
     104    { 
     105        $this->expectException('stubDatabaseCreatorException'); 
     106        $this->dbCreator->createTable(new stubReflectionClass('MockNoEntityAnnotationPersistable')); 
     107    } 
     108 
     109    /** 
     110     * check that a class that is missing the DBTable annotation throws an exception 
     111     */ 
    102112    public function testClassWithoutDBTableAnnotation() 
    103113    { 
    104         $this->expectException('stubDatabaseCreatorException'); 
    105         $this->dbCreator->createTable('MockNoTableAnnotationPersistable'); 
     114        $this->dbCreator->createTable(new stubReflectionClass('MockNoTableAnnotationPersistable')); 
     115        $tableDescription = $this->mockQueryBuilder->getTableDescription(); 
     116        $this->assertEqual($tableDescription->getName(), 'MockNoTableAnnotationPersistables'); 
     117        $columns = $tableDescription->getColumns(); 
     118        $this->assertEqual(count($columns), 3); 
     119        $this->assertEqual($columns[1]->getName(), 'id'); 
     120        $this->assertEqual($columns[1]->getType(), 'INT'); 
     121        $this->assertEqual($columns[1]->getSize(), 10); 
     122        $this->assertEqual($columns[2]->getName(), 'bar'); 
     123        $this->assertEqual($columns[2]->getType(), 'VARCHAR'); 
     124        $this->assertEqual($columns[2]->getSize(), 10); 
     125        $this->assertEqual($columns[3]->getName(), 'defaultValue'); 
     126        $this->assertEqual($columns[3]->getType(), 'VARCHAR'); 
     127        $this->assertEqual($columns[3]->getSize(), 255); 
    106128    } 
    107129 
     
    111133    public function testClassWithDBTableAnnotation() 
    112134    { 
    113         $this->dbCreator->createTable('MockSinglePrimaryKeyPersistable'); 
     135        $this->dbCreator->createTable(new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 
    114136        $tableDescription = $this->mockQueryBuilder->getTableDescription(); 
    115         $columns          = $tableDescription->getColumns(); 
     137        $this->assertEqual($tableDescription->getName(), 'foo'); 
     138        $columns = $tableDescription->getColumns(); 
    116139        $this->assertEqual(count($columns), 3); 
    117140        $this->assertEqual($columns[1]->getName(), 'id');