<commit_before><?php

/**
 * Haefko - your php5 framework
 *
 * @author      <NAME>
 * @copyright   Copyright (c) 2007 - 2009, <NAME>
 * @link        http://haefko.programujte.com
 * @license     http://www.opensource.org/licenses/mit-license.html
 * @version     0.8 - $Id$
 * @package     Haefko_Database
 */


require_once dirname(__FILE__) . '/result-node.php';


class DbResult extends Object implements Countable, IteratorAggregate
{


	/** @var Paginator */
	public $paginator;

	/** @var string */
	private $query;

	/** @var DbDriver */
	private $driver;

	/** @var bool */
	private $executed = false;

	/** @var array */
	private $cols = array();

	/** @var bool */
	private $tables = false;

	/** @var array Stored rows */
	private $rows = array();

	/** @var null|array Stored row */
	private $stored = array();

	/** @var array */
	private $association = array();

	/** @var boll|array */
	private $pagination = false;


	/**
	 * Constructor
	 * @param   string     sql query
	 * @param   DbDriver   driver instance
	 * @return  void
	 */
	public function __construct($query, DbDriver $driver)
	{
		$this->query = $query;
		$this->driver = $driver;
	}


	/**
	 * Executes sql query
	 * @return DbResult    $this
	 */
	public function execute()
	{
		# pagination
		if ($this->pagination !== false) {
			if (empty($this->pagination[2]))
				$this->pagination[2] = db::fetchField(preg_replace('#select (.+) from#si', 'SELECT COUNT(*) FROM', $this->query));

			$this->query .= ' LIMIT ' . ($this->pagination[0] - 1) * $this->pagination[1] . ', ' . $this->pagination[1];
		}


		# run query
		$time = microtime(true);
		$this->driver = $this->driver->query($this->query);
		$this->executed = true;
		Db::debug($this->query, $time);

		# pagination
		if ($this->pagination !== false) {
			require_once dirname(__FILE__) . '/../paginator.php';
			$this->paginator = new Paginator($this->pagination[0], ceil($this->pagination[2] / $this->pagination[1]));
		}


		# columns & tables
		$this->cols = $this->driver->getResultColomns();
		
		$tables = array();
		foreach ($this->cols as $col) {
			if (!empty($col[0]))
				$tables[$col[0]] = true;
		}

		$this->tables = count($tables) > 1;


		return $this;
	}


	/**
	 * Sets association
	 * @param   string     main table
	 * @param   array      other tables in relation hasMany
	 * @return  DbResult   $this
	 */
	public function associate($main, $hasMany = array())
	{
		$this->association[0] = $main;
		$this->association[1] = (array) $hasMany;

		return $this;
	}


	/**
	 * Sets pagination
	 * @param   int        page
	 * @param   int        limit
	 * @param   int        count pages
	 * @throws  Exception
	 * @return  DbResult   $this
	 */
	public function paginate($page, $limit = 10, $count = null)
	{
		if ($this->executed)
			throw new Exception("You can't paginate excecuted query.");

		if ($page < 1)
			$page = 1;

		$this->pagination[0] = $page;
		$this->pagination[1] = (int) $limit;
		if (!empty($count))
			$this->pagination[2] = (int) $count;

		return $this;
	}


	/**
	 * Returns first field value
	 * @return  mixed
	 */
	public function fetchField()
	{
		$this->checkExecution();

		if (empty($this->rows))
			$this->rows = array($this->fetch());

		if (!is_array($this->rows[0]))
			return null;<commit_msg>* fix issue #19<commit_after><?php

/**
 * Haefko - your php5 framework
 *
 * @author      <NAME>
 * @copyright   Copyright (c) 2007 - 2009, <NAME>
 * @link        http://haefko.programujte.com
 * @license     http://www.opensource.org/licenses/mit-license.html
 * @version     0.8 - $Id$
 * @package     Haefko_Database
 */


require_once dirname(__FILE__) . '/result-node.php';


class DbResult extends Object implements Countable, IteratorAggregate
{


	/** @var Paginator */
	public $paginator;

	/** @var string */
	private $query;

	/** @var DbDriver */
	private $driver;

	/** @var bool */
	private $executed = false;

	/** @var array */
	private $cols = array();

	/** @var bool */
	private $tables = false;

	/** @var array Stored rows */
	private $rows = array();

	/** @var null|array Stored row */
	private $stored = array();

	/** @var array */
	private $association = array();

	/** @var boll|array */
	private $pagination = false;


	/**
	 * Constructor
	 * @param   string     sql query
	 * @param   DbDriver   driver instance
	 * @return  void
	 */
	public function __construct($query, DbDriver $driver)
	{
		$this->query = $query;
		$this->driver = $driver;
	}


	/**
	 * Executes sql query
	 * @return DbResult    $this
	 */
	public function execute()
	{
		# pagination
		if ($this->pagination !== false) {
			if (empty($this->pagination[2]))
				$this->pagination[2] = db::fetchField(preg_replace('#select (.+) from#si', 'SELECT COUNT(*) FROM', $this->query));

			$this->query .= ' LIMIT ' . ($this->pagination[0] - 1) * $this->pagination[1] . ', ' . $this->pagination[1];
		}


		# run query
		$time = microtime(true);
		$this->driver = $this->driver->query($this->query);
		$this->executed = true;
		Db::debug($this->query, $time);

		# pagination
		if ($this->pagination !== false) {
			require_once dirname(__FILE__) . '/../paginator.php';
			$this->paginator = new Paginator($this->pagination[0], ceil($this->pagination[2] / $this->pagination[1]));
		}


		# columns & tables
		$this->cols = $this->driver->getResultColomns();
		
		$tables = array();
		foreach ($this->cols as $col) {
			if (!empty($col[0]))
				$tables[$col[0]] = true;
		}

		$this->tables = count($tables) > 1;


		return $this;
	}


	/**
	 * Sets association
	 * @param   string     main table
	 * @param   array      other tables in relation hasMany
	 * @return  DbResult   $this
	 */
	public function associate($main, $hasMany = array())
	{
		$this->association[0] = $main;
		$this->association[1] = (array) $hasMany;

		return $this;
	}


	/**
	 * Sets pagination
	 * @param   int        page
	 * @param   int        limit
	 * @param   int        count pages
	 * @throws  Exception
	 * @return  DbResult   $this
	 */
	public function paginate($page, $limit = 10, $count = null)
	{
		if ($this->executed)
			throw new Exception("You can't paginate excecuted query.");

		if ($page < 1)
			$page = 1;

		$this->pagination[0] = $page;
		$this->pagination[1] = (int) $limit;
		if (!empty($count))
			$this->pagination[2] = (int) $count;

		return $this;
	}


	/**
	 * Returns first field value
	 * @return  mixed
	 */
	public function fetchField()
	{
		$this->checkExecution();

		if (empty($this->rows))
			$this->rows = array($this->fetch());

		if (empty($this->rows[0]))
			return null;
{'fraction_non_alphanumeric': 0.18208092485549132, 'fraction_numerical': 0.010343778521448129, 'mean_word_length': 2.970410628019324, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5492378', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2217, 'n_words': 538}