Changeset 899
- Timestamp:
- 09/11/07 17:03:58 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockMultiplePrimaryKeyPersistable.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoEntityAnnotationPersistable.php (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php
r482 r899 9 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 10 10 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 11 'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation',12 11 'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 12 'net.stubbles.rdbms.persistence.annotations.stubEntityAnnotation', 13 'net.stubbles.rdbms.persistence.annotations.stubTransientAnnotation', 13 14 'net.stubbles.rdbms.persistence.creator.stubDatabaseCreatorException', 14 15 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', … … 79 80 * creates the table description from the given class 80 81 * 81 * @param st ring $fqClassName the full qualified classname of the class to create the table for82 * @param stubBaseReflectionClass $entityClass 82 83 * @throws stubDatabaseCreatorException 83 84 */ 84 public function createTable( $fqClassName)85 public function createTable(stubBaseReflectionClass $entityClass) 85 86 { 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.'); 89 89 } 90 90 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(); 93 99 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) { 97 101 continue; 98 102 } 99 103 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); 101 120 } 102 121 trunk/src/test/php/net/stubbles/rdbms/persistence/MockMultiplePrimaryKeyPersistable.php
r492 r899 12 12 * @package stubbles 13 13 * @subpackage rdbms_persistence_test 14 * @Entity 14 15 * @DBTable(name='foo') 15 16 */ trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php
r482 r899 12 12 * @package stubbles 13 13 * @subpackage rdbms_persistence_test 14 * @Entity 14 15 */ 15 16 class MockNoTableAnnotationPersistable extends stubAbstractPersistable 16 17 { 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 } 18 82 } 19 83 ?> trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php
r492 r899 12 12 * @package stubbles 13 13 * @subpackage rdbms_persistence_test 14 * @Entity 14 15 * @DBTable(name='foo') 15 16 */ … … 91 92 * @DBColumn(name='baz', type='VARCHAR', size=10) 92 93 * @DBJoin(name='blub', type='INNER', conditionType='ON') 94 * @Transient 93 95 */ 94 96 public function otherTable() 95 97 { 98 // FIXME: this method should be removed as DBJoin will not be supported for the next time 96 99 return 'this is baz'; 97 100 } trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php
r482 r899 10 10 Mock::generate('stubDatabaseConnection'); 11 11 require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 12 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 12 13 require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 13 14 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; … … 100 101 * check that a class that is missing the DBTable annotation throws an exception 101 102 */ 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 */ 102 112 public function testClassWithoutDBTableAnnotation() 103 113 { 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); 106 128 } 107 129 … … 111 133 public function testClassWithDBTableAnnotation() 112 134 { 113 $this->dbCreator->createTable( 'MockSinglePrimaryKeyPersistable');135 $this->dbCreator->createTable(new stubReflectionClass('MockSinglePrimaryKeyPersistable')); 114 136 $tableDescription = $this->mockQueryBuilder->getTableDescription(); 115 $columns = $tableDescription->getColumns(); 137 $this->assertEqual($tableDescription->getName(), 'foo'); 138 $columns = $tableDescription->getColumns(); 116 139 $this->assertEqual(count($columns), 3); 117 140 $this->assertEqual($columns[1]->getName(), 'id');
