summaryrefslogtreecommitdiff
path: root/vendor/doctrine/dbal/src/Cache
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/dbal/src/Cache')
-rw-r--r--vendor/doctrine/dbal/src/Cache/ArrayResult.php101
-rw-r--r--vendor/doctrine/dbal/src/Cache/CacheException.php12
-rw-r--r--vendor/doctrine/dbal/src/Cache/Exception/NoCacheKey.php16
-rw-r--r--vendor/doctrine/dbal/src/Cache/Exception/NoResultDriverConfigured.php16
-rw-r--r--vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php91
5 files changed, 236 insertions, 0 deletions
diff --git a/vendor/doctrine/dbal/src/Cache/ArrayResult.php b/vendor/doctrine/dbal/src/Cache/ArrayResult.php
new file mode 100644
index 0000000..65b8652
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Cache/ArrayResult.php
@@ -0,0 +1,101 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Cache;
6
7use Doctrine\DBAL\Driver\FetchUtils;
8use Doctrine\DBAL\Driver\Result;
9
10use function array_values;
11use function count;
12use function reset;
13
14/** @internal The class is internal to the caching layer implementation. */
15final class ArrayResult implements Result
16{
17 private readonly int $columnCount;
18 private int $num = 0;
19
20 /** @param list<array<string, mixed>> $data */
21 public function __construct(private array $data)
22 {
23 $this->columnCount = $data === [] ? 0 : count($data[0]);
24 }
25
26 public function fetchNumeric(): array|false
27 {
28 $row = $this->fetch();
29
30 if ($row === false) {
31 return false;
32 }
33
34 return array_values($row);
35 }
36
37 public function fetchAssociative(): array|false
38 {
39 return $this->fetch();
40 }
41
42 public function fetchOne(): mixed
43 {
44 $row = $this->fetch();
45
46 if ($row === false) {
47 return false;
48 }
49
50 return reset($row);
51 }
52
53 /**
54 * {@inheritDoc}
55 */
56 public function fetchAllNumeric(): array
57 {
58 return FetchUtils::fetchAllNumeric($this);
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 public function fetchAllAssociative(): array
65 {
66 return FetchUtils::fetchAllAssociative($this);
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 public function fetchFirstColumn(): array
73 {
74 return FetchUtils::fetchFirstColumn($this);
75 }
76
77 public function rowCount(): int
78 {
79 return count($this->data);
80 }
81
82 public function columnCount(): int
83 {
84 return $this->columnCount;
85 }
86
87 public function free(): void
88 {
89 $this->data = [];
90 }
91
92 /** @return array<string, mixed>|false */
93 private function fetch(): array|false
94 {
95 if (! isset($this->data[$this->num])) {
96 return false;
97 }
98
99 return $this->data[$this->num++];
100 }
101}
diff --git a/vendor/doctrine/dbal/src/Cache/CacheException.php b/vendor/doctrine/dbal/src/Cache/CacheException.php
new file mode 100644
index 0000000..a6913ed
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Cache/CacheException.php
@@ -0,0 +1,12 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Cache;
6
7use Doctrine\DBAL\Exception;
8
9/** @psalm-immutable */
10class CacheException extends \Exception implements Exception
11{
12}
diff --git a/vendor/doctrine/dbal/src/Cache/Exception/NoCacheKey.php b/vendor/doctrine/dbal/src/Cache/Exception/NoCacheKey.php
new file mode 100644
index 0000000..9fc58b7
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Cache/Exception/NoCacheKey.php
@@ -0,0 +1,16 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Cache\Exception;
6
7use Doctrine\DBAL\Cache\CacheException;
8
9/** @psalm-immutable */
10final class NoCacheKey extends CacheException
11{
12 public static function new(): self
13 {
14 return new self('No cache key was set.');
15 }
16}
diff --git a/vendor/doctrine/dbal/src/Cache/Exception/NoResultDriverConfigured.php b/vendor/doctrine/dbal/src/Cache/Exception/NoResultDriverConfigured.php
new file mode 100644
index 0000000..14e41f7
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Cache/Exception/NoResultDriverConfigured.php
@@ -0,0 +1,16 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Cache\Exception;
6
7use Doctrine\DBAL\Cache\CacheException;
8
9/** @psalm-immutable */
10final class NoResultDriverConfigured extends CacheException
11{
12 public static function new(): self
13 {
14 return new self('Trying to cache a query but no result driver is configured.');
15 }
16}
diff --git a/vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php b/vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php
new file mode 100644
index 0000000..b085f6c
--- /dev/null
+++ b/vendor/doctrine/dbal/src/Cache/QueryCacheProfile.php
@@ -0,0 +1,91 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Doctrine\DBAL\Cache;
6
7use Doctrine\DBAL\Cache\Exception\NoCacheKey;
8use Doctrine\DBAL\Connection;
9use Psr\Cache\CacheItemPoolInterface;
10
11use function hash;
12use function serialize;
13use function sha1;
14
15/**
16 * Query Cache Profile handles the data relevant for query caching.
17 *
18 * It is a value object, setter methods return NEW instances.
19 *
20 * @psalm-import-type WrapperParameterType from Connection
21 */
22class QueryCacheProfile
23{
24 public function __construct(
25 private readonly int $lifetime = 0,
26 private readonly ?string $cacheKey = null,
27 private readonly ?CacheItemPoolInterface $resultCache = null,
28 ) {
29 }
30
31 public function getResultCache(): ?CacheItemPoolInterface
32 {
33 return $this->resultCache;
34 }
35
36 public function getLifetime(): int
37 {
38 return $this->lifetime;
39 }
40
41 /** @throws CacheException */
42 public function getCacheKey(): string
43 {
44 if ($this->cacheKey === null) {
45 throw NoCacheKey::new();
46 }
47
48 return $this->cacheKey;
49 }
50
51 /**
52 * Generates the real cache key from query, params, types and connection parameters.
53 *
54 * @param list<mixed>|array<string, mixed> $params
55 * @param array<string, mixed> $connectionParams
56 * @psalm-param array<int, WrapperParameterType>|array<string, WrapperParameterType> $types
57 *
58 * @return array{string, string}
59 */
60 public function generateCacheKeys(string $sql, array $params, array $types, array $connectionParams = []): array
61 {
62 if (isset($connectionParams['password'])) {
63 unset($connectionParams['password']);
64 }
65
66 $realCacheKey = 'query=' . $sql .
67 '&params=' . serialize($params) .
68 '&types=' . serialize($types) .
69 '&connectionParams=' . hash('sha256', serialize($connectionParams));
70
71 // should the key be automatically generated using the inputs or is the cache key set?
72 $cacheKey = $this->cacheKey ?? sha1($realCacheKey);
73
74 return [$cacheKey, $realCacheKey];
75 }
76
77 public function setResultCache(CacheItemPoolInterface $cache): QueryCacheProfile
78 {
79 return new QueryCacheProfile($this->lifetime, $this->cacheKey, $cache);
80 }
81
82 public function setCacheKey(?string $cacheKey): self
83 {
84 return new QueryCacheProfile($this->lifetime, $cacheKey, $this->resultCache);
85 }
86
87 public function setLifetime(int $lifetime): self
88 {
89 return new QueryCacheProfile($lifetime, $this->cacheKey, $this->resultCache);
90 }
91}