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

code/SQLite3Query.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * A result-set from a SQLite3 database.
5
 * 
6
 * @package SQLite3
7
 */
8
class SQLite3Query extends SS_Query
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
11
    /**
12
     * The SQLite3Connector object that created this result set.
13
     * 
14
     * @var SQLite3Connector
15
     */
16
    protected $database;
17
18
    /**
19
     * The internal sqlite3 handle that points to the result set.
20
     * 
21
     * @var SQLite3Result
22
     */
23
    protected $handle;
24
25
    /**
26
     * Hook the result-set given into a Query class, suitable for use by framework.
27
     * @param SQLite3Connector $database The database object that created this query.
28
     * @param SQLite3Result $handle the internal sqlite3 handle that is points to the resultset.
29
     */
30
    public function __construct(SQLite3Connector $database, SQLite3Result $handle)
31
    {
32
        $this->database = $database;
33
        $this->handle = $handle;
34
    }
35
36
    public function __destruct()
37
    {
38
        if ($this->handle) {
39
            $this->handle->finalize();
40
        }
41
    }
42
43
    public function seek($row)
44
    {
45
        $this->handle->reset();
46
        $i=0;
47
        while ($i < $row && $row = @$this->handle->fetchArray()) {
48
            $i++;
49
        }
50
        return true;
51
    }
52
53
    /**
54
     * @todo This looks terrible but there is no SQLite3::get_num_rows() implementation
55
     */
56
    public function numRecords()
57
    {
58
        $c=0;
59
        while ($this->handle->fetchArray()) {
60
            $c++;
61
        }
62
        $this->handle->reset();
63
        return $c;
64
    }
65
66
    public function nextRecord()
67
    {
68
        if ($data = $this->handle->fetchArray(SQLITE3_ASSOC)) {
69
            return $data;
70
        } else {
71
            return false;
72
        }
73
    }
74
}
75