Completed
Pull Request — 2.10 (#3834)
by Máté
12:16
created

ResultCacheStatement::setFetchMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

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