Changeset 913
- Timestamp:
- 09/12/07 12:51:01 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraser.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php (modified) (9 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php (modified) (8 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraser.php
r910 r913 9 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 10 10 'net.stubbles.rdbms.criteria.stubCriterion', 11 'net.stubbles.rdbms.criteria.stubAndCriterion', 11 12 'net.stubbles.rdbms.criteria.stubEqualCriterion', 12 13 'net.stubbles.rdbms.persistence.stubPersistenceHelper', … … 105 106 } 106 107 108 if ($criterion->hasCriterion() === false) { 109 throw new stubDatabaseEraserException('Can not delete instance of ' . $entity->getClassName() . ' by its primary keys as it has no primary key.'); 110 } 111 107 112 try { 108 113 $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createDelete($table, $criterion)); trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r907 r913 10 10 'net.stubbles.rdbms.criteria.stubCriterion', 11 11 'net.stubbles.rdbms.criteria.stubEqualCriterion', 12 'net.stubbles.rdbms.persistence.stubPersist able',12 'net.stubbles.rdbms.persistence.stubPersistenceHelper', 13 13 'net.stubbles.rdbms.persistence.stubSetterMethodHelper', 14 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation',15 'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation',16 'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation',17 14 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderException', 18 15 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderResult', … … 27 24 * @subpackage rdbms_persistence_finder 28 25 */ 29 class stubDatabaseFinder extends stub BaseObject26 class stubDatabaseFinder extends stubPersistenceHelper 30 27 { 31 28 /** … … 65 62 public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 66 63 { 67 if (isset(self::$instances[$connection->hashCode()]) == false || true== $refresh) {64 if (isset(self::$instances[$connection->hashCode()]) === false || true === $refresh) { 68 65 self::$instances[$connection->hashCode()] = new self($connection); 69 66 } … … 85 82 * get a persistable object from database by its primary keys 86 83 * 87 * @param stubPersistable $persistable 84 * @param stubBaseReflectionClass $entityClass class information about the entity 85 * @param array $primaryKeys list of primary keys (name => value) 86 * @param array $arguments arguments for the constructor 87 * @return object 88 88 * @throws stubDatabaseFinderException 89 89 * @throws stubPersistenceException 90 90 */ 91 public function findByPrimaryKeys(stub Persistable $persistable)91 public function findByPrimaryKeys(stubBaseReflectionClass $entityClass, array $primaryKeys, array $arguments = null) 92 92 { 93 $refObject = $persistable->getClass(); 94 $setterMethodHelper = new stubSetterMethodHelper($refObject); 95 $select = $this->createSelect($refObject, $setterMethodHelper); 93 if ($entityClass->hasAnnotation('Entity') === false) { 94 throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 95 } 96 97 $setterMethodHelper = new stubSetterMethodHelper($entityClass); 98 $select = $this->createSelect($entityClass, $setterMethodHelper, $primaryKeys); 96 99 try { 97 100 $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createSelect($select)); … … 99 102 $result->free(); 100 103 } catch (stubException $se) { 101 throw new stubDatabaseFinderException('Can not find instance of ' . $ refObject->getFullQualifiedClassName() . ' by its primary keys.', $se);104 throw new stubDatabaseFinderException('Can not find instance of ' . $entityClass->getFullQualifiedClassName() . ' by its primary keys.', $se); 102 105 } 103 106 104 107 if (false === $data) { 105 return ;108 return null; 106 109 } 107 110 108 $setterMethodHelper->applySetterMethods($persistable, $data); 109 $persistable->setPersistent(true); 111 $entity = ((null === $arguments) ? ($entityClass->newInstance()) : ($entityClass->newInstanceArgs($arguments))); 112 $setterMethodHelper->applySetterMethods($entity, $data); 113 return $entity; 110 114 } 111 115 … … 116 120 * @param string $persistableClassName non qualified classname of the persistable class to find instances of 117 121 * @param array $arguments optional arguments for constructor 118 * @return stubDatabaseFinderResult list of instances of $ persistableClassNamefound with $criterion122 * @return stubDatabaseFinderResult list of instances of $entityClass found with $criterion 119 123 * @throws stubDatabaseFinderException 120 124 * @throws stubPersistenceException 121 125 */ 122 public function findByCriterion(stubCriterion $criterion, $persistableClassName, array $arguments = null)126 public function findByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass, array $arguments = null) 123 127 { 124 try { 125 $refClass = new stubReflectionClass($persistableClassName); 126 } catch (ReflectionException $re) { 127 throw new stubDatabaseFinderException('Can not find any instance of ' . $refClass->getFullQualifiedClassName() . ' by criterion ' . $criterion, $re); 128 if ($entityClass->hasAnnotation('Entity') === false) { 129 throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 128 130 } 129 131 130 if ($refClass->implementsInterface('stubPersistable') == false) { 131 throw new stubDatabaseFinderException($refClass->getFullQualifiedClassName() . ' does not implement net.stubbles.rdbms.persistence.stubPersistable.'); 132 } 133 134 $setterMethodHelper = new stubSetterMethodHelper($refClass); 135 $select = $this->createSelect($refClass, $setterMethodHelper); 132 $setterMethodHelper = new stubSetterMethodHelper($entityClass); 133 $select = $this->createSelect($entityClass, $setterMethodHelper); 136 134 $select->addCriterion($criterion); 137 135 try { … … 140 138 $result->free(); 141 139 } catch (stubDatabaseException $se) { 142 throw new stubDatabaseFinderException('Can not find any instance of ' . $ refClass->getFullQualifiedClassName() . ' by criterion ' . $criterion, $se);140 throw new stubDatabaseFinderException('Can not find any instance of ' . $entityClass->getFullQualifiedClassName() . ' by criterion ' . $criterion, $se); 143 141 } 144 142 … … 147 145 } 148 146 149 $finderResult = new stubDatabaseFinderResult($ refClass, $data, $setterMethodHelper, $arguments);147 $finderResult = new stubDatabaseFinderResult($entityClass, $data, $setterMethodHelper, $arguments); 150 148 return $finderResult; 151 149 } … … 154 152 * reads annotations and returns data in a usable format 155 153 * 156 * @param stubBaseReflectionClass $ refBaseClass154 * @param stubBaseReflectionClass $entityClass 157 155 * @param stubSetterMethodHelper $setterMethodHelper 158 156 * @return stubDatabaseSelect 159 157 * @throws stubDatabaseFinderException 160 158 */ 161 protected function createSelect(stubBaseReflectionClass $ refBaseClass, stubSetterMethodHelper $setterMethodHelper)159 protected function createSelect(stubBaseReflectionClass $entityClass, stubSetterMethodHelper $setterMethodHelper, array $primaryKeys = array()) 162 160 { 163 if ($refBaseClass->hasAnnotation('DBTable') == false) { 164 throw new stubDatabaseFinderException('Can not find instance of ' . $refBaseClass->getFullQualifiedClassName() . ', class is missing the DBTable annotation.'); 165 } 166 167 $select = new stubDatabaseSelect($refBaseClass->getAnnotation('DBTable')->getTableDescription()); 168 $methods = $refBaseClass->getMethods(); 161 162 $select = new stubDatabaseSelect($this->getTableDescription($entityClass)); 163 $methods = $entityClass->getMethods(); 169 164 foreach ($methods as $method) { 170 if ($method->hasAnnotation('DBColumn') == false) { 165 $column = $this->getTableColumn($method); 166 if (null === $column) { 171 167 continue; 172 168 } 173 169 174 $dbColumn = $method->getAnnotation('DBColumn')->getTableColumn(); 175 if ($method->hasAnnotation('DBJoin') == true) { 176 $tableJoin = $method->getAnnotation('DBJoin')->getTableJoin(); 177 $tableName = $tableJoin->getName(); 178 $select->addJoin($tableJoin); 179 } else { 180 $tableName = $select->getBaseTableName(); 170 if ($column->isPrimaryKey() === true && isset($primaryKeys[$column->getName()]) === true) { 171 $select->addCriterion(new stubEqualCriterion($column->getName(), $primaryKeys[$column->getName(), $select->getBaseTableName())); 181 172 } 182 173 183 if ($refBaseClass instanceof stubReflectionObject && $dbColumn->isPrimaryKey() == true) { 184 $select->addCriterion(new stubEqualCriterion($dbColumn->getName(), $method->invoke($refBaseClass->getObjectInstance()), $tableName)); 185 } 186 187 $setterMethodHelper->addSetterMethod($dbColumn, $method->getName()); 174 $setterMethodHelper->addSetterMethod($column, $method->getName()); 188 175 } 189 176 trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php
r907 r913 9 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 10 10 'net.stubbles.rdbms.criteria.stubEqualCriterion', 11 'net.stubbles.rdbms.persistence.stubPersist able',11 'net.stubbles.rdbms.persistence.stubPersistenceHelper', 12 12 'net.stubbles.rdbms.persistence.stubSetterMethodHelper', 13 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation',14 'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation',15 'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation',16 13 'net.stubbles.rdbms.persistence.serializer.stubDatabaseSerializerException', 17 14 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', … … 24 21 * @subpackage rdbms_persistence_serializer 25 22 */ 26 class stubDatabaseSerializer extends stub BaseObject23 class stubDatabaseSerializer extends stubPersistenceHelper 27 24 { 28 25 /** … … 62 59 public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 63 60 { 64 if (isset(self::$instances[$connection->hashCode()]) == false || true== $refresh) {61 if (isset(self::$instances[$connection->hashCode()]) === false || true === $refresh) { 65 62 self::$instances[$connection->hashCode()] = new self($connection); 66 63 } … … 85 82 * @throws stubDatabaseSerializerException 86 83 * @throws stubPersistenceException 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(); 84 */ 85 public function serialize($entity) 86 { 87 if (is_object($entity) === false) { 88 throw new stubIllegalArgumentException('Can only serialize objects.'); 89 } 90 91 $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionClass($entity))); 93 92 if ($entityClass->hasAnnotation('Entity') === false) { 94 93 throw new stubPersistenceException('Class ' . $entity->getClassName() . ' is not an entity.'); 95 94 } 96 95 97 $ baseTableName = $this->getTableDescription($entityClass)->getName();96 $tableRow = new stubDatabaseTableRow($this->getTableDescription($entityClass)->getName()); 98 97 $methods = $refObject->getMethods(); 99 $tableRows = array();100 98 $singlePrimaryKey = null; 101 $ isPersistent = null;99 $defaultValues = array(); 102 100 foreach ($methods as $method) { 103 101 $column = $this->getTableColumn($method); … … 106 104 } 107 105 108 $tableName = (($method->hasAnnotation('DBJoin') == true) ? ($method->getAnnotation('DBJoin')->getTableJoin()->getName()) : ($baseTableName));109 if (isset($tableRows[$tableName]) == false) {110 $tableRows[$tableName] = new stubDatabaseTableRow($tableName);111 }112 113 106 try { 114 107 $value = $method->invoke($entity); … … 119 112 if ($column->isPrimaryKey() === true) { 120 113 if (null === $value) { 121 $isPersistent = false;122 114 if (null !== $singlePrimaryKey) { 123 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());115 throw new stubDatabaseSerializerException('Persistence error for ' . $entity->getClassName() . ': only one primary key can be null, but at least two primary keys are null: ' . $singlePrimaryKey['propertyName'] . ' and ' . $method->getName()); 124 116 } 125 117 … … 129 121 continue; 130 122 } else { 131 $isPersistent = true; 132 $tableRows[$tableName]->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableName)); 123 $tableRow->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableName)); 133 124 } 134 125 } elseif (null === $value) { 135 $value = $dbColumn->getDefaultValue(); 136 $setterMethod = stubSetterMethodHelper::create($column, $entityClass, $method->getName()); 137 $setterMethod->invoke($entity, $value); 138 } 139 140 $tableRows[$tableName]->setColumn($column->getName(), $value); 126 $value = $column->getDefaultValue(); 127 if ($column->isNullable() === false && null === $value) { 128 throw new stubDatabaseSerializerException('Persistence error for ' . $entity->getClassName() . ': column ' . $column->getName() . ' is not allowed to be null but return value from method ' . $method->getName() . ' and default value are both null.'); 129 } 130 131 $defaultValues[] = array('setterMethod' => stubSetterMethodHelper::create($column, $entityClass, $method->getName()), 132 'value' => $value 133 ); 134 } 135 136 $tableRow->setColumn($column->getName(), $value); 137 } 138 139 if ($tableRow->hasCriterion() === false) { 140 foreach ($defaultValues as $defaultValue) { 141 $defaultValue['setterMethod']->invoke($entity, $defaultValue['value']); 142 } 141 143 } 142 144 143 145 try { 144 $this->processQueries($this->getQuer ies($tableRows, $isPersistent), $entity, $singlePrimaryKey);146 $this->processQueries($this->getQuery($tableRow), $entity, $singlePrimaryKey); 145 147 } catch (stubDatabaseException $dbe) { 146 148 throw new stubDatabaseSerializerException('Can not persist ' . $entity->getClassName() . ': a database error occured.', $dbe); … … 149 151 150 152 /** 151 * build the queries out of the serialized value 152 * 153 * @param array<string,stubDatabaseTableRow> $tableRows 154 * @param bool $isPersistent 153 * build the query out of the serialized value 154 * 155 * @param stubDatabaseTableRow $tableRow 155 156 * @return array<string,string> 156 157 * @throws 157 158 */ 158 protected function getQuer ies(array $tableRows, $isPersistent)159 protected function getQuery(stubDatabaseTableRow $tableRow) 159 160 { 160 161 $queryBuilder = stubDatabaseQueryBuilderFactory::create($this->connection); 161 162 try { 162 if ( true == $isPersistent) {163 return $queryBuilder->createUpdate( $tableRows);164 } 165 166 return $queryBuilder->createInsert( $tableRows);163 if ($tableRow->hasCriterion() === true) { 164 return $queryBuilder->createUpdate(array($tableRow)); 165 } 166 167 return $queryBuilder->createInsert(array($tableRow)); 167 168 } catch (stubIllegalArgumentException $iae) { 168 169 throw new stubDatabaseSerializerException('Creating the queries failed.', $iae); trunk/src/test/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraserTestCase.php
r655 r913 1 1 <?php 2 2 /** 3 * Test for net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser 3 * Test for net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 12 12 Mock::generate('stubCriterion'); 13 13 require_once dirname(__FILE__) . '/../../querybuilder/TeststubDatabaseQueryBuilder.php'; 14 require_once dirname(__FILE__) . '/../MockNoEntityAnnotationPersistable.php'; 14 15 require_once dirname(__FILE__) . '/../MockNoTableAnnotationPersistable.php'; 15 16 require_once dirname(__FILE__) . '/../MockSinglePrimaryKeyPersistable.php'; 16 require_once dirname(__FILE__) . '/../TestNonPersistable.php';17 17 /** 18 * Test for net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser 18 * Test for net.stubbles.rdbms.persistence.eraser.stubDatabaseEraser. 19 19 * 20 20 * @package stubbles … … 123 123 public function testByPrimaryKeys() 124 124 { 125 $singlePrimaryKey = new MockSinglePrimaryKeyPersistable(); 126 $singlePrimaryKey->setId('mock'); 127 $singlePrimaryKey->setPersistent(true); 125 $entity = new MockSinglePrimaryKeyPersistable(); 126 $entity->setId('mock'); 128 127 $this->mockConnection->setReturnValue('query', new MockstubDatabaseResult()); 129 $this->dbEraser->deleteByPrimaryKeys($singlePrimaryKey); 130 $this->assertFalse($singlePrimaryKey->isPersistent()); 128 $this->dbEraser->deleteByPrimaryKeys($entity); 131 129 $this->assertEqual($this->mockQueryBuilder->getDeleteTable(), 'foo'); 132 130 $this->assertEqual($this->mockQueryBuilder->getDeleteCriterion()->toSQL(), "(`foo`.`id` = 'mock')"); … … 155 153 * test that trying to delete a class that does not implement stubPersistable throws an exception 156 154 */ 157 public function testByCriterionNon PersistableClass()155 public function testByCriterionNonEntityClass() 158 156 { 159 $this->expectException('stub DatabaseEraserException');160 $this->dbEraser->deleteByCriterion(new MockstubCriterion(), ' TestNonPersistable');157 $this->expectException('stubPersistenceException'); 158 $this->dbEraser->deleteByCriterion(new MockstubCriterion(), 'MockNoEntityAnnotationPersistable'); 161 159 } 162 160 }
