MediaWiki  master
SqliteResultWrapper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use ArrayIterator;
6 use PDO;
7 use PDOStatement;
8 
11  private $result;
13  private $rows;
14 
19  public function __construct( PDOStatement $result ) {
20  $this->result = $result;
21  // SQLite doesn't allow buffered results or data seeking etc, so we'll
22  // use fetchAll. PDO has PDO::CURSOR_SCROLL but the SQLite C API doesn't
23  // support it, so the driver raises an error if it is used.
24  $this->rows = $result->fetchAll( PDO::FETCH_OBJ );
25  }
26 
34  public function getInternalResult() {
35  return $this->result;
36  }
37 
38  protected function doNumRows() {
39  return count( $this->rows );
40  }
41 
42  protected function doFetchObject() {
43  return $this->rows[$this->currentPos] ?? false;
44  }
45 
46  protected function doFetchRow() {
47  $obj = $this->doFetchObject();
48  if ( is_object( $obj ) ) {
49  $i = 0;
50  $row = get_object_vars( $obj );
51  foreach ( $row as $value ) {
52  $row[$i++] = $value;
53  }
54  return $row;
55  } else {
56  return $obj;
57  }
58  }
59 
60  protected function doSeek( $pos ) {
61  // Nothing to do -- parent updates $this->currentPos
62  }
63 
64  protected function doFree() {
65  $this->rows = null;
66  $this->result = null;
67  }
68 
69  protected function doGetFieldNames() {
70  if ( $this->rows ) {
71  return array_keys( get_object_vars( $this->rows[0] ) );
72  } else {
73  return [];
74  }
75  }
76 }
Wikimedia\Rdbms\SqliteResultWrapper\doNumRows
doNumRows()
Get the number of rows in the result set.
Definition: SqliteResultWrapper.php:38
Wikimedia\Rdbms\SqliteResultWrapper\doFetchRow
doFetchRow()
Get the next row as an array containing the data duplicated, once with string keys and once with nume...
Definition: SqliteResultWrapper.php:46
Wikimedia\Rdbms\SqliteResultWrapper\doFetchObject
doFetchObject()
Get the next row as a stdClass object, or false if iteration has proceeded past the end.
Definition: SqliteResultWrapper.php:42
Wikimedia\Rdbms\SqliteResultWrapper
Definition: SqliteResultWrapper.php:9
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\SqliteResultWrapper\doFree
doFree()
Free underlying data.
Definition: SqliteResultWrapper.php:64
Wikimedia\Rdbms\ResultWrapper\$currentPos
int $currentPos
The offset of the current row that would be returned by current() and may have been previously return...
Definition: ResultWrapper.php:34
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:23
Wikimedia\Rdbms\SqliteResultWrapper\$rows
ArrayIterator null $rows
Definition: SqliteResultWrapper.php:13
Wikimedia\Rdbms\SqliteResultWrapper\getInternalResult
getInternalResult()
Get the underlying result object or array.
Definition: SqliteResultWrapper.php:34
Wikimedia\Rdbms\SqliteResultWrapper\doGetFieldNames
doGetFieldNames()
Get the field names in the result set.
Definition: SqliteResultWrapper.php:69
Wikimedia\Rdbms\SqliteResultWrapper\doSeek
doSeek( $pos)
Modify the current cursor position to the row with the specified offset.
Definition: SqliteResultWrapper.php:60
Wikimedia\Rdbms\ResultWrapper\count
count()
Get the number of rows in a result object.
Definition: ResultWrapper.php:125
Wikimedia\Rdbms\SqliteResultWrapper\__construct
__construct(PDOStatement $result)
Definition: SqliteResultWrapper.php:19
Wikimedia\Rdbms\SqliteResultWrapper\$result
PDOStatement null $result
Definition: SqliteResultWrapper.php:11