Passed
Push — master ( 288a96...964b09 )
by Daniel
10:30
created

PDOQuery::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use PDOStatement;
6
use PDO;
7
use ArrayIterator;
8
9
/**
10
 * A result-set from a PDO database.
11
 */
12
class PDOQuery extends Query
13
{
14
    /**
15
     * The internal MySQL handle that points to the result set.
16
     * @var PDOStatement
17
     */
18
    protected $statement = null;
19
20
    protected $results = null;
21
22
    /**
23
     * Hook the result-set given into a Query class, suitable for use by SilverStripe.
24
     * @param PDOStatement $statement The internal PDOStatement containing the results
25
     */
26
    public function __construct(PDOStatement $statement)
27
    {
28
        $this->statement = $statement;
29
        // Since no more than one PDOStatement for any one connection can be safely
30
        // traversed, each statement simply requests all rows at once for safety.
31
        // This could be re-engineered to call fetchAll on an as-needed basis
32
        $this->results = $statement->fetchAll(PDO::FETCH_ASSOC);
33
        $statement->closeCursor();
34
    }
35
36
    public function getIterator()
37
    {
38
        return new ArrayIterator($this->results);
39
    }
40
41
    public function numRecords()
42
    {
43
        return count($this->results);
44
    }
45
}
46