SelectQueryFetchStrategy ======================== * Published: 2023-11-16 * Author: Nickolas Burr .. contents:: Table of Contents :local: Related ------- * :doc:`SelectQueryFetchStrategyInterface` Description ----------- This is the implementation class for ``SelectQueryFetchStrategyInterface``. Usage ----- .. code-block:: php _fetchStrategy->fetchOne($this->getSelect()); } } Source ------ .. code-block:: php getCacheValue( $select, $bind, __METHOD__ ); if (!empty($val)) { return (array) $this->serializer->unserialize($val); } try { /** @var mixed[]|false $data */ $data = $select->getConnection()->fetchAll($select, $bind); if ($data !== false) { $this->cache?->save( $this->serializer->serialize($data), $key, $this->cacheTags, $this->cacheTtl ); } return (array) $data; } catch (Throwable $e) { $this->logger->error($e->getMessage()); return []; } } /** * {@inheritdoc} */ public function fetchAssoc(Select $select, array $bind = []): array { /** @var string|null $val */ /** @var string|null $key */ [$val, $key] = $this->getCacheValue( $select, $bind, __METHOD__ ); if (!empty($val)) { return (array) $this->serializer->unserialize($val); } try { /** @var mixed[]|false $data */ $data = $select->getConnection()->fetchAssoc($select, $bind); if ($data !== false) { $this->cache?->save( $this->serializer->serialize($data), $key, $this->cacheTags, $this->cacheTtl ); } return (array) $data; } catch (Throwable $e) { $this->logger->error($e->getMessage()); return []; } } /** * {@inheritdoc} */ public function fetchCol(Select $select, array $bind = []): array { /** @var string|null $val */ /** @var string|null $key */ [$val, $key] = $this->getCacheValue( $select, $bind, __METHOD__ ); if (!empty($val)) { return (array) $this->serializer->unserialize($val); } try { /** @var mixed[]|false $data */ $data = $select->getConnection()->fetchCol($select, $bind); if ($data !== false) { $this->cache?->save( $this->serializer->serialize($data), $key, $this->cacheTags, $this->cacheTtl ); } return (array) $data; } catch (Throwable $e) { $this->logger->error($e->getMessage()); return []; } } /** * {@inheritdoc} */ public function fetchOne(Select $select, array $bind = []): ?string { /** @var string|null $val */ /** @var string|null $key */ [$val, $key] = $this->getCacheValue( $select, $bind, __METHOD__ ); if (!empty($val)) { return $val; } try { /** @var string|false $data */ $data = $select->getConnection()->fetchOne($select, $bind); if ($data !== false) { $this->cache?->save( $this->serializer->serialize($data), $key, $this->cacheTags, $this->cacheTtl ); } return (string) $data ?: null; } catch (Throwable $e) { $this->logger->error($e->getMessage()); return []; } } /** * {@inheritdoc} */ public function fetchPairs(Select $select, array $bind = []): array { /** @var string|null $val */ /** @var string|null $key */ [$val, $key] = $this->getCacheValue( $select, $bind, __METHOD__ ); if (!empty($val)) { return (array) $this->serializer->unserialize($val); } try { /** @var mixed[]|false $data */ $data = $select->getConnection()->fetchPairs($select, $bind); if ($data !== false) { $this->cache?->save( $this->serializer->serialize($data), $key, $this->cacheTags, $this->cacheTtl ); } return (array) $data; } catch (Throwable $e) { $this->logger->error($e->getMessage()); return []; } } /** * {@inheritdoc} */ public function fetchRow(Select $select, array $bind = []): array { /** @var string|null $val */ /** @var string|null $key */ [$val, $key] = $this->getCacheValue( $select, $bind, __METHOD__ ); if (!empty($val)) { return (array) $this->serializer->unserialize($val); } try { /** @var mixed[]|false $data */ $data = $select->getConnection()->fetchRow($select, $bind); if ($data !== false) { $this->cache?->save( $this->serializer->serialize($data), $key, $this->cacheTags, $this->cacheTtl ); } return (array) $data; } catch (Throwable $e) { $this->logger->error($e->getMessage()); return []; } } /** * @param Select $select * @param mixed[] $bind * @param string $caller * @return mixed[]|null */ private function getCacheValue( Select $select, array $bind, string $caller ): ?array { if ($this->cache === null) { return null; } if (!empty($bind)) { ksort($bind, SORT_STRING); } /** @var string $params */ $params = json_encode( $bind, JSON_FORCE_OBJECT, self::JSON_MAX_DEPTH ); if (json_last_error() !== JSON_ERROR_NONE) { throw new UnexpectedValueException( sprintf( 'Unable to serialize bind parameters: "%s"', json_last_error_msg() ) ); } /** @var string $cacheKey */ $cacheKey = sprintf( 'sqfs-%s-%s', $caller, hash( $this->algo, implode('', [$select, $params]) ) ); return [ $this->cache?->load($cacheKey) ?: null, $cacheKey, ]; } } .. code-block:: xml