<commit_before><?php
namespace EngineWorks\DBAL\Sqlite;

use EngineWorks\DBAL\CommonTypes;
use EngineWorks\DBAL\Result as ResultInterface;
use EngineWorks\DBAL\Traits\ResultGetFieldsCachedTrait;
use SQLite3Result;

class Result implements ResultInterface
{
    use ResultGetFieldsCachedTrait;

    /**
     * Sqlite3 element
     * @var SQLite3Result
     */
    private $query = false;

    /**
     * The number of the result rows
     * @var int
     */
    private $numRows;

    /**
     * Result based on Sqlite3
     * @param SQLite3Result $result
     * @param int $numRows If negative number then the number of rows will be obtained
     * from fetching all the rows and reset the result
     */
    public function __construct(SQLite3Result $result, $numRows)
    {
        $this->query = $result;
        if ($numRows < 0) {
            $numRows = $this->obtainNumRows();
        }
        $this->numRows = $numRows;
    }

    /**
     * Close the query and remove property association
     */
    public function __destruct()
    {
        $this->query->finalize();
        $this->query = null;
    }

    /**
     * Internal method to retrieve the number of rows if not supplied from constructor
     *
     * @return int
     */
    private function obtainNumRows()
    {
        $count = 0;
        while (false !== $this->query->fetchArray(SQLITE3_NUM)) {
            $count = $count + 1;
        }
        $this->query->reset();
        return $count;
    }

    public function realGetFields()
    {
        $fields = [];
        $numcolumns = $this->query->numColumns();
        for ($i = 0; $i < $numcolumns; $i++) {
            $fields[] = [
                'name' => $this->query->columnName($i),
                'commontype' => $this->getCommonType($this->query->columnType($i)),
                'table' => '',
                'flags' => null,  // extra: used for getting the ids in the query
            ];
        }
        return $fields;
    }

    /**
     * Private function to get the CommonType from the information of the field
     *
     * @param int $field
     * @return string
     */
    private function getCommonType($field)
    {
        static $types = [
            SQLITE3_INTEGER => CommonTypes::TINT,
            SQLITE3_FLOAT => CommonTypes::TNUMBER,
            SQLITE3_TEXT => CommonTypes::TTEXT,
            // static::SQLITE3_BLOB => CommonTypes::TTEXT,
            // static::SQLITE3_NULL => CommonTypes::TTEXT,
        ];
        return (array_key_exists($field, $types)) ? $types[$field] : CommonTypes::TTEXT;
    }

    public function getIdFields()
    {
        // TODO: investigate how to get the ID Fields from the SQLite3Result
        return false;
    }

    public function resultCount()
    {
        return $this->numRows;
    }

    public function fetchRow()
    {
        $return = $this->query->fetchArray(SQLITE3_ASSOC);
        return (! is_array($return)) ? false : $return;
    }

    public function moveTo($offset)
    {
        if ($offset < 0) {
            return false;
        }
        if (! $this->numRows) {
            return false;
        }
        if ($offset > $this->numRows - 1) {
            return false;
        }
        for ($i = 0; $i < $offset; $i++) {
            if (false === $this->query->fetchArray(SQLITE3_NUM)) {
                return false;
            }
        }
        return true;
    }

    public function moveFirst()
    {
        if ($this->resultCount() <= 0) {
            return false;<commit_msg>Use traits to implement interfaces Fix method moveTo<commit_after><?php
namespace EngineWorks\DBAL\Sqlite;

use EngineWorks\DBAL\CommonTypes;
use EngineWorks\DBAL\Result as ResultInterface;
use EngineWorks\DBAL\Traits\ResultGetFieldsCachedTrait;
use EngineWorks\DBAL\Traits\ResultImplementsCountable;
use EngineWorks\DBAL\Traits\ResultImplementsIterator;
use SQLite3Result;

class Result implements ResultInterface
{
    use ResultGetFieldsCachedTrait;
    use ResultImplementsCountable;
    use ResultImplementsIterator;

    /**
     * Sqlite3 element
     * @var SQLite3Result
     */
    private $query = false;

    /**
     * The number of the result rows
     * @var int
     */
    private $numRows;

    /**
     * Result based on Sqlite3
     * @param SQLite3Result $result
     * @param int $numRows If negative number then the number of rows will be obtained
     * from fetching all the rows and reset the result
     */
    public function __construct(SQLite3Result $result, $numRows)
    {
        $this->query = $result;
        if ($numRows < 0) {
            $numRows = $this->obtainNumRows();
        }
        $this->numRows = $numRows;
    }

    /**
     * Close the query and remove property association
     */
    public function __destruct()
    {
        @$this->query->finalize();
        $this->query = null;
    }

    /**
     * Internal method to retrieve the number of rows if not supplied from constructor
     *
     * @return int
     */
    private function obtainNumRows()
    {
        $count = 0;
        while (false !== $this->query->fetchArray(SQLITE3_NUM)) {
            $count = $count + 1;
        }
        $this->query->reset();
        return $count;
    }

    public function realGetFields()
    {
        $fields = [];
        $numcolumns = $this->query->numColumns();
        for ($i = 0; $i < $numcolumns; $i++) {
            $fields[] = [
                'name' => $this->query->columnName($i),
                'commontype' => $this->getCommonType($this->query->columnType($i)),
                'table' => '',
                'flags' => null,  // extra: used for getting the ids in the query
            ];
        }
        return $fields;
    }

    /**
     * Private function to get the CommonType from the information of the field
     *
     * @param int $field
     * @return string
     */
    private function getCommonType($field)
    {
        static $types = [
            SQLITE3_INTEGER => CommonTypes::TINT,
            SQLITE3_FLOAT => CommonTypes::TNUMBER,
            SQLITE3_TEXT => CommonTypes::TTEXT,
            // static::SQLITE3_BLOB => CommonTypes::TTEXT,
            // static::SQLITE3_NULL => CommonTypes::TTEXT,
        ];
        return (array_key_exists($field, $types)) ? $types[$field] : CommonTypes::TTEXT;
    }

    public function getIdFields()
    {
        // TODO: investigate how to get the ID Fields from the SQLite3Result
        return false;
    }

    public function resultCount()
    {
        return $this->numRows;
    }

    public function fetchRow()
    {
        $return = $this->query->fetchArray(SQLITE3_ASSOC);
        return (! is_array($return)) ? false : $return;
    }

    public function moveTo($offset)
    {
        // there are no records
        if ($this->resultCount() <= 0) {
            return false;
        }
        // the offset is out of bounds
        if ($offset < 0 || $offset > $this->numRows - 1) {
            return false;
        }
        // if the offset is on previous
        if (! $this->moveFirst()) {
            return false;
        }
        // move to the offset
        for ($i = 0; $i < $offset; $i++) {
            if (false === $this->query->fetchArray(SQLITE3_NUM)) {
                return false;
            }
        }
        return true;
    }

    public function moveFirst()
    {
        if ($this->resultCount() <= 0) {
            return false;
{'fraction_non_alphanumeric': 0.12015086206896551, 'fraction_numerical': 0.006330818965517241, 'mean_word_length': 1.8879813302217037, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2000332', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2109, 'n_words': 554}