Changeset 482
- Timestamp:
- 04/12/07 22:23:28 (1 year ago)
- Files:
-
- trunk/src/main/php/net/stubbles/rdbms/criteria/stubAbstractCompositeCriterion.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/rdbms/pdo/stubDatabasePDOConnection.php (modified) (15 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/annotations/stubDBColumnAnnotation.php (modified) (12 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/annotations/stubDBJoinAnnotation.php (added)
- trunk/src/main/php/net/stubbles/rdbms/persistence/annotations/stubDBTableAnnotation.php (modified) (8 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php (modified) (9 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php (modified) (13 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializer.php (modified) (11 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializerException.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubAbstractPersistable.php (added)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubPersistable.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/persistence/stubSetterMethodFactory.php (modified) (3 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseMySQLQueryBuilder.php (modified) (8 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseQueryBuilder.php (modified) (2 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseQueryBuilderFactory.php (modified) (7 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelect.php (added)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableColumn.php (modified) (24 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableDescription.php (modified) (10 diffs)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableJoin.php (added)
- trunk/src/main/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableRow.php (added)
- trunk/src/main/php/net/stubbles/rdbms/stubDatabaseConnection.php (modified) (9 diffs)
- trunk/src/test/php/net/stubbles/rdbms/RDBMSTestSuite.php (modified) (1 diff)
- trunk/src/test/php/net/stubbles/rdbms/criteria/stubAndCriterionTestCase.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/rdbms/criteria/stubOrCriterionTestCase.php (modified) (3 diffs)
- trunk/src/test/php/net/stubbles/rdbms/persistence (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockMultiplePrimaryKeyPersistable.php (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockNoTableAnnotationPersistable.php (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/MockSinglePrimaryKeyPersistable.php (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/creator (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreatorTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/finder (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinderTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/serializer (added)
- trunk/src/test/php/net/stubbles/rdbms/persistence/serializer/stubDatabaseSerializerTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/TeststubDatabaseQueryBuilder.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseMySQLQueryBuilderTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseQueryBuilderFactoryTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseSelectTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableColumnTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableDescriptionTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableJoinTestCase.php (added)
- trunk/src/test/php/net/stubbles/rdbms/querybuilder/stubDatabaseTableRowTestCase.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/rdbms/criteria/stubAbstractCompositeCriterion.php
r321 r482 22 22 */ 23 23 protected $criteria = array(); 24 24 25 25 /** 26 26 * add a criterion to the composition … … 32 32 $this->criteria[] = $criterion; 33 33 } 34 34 35 /** 36 * checks whether there is any criterion 37 * 38 * @return bool 39 */ 40 public function hasCriterion() 41 { 42 return (count($this->criteria) > 0); 43 } 44 35 45 /** 36 46 * returns the criterion as sql … … 56 66 return $sql . ')'; 57 67 } 58 68 59 69 /** 60 70 * returns the the operator to connect the criteria trunk/src/main/php/net/stubbles/rdbms/pdo/stubDatabasePDOConnection.php
r396 r482 31 31 */ 32 32 protected $pdo = null; 33 33 34 34 /** 35 35 * constructor … … 45 45 $this->connectionData = $connectionData; 46 46 } 47 47 48 48 /** 49 49 * destructor … … 53 53 $this->disconnect(); 54 54 } 55 55 56 56 /** 57 57 * establishes the connection … … 72 72 } 73 73 } 74 74 75 75 /** 76 76 * tries to create a new pdo instance … … 86 86 ); 87 87 } 88 88 89 89 /** 90 90 * disconnects the database … … 94 94 $this->pdo = null; 95 95 } 96 96 97 97 /** 98 98 * returns the connection data used for the connection … … 129 129 } 130 130 } 131 131 132 132 /** 133 133 * start a transaction … … 140 140 return $this->__call('beginTransaction', array()); 141 141 } 142 142 143 143 /** 144 144 * commit a transaction … … 151 151 return $this->__call('commit', array()); 152 152 } 153 153 154 154 /** 155 155 * rollback a transaction … … 162 162 return $this->__call('rollBack', array()); 163 163 } 164 164 165 165 /** 166 166 * creates a prepared statement … … 185 185 } 186 186 } 187 187 188 188 /** 189 189 * executes a SQL statement … … 252 252 return $statement; 253 253 } 254 254 255 255 /** 256 256 * execute an SQL statement and return the number of affected rows … … 273 273 } 274 274 } 275 275 276 276 /** 277 277 * returns the last insert id … … 281 281 * @throws stubDatabaseException 282 282 */ 283 public function getLastInsertI D($name = null)284 { 285 if (null === $this->pdo) { 286 $this->connect();283 public function getLastInsertId($name = null) 284 { 285 if (null === $this->pdo) { 286 throw new stubDatabaseException('Not connected: can not retrieve last insert id.'); 287 287 } 288 288 … … 293 293 } 294 294 } 295 295 296 296 /** 297 297 * returns the database name (e.g. MySQL or PostgreSQL) trunk/src/main/php/net/stubbles/rdbms/persistence/annotations/stubDBColumnAnnotation.php
r432 r482 8 8 */ 9 9 stubClassLoader::load('net.stubbles.reflection.annotations.stubAnnotation', 10 'net.stubbles.reflection.annotations.stubAbstractAnnotation' 10 'net.stubbles.reflection.annotations.stubAbstractAnnotation', 11 'net.stubbles.rdbms.querybuilder.stubDatabaseTableColumn' 11 12 ); 12 13 /** … … 19 20 { 20 21 /** 21 * order of column within table22 * the db table column container 22 23 * 23 * @var int24 * @var stubDatabaseTableColumn 24 25 */ 25 protected $order = 0; 26 protected $dbTableColumn; 27 26 28 /** 27 * name of the column 29 * constructor 30 */ 31 public function __construct() 32 { 33 $this->dbTableColumn = new stubDatabaseTableColumn(); 34 } 35 36 /** 37 * returns the table column container 28 38 * 29 * @ var string39 * @return stubDatabaseTableColumn 30 40 */ 31 protected $name; 32 /** 33 * type of the column 34 * 35 * @var string 36 */ 37 protected $type; 38 /** 39 * size of the field 40 * 41 * @var int|string 42 */ 43 protected $size; 44 /** 45 * switch whether column is unsigned or not 46 * 47 * @var bool 48 */ 49 protected $isUnsigned = false; 50 /** 51 * switch whether the column can be null or not 52 * 53 * @var bool 54 */ 55 protected $isNullable = true; 56 /** 57 * default value of the column 58 * 59 * @var mixed 60 */ 61 protected $defaultValue = null; 62 /** 63 * switch whether column is a primary key or not 64 * 65 * @var bool 66 */ 67 protected $isPrimaryKey = false; 68 /** 69 * switch whether column is a key or not 70 * 71 * @var bool 72 */ 73 protected $isKey = false; 74 /** 75 * the name of the setter method to use for restoring the value from database 76 * 77 * @var string 78 */ 79 protected $setterMethod = null; 41 public function getTableColumn() 42 { 43 return $this->dbTableColumn; 44 } 80 45 81 46 /** … … 96 61 public function setOrder($order) 97 62 { 98 $this-> order = $order;63 $this->dbTableColumn->setOrder($order); 99 64 } 100 101 /** 102 * returns the order within the table 103 * 104 * @return int 105 */ 106 public function getOrder() 107 { 108 return $this->order; 109 } 110 65 111 66 /** 112 67 * sets the name of the column … … 116 71 public function setName($name) 117 72 { 118 $this-> name = $name;73 $this->dbTableColumn->setName($name); 119 74 } 120 75 121 /**122 * returns the name of the column123 *124 * @return string125 */126 public function getName()127 {128 return $this->name;129 }130 131 76 /** 132 77 * sets the type of the column … … 136 81 public function setType($type) 137 82 { 138 $this-> type = $type;83 $this->dbTableColumn->setType($type); 139 84 } 140 85 141 /**142 * returns the type of the column143 *144 * @return string145 */146 public function getType()147 {148 return $this->type;149 }150 151 86 /** 152 87 * sets the size of the column … … 156 91 public function setSize($size) 157 92 { 158 $this-> size = $size;93 $this->dbTableColumn->setSize($size); 159 94 } 160 95 161 /**162 * returns the size of the column163 *164 * @return int|string165 */166 public function getSize()167 {168 return $this->size;169 }170 171 96 /** 172 97 * set whether the column may be null or not … … 176 101 public function setIsUnsigned($isUnsigned) 177 102 { 178 if (is_string($isUnsigned) == true && 'false' == $isUnsigned) { 179 $isUnsigned = false; 180 } 181 182 $this->isUnsigned = $isUnsigned; 103 $this->dbTableColumn->setIsUnsigned($this->castToBool($isUnsigned)); 183 104 } 184 185 /** 186 * check whether the column may be null or not 187 * 188 * @return bool 189 */ 190 public function isUnsigned() 191 { 192 return (strstr(strtoupper($this->type), 'INT') == true && true == $this->isUnsigned); 193 } 194 105 195 106 /** 196 107 * set whether the column may be null or not … … 200 111 public function setIsNullable($isNullable) 201 112 { 202 if (is_string($isNullable) == true && 'false' == $isNullable) { 203 $isNullable = false; 204 } 205 206 $this->isNullable = $isNullable; 113 $this->dbTableColumn->setIsNullable($this->castToBool($isNullable)); 207 114 } 208 209 /** 210 * check whether the column may be null or not 211 * 212 * @return bool 213 */ 214 public function isNullable() 215 { 216 return (false == $this->isPrimaryKey && true == $this->isNullable); 217 } 218 115 219 116 /** 220 117 * sets the default value of the column … … 224 121 public function setDefaultValue($defaultValue) 225 122 { 226 $this->d efaultValue = $defaultValue;123 $this->dbTableColumn->setDefaultValue($defaultValue); 227 124 } 228 125 229 /**230 * returns the default value of the column231 *232 * @return mixed233 */234 public function getDefaultValue()235 {236 return $this->defaultValue;237 }238 239 126 /** 240 127 * set whether the column is a primary key or not … … 244 131 public function setIsPrimaryKey($isPrimaryKey) 245 132 { 246 if (is_string($isPrimaryKey) == true && 'false' == $isPrimaryKey) { 247 $isPrimaryKey = false; 248 } 249 250 $this->isPrimaryKey = $isPrimaryKey; 251 } 252 253 /** 254 * check whether the column is a primary key or not 255 * 256 * @return bool 257 */ 258 public function isPrimaryKey() 259 { 260 return $this->isPrimaryKey; 133 $this->dbTableColumn->setIsPrimaryKey($this->castToBool($isPrimaryKey)); 261 134 } 262 135 … … 268 141 public function setIsKey($isKey) 269 142 { 270 if (is_string($isKey) == true && 'false' == $isKey) { 271 $isKey = false; 272 } 273 274 $this->isKey = $isKey; 143 $this->dbTableColumn->setIsKey($this->castToBool($isKey)); 275 144 } 276 277 /** 278 * check whether the column is a primary key or not 279 * 280 * @return bool 281 */ 282 public function isKey() 283 { 284 return (false == $this->isPrimaryKey && true == $this->isKey); 285 } 286 145 287 146 /** 288 147 * set the name of the setter method … … 292 151 public function setSetterMethod($setterMethod) 293 152 { 294 $this->setterMethod = $setterMethod; 153 $this->dbTableColumn->setSetterMethod($setterMethod); 154 } 155 156 /** 157 * set the character set of the table 158 * 159 * @param string $characterSet 160 */ 161 public function setCharacterSet($characterSet) 162 { 163 $this->dbTableColumn->setCharacterSet($characterSet); 295 164 } 296 165 297 166 /** 298 * checks whether the name of the setter method is known167 * set the collation of the table 299 168 * 300 * @ return bool169 * @param string $collation 301 170 */ 302 public function hasSetterMethod()171 public function setCollation($collation) 303 172 { 304 return (null !== $this->setterMethod);173 $this->dbTableColumn->setCollation($collation); 305 174 } 306 175 307 176 /** 308 * returns the name of the setter method177 * helper method to cast a value to bool 309 178 * 310 * @return string 179 * @param mixed $value 180 * @return bool 311 181 */ 312 p ublic function getSetterMethod()182 protected function castToBool($value) 313 183 { 314 return $this->setterMethod; 184 if (is_string($value) == true && 'false' == $value) { 185 return false; 186 } 187 188 return (bool) $value; 315 189 } 316 190 } trunk/src/main/php/net/stubbles/rdbms/persistence/annotations/stubDBTableAnnotation.php
r432 r482 8 8 */ 9 9 stubClassLoader::load('net.stubbles.reflection.annotations.stubAnnotation', 10 'net.stubbles.reflection.annotations.stubAbstractAnnotation' 10 'net.stubbles.reflection.annotations.stubAbstractAnnotation', 11 'net.stubbles.rdbms.querybuilder.stubDatabaseTableDescription' 11 12 ); 12 13 /** … … 19 20 { 20 21 /** 21 * the name of the table22 * the table description container 22 23 * 23 * @var st ring24 * @var stubDatabaseTableDescription 24 25 */ 25 protected $name; 26 protected $tableDescription; 27 26 28 /** 27 * type of the table 29 * constructor 30 */ 31 public function __construct() 32 { 33 $this->tableDescription = new stubDatabaseTableDescription(); 34 } 35 36 /** 37 * returns the table description container 28 38 * 29 * @ var string39 * @return stubDatabaseTableDescription 30 40 */ 31 protected $type; 32 /** 33 * the character set to use for this table 34 * 35 * @var string 36 */ 37 protected $characterSet = null; 38 /** 39 * the collation to use for this table 40 * 41 * @var string 42 */ 43 protected $collation = null; 44 /** 45 * an optional comment for this table 46 * 47 * @var string 48 */ 49 protected $comment = null; 41 public function getTableDescription() 42 { 43 return $this->tableDescription; 44 } 50 45 51 46 /** … … 56 51 public function getAnnotationTarget() 57 52 { 58 return stubAnnotation::TARGET_CLASS + stubAnnotation::TARGET_METHOD;53 return stubAnnotation::TARGET_CLASS; 59 54 } 60 55 … … 66 61 public function setName($name) 67 62 { 68 $this-> name = $name;63 $this->tableDescription->setName($name); 69 64 } 70 65 71 /**72 * returns the name of the table73 *74 * @return string75 */76 public function getName()77 {78 return $this->name;79 }80 81 66 /** 82 67 * sets the type of the table … … 86 71 public function setType($type) 87 72 { 88 $this->t ype = $type;73 $this->tableDescription->setType($type); 89 74 } 90 75 91 /**92 * returns the type of the table93 *94 * @return string95 */96 public function getType()97 {98 return $this->type;99 }100 101 76 /** 102 77 * sets the character set of the table … … 106 81 public function setCharacterSet($characterSet) 107 82 { 108 $this-> characterSet = $characterSet;83 $this->tableDescription->setCharacterSet($characterSet); 109 84 } 110 111 /** 112 * check if a character set is set 113 * 114 * @return bool 115 */ 116 public function hasCharacterSet() 117 { 118 return (null !== $this->characterSet); 119 } 120 121 /** 122 * returns the character set of the table if set, else null 123 * 124 * @return string 125 */ 126 public function getCharacterSet() 127 { 128 return $this->characterSet; 129 } 130 85 131 86 /** 132 87 * sets the collation of the table … … 136 91 public function setCollation($collation) 137 92 { 138 $this-> collation = $collation;93 $this->tableDescription->setCollation($collation); 139 94 } 140 141 /** 142 * check if a collation is set 143 * 144 * @return bool 145 */ 146 public function hasCollation() 147 { 148 return (null !== $this->collation); 149 } 150 151 /** 152 * returns the collation of the table if set, else null 153 * 154 * @return string 155 */ 156 public function getCollation() 157 { 158 return $this->collation; 159 } 160 95 161 96 /** 162 97 * sets the comment of the table … … 166 101 public function setComment($comment) 167 102 { 168 $this->comment = $comment; 169 } 170 171 /** 172 * check if a comment is set 173 * 174 * @return bool 175 */ 176 public function hasComment() 177 { 178 return (null !== $this->comment); 179 } 180 181 /** 182 * returns the comment of the table if set, else null 183 * 184 * @return string 185 */ 186 public function getComment() 187 { 188 return $this->comment; 103 $this->tableDescription->setComment($comment); 189 104 } 190 105 } trunk/src/main/php/net/stubbles/rdbms/persistence/creator/stubDatabaseCreator.php
r333 r482 8 8 */ 9 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 10 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 11 'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', 10 12 'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 11 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation',12 13 'net.stubbles.rdbms.persistence.creator.stubDatabaseCreatorException', 13 14 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 14 'net.stubbles.rdbms.querybuilder.stubDatabaseTableDescription',15 'net.stubbles.rdbms.querybuilder.stubDatabaseTableColumn',16 15 'net.stubbles.reflection.reflection' 17 16 ); … … 24 23 class stubDatabaseCreator extends stubBaseObject 25 24 { 26 27 25 /** 28 26 * list of creator instances … … 37 35 */ 38 36 protected $connection; 39 37 40 38 /** 41 39 * constructor … … 43 41 * @param stubDatabaseConnection $connection connection to use for creating the tables 44 42 */ 45 protected f unction __construct(stubDatabaseConnection $connection)43 protected final function __construct(stubDatabaseConnection $connection) 46 44 { 47 45 $this->connection = $connection; 48 46 } 49 47 50 48 /** 51 49 * method to return instances of the creator depending of the connection … … 56 54 * 57 55 * @param stubDatabaseConnection $connection connection to use for creating the tables 56 * @param bool $refresh optional set to true to recreate the instance 58 57 * @return stubDatabaseFinder 59 58 */ 60 public function getInstance(stubDatabaseConnection $connection )59 public function getInstance(stubDatabaseConnection $connection, $refresh = false) 61 60 { 62 if (isset(self::$instances[$connection->hashCode()]) == false ) {61 if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { 63 62 self::$instances[$connection->hashCode()] = new self($connection); 64 63 } … … 66 65 return self::$instances[$connection->hashCode()]; 67 66 } 68 67 69 68 /** 70 69 * cloning is forbidden … … 76 75 throw new stubDatabaseCreatorException('Cloning ' . $this->getClassName() . ' is not allowed.'); 77 76 } 78 77 79 78 /** 80 79 * creates the table description from the given class … … 85 84 public function createTable($fqClassName) 86 85 { 87 $nqClassName = stubClassLoader::getNonQualifiedClassName($fqClassName); 88 if (class_exists($nqClassName, false) == false) { 89 stubClassLoader::load($fqClassName); 90 } 91 92 $refClass = new stubReflectionClass($nqClassName); 86 $refClass = new stubReflectionClass($fqClassName); 93 87 if ($refClass->hasAnnotation('DBTable') == false) { 94 88 throw new stubDatabaseCreatorException('Class ' . $refClass->getName() . ' is missing the DBTable annotation.'); 95 89 } 96 90 97 $dbTable = $refClass->getAnnotation('DBTable'); 98 $tableDescription = new stubDatabaseTableDescription(); 99 $tableDescription->setName($dbTable->getName()); 100 $tableDescription->setType($dbTable->getType()); 101 $tableDescription->setCharacterSet($dbTable->getCharacterSet()); 102 $tableDescription->setCollation($dbTable->getCollation()); 103 $tableDescription->setComment($dbTable->getComment()); 104 $methods = $refClass->getMethods(); 91 $tableDescription = $refClass->getAnnotation('DBTable')->getTableDescription(); 92 $methods = $refClass->getMethods(); 105 93 foreach ($methods as $method) { 106 94 if ($method->hasAnnotation('DBColumn') == false) { 107 95 continue; 108 } elseif ($method->hasAnnotation('DB Table') == true && $method->getAnnotation('DBTable')->getName() != $tableDescription->getName()) {96 } elseif ($method->hasAnnotation('DBJoin') == true && $method->getAnnotation('DBJoin')->getTableJoin()->getName() != $tableDescription->getName()) { 109 97 continue; 110 98 } 111 99 112 $dbColumn = $method->getAnnotation('DBColumn'); 113 $columnDescription = new stubDatabaseTableColumn(); 114 $columnDescription->setOrder($dbColumn->getOrder()); 115 $columnDescription->setName($dbColumn->getName()); 116 $columnDescription->setType($dbColumn->getType()); 117 118 if (strtoupper($dbColumn->getType()) != 'TEXT') { 119 $columnDescription->setSize(str_replace('_', ',', $dbColumn->getSize())); 120 } 121 122 $columnDescription->setIsUnsigned($dbColumn->isUnsigned()); 123 $columnDescription->setIsNullable($dbColumn->isNullable()); 124 $columnDescription->setDefaultValue($dbColumn->getDefaultValue()); 125 $columnDescription->setIsPrimaryKey($dbColumn->isPrimaryKey()); 126 $columnDescription->setIsKey($dbColumn->isKey()); 127 $tableDescription->addColumn($columnDescription); 100 $tableDescription->addColumn($method->getAnnotation('DBColumn')->getTableColumn()); 128 101 } 129 102 … … 134 107 } 135 108 } 109 110 /** 111 * returns a unique hash code for the class 112 * 113 * Two creators are equal if they use the same connection. 114 * 115 * @return string 116 */ 117 public function hashCode() 118 { 119 return 'creator:' . $this->connection->hashCode(); 120 } 136 121 } 137 122 ?> trunk/src/main/php/net/stubbles/rdbms/persistence/finder/stubDatabaseFinder.php
r256 r482 9 9 stubClassLoader::load('net.stubbles.rdbms.stubDatabaseConnection', 10 10 'net.stubbles.rdbms.criteria.stubCriterion', 11 'net.stubbles.rdbms.criteria.stubAndCriterion',12 11 'net.stubbles.rdbms.criteria.stubEqualCriterion', 13 12 'net.stubbles.rdbms.persistence.stubPersistable', 14 13 'net.stubbles.rdbms.persistence.stubSetterMethodFactory', 14 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation', 15 'net.stubbles.rdbms.persistence.annotations.stubDBJoinAnnotation', 15 16 'net.stubbles.rdbms.persistence.annotations.stubDBTableAnnotation', 16 'net.stubbles.rdbms.persistence.annotations.stubDBColumnAnnotation',17 17 'net.stubbles.rdbms.persistence.finder.stubDatabaseFinderException', 18 18 'net.stubbles.rdbms.querybuilder.stubDatabaseQueryBuilderFactory', 19 'net.stubbles.rdbms.querybuilder.stubDatabaseSelect', 19 20 'net.stubbles.reflection.reflection' 20 21 ); … … 39 40 */ 40 41 protected $connection; 41 42 42 43 /** 43 44 * constructor … … 45 46 * @param stubDatabaseConnection $connection connection to use for finding the data 46 47 */ 47 protected f unction __construct(stubDatabaseConnection $connection)48 protected final function __construct(stubDatabaseConnection $connection) 48 49 { 49 50 $this->connection = $connection; 50 51 } 51 52 52 53 /** 53 54 * method to return instances of the finder depending of the connection … … 58 59 * 59 60 * @param stubDatabaseConnection $connection connection to use for finding the data 61 * @param bool $refresh optional set to true to recreate the instance 60 62 * @return stubDatabaseFinder 61 63 */ 62 public function getInstance(stubDatabaseConnection $connection )63 { 64 if (isset(self::$instances[$connection->hashCode()]) == false ) {64 public function getInstance(stubDatabaseConnection $connection, $refresh = false) 65 { 66 if (isset(self::$instances[$connection->hashCode()]) == false || true == $refresh) { 65 67 self::$instances[$connection->hashCode()] = new self($connection); 66 68 } … … 68 70 return self::$instances[$connection->hashCode()]; 69 71 } 70 72 71 73 /** 72 74 * cloning is forbidden … … 78 80 throw new stubDatabaseFinderException('Cloning ' . $this->getClassName() . ' is not allowed.'); 79 81 } 80 82 81 83 /** 82 84 * get a persistable object from database by its primary keys … … 88 90 public function findByPrimaryKeys(stubPersistable $persistable) 89 91 { 90 $refObject = $persistable->getClass();91 $ annotationData = $this->getAnnotationData($refObject);92 $refObject = $persistable->getClass(); 93 $select = $this->createSelect($refObject); 92 94 try { 93 $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createSelect($ annotationData['criterion'], $annotationData['tables']));95 $result = $this->connection->query(stubDatabaseQueryBuilderFactory::create($this->connection)->createSelect($select)); 94 96 $data = $result->fetch(); 97 $result->free(); 95 98 } catch (stubException $se) { 96 99 throw new stubDatabaseFinderException('Can not find instance of ' . $refObject->getFullQualifiedClassName() . ' by its primary keys.', $se); … … 100 103 return; 101 104 } 102 $result->free(); 103 104 foreach ($annotationData['setterMethods'] as $dataName => $setterMethod) { 105 if (isset($data[$dataName]) == false) { 106 continue; 107 } 108 109 $setterMethod->invoke($persistable, $data[$dataName]); 110 } 111 112 $persistable->setPersistent(true); 105 106 $select->fillPersistable($persistable, $data); 113 107 } 114 108 … … 118 112 * @param stubCriterion $criterion 119 113 * @param string $persistableClassName non qualified classname of the persistable class to find instances of 114 * @param array $arguments optional arguments for constructor 120 115 * @return array<$persistableClassName> list of instances of $persistableClassName found with $criterion
