Completed
Pull Request — master (#3331)
by Michael
17:12
created

ResultCacheStatement::closeCursor()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 3
nop 0
crap 4
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 190
    public function __construct(Statement $stmt, Cache $resultCache, $cacheKey, $realKey, $lifetime)
66
    {
67 190
        $this->statement   = $stmt;
68 190
        $this->resultCache = $resultCache;
69 190
        $this->cacheKey    = $cacheKey;
70 190
        $this->realKey     = $realKey;
71 190
        $this->lifetime    = $lifetime;
72 190
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 171
    public function closeCursor()
78
    {
79 171
        $this->statement->closeCursor();
80 171
        if (! $this->emptied || $this->data === null) {
81 19
            return true;
82
        }
83
84 171
        $data = $this->resultCache->fetch($this->cacheKey);
85 171
        if (! $data) {
86 171
            $data = [];
87
        }
88 171
        $data[$this->realKey] = $this->data;
89
90 171
        $this->resultCache->save($this->cacheKey, $data, $this->lifetime);
91 171
        unset($this->data);
92
93 171
        return true;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 76
    public function columnCount()
100
    {
101 76
        return $this->statement->columnCount();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 190
    public function setFetchMode($fetchMode, $arg2 = null, $arg3 = null)
108
    {
109 190
        $this->defaultFetchMode = $fetchMode;
110
111 190
        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 190
    public function fetch($fetchMode = null, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0)
128
    {
129 190
        if ($this->data === null) {
130 190
            $this->data = [];
131
        }
132
133 190
        $row = $this->statement->fetch(FetchMode::ASSOCIATIVE);
134
135 190
        if ($row) {
136 152
            $this->data[] = $row;
137
138 152
            $fetchMode = $fetchMode ?: $this->defaultFetchMode;
139
140 152
            if ($fetchMode === FetchMode::ASSOCIATIVE) {
141 76
                return $row;
142
            }
143
144 114
            if ($fetchMode === FetchMode::NUMERIC) {
145 57
                return array_values($row);
146
            }
147
148 57
            if ($fetchMode === FetchMode::MIXED) {
149 38
                return array_merge($row, array_values($row));
150
            }
151
152 19
            if ($fetchMode === FetchMode::COLUMN) {
153 19
                return reset($row);
154
            }
155
156
            throw new InvalidArgumentException('Invalid fetch-style given for caching result.');
157
        }
158
159 190
        $this->emptied = true;
160
161 190
        return false;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
168
    {
169
        return $this->statement->fetchAll($fetchMode, $fetchArgument, $ctorArgs);
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function fetchColumn($columnIndex = 0)
176
    {
177
        $row = $this->fetch(FetchMode::NUMERIC);
178
179
        // TODO: verify that return false is the correct behavior
180
        return $row[$columnIndex] ?? false;
181
    }
182
183
    /**
184
     * Returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
185
     * executed by the corresponding object.
186
     *
187
     * If the last SQL statement executed by the associated Statement object was a SELECT statement,
188
     * some databases may return the number of rows returned by that statement. However,
189
     * this behaviour is not guaranteed for all databases and should not be
190
     * relied on for portable applications.
191
     *
192
     * @return int The number of rows.
193
     */
194
    public function rowCount()
195
    {
196
        return $this->statement->rowCount();
197
    }
198
}
199