Changeset 907
- Timestamp:
- 09/11/07 23:48:38 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraser.php (modified) (7 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php (modified) (1 diff)
- trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php (modified) (4 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubPersistenceHelper.php (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php
r903 r907 8 8 */ 9 9 stubClassLoader::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', 15 11 'net.stubbles.rdbms.persistence.creator.stubDatabaseCreatorException', 16 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 17 'net.stubbles.reflection.reflection' 12 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory' 18 13 ); 19 14 /** … … 23 18 * @subpackage rdbms_persistence_creator 24 19 */ 25 class stubDatabaseCreator extends stub BaseObject20 class stubDatabaseCreator extends stubPersistenceHelper 26 21 { 27 22 /** … … 59 54 * @return stubDatabaseFinder 60 55 */ 61 public function getInstance(stubDatabaseConnection $connection, $refresh = false)56 public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 62 57 { 63 58 if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { … … 79 74 80 75 /** 81 * creates the table description from the given class76 * creates the table description from the given entity class 82 77 * 83 * @param stubBaseReflectionClass $entityClass78 * @param stubBaseReflectionClass $entityClass 84 79 * @throws stubDatabaseCreatorException 80 * @throws stubPersistenceException 85 81 */ 86 82 public function createTable(stubBaseReflectionClass $entityClass) 87 83 { 88 84 if ($entityClass->hasAnnotation('Entity') === false) { 89 throw new stub DatabaseCreatorException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.');85 throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); 90 86 } 91 87 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(); 100 90 foreach ($methods as $method) { 101 if ($method->hasAnnotation('Transient') === true) { 91 $column = $this->getTableColumn($method); 92 if (null === $column) { 102 93 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 method118 continue;119 }120 121 if ($returnType instanceof stubReflectionClass) {122 // not supported yet123 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);153 94 } 154 95 trunk/src/main/php/net/stubbles/rdbms/persistence/eraser/stubDatabaseEraser.php
r655 r907 10 10 'net.stubbles.rdbms.criteria.stubCriterion', 11 11 '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', 16 13 'net.stubbles.rdbms.persistence.eraser.stubDatabaseEraserException', 17 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 18 'net.stubbles.reflection.reflection' 14 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory' 19 15 ); 20 16 /** … … 24 20 * @subpackage rdbms_persistence_eraser 25 21 */ 26 class stubDatabaseEraser extends stub BaseObject22 class stubDatabaseEraser extends stubPersistenceHelper 27 23 { 28 24 /** … … 60 56 * @return stubDatabaseEraser 61 57 */ 62 public function getInstance(stubDatabaseConnection $connection, $refresh = false)58 public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 63 59 { 64 60 if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { … … 80 76 81 77 /** 82 * delete a persistable objectfrom database by its primary keys78 * delete an entity from database by its primary keys 83 79 * 84 * @param stub Persistable $persistable80 * @param stubObject $entity 85 81 * @throws stubDatabaseEraserException 86 82 * @throws stubPersistenceException 87 83 */ 88 public function deleteByPrimaryKeys(stub Persistable $persistable)84 public function deleteByPrimaryKeys(stubObject $entity) 89 85 { 90 $ refObject = $persistable->getClass();91 if ($ refObject->hasAnnotation('DBTable')== false) {92 throw new stub DatabaseEraserException('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.'); 93 89 } 94 90 95 $table = $ refObject->getAnnotation('DBTable')->getTableDescription()->getName();96 $methods = $ refObject->getMethods();91 $table = $this->getTableDescription($entityClass)->getName(); 92 $methods = $entityClass->getMethods(); 97 93 $criterion = new stubAndCriterion(); 98 94 foreach ($methods as $method) { 99 if ($method->hasAnnotation('DBColumn') == false) { 100 continue; 101 } elseif ($method->hasAnnotation('DBJoin') == true) { 95 if ($method->hasAnnotation('Id') === false) { 102 96 continue; 103 97 } 104 98 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; 108 102 } 103 104 $criterion->addCriterion(new stubEqualCriterion($column->getName(), $method->invoke($entity), $table)); 109 105 } 110 106 … … 113 109 $result->free(); 114 110 } 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); 116 112 } 117 118 $persistable->setPersistent(false);119 113 } 120 114 … … 123 117 * 124 118 * @param stubCriterion $criterion the criterion that denotes all instances to delete 125 * @param st ring $persistableClassName non qualified classname of the persistable class to delete instances of119 * @param stubBaseReflectionClass $entityClass 126 120 * @return int amount of erased instances 127 121 * @throws stubDatabaseEraserException 128 122 */ 129 public function deleteByCriterion(stubCriterion $criterion, $persistableClassName)123 public function deleteByCriterion(stubCriterion $criterion, stubBaseReflectionClass $entityClass) 130 124 { 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.'); 135 127 } 136 128 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(); 146 130 try { 147 131 $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createDelete($table, $criterion)); … … 149 133 $result->free(); 150 134 } 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); 152 136 } 153 137 trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r881 r907 63 63 * @return stubDatabaseFinder 64 64 */ 65 public function getInstance(stubDatabaseConnection $connection, $refresh = false)65 public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 66 66 { 67 67 if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php
r881 r907 60 60 * @return stubDatabaseFinder 61 61 */ 62 public function getInstance(stubDatabaseConnection $connection, $refresh = false)62 public static function getInstance(stubDatabaseConnection $connection, $refresh = false) 63 63 { 64 64 if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { … … 80 80 81 81 /** 82 * takes a stubPersistableand serializes it into the database83 * 84 * @param stub Persistable $persistable82 * takes an entity and serializes it into the database 83 * 84 * @param stubObject $entity 85 85 * @throws stubDatabaseSerializerException 86 86 * @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.'); 93 95 } 94 96 95 $baseTableName = $ refObject->getAnnotation('DBTable')->getTableDescription()->getName();97 $baseTableName = $this->getTableDescription($entityClass)->getName(); 96 98 $methods = $refObject->getMethods(); 97 99 $tableRows = array(); 98 100 $singlePrimaryKey = null; 101 $isPersistent = null; 99 102 foreach ($methods as $method) { 100 if ($method->isPublic() == false || $method->hasAnnotation('DBColumn') == false) { 103 $column = $this->getTableColumn($method); 104 if (null === $column) { 101 105 continue; 102 106 } … … 108 112 109 113 try { 110 $value = $method->invoke($ persistable);114 $value = $method->invoke($entity); 111 115 } 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) { 117 120 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; 122 122 if (null !== $singlePrimaryKey) { 123 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()); … … 129 129 continue; 130 130 } else { 131 $tableRows[$tableName]->addCriterion(new stubEqualCriterion($dbColumn->getName(), $value, $tableName)); 131 $isPersistent = true; 132 $tableRows[$tableName]->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableName)); 132 133 } 133 } elseif (null === $value && $persistable->isPersistent() == false) {134 } elseif (null === $value) { 134 135 $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); 140 141 } 141 142 142 143 try { 143 $this->processQueries($this->getQueries($tableRows, $ persistable->isPersistent()), $persistable, $singlePrimaryKey);144 $this->processQueries($this->getQueries($tableRows, $isPersistent), $entity, $singlePrimaryKey); 144 145 } 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 } 149 148 } 150 149 trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php
r903 r907 103 103 public function testClassWithoutEntityAnnotation() 104 104 { 105 $this->expectException('stub DatabaseCreatorException');105 $this->expectException('stubPersistenceException'); 106 106 $this->dbCreator->createTable(new stubReflectionClass('MockNoEntityAnnotationPersistable')); 107 107 }
