Changeset 881
- Timestamp:
- 08/23/07 16:26:38 (1 year ago)
- Files:
-
- trunk/experiments/people/mikey/persistence/MyNewsArticle.php (modified) (1 diff)
- trunk/experiments/people/mikey/persistence/listEntries.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php (modified) (8 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderResult.php (added)
- trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubSetterMethodHelper.php (moved) (moved from trunk/src/main/php/net/stubbles/rdbms/persistence/stubSetterMethodFactory.php) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelect.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php (modified) (2 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/stubSetterMethodHelperTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelectTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/experiments/people/mikey/persistence/MyNewsArticle.php
r512 r881 213 213 * @param stubDatabaseFinder $finder the finder to use 214 214 * @param int $status status that the instances must have 215 * @return array<MyNewsArticle>215 * @return stubDatabaseFinderResult<MyNewsArticle> 216 216 */ 217 217 public static function findByStatus(stubDatabaseFinder $finder, $status) trunk/experiments/people/mikey/persistence/listEntries.php
r512 r881 12 12 $finder = stubDatabaseFinder::getInstance(stubDatabaseConnectionPool::getConnection()); 13 13 $newsArticles = MyNewsArticle::findByStatus($finder, 5); 14 var_dump($newsArticles); 14 var_dump($newsArticles->count()); 15 foreach ($newsArticles as $newsArticle) { 16 var_dump($newsArticle); 17 } 15 18 ?> trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r482 r881 11 11 'net.stubbles.rdbms.criteria.stubEqualCriterion', 12 12 'net.stubbles.rdbms.persistence.stubPersistable', 13 'net.stubbles.rdbms.persistence.stubSetterMethod Factory',13 'net.stubbles.rdbms.persistence.stubSetterMethodHelper', 14 14 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 15 15 'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', 16 16 'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 17 17 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderException', 18 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderResult', 18 19 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 19 20 'net.stubbles.rdbms.querybuilder.stubDatabaseSelect', … … 91 92 { 92 93 $refObject = $persistable->getClass(); 93 $select = $this->createSelect($refObject); 94 $setterMethodHelper = new stubSetterMethodHelper($refObject); 95 $select = $this->createSelect($refObject, $setterMethodHelper); 94 96 try { 95 97 $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createSelect($select)); … … 104 106 } 105 107 106 $select->fillPersistable($persistable, $data); 108 $setterMethodHelper->applySetterMethods($persistable, $data); 109 $persistable->setPersistent(true); 107 110 } 108 111 … … 110 113 * finds all instances of $persistableClassName by given criterion 111 114 * 112 * @param stubCriterion $criterion113 * @param string $persistableClassName non qualified classname of the persistable class to find instances of114 * @param array $arguments optional arguments for constructor115 * @return array<$persistableClassName>list of instances of $persistableClassName found with $criterion115 * @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 116 119 * @throws stubDatabaseFinderException 117 120 * @throws stubPersistenceException … … 129 132 } 130 133 131 $select = $this->createSelect($refClass); 134 $setterMethodHelper = new stubSetterMethodHelper($refClass); 135 $select = $this->createSelect($refClass, $setterMethodHelper); 132 136 $select->addCriterion($criterion); 133 137 try { … … 140 144 141 145 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; 166 151 } 167 152 … … 170 155 * 171 156 * @param stubBaseReflectionClass $refBaseClass 157 * @param stubSetterMethodHelper $setterMethodHelper 172 158 * @return stubDatabaseSelect 173 159 * @throws stubDatabaseFinderException 174 160 */ 175 protected function createSelect(stubBaseReflectionClass $refBaseClass )161 protected function createSelect(stubBaseReflectionClass $refBaseClass, stubSetterMethodHelper $setterMethodHelper) 176 162 { 177 163 if ($refBaseClass->hasAnnotation('DBTable') == false) { … … 199 185 } 200 186 201 $select->addSetterMethod($dbColumn->getName(), stubSetterMethodFactory::create($dbColumn, $refBaseClass, $method->getName()));187 $setterMethodHelper->addSetterMethod($dbColumn, $method->getName()); 202 188 } 203 189 trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php
r482 r881 10 10 'net.stubbles.rdbms.criteria.stubEqualCriterion', 11 11 'net.stubbles.rdbms.persistence.stubPersistable', 12 'net.stubbles.rdbms.persistence.stubSetterMethod Factory',12 'net.stubbles.rdbms.persistence.stubSetterMethodHelper', 13 13 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 14 14 'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', … … 133 133 } elseif (null === $value && $persistable->isPersistent() == false) { 134 134 $value = $dbColumn->getDefaultValue(); 135 $setterMethod = stubSetterMethod Factory::create($dbColumn, $refObject, $method->getName());135 $setterMethod = stubSetterMethodHelper::create($dbColumn, $refObject, $method->getName()); 136 136 $setterMethod->invoke($persistable, $value); 137 137 } trunk/src/main/php/net/stubbles/rdbms/persistence/stubSetterMethodHelper.php
r482 r881 1 1 <?php 2 2 /** 3 * Factory to get the appropriate setter method equivalent to a getter method.3 * Helper class to work with setter methods. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 9 9 stubClassLoader::load('net.stubbles.rdbms.querybuilder.stubDatabaseTableColumn', 10 10 'net.stubbles.rdbms.persistence.stubPersistenceException', 11 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation',12 11 'net.stubbles.reflection.reflection' 13 12 ); 14 13 /** 15 * Factory to get the appropriate setter method equivalent to a getter method.14 * Helper class to work with setter methods. 16 15 * 17 * @static18 16 * @package stubbles 19 17 * @subpackage rdbms_persistence 20 18 */ 21 class stubSetterMethod Factory19 class stubSetterMethodHelper extends stubBaseObject 22 20 { 23 21 /** 24 * creater setter method from given annotation for given class22 * reflection instance for class to collect setter methods of 25 23 * 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 30 86 */ 31 87 public static function create(stubDatabaseTableColumn $dbColumn, stubBaseReflectionClass $refBaseClass, $getterMethodName) trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelect.php
r536 r881 33 33 */ 34 34 protected $criterion; 35 /**36 * list of setter methods37 *38 * @var array<string,ReflectionMethod>39 */40 protected $setterMethods = array();41 35 42 36 /** … … 118 112 return $this->criterion; 119 113 } 120 121 /**122 * adds a setter method123 *124 * @param string $columnName name of the column the setter method is responsible for125 * @param ReflectionMethod $setterMethod126 */127 public function addSetterMethod($columnName, ReflectionMethod $setterMethod)128 {129 $this->setterMethods[$columnName] = $setterMethod;130 }131 132 /**133 * fills the persistable object with the given data134 *135 * @param stubPersistable $persistable136 * @param array $data137 */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 }150 114 } 151 115 ?> trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php
r746 r881 43 43 44 44 // persistence 45 $this->addTestFile($dir . '/persistence/stubSetterMethodHelperTestCase.php'); 45 46 $this->addTestFile($dir . '/persistence/creator/stubDatabaseCreatorTestCase.php'); 46 47 $this->addTestFile($dir . '/persistence/eraser/stubDatabaseEraserTestCase.php'); trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php
r655 r881 181 181 $mockResult->setReturnValueAt(0, 'fetchAll', false); 182 182 $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.'); 189 191 $select = $this->mockQueryBuilder->getSelect(); 190 192 $this->assertEqual($select->getBaseTableName(), 'foo'); … … 213 215 $this->mockConnection->setReturnValue('query', $mockResult); 214 216 $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()); 221 225 } 222 226 } trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelectTestCase.php
r482 r881 67 67 $this->assertTrue($this->select->hasCriterion()); 68 68 } 69 70 /**71 * test that the setter methods are applied as expected72 */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 }85 69 } 86 70 ?>
