Completed
Pull Request — master (#23)
by Damian
08:37
created

SQLite3Query::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace SilverStripe\SQLite;
4
5
use SilverStripe\ORM\Connect\SS_Query;
6
use SQLite3Result;
7
8
/**
9
 * A result-set from a SQLite3 database.
10
 *
11
 * @package SQLite3
12
 */
13
class SQLite3Query extends SS_Query
14
{
15
16
    /**
17
     * The SQLite3Connector object that created this result set.
18
     *
19
     * @var SQLite3Connector
20
     */
21
    protected $database;
22
23
    /**
24
     * The internal sqlite3 handle that points to the result set.
25
     *
26
     * @var SQLite3Result
27
     */
28
    protected $handle;
29
30
    /**
31
     * Hook the result-set given into a Query class, suitable for use by framework.
32
     * @param SQLite3Connector $database The database object that created this query.
33
     * @param SQLite3Result $handle the internal sqlite3 handle that is points to the resultset.
34
     */
35
    public function __construct(SQLite3Connector $database, SQLite3Result $handle)
36
    {
37
        $this->database = $database;
38
        $this->handle = $handle;
39
    }
40
41
    public function __destruct()
42
    {
43
        if ($this->handle) {
44
            $this->handle->finalize();
45
        }
46
    }
47
48
    public function seek($row)
49
    {
50
        $this->handle->reset();
51
        $i=0;
52
        while ($i < $row && $row = @$this->handle->fetchArray()) {
53
            $i++;
54
        }
55
        return true;
56
    }
57
58
    /**
59
     * @todo This looks terrible but there is no SQLite3::get_num_rows() implementation
60
     */
61
    public function numRecords()
62
    {
63
        $c=0;
64
        while ($this->handle->fetchArray()) {
65
            $c++;
66
        }
67
        $this->handle->reset();
68
        return $c;
69
    }
70
71
    public function nextRecord()
72
    {
73
        if ($data = $this->handle->fetchArray(SQLITE3_ASSOC)) {
74
            return $data;
75
        } else {
76
            return false;
77
        }
78
    }
79
}
80