Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
12 / 18
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SqliteResultWrapper
66.67% covered (warning)
66.67%
12 / 18
57.14% covered (warning)
57.14%
4 / 7
13.70
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 doNumRows
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 doFetchObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 doFetchRow
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
3.02
 doSeek
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 doFree
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 doGetFieldNames
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use PDO;
6use PDOStatement;
7
8class SqliteResultWrapper extends ResultWrapper {
9    /** @var PDOStatement|null */
10    private $result;
11    /** @var array|null */
12    private $rows;
13
14    /**
15     * @internal
16     * @param PDOStatement $result
17     */
18    public function __construct( PDOStatement $result ) {
19        $this->result = $result;
20        // SQLite doesn't allow buffered results or data seeking etc, so we'll
21        // use fetchAll. PDO has PDO::CURSOR_SCROLL but the SQLite C API doesn't
22        // support it, so the driver raises an error if it is used.
23        $this->rows = $result->fetchAll( PDO::FETCH_OBJ );
24    }
25
26    /** @inheritDoc */
27    protected function doNumRows() {
28        return count( $this->rows );
29    }
30
31    /** @inheritDoc */
32    protected function doFetchObject() {
33        return $this->rows[$this->currentPos] ?? false;
34    }
35
36    /** @inheritDoc */
37    protected function doFetchRow() {
38        $obj = $this->doFetchObject();
39        if ( is_object( $obj ) ) {
40            $i = 0;
41            $row = get_object_vars( $obj );
42            foreach ( $row as $value ) {
43                $row[$i++] = $value;
44            }
45            return $row;
46        } else {
47            return $obj;
48        }
49    }
50
51    /** @inheritDoc */
52    protected function doSeek( $pos ) {
53        // Nothing to do -- parent updates $this->currentPos
54    }
55
56    /** @inheritDoc */
57    protected function doFree() {
58        $this->rows = null;
59        $this->result = null;
60    }
61
62    /** @inheritDoc */
63    protected function doGetFieldNames() {
64        if ( $this->rows ) {
65            return array_keys( get_object_vars( $this->rows[0] ) );
66        } else {
67            return [];
68        }
69    }
70}