<?php
namespace SabaiApps\Framework\DB;

/**
 * MySQLiRowset class for managing rowsets returned by MySQLi queries.
 * Provides methods for fetching rows and navigating the rowset.
 */
class MySQLRowset extends AbstractRowset {

	/**
	 * @var resource MySQL result resource.
	 */
	protected $_rs;

	/**
	 * Constructor.
	 *
	 * @param resource $rs MySQL result resource.
	 */
	public function __construct( $rs ) {
		$this->_rs = $rs;
	}

	/**
	 * Fetches a single column value from the current row.
	 *
	 * @param int $index Column index (default is 0).
	 * @return mixed Column value or false on failure.
	 */
	public function fetchColumn( $index = 0 ) {
		return ( $row = mysqli_fetch_row( $this->_rs ) ) ? $row[ $index ] : false;
	}

	/**
	 * Fetches the current row as a numeric array.
	 *
	 * @return array|false Row data or false on failure.
	 */
	public function fetchRow() {
		return mysqli_fetch_row( $this->_rs );
	}

	/**
	 * Fetches the current row as an associative array.
	 *
	 * @return array|false Row data or false on failure.
	 */
	public function fetchAssoc() {
		return mysqli_fetch_assoc( $this->_rs );
	}

	/**
	 * Moves the internal pointer to the specified row offset.
	 *
	 * @param int $offset Row offset.
	 * @return bool True on success, false on failure.
	 */
	public function seek( $offset = 0 ) {
		return false !== mysqli_data_seek( $this->_rs, $offset );
	}

	/**
	 * Gets the number of rows in the rowset.
	 *
	 * @return int Number of rows.
	 */
	public function rowCount() {
		return mysqli_num_rows( $this->_rs );
	}
}
