| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | stubClassLoader::load('net::stubbles::rdbms::stubDatabaseConnection', |
|---|
| 10 | 'net::stubbles::rdbms::criteria::stubEqualCriterion', |
|---|
| 11 | 'net::stubbles::rdbms::persistence::stubPersistenceHelper', |
|---|
| 12 | 'net::stubbles::rdbms::persistence::stubSetterMethodHelper', |
|---|
| 13 | 'net::stubbles::rdbms::persistence::serializer::stubDatabaseSerializerException', |
|---|
| 14 | 'net::stubbles::rdbms::querybuilder::stubDatabaseQueryBuilderFactory', |
|---|
| 15 | 'net::stubbles::rdbms::querybuilder::stubDatabaseTableRow' |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | class stubDatabaseSerializer extends stubPersistenceHelper |
|---|
| 24 | { |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | protected static $instances = array(); |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | protected $connection; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | const INSERT = 'insert'; |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | const UPDATE = 'update'; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | protected final function __construct(stubDatabaseConnection $connection) |
|---|
| 52 | { |
|---|
| 53 | $this->connection = $connection; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | public static function getInstance(stubDatabaseConnection $connection, $refresh = false) |
|---|
| 68 | { |
|---|
| 69 | if (isset(self::$instances[$connection->hashCode()]) === false || true === $refresh) { |
|---|
| 70 | self::$instances[$connection->hashCode()] = new self($connection); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | return self::$instances[$connection->hashCode()]; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | protected final function __clone() |
|---|
| 82 | { |
|---|
| 83 | throw new stubDatabaseFinderException('Cloning ' . $this->getClassName() . ' is not allowed.'); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | public function insert($entity) |
|---|
| 96 | { |
|---|
| 97 | if (is_object($entity) === false) { |
|---|
| 98 | throw new stubIllegalArgumentException('Can only serialize objects.'); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionObject($entity))); |
|---|
| 102 | if ($entityClass->hasAnnotation('Entity') === false) { |
|---|
| 103 | throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | $stuff = $this->processEntity($entityClass, $entity, self::INSERT); |
|---|
| 107 | try { |
|---|
| 108 | $this->processInsertQueries($this->getInsertQuery($stuff['tableRow'], $entity, $stuff['defaultValues']), $entity, array_shift($stuff['primaryKeys'])); |
|---|
| 109 | } catch (stubDatabaseException $dbe) { |
|---|
| 110 | throw new stubDatabaseSerializerException('Can not persist ' . $entityClass->getFullQualifiedClassName() . ': a database error occured.', $dbe); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | return self::INSERT; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | public function update($entity) |
|---|
| 126 | { |
|---|
| 127 | if (is_object($entity) === false) { |
|---|
| 128 | throw new stubIllegalArgumentException('Can only serialize objects.'); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionObject($entity))); |
|---|
| 132 | if ($entityClass->hasAnnotation('Entity') === false) { |
|---|
| 133 | throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | $stuff = $this->processEntity($entityClass, $entity, self::UPDATE); |
|---|
| 137 | try { |
|---|
| 138 | $this->processUpdateQueries($this->getUpdateQuery($stuff['tableRow'], $stuff['defaultValues'])); |
|---|
| 139 | } catch (stubDatabaseException $dbe) { |
|---|
| 140 | throw new stubDatabaseSerializerException('Can not persist ' . $entityClass->getFullQualifiedClassName() . ': a database error occured.', $dbe); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | return self::UPDATE; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | public function serialize($entity) |
|---|
| 156 | { |
|---|
| 157 | if (is_object($entity) === false) { |
|---|
| 158 | throw new stubIllegalArgumentException('Can only serialize objects.'); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | $entityClass = (($entity instanceof stubObject) ? ($entity->getClass()) : (new stubReflectionObject($entity))); |
|---|
| 162 | if ($entityClass->hasAnnotation('Entity') === false) { |
|---|
| 163 | throw new stubPersistenceException('Class ' . $entityClass->getFullQualifiedClassName() . ' is not an entity.'); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | $stuff = $this->processEntity($entityClass, $entity); |
|---|
| 167 | if (count($stuff['primaryKeys']) > 1) { |
|---|
| 168 | throw new stubDatabaseSerializerException('Persistence error for ' . $entityClass->getFullQualifiedClassName() . ': only one primary key can be null, but at least two primary keys are null: ' . join(', ', array_keys($stuff['primaryKeys']))); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | if ($stuff['tableRow']->hasCriterion() === true) { |
|---|
| 172 | try { |
|---|
| 173 | $this->processUpdateQueries($this->getUpdateQuery($stuff['tableRow'], $stuff['defaultValues'])); |
|---|
| 174 | } catch (stubDatabaseException $dbe) { |
|---|
| 175 | throw new stubDatabaseSerializerException('Can not persist ' . $entityClass->getFullQualifiedClassName() . ': a database error occured.', $dbe); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | return self::UPDATE; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | try { |
|---|
| 182 | $this->processInsertQueries($this->getInsertQuery($stuff['tableRow'], $entity, $stuff['defaultValues']), $entity, array_shift($stuff['primaryKeys'])); |
|---|
| 183 | } catch (stubDatabaseException $dbe) { |
|---|
| 184 | throw new stubDatabaseSerializerException('Can not persist ' . $entityClass->getFullQualifiedClassName() . ': a database error occured.', $dbe); |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | return self::INSERT; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | protected function processEntity(stubBaseReflectionClass $entityClass, $entity, $type = null) |
|---|
| 200 | { |
|---|
| 201 | $tableRow = new stubDatabaseTableRow($this->getTableDescription($entityClass)->getName()); |
|---|
| 202 | $methods = $entityClass->getMethods(); |
|---|
| 203 | $primaryKeys = array(); |
|---|
| 204 | $defaultValues = array(); |
|---|
| 205 | foreach ($methods as $method) { |
|---|
| 206 | $column = $this->getTableColumn($method); |
|---|
| 207 | if (null === $column) { |
|---|
| 208 | continue; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | try { |
|---|
| 212 | $value = $method->invoke($entity); |
|---|
| 213 | } catch (ReflectionException $re) { |
|---|
| 214 | throw new stubDatabaseSerializerException('Can not get return value of ' . $entityClass->getFullQualifiedClassName() . '::' . $method->getName() . '(), invocation failed.', $re); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if ($column->isPrimaryKey() === true) { |
|---|
| 218 | if (null === $value && self::UPDATE === $type) { |
|---|
| 219 | throw new stubDatabaseSerializerException('Persistence error for ' . $entityClass->getFullQualifiedClassName() . ': should be updated, but one primary key column is null: ' . $method->getName()); |
|---|
| 220 | } elseif (null === $value) { |
|---|
| 221 | $primaryKeys[$method->getName()] = array('setterMethod' => stubSetterMethodHelper::create($column, $entityClass, $method->getName()), |
|---|
| 222 | 'tableName' => $tableRow->getTableName() |
|---|
| 223 | ); |
|---|
| 224 | } elseif (self::INSERT === $type) { |
|---|
| 225 | $tableRow->setColumn($column->getName(), $value); |
|---|
| 226 | } else { |
|---|
| 227 | $tableRow->addCriterion(new stubEqualCriterion($column->getName(), $value, $tableRow->getTableName())); |
|---|
| 228 | } |
|---|
| 229 | } elseif (null === $value) { |
|---|
| 230 | $defaultValue = $column->getDefaultValue(); |
|---|
| 231 | if ($column->isNullable() === false && null === $defaultValue) { |
|---|
| 232 | throw new stubDatabaseSerializerException('Persistence error for ' . $entityClass->getFullQualifiedClassName() . ': column ' . $column->getName() . ' is not allowed to be null but return value from method ' . $method->getName() . ' and default value are both null.'); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | $defaultValues[] = array('setterMethod' => stubSetterMethodHelper::create($column, $entityClass, $method->getName()), |
|---|
| 236 | 'value' => $value, |
|---|
| 237 | 'defaultValue' => $defaultValue, |
|---|
| 238 | 'column' => $column->getName() |
|---|
| 239 | ); |
|---|
| 240 | } else { |
|---|
| 241 | $tableRow->setColumn($column->getName(), $value); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | return array('tableRow' => $tableRow, |
|---|
| 246 | 'defaultValues' => $defaultValues, |
|---|
| 247 | 'primaryKeys' => $primaryKeys |
|---|
| 248 | ); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | protected function getInsertQuery(stubDatabaseTableRow $tableRow, $entity, array $defaultValues) |
|---|
| 261 | { |
|---|
| 262 | $queryBuilder = stubDatabaseQueryBuilderFactory::create($this->connection); |
|---|
| 263 | try { |
|---|
| 264 | |
|---|
| 265 | foreach ($defaultValues as $defaultValue) { |
|---|
| 266 | $defaultValue['setterMethod']->invokeArgs($entity, array($defaultValue['defaultValue'])); |
|---|
| 267 | $tableRow->setColumn($defaultValue['column'], $defaultValue['defaultValue']); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | return $queryBuilder->createInsert(array($tableRow->getTableName() => $tableRow)); |
|---|
| 271 | } catch (stubIllegalArgumentException $iae) { |
|---|
| 272 | throw new stubDatabaseSerializerException('Creating the queries failed.', $iae); |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | protected function getUpdateQuery(stubDatabaseTableRow $tableRow, array $defaultValues) |
|---|
| 285 | { |
|---|
| 286 | $queryBuilder = stubDatabaseQueryBuilderFactory::create($this->connection); |
|---|
| 287 | try { |
|---|
| 288 | foreach ($defaultValues as $defaultValue) { |
|---|
| 289 | $tableRow->setColumn($defaultValue['column'], $defaultValue['value']); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | return $queryBuilder->createUpdate(array($tableRow->getTableName() => $tableRow)); |
|---|
| 293 | } catch (stubIllegalArgumentException $iae) { |
|---|
| 294 | throw new stubDatabaseSerializerException('Creating the queries failed.', $iae); |
|---|
| 295 | } |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | protected function processInsertQueries(array $queries, $entity, array $singlePrimaryKey = null) |
|---|
| 307 | { |
|---|
| 308 | foreach ($queries as $tableName => $query) { |
|---|
| 309 | $this->connection->exec($query); |
|---|
| 310 | if (null !== $singlePrimaryKey && $singlePrimaryKey['tableName'] == $tableName) { |
|---|
| 311 | $singlePrimaryKey['setterMethod']->invokeArgs($entity, array($this->connection->getLastInsertId())); |
|---|
| 312 | } |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | protected function processUpdateQueries(array $queries) |
|---|
| 323 | { |
|---|
| 324 | foreach ($queries as $tableName => $query) { |
|---|
| 325 | $this->connection->exec($query); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | public function hashCode() |
|---|
| 337 | { |
|---|
| 338 | return 'serializer:' . $this->connection->hashCode(); |
|---|
| 339 | } |
|---|
| 340 | } |
|---|
| 341 | ?> |
|---|