Completed
Push — 2.9 ( 62dacc...5d4f93 )
by Sergei
68:39 queued 53:35
created

ResultCacheStatement   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Test Coverage

Coverage 83.02%

Importance

Changes 0
Metric Value
wmc 19
eloc 51
dl 0
loc 170
rs 10
c 0
b 0
f 0
ccs 44
cts 53
cp 0.8302

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fetchColumn() 0 6 1
A rowCount() 0 3 1
A setFetchMode() 0 5 1
A closeCursor() 0 17 4
B fetch() 0 35 8
A columnCount() 0 3 1
A getIterator() 0 5 1
A fetchAll() 0 6 1
1
<?php
2
3
namespace Doctrine\DBAL\Cache;
4
5
use ArrayIterator;
6
use Doctrine\Common\Cache\Cache;
7
use Doctrine\DBAL\Driver\ResultStatement;
8
use Doctrine\DBAL\Driver\Statement;
9
use Doctrine\DBAL\FetchMode;
10
use InvalidArgumentException;
11
use IteratorAggregate;
12
use PDO;
13
use function array_merge;
14
use function array_values;
15
use function reset;
16
17
/**
18
 * Cache statement for SQL results.
19
 *
20
 * A result is saved in multiple cache keys, there is the originally specified
21
 * cache key which is just pointing to result rows by key. The following things
22
 * have to be ensured:
23
 *
24
 * 1. lifetime of the original key has to be longer than that of all the individual rows keys
25
 * 2. if any one row key is missing the query has to be re-executed.
26
 *
27
 * Also you have to realize that the cache will load the whole result into memory at once to ensure 2.
28
 * This means that the memory usage for cached results might increase by using this feature.
29
 */
30
class ResultCacheStatement implements IteratorAggregate, ResultStatement
31
{
32
    /** @var Cache */
33
    private $resultCache;
34
35
    /** @var string */
36
    private $cacheKey;
37
38
    /** @var string */
39
    private $realKey;
40
41
    /** @var int */
42
    private $lifetime;
43
44
    /** @var Statement */
45
    private $statement;
46
47
    /**
48
     * Did we reach the end of the statement?
49
     *
50
     * @var bool
51
     */
52
    private $emptied = false;
53
54
    /** @var mixed[] */
55
    private $data;
56
57
    /** @var int */
58
    private $defaultFetchMode = FetchMode::MIXED;
59
60
    /**
61
     * @param string $cacheKey
62
     * @param string $realKey
63
     * @param int    $lifetime
64
     */
65 297
    public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
66
    {
67 297
        $this->statement   = $stmt;
68 297
        $this->resultCache = $resultCache;
69 297
        $this->cacheKey    = $cacheKey;
70 297
        $this->realKey     = $realKey;
71 297
        $this->lifetime    = $lifetime;
72 297
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 270
    public function closeCursor()
78
    {
79 270
        $this->statement->closeCursor();
80 270
        if (! $this->emptied || $this->data === null) {
81 27
            return true;
82
        }
83
84 270
        $data = $this->resultCache->fetch($this->cacheKey);
85 270
        if (! $data) {
86 270
            $data = [];
87
        }
88 270
        $data[$this->realKey] = $this->data;
89
90 270
        $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
91 270
        unset($this->data);
92
93 270
        return true;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 108
    public function columnCount()
100
    {
101 108
        return $this->statement->columnCount();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 297
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
108
    {
109 297
        $this->defaultFetchMode = $fetchMode;
110
111 297
        return true;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getIterator()
118
    {
119
        $data = $this->fetchAll();
120
121
        return new ArrayIterator($data);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 270
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
128
    {
129 270
        if ($this->data === null) {
130 270
            $this->data = [];
131
        }
132
133 270
        $row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
134
135 270
        if ($row) {
136 216
            $this->data[] = $row;
137
138 216
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
139
140 216
            if ($fetchMode === FetchMode::ASSOCIATIVE) {
141 108
                return $row;
142
            }
143
144 162
            if ($fetchMode === FetchMode::NUMERIC) {
145 81
                return array_values($row);
146
            }
147
148 81
            if ($fetchMode === FetchMode::MIXED) {
149 54
                return array_merge($row, array_values($row));
150
            }
151
152 27
            if ($fetchMode === FetchMode::COLUMN) {
153 27
                return reset($row);
154
            }
155
156
            throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
157
        }
158
159 270
        $this->emptied = true;
160
161 270
        return false;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 27
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
168
    {
169 27
        $this->data    = $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
170 27
        $this->emptied = true;
171
172 27
        return $this->data;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function fetchColumn($columnIndex = 0)
179
    {
180
        $row = $this->fetch(FetchMode::NUMERIC);
181
182
        // TODO: verify that return false is the correct behavior
183
        return $row[$columnIndex] ?? false;
184
    }
185
186
    /**
187
     * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
188
     * executed by the corresponding object.
189
     *
190
     * If the last SQL statement executed by the associated Statement object was a SELECT statement,
191
     * some databases may return the number of rows returned by that statement. However,
192
     * this behaviour is not guaranteed for all databases and should not be
193
     * relied on for portable applications.
194
     *
195
     * @return int The number of rows.
196
     */
197
    public function rowCount()
198
    {
199
        return $this->statement->rowCount();
200
    }
201
}
202