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

MySQLQuery::getIterator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
/**
6
 * A result-set from a MySQL database (using MySQLiConnector)
7
 */
8
class MySQLQuery extends Query
9
{
10
11
    /**
12
     * The internal MySQL handle that points to the result set.
13
     * Select queries will have mysqli_result as a value.
14
     * Non-select queries will not
15
     *
16
     * @var mixed
17
     */
18
    protected $handle;
19
20
    /**
21
     * Hook the result-set given into a Query class, suitable for use by SilverStripe.
22
     *
23
     * @param MySQLiConnector $database The database object that created this query.
24
     * @param mixed $handle the internal mysql handle that is points to the resultset.
25
     * Non-mysqli_result values could be given for non-select queries (e.g. true)
26
     */
27
    public function __construct($database, $handle)
28
    {
29
        $this->handle = $handle;
30
    }
31
32
    public function __destruct()
33
    {
34
        if (is_object($this->handle)) {
35
            $this->handle->free();
36
        }
37
    }
38
39
    public function getIterator()
40
    {
41
        if (is_object($this->handle)) {
42
            while ($data = $this->handle->fetch_assoc()) {
43
                yield $data;
44
            }
45
        }
46
    }
47
48
    public function numRecords()
49
    {
50
        if (is_object($this->handle)) {
51
            return $this->handle->num_rows;
52
        }
53
54
        return null;
55
    }
56
}
57