Lines
97.22%
245 / 252
Methods
90.32%
28 / 31
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| readEntity | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 3 | ||
| readResolvedReferences | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 4 | ||
| readLock | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| readEntityDoubleTypes | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 2 | ||
| readList | 96.29% | 26 / 27 | 0.00% | 0 / 1 | 9 | ||
| readAvailabilityListByScope | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 7 | ||
| readAvailabilityListBefore | 100.00% | 14 / 14 | 100.00% | 1 / 1 | 4 | ||
| readOpeningHoursListByDate | 100.00% | 16 / 16 | 100.00% | 1 / 1 | 4 | ||
| readByAppointment | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 1 | ||
| writeEntity | 88.88% | 8 / 9 | 0.00% | 0 / 1 | 2.01 | ||
| updateEntity | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 1 | ||
| deleteEntity | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] init | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] getWriter | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] getReader | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsbackend\Base] fetchPreparedStatement | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsbackend\Base] startExecute | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] pdoExceptionHandler | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 4 | ||
| [BO\Zmsbackend\Base] fetchStatement | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchOne | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] fetchRow | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchValue | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchAll | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchHandle | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchList | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] writeItem | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] deleteItem | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] perform | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchAffected | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] hashStringValue | 50.00% | 5 / 10 | 0.00% | 0 / 1 | 8.12 | ||
| 11 | class Availability extends \BO\Zmsbackend\Base implements \BO\Zmsbackend\Interfaces\ResolveReferences | |
| 12 | { | |
| 13 | public static $cache = []; | |
| 14 | ||
| 15 | /** | |
| 16 | * @param false|int|string $availabilityId | |
| 17 | * | |
| 18 | */ | |
| 19 | public function readEntity(string|int|false $availabilityId, int $resolveReferences = 0, bool $preferCache = false) | |
| 20 | { | |
| 21 | $cacheKey = "$availabilityId-$resolveReferences"; | |
| 22 | if (!$preferCache || !array_key_exists($cacheKey, self::$cache)) { | |
| 23 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 24 | $query | |
| 25 | ->addEntityMapping() | |
| 26 | ->addResolvedReferences($resolveReferences) | |
| 27 | ->addConditionAvailabilityId($availabilityId); | |
| 28 | $availability = $this->fetchOne($query, new Entity()); | |
| 29 | $availability = $this->readResolvedReferences($availability, $resolveReferences, $preferCache); | |
| 30 | self::$cache[$cacheKey] = $availability; | |
| 31 | } | |
| 32 | return clone self::$cache[$cacheKey]; | |
| 33 | } | |
| 34 | ||
| 35 | /** | |
| 36 | * @return \BO\Zmsentities\Schema\Entity | |
| 37 | */ | |
| 38 | #[\Override] | |
| 39 | public function readResolvedReferences( | |
| 40 | \BO\Zmsentities\Schema\Entity $entity, | |
| 41 | int $resolveReferences, | |
| 42 | bool $preferCache = false | |
| 43 | ) { | |
| 44 | if (1 <= $resolveReferences && $entity->hasId()) { | |
| 45 | if (isset($entity->scope['id'])) { | |
| 46 | $entity['scope'] = (new \BO\Zmsbackend\Scope\Service\Scope())->readEntity( | |
| 47 | $entity->scope['id'], | |
| 48 | $resolveReferences - 1, | |
| 49 | !$preferCache | |
| 50 | ); | |
| 51 | } | |
| 52 | } | |
| 53 | return $entity; | |
| 54 | } | |
| 55 | ||
| 56 | public function readLock($availabilityId) | |
| 57 | { | |
| 58 | return $this->perform(\BO\Zmsbackend\Availability\Repository\Availability::QUERY_GET_LOCK, ['availabilityId' => $availabilityId]); | |
| 59 | } | |
| 60 | ||
| 61 | public function readEntityDoubleTypes($availabilityId, int $resolveReferences = 0) | |
| 62 | { | |
| 63 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 64 | $query | |
| 65 | ->addEntityMapping('openinghours') | |
| 66 | ->addResolvedReferences($resolveReferences) | |
| 67 | ->addConditionAvailabilityId($availabilityId) | |
| 68 | ->addConditionDoubleTypes(); | |
| 69 | $availability = $this->fetchOne($query, new Entity()); | |
| 70 | $availability = $this->readResolvedReferences($availability, $resolveReferences); | |
| 71 | return ($availability->hasId()) ? $availability : null; | |
| 72 | } | |
| 73 | ||
| 74 | public function readList( | |
| 75 | $scopeId, | |
| 76 | int $resolveReferences = 0, | |
| 77 | ?\DateTimeInterface $startDate = null, | |
| 78 | ?\DateTimeInterface $endDate = null | |
| 79 | ) { | |
| 80 | $scope = new \BO\Zmsentities\Scope(['id' => $scopeId]); | |
| 81 | if (1 <= $resolveReferences) { | |
| 82 | $scope = (new \BO\Zmsbackend\Scope\Service\Scope())->readEntity($scopeId, $resolveReferences - 1); | |
| 83 | } | |
| 84 | $collection = $this->readAvailabilityListByScope($scope, $resolveReferences, $startDate, $endDate); | |
| 85 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 86 | $query | |
| 87 | ->addEntityMapping('openinghours') | |
| 88 | ->addConditionOpeningHours() | |
| 89 | ->addResolvedReferences($resolveReferences) | |
| 90 | ->addConditionScopeId($scopeId); | |
| 91 | if ($startDate && $endDate) { | |
| 92 | $query->addConditionTimeframe($startDate, $endDate); | |
| 93 | } elseif ($startDate) { | |
| 94 | $query->addConditionSkipOld($startDate); | |
| 95 | } | |
| 96 | $result = $this->fetchList($query, new Entity()); | |
| 97 | if (count($result)) { | |
| 98 | foreach ($result as $entity) { | |
| 99 | $entity['scope'] = $scope; | |
| 100 | $entity->workstationCount['intern'] = 0; | |
| 101 | $entity->workstationCount['public'] = 0; | |
| 102 | if ($entity['type'] == 'appointment') { | |
| 103 | $entity['description'] = ''; | |
| 104 | $entity->id = '__spontan__' . $entity->id; | |
| 105 | $entity['type'] = 'openinghours'; | |
| 106 | } | |
| 107 | $entity['type'] = ($entity['type'] != 'appointment') ? 'openinghours' : $entity['type']; | |
| 108 | $collection->addEntity($entity); | |
| 109 | } | |
| 110 | } | |
| 111 | return $collection; | |
| 112 | } | |
| 113 | ||
| 114 | public function readAvailabilityListByScope( | |
| 115 | \BO\Zmsentities\Scope $scope, | |
| 116 | int $resolveReferences = 0, | |
| 117 | ?\DateTimeImmutable $startDate = null, | |
| 118 | ?\DateTimeImmutable $endDate = null | |
| 119 | ): Collection { | |
| 120 | $collection = new Collection(); | |
| 121 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 122 | $query | |
| 123 | ->addEntityMapping() | |
| 124 | ->addResolvedReferences($resolveReferences) | |
| 125 | ->addConditionAppointmentHours() | |
| 126 | ->addConditionScopeId($scope->id); | |
| 127 | if ($startDate && $endDate) { | |
| 128 | $query->addConditionTimeframe($startDate, $endDate); | |
| 129 | } elseif ($startDate) { | |
| 130 | $query->addConditionSkipOld($startDate); | |
| 131 | } | |
| 132 | ||
| 133 | $result = $this->fetchList($query, new Entity()); | |
| 134 | if (count($result)) { | |
| 135 | foreach ($result as $entity) { | |
| 136 | if ($entity instanceof Entity) { | |
| 137 | $entity['scope'] = clone $scope; | |
| 138 | //skip resolveReferences for using the scope given, TODO check resolvedLevel from scope | |
| 139 | $collection->addEntity($entity); | |
| 140 | } | |
| 141 | } | |
| 142 | } | |
| 143 | return $collection; | |
| 144 | } | |
| 145 | ||
| 146 | /* not in use | |
| 147 | public function readAvailabilityListByDate($scopeId, \DateTimeInterface $now, $resolveReferences = 0) | |
| 148 | { | |
| 149 | $collection = new Collection(); | |
| 150 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 151 | $query | |
| 152 | ->addEntityMapping() | |
| 153 | ->addResolvedReferences($resolveReferences) | |
| 154 | ->addConditionScopeId($scopeId) | |
| 155 | ->addConditionAppointmentHours() | |
| 156 | ->addConditionDate($now); | |
| 157 | $result = $this->fetchList($query, new Entity()); | |
| 158 | if (count($result)) { | |
| 159 | foreach ($result as $entity) { | |
| 160 | if ($entity instanceof Entity) { | |
| 161 | $entity = $this->readResolvedReferences($entity, $resolveReferences); | |
| 162 | $collection->addEntity($entity); | |
| 163 | } | |
| 164 | } | |
| 165 | } | |
| 166 | return $collection; | |
| 167 | } | |
| 168 | */ | |
| 169 | ||
| 170 | /* | |
| 171 | ** Returns a list of availabilities with end date older than 4 weeks | |
| 172 | */ | |
| 173 | public function readAvailabilityListBefore(\DateTimeImmutable $datetime, int $resolveReferences = 0): Collection | |
| 174 | { | |
| 175 | $collection = new Collection(); | |
| 176 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 177 | $query | |
| 178 | ->addEntityMapping() | |
| 179 | ->addResolvedReferences($resolveReferences) | |
| 180 | ->addConditionAppointmentHours() | |
| 181 | ->addConditionOnlyOld($datetime); | |
| 182 | $result = $this->fetchList($query, new Entity()); | |
| 183 | if (count($result)) { | |
| 184 | foreach ($result as $entity) { | |
| 185 | if ($entity instanceof Entity) { | |
| 186 | $entity = $this->readResolvedReferences($entity, $resolveReferences); | |
| 187 | $collection->addEntity($entity); | |
| 188 | } | |
| 189 | } | |
| 190 | } | |
| 191 | return $collection; | |
| 192 | } | |
| 193 | ||
| 194 | public function readOpeningHoursListByDate($scopeId, \DateTimeInterface $now, int $resolveReferences = 0): Collection | |
| 195 | { | |
| 196 | $collection = new Collection(); | |
| 197 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 198 | $query | |
| 199 | ->addEntityMapping('openinghours') | |
| 200 | ->addResolvedReferences($resolveReferences) | |
| 201 | ->addConditionScopeId($scopeId) | |
| 202 | ->addConditionOpeningHours() | |
| 203 | ->addConditionDate($now); | |
| 204 | $result = $this->fetchList($query, new Entity()); | |
| 205 | if (count($result)) { | |
| 206 | foreach ($result as $entity) { | |
| 207 | if ($entity instanceof Entity) { | |
| 208 | $entity = $this->readResolvedReferences($entity, $resolveReferences); | |
| 209 | $entity->type = 'openinghours'; | |
| 210 | $collection->addEntity($entity); | |
| 211 | } | |
| 212 | } | |
| 213 | } | |
| 214 | return $collection; | |
| 215 | } | |
| 216 | ||
| 217 | public function readByAppointment(\BO\Zmsentities\Appointment $appointment, int $resolveReferences = 0) | |
| 218 | { | |
| 219 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::SELECT); | |
| 220 | $query->addEntityMapping(); | |
| 221 | $query->addResolvedReferences($resolveReferences); | |
| 222 | $query->addConditionScopeId($appointment->toProperty()->scope->id->get()); | |
| 223 | $query->addConditionDate($appointment->toDateTime()); | |
| 224 | $query->addConditionAppointmentTime($appointment->toDateTime()); | |
| 225 | $entity = $this->fetchOne($query, new Entity()); | |
| 226 | $entity->scope = $appointment->scope; | |
| 227 | $entity = $this->readResolvedReferences($entity, $resolveReferences); | |
| 228 | return $entity; | |
| 229 | } | |
| 230 | ||
| 231 | /** | |
| 232 | * write an availability | |
| 233 | * | |
| 234 | * @param \BO\Zmsentities\Availability $entity | |
| 235 | * @param int $resolveReferences | |
| 236 | * | |
| 237 | * @return Entity | |
| 238 | */ | |
| 239 | public function writeEntity(\BO\Zmsentities\Availability $entity, int $resolveReferences = 0) | |
| 240 | { | |
| 241 | self::$cache = []; | |
| 242 | $entity->testValid(); | |
| 243 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::INSERT); | |
| 244 | $values = $query->reverseEntityMapping($entity); | |
| 245 | $query->addValues($values); | |
| 246 | if (!$this->writeItem($query)) { | |
| 247 | throw new \BO\Zmsbackend\Availability\Exception\AvailabilityCreateFailed(); | |
| 248 | } | |
| 249 | $entity->id = $this->getWriter()->lastInsertId(); | |
| 250 | return $this->readEntity($entity->id, $resolveReferences); | |
| 251 | } | |
| 252 | ||
| 253 | /** | |
| 254 | * update an availability | |
| 255 | * | |
| 256 | * @param int|string $entityId | |
| 257 | * | |
| 258 | * @return Entity | |
| 259 | * | |
| 260 | */ | |
| 261 | public function updateEntity($entityId, \BO\Zmsentities\Availability $entity, int $resolveReferences = 0) | |
| 262 | { | |
| 263 | self::$cache = []; | |
| 264 | $entity->testValid(); | |
| 265 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::UPDATE); | |
| 266 | $query->addConditionAvailabilityId($entityId); | |
| 267 | $values = $query->reverseEntityMapping($entity); | |
| 268 | $query->addValues($values); | |
| 269 | $this->writeItem($query); | |
| 270 | return $this->readEntity($entityId, $resolveReferences); | |
| 271 | } | |
| 272 | ||
| 273 | /** | |
| 274 | * remove an availability | |
| 275 | * | |
| 276 | * @param int|string $availabilityId | |
| 277 | * | |
| 278 | * @return Entity|null | |
| 279 | */ | |
| 280 | public function deleteEntity($availabilityId) | |
| 281 | { | |
| 282 | self::$cache = []; | |
| 283 | $query = new \BO\Zmsbackend\Availability\Repository\Availability(\BO\Zmsbackend\Query\Base::DELETE); | |
| 284 | $query->addConditionAvailabilityId($availabilityId); | |
| 285 | return $this->deleteItem($query); | |
| 286 | } | |
| 287 | } |
Inherited from BO\Zmsbackend\Base
| 38 | public function __construct(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null) | |
| 39 | { | |
| 40 | $this->writeDb = $writeConnection; | |
| 41 | $this->readDb = $readConnection; | |
| 42 | } |
| 44 | public static function init(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null): static | |
| 45 | { | |
| 46 | $instance = new static($writeConnection, $readConnection); | |
| 47 | return $instance; | |
| 48 | } |
| 50 | public function getWriter(): Connection\Pdo | |
| 51 | { | |
| 52 | if (null === $this->writeDb) { | |
| 53 | $this->writeDb = Connection\Select::getWriteConnection(); | |
| 54 | $this->readDb = $this->writeDb; | |
| 55 | } | |
| 56 | return $this->writeDb; | |
| 57 | } |
| 59 | public function getReader(): Connection\Pdo | |
| 60 | { | |
| 61 | if (null !== $this->writeDb) { | |
| 62 | // if readDB gets a reset, still use writeDB | |
| 63 | return $this->writeDb; | |
| 64 | } | |
| 65 | if (null === $this->readDb) { | |
| 66 | $this->readDb = Connection\Select::getReadConnection(); | |
| 67 | } | |
| 68 | return $this->readDb; | |
| 69 | } |
| 71 | public function fetchPreparedStatement(Query\Base|string $query) | |
| 72 | { | |
| 73 | $sql = "$query"; | |
| 74 | $reader = $this->getReader(); | |
| 75 | if (spl_object_hash($reader) != static::$preparedConnectionId) { | |
| 76 | // do not use prepared statements on a different connection | |
| 77 | static::$preparedCache = []; | |
| 78 | static::$preparedConnectionId = spl_object_hash($reader); | |
| 79 | } | |
| 80 | if (!isset(static::$preparedCache[$sql])) { | |
| 81 | $prepared = $this->getReader()->prepare($sql); | |
| 82 | static::$preparedCache[$sql] = $prepared; | |
| 83 | } | |
| 84 | return static::$preparedCache[$sql]; | |
| 85 | } |
| 87 | public function startExecute($statement, array $parameters) | |
| 88 | { | |
| 89 | $statement = static::pdoExceptionHandler(function () use ($statement, $parameters) { | |
| 90 | $statement->execute($parameters); | |
| 91 | return $statement; | |
| 92 | }); | |
| 93 | return $statement; | |
| 94 | } |
| 96 | protected static function pdoExceptionHandler(\Closure $pdoFunction, $parameters = []) | |
| 97 | { | |
| 98 | try { | |
| 99 | $statement = $pdoFunction($parameters); | |
| 100 | } catch (\PDOException $pdoException) { | |
| 101 | if (stripos($pdoException->getMessage(), 'Lock wait timeout') !== false) { | |
| 102 | throw new \BO\Zmsbackend\Exception\Pdo\LockTimeout(); | |
| 103 | } | |
| 104 | //@codeCoverageIgnoreStart | |
| 105 | if (stripos($pdoException->getMessage(), 'Deadlock found') !== false) { | |
| 106 | throw new \BO\Zmsbackend\Exception\Pdo\DeadLockFound(); | |
| 107 | } | |
| 108 | //@codeCoverageIgnoreEnd | |
| 109 | $message = "SQL: " | |
| 110 | . " Err: " | |
| 111 | . $pdoException->getMessage() | |
| 112 | //. " || Statement: " | |
| 113 | //.$statement->queryString | |
| 114 | //." || Parameters=". var_export($parameters, true) | |
| 115 | ; | |
| 116 | throw new \BO\Zmsbackend\Exception\Pdo\PDOFailed($message, 0, $pdoException); | |
| 117 | } | |
| 118 | return $statement; | |
| 119 | } |
| 121 | public function fetchStatement(Query\Base $query) | |
| 122 | { | |
| 123 | $parameters = $query->getParameters(); | |
| 124 | $statement = $this->startExecute($this->fetchPreparedStatement($query), $parameters); | |
| 125 | return $statement; | |
| 126 | } |
| 133 | public function fetchOne(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity) | |
| 134 | { | |
| 135 | $statement = $this->fetchStatement($query); | |
| 136 | $data = $statement->fetch(\PDO::FETCH_ASSOC); | |
| 137 | if ($data) { | |
| 138 | $entity->exchangeArray($query->postProcessJoins($data)); | |
| 139 | $entity->setResolveLevel($query->getResolveLevel()); | |
| 140 | } | |
| 141 | $statement->closeCursor(); | |
| 142 | return $entity; | |
| 143 | } |
| 145 | public function fetchRow(Query\Base|string $query, array|null $parameters = null) | |
| 146 | { | |
| 147 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 148 | $prepared = $this->fetchPreparedStatement($query); | |
| 149 | $prepared->execute($parameters); | |
| 150 | $row = $prepared->fetch(\PDO::FETCH_ASSOC); | |
| 151 | $prepared->closeCursor(); | |
| 152 | return $row; | |
| 153 | }); | |
| 154 | } |
| 156 | public function fetchValue(Query\Base|string $query, array|null $parameters = null) | |
| 157 | { | |
| 158 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 159 | $prepared = $this->fetchPreparedStatement($query); | |
| 160 | $prepared->execute($parameters); | |
| 161 | $value = $prepared->fetchColumn(); | |
| 162 | $prepared->closeCursor(); | |
| 163 | return $value; | |
| 164 | }); | |
| 165 | } |
| 167 | public function fetchAll(Query\Base|string $query, array|null $parameters = null) | |
| 168 | { | |
| 169 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 170 | $prepared = $this->fetchPreparedStatement($query); | |
| 171 | $prepared->execute($parameters); | |
| 172 | $list = $prepared->fetchAll(\PDO::FETCH_ASSOC); | |
| 173 | $prepared->closeCursor(); | |
| 174 | return $list; | |
| 175 | }); | |
| 176 | } |
| 178 | public function fetchHandle(Query\Base|string $query, array|null $parameters = null) | |
| 179 | { | |
| 180 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 181 | $prepared = $this->fetchPreparedStatement($query); | |
| 182 | $prepared->execute($parameters); | |
| 183 | return $prepared; | |
| 184 | }); | |
| 185 | } |
| 187 | public function fetchList(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity, array|\ArrayAccess $resultList = []) | |
| 188 | { | |
| 189 | $statement = $this->fetchStatement($query); | |
| 190 | while ($data = $statement->fetch(\PDO::FETCH_ASSOC)) { | |
| 191 | $dataEntity = clone $entity; | |
| 192 | $dataEntity->exchangeArray($query->postProcessJoins($data)); | |
| 193 | $dataEntity->setResolveLevel($query->getResolveLevel()); | |
| 194 | $resultList[] = $dataEntity; | |
| 195 | } | |
| 196 | $statement->closeCursor(); | |
| 197 | return $resultList; | |
| 198 | } |
| 206 | public function writeItem(Query\Base $query) | |
| 207 | { | |
| 208 | return static::pdoExceptionHandler(function () use ($query) { | |
| 209 | $this->getWriter(); //Switch to writer for write/delete | |
| 210 | $statement = $this->fetchPreparedStatement($query); | |
| 211 | $status = $statement->execute($query->getParameters()); | |
| 212 | $statement->closeCursor(); | |
| 213 | return $status; | |
| 214 | }); | |
| 215 | } |
| 221 | public function deleteItem(Query\Base $query) | |
| 222 | { | |
| 223 | return $this->writeItem($query); | |
| 224 | } |
| 226 | public function perform(string $sql, array|null $parameters = null) | |
| 227 | { | |
| 228 | return static::pdoExceptionHandler(function () use ($sql, $parameters) { | |
| 229 | $this->getWriter(); //Switch to writer for perform | |
| 230 | $prepared = $this->fetchPreparedStatement($sql); | |
| 231 | $status = $prepared->execute($parameters); | |
| 232 | $prepared->closeCursor(); | |
| 233 | return $status; | |
| 234 | }); | |
| 235 | } |
| 237 | public function fetchAffected(string $sql, array|null $parameters = null) | |
| 238 | { | |
| 239 | return static::pdoExceptionHandler(function () use ($sql, $parameters) { | |
| 240 | $this->getWriter(); //Switch to writer for perform | |
| 241 | $prepared = $this->fetchPreparedStatement($sql); | |
| 242 | $prepared->execute($parameters); | |
| 243 | $count = $prepared->rowCount(); | |
| 244 | $prepared->closeCursor(); | |
| 245 | return $count; | |
| 246 | }); | |
| 247 | } |
| 268 | public function hashStringValue(string $value, ?callable $callable = null): string | |
| 269 | { | |
| 270 | if ($callable === null) { | |
| 271 | $callable = 'sha1'; | |
| 272 | } | |
| 273 | ||
| 274 | if (is_callable($callable)) { | |
| 275 | $hash = $callable($value); | |
| 276 | ||
| 277 | if (is_string($callable)) { | |
| 278 | return $callable . ':' . $hash; | |
| 279 | } | |
| 280 | if ($callable instanceof \Closure) { | |
| 281 | return 'closure:' . $hash; | |
| 282 | } | |
| 283 | // else | |
| 284 | return 'custom:' . $hash; | |
| 285 | } | |
| 286 | ||
| 287 | throw new \InvalidArgumentException('hashStringValue() should be called with a callable as second parameter.'); | |
| 288 | } |