<commit_before><?php

namespace Reduce\Db;
    
use Iterator;
use ArrayAccess;
use Countable;
use JsonSerializable;
use Reduce\Db\Row;
use Reduce\Db\Query\QueryBuilder;
    
class ResultSet implements Iterator, ArrayAccess, Countable, JsonSerializable 
{
    protected $queryBuilder;
    protected $data;
    protected $single = false;
    protected $executed = false;
    protected $tableName;
    
    public function __construct($tableName, QueryBuilder $queryBuilder, $singleResultSet = false) 
    {
        $this->tableName    = $tableName;
        $this->queryBuilder = $queryBuilder;
        $this->single       = $singleResultSet;
        
        $this->queryBuilder->select('*')->from($tableName);
    }
    
    public function __call($name, $args)
    {
        if (method_exists($this->getQueryBuilder(), $name)) {
            call_user_func_array([$this->getQueryBuilder(), $name], $args);
            return $this;
        }
        
        $condition = sprintf('%s.id = %s.%s_id', $this->getTableName(), $name, $this->getTableName());
        
        $this->queryBuilder->leftJoin($this->getTableName(), $name, $name, $condition);
        
        return $this;
    }
    
    public function getTableName()
    {
        return $this->tableName;
    }
    
    public function getQueryBuilder()
    {
        return $this->queryBuilder;
    }
    
    protected function execute()
    {
        if (!$this->executed) {
            $this->data = $this->getQueryBuilder()->execute()->fetchAll();
            $this->executed = true;
        }
    }
    
    protected function createRow($data = [])
    {
        return new Row($this, $data);
    }
    
    public function offsetGet($key)
    {
        if ($this->single) {
            $this->getQueryBuilder()->where('id = ?', $key);
            
            return $this->createRow();
        }
        
        if (isset($this->data[$key])) {
            $this->execute();
            return $this->data[$key];
        }
        
        return [];
    }
    
    public function offsetSet($key, $value)
    {}
    
    public function offsetUnset($key)
    {}
    
    public function offsetExists($key)
    {
        return !! $this->offsetGet($key);
    }
    
    public function count()
    {
        $this->execute();
        return count($this->data);
    }
    
    public function rewind() 
    {
        $this->execute();
        reset($this->data);
    }
	
    public function current() 
    {
        return $this->createRow(current($this->data));
    }
	
    public function key() 
    {
        $this->execute();
        return key($this->data);
    }
	
    public function next() 
    {
        next($this->data);
    }
	
    public function valid() 
    {
        $key = key($this->data);
        return ($key !== null);
    }
    
    public function toArray()
    {
        $this->execute();
        return $this->data;
    }
    
    public function jsonSerialize()
    {
	$this->execute();
        return $this->toArray();
    }
    
    public function __toString()
    {
        return $this->getQueryBuilder()->__toString();
    }
}
<commit_msg>Moving execute to before condition test<commit_after><?php

namespace Reduce\Db;
    
use Iterator;
use ArrayAccess;
use Countable;
use JsonSerializable;
use Reduce\Db\Row;
use Reduce\Db\Query\QueryBuilder;
    
class ResultSet implements Iterator, ArrayAccess, Countable, JsonSerializable 
{
    protected $queryBuilder;
    protected $data;
    protected $single = false;
    protected $executed = false;
    protected $tableName;
    
    public function __construct($tableName, QueryBuilder $queryBuilder, $singleResultSet = false) 
    {
        $this->tableName    = $tableName;
        $this->queryBuilder = $queryBuilder;
        $this->single       = $singleResultSet;
        
        $this->queryBuilder->select('*')->from($tableName);
    }
    
    public function __call($name, $args)
    {
        if (method_exists($this->getQueryBuilder(), $name)) {
            call_user_func_array([$this->getQueryBuilder(), $name], $args);
            return $this;
        }
        
        $condition = sprintf('%s.id = %s.%s_id', $this->getTableName(), $name, $this->getTableName());
        
        $this->queryBuilder->leftJoin($this->getTableName(), $name, $name, $condition);
        
        return $this;
    }
    
    public function getTableName()
    {
        return $this->tableName;
    }
    
    public function getQueryBuilder()
    {
        return $this->queryBuilder;
    }
    
    protected function execute()
    {
        if (!$this->executed) {
            $this->data = $this->getQueryBuilder()->execute()->fetchAll();
            $this->executed = true;
        }
    }
    
    protected function createRow($data = [])
    {
        return new Row($this, $data);
    }
    
    public function offsetGet($key)
    {
        if ($this->single) {
            $this->getQueryBuilder()->where('id = ?', $key);
            
            return $this->createRow();
        }
        
        $this->execute();
	    
        if (isset($this->data[$key])) {
            return $this->data[$key];
        }
        
        return [];
    }
    
    public function offsetSet($key, $value)
    {}
    
    public function offsetUnset($key)
    {}
    
    public function offsetExists($key)
    {
        return !! $this->offsetGet($key);
    }
    
    public function count()
    {
        $this->execute();
        return count($this->data);
    }
    
    public function rewind() 
    {
        $this->execute();
        reset($this->data);
    }
	
    public function current() 
    {
        return $this->createRow(current($this->data));
    }
	
    public function key() 
    {
        $this->execute();
        return key($this->data);
    }
	
    public function next() 
    {
        next($this->data);
    }
	
    public function valid() 
    {
        $key = key($this->data);
        return ($key !== null);
    }
    
    public function toArray()
    {
        $this->execute();
        return $this->data;
    }
    
    public function jsonSerialize()
    {
	$this->execute();
        return $this->toArray();
    }
    
    public function __toString()
    {
        return $this->getQueryBuilder()->__toString();
    }
}

{'fraction_non_alphanumeric': 0.14335553441167143, 'fraction_numerical': 0.0, 'mean_word_length': 1.9118190212373039, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 93, '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': '6841397', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1827, 'n_words': 357}