<?php
namespace CodeIgniter\Database\SQLite3;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\ResultInterface;
use CodeIgniter\Entity;
class Result extends BaseResult implements ResultInterface
{
public function getFieldCount(): int
{
return $this->resultID->numColumns();
}
public function getFieldNames(): array
{
$fieldNames = [];
for ($i = 0, $c = $this->getFieldCount(); $i < $c; $i ++)
{
$fieldNames[] = $this->resultID->columnName($i);
}
return $fieldNames;
}
public function getFieldData(): array
{
static $data_types = [
SQLITE3_INTEGER => 'integer',
SQLITE3_FLOAT => 'float',
SQLITE3_TEXT => 'text',
SQLITE3_BLOB => 'blob',
SQLITE3_NULL => 'null',
];
$retVal = [];
for ($i = 0, $c = $this->getFieldCount(); $i < $c; $i ++)
{
$retVal[$i] = new \stdClass();
$retVal[$i]->name = $this->resultID->columnName($i);
$type = $this->resultID->columnType($i);
$retVal[$i]->type = isset($data_types[$type]) ? $data_types[$type] : $type;
$retVal[$i]->max_length = null;
}
return $retVal;
}
public function freeResult()
{
if (is_object($this->resultID))
{
$this->resultID->finalize();
$this->resultID = false;
}
}
public function dataSeek(int $n = 0)
{
if ($n !== 0)
{
throw new DatabaseException('SQLite3 doesn\'t support seeking to other offset.');
}
return $this->resultID->reset();
}
protected function fetchAssoc()
{
return $this->resultID->fetchArray(SQLITE3_ASSOC);
}
protected function fetchObject(string $className = 'stdClass')
{
if (($row = $this->fetchAssoc()) === false)
{
return false;
}
elseif ($className === 'stdClass')
{
return (object) $row;
}
$classObj = new $className();
if (is_subclass_of($className, Entity::class))
{
return $classObj->setAttributes($row);
}
$classSet = \Closure::bind(function ($key, $value) {
$this->$key = $value;
}, $classObj, $className
);
foreach (array_keys($row) as $key)
{
$classSet($key, $row[$key]);
}
return $classObj;
}
}