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

SQLite3Query   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 6 2
A seek() 0 9 3
A numRecords() 0 9 2
A nextRecord() 0 8 2
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