2013-04-03 06:49:59 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-29 03:55:45 +02:00
|
|
|
namespace SilverStripe\SQLite;
|
|
|
|
|
2016-09-09 05:46:48 +02:00
|
|
|
use SilverStripe\ORM\Connect\Query;
|
2016-06-29 03:55:45 +02:00
|
|
|
use SQLite3Result;
|
|
|
|
|
2013-04-03 06:49:59 +02:00
|
|
|
/**
|
|
|
|
* A result-set from a SQLite3 database.
|
|
|
|
*/
|
2016-09-09 05:46:48 +02:00
|
|
|
class SQLite3Query extends Query
|
2015-12-17 19:11:01 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The SQLite3Connector object that created this result set.
|
2016-06-29 03:55:45 +02:00
|
|
|
*
|
2015-12-17 19:11:01 +01:00
|
|
|
* @var SQLite3Connector
|
|
|
|
*/
|
|
|
|
protected $database;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The internal sqlite3 handle that points to the result set.
|
2016-06-29 03:55:45 +02:00
|
|
|
*
|
2015-12-17 19:11:01 +01:00
|
|
|
* @var SQLite3Result
|
|
|
|
*/
|
|
|
|
protected $handle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook the result-set given into a Query class, suitable for use by framework.
|
|
|
|
* @param SQLite3Connector $database The database object that created this query.
|
|
|
|
* @param SQLite3Result $handle the internal sqlite3 handle that is points to the resultset.
|
|
|
|
*/
|
|
|
|
public function __construct(SQLite3Connector $database, SQLite3Result $handle)
|
|
|
|
{
|
|
|
|
$this->database = $database;
|
|
|
|
$this->handle = $handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if ($this->handle) {
|
|
|
|
$this->handle->finalize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function seek($row)
|
|
|
|
{
|
|
|
|
$this->handle->reset();
|
|
|
|
$i=0;
|
2017-01-12 18:08:05 +01:00
|
|
|
while ($i <= $row && $result = @$this->handle->fetchArray(SQLITE3_ASSOC)) {
|
2015-12-17 19:11:01 +01:00
|
|
|
$i++;
|
|
|
|
}
|
2017-01-12 18:08:05 +01:00
|
|
|
return $result;
|
2015-12-17 19:11:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo This looks terrible but there is no SQLite3::get_num_rows() implementation
|
|
|
|
*/
|
|
|
|
public function numRecords()
|
|
|
|
{
|
2018-03-12 14:04:43 +01:00
|
|
|
// Some queries are not iterable using fetchArray like CREATE statement
|
|
|
|
if (!$this->handle->numColumns()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-12 18:08:05 +01:00
|
|
|
$this->handle->reset();
|
2015-12-17 19:11:01 +01:00
|
|
|
$c=0;
|
|
|
|
while ($this->handle->fetchArray()) {
|
|
|
|
$c++;
|
|
|
|
}
|
|
|
|
$this->handle->reset();
|
|
|
|
return $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function nextRecord()
|
|
|
|
{
|
|
|
|
if ($data = $this->handle->fetchArray(SQLITE3_ASSOC)) {
|
|
|
|
return $data;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 06:49:59 +02:00
|
|
|
}
|