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

PDOQuery   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A numRecords() 0 3 1
A getIterator() 0 3 1
A __construct() 0 8 1
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