Queryer/Engine/Sqlite3/Sqlite3DriverResult.php
<?php
namespace Queryer\Engine\Sqlite3;
use Queryer\Driver\DatabaseDriverResult;
/**
* Class Sqlite3DriverResult
* @package Queryer\Engine\Sqlite3
*/
class Sqlite3DriverResult extends DatabaseDriverResult
{
/**
* The number of rows affected by the query.
* @var int
*/
private $affectedRows;
/**
* The insert ID generated by the query.
* @var int
*/
private $insertId;
/**
* The query executed that resulted in this object.
* @var string
*/
private $query;
/**
* The current position in the result.
* @var int
*/
private $currentOffset;
/**
* Initializes the SQLite3 Driver Result object.
*
* @param mixed $result
* @param int $affectedRows
* @param int $insertId
* @param int $errorCode
* @param string $errorMessage
* @param string $query
*/
public function __construct($result, $affectedRows, $insertId, $errorCode, $errorMessage, $query)
{
$this->affectedRows = $affectedRows;
$this->insertId = $insertId;
$this->query = $query;
$this->currentOffset = 0;
parent::__construct($result, $errorCode, $errorMessage);
}
/**
* Returns the number of rows that were affected by the query.
*
* @return int
*/
public function getAffectedRows()
{
return $this->affectedRows;
}
/**
* Returns the ID for the row inserted into a table with an auto incrementing column.
*
* @return int
*/
public function getInsertId()
{
return $this->insertId;
}
/**
* Returns the number of rows in the result set.
*
* @return int
*/
public function getNumRows()
{
static $numRows = null;
if (!is_null($numRows))
{
return $numRows;
}
// It looks like we have work to do!
$prevOffset = $this->getCurrentOffset();
/** @var \SQLite3Result $result */
$result = $this->getResult();
$result->reset();
$numRows = 0;
while($result->fetchArray() !== false)
{
$numRows++;
}
// Reset the position.
$this->seek($prevOffset);
return $numRows;
}
/**
* Moves the pointer of the result set to the specified offset.
*
* @param int $offset
* @return bool
*/
public function seek($offset)
{
/** @var \SQLite3Result $result */
$result = $this->getResult();
// Save the previous.
$prevOffset = $this->getCurrentOffset();
$this->setCurrentOffset($offset);
// Reset to the beginning.
$result->reset();
// Keep moving along.
while($offset > 0 && $result->fetchArray(SQLITE_NUM) !== false)
{
$offset--;
}
// If it didn't work, reset to the previous offset.
if ($offset > 0)
{
$this->seek($prevOffset);
}
return $offset === 0;
}
/**
* Returns the row (associative) or false if there are no more rows in the result set.
*
* @return array|bool
*/
public function fetchAssoc()
{
/** @var \SQLite3Result $result */
$result = $this->getResult();
$row = $result->fetchArray(SQLITE3_ASSOC);
if ($row !== false)
{
$this->incrementCurrentOffset();
}
return $row;
}
/**
* Returns the row (numeric index only) or false if there are no more rows in the result set.
*
* @return array|bool
*/
public function fetchRow()
{
/** @var \SQLite3Result $result */
$result = $this->getResult();
$row = $result->fetchArray(SQLITE3_NUM);
if ($row !== false)
{
$this->incrementCurrentOffset();
}
return $row;
}
/**
* Returns a string containing the generated query that was ran against the database to result in this object. This
* is potentially useful for debugging.
*
* @return string
*/
public function getQuery()
{
return $this->query;
}
/**
* Returns the current offset.
*
* @return int
*/
private function getCurrentOffset()
{
return $this->currentOffset;
}
/**
* Sets the current offset.
*
* @param int $offset
*/
private function setCurrentOffset($offset)
{
$this->currentOffset = (int)$offset;
}
/**
* Increments the current offset by one.
*/
private function incrementCurrentOffset()
{
$this->currentOffset++;
}
}