Classes

File io/db/driver/pdo/PDOSelectQueryResult.class.php

File io/db/driver/pdo/PDOSelectQueryResult.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 
<?php
/**
 * @package     IO
 * @subpackage  DB\driver\pdo
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2014 12 22
 * @since       PHPBoost 3.0 - 2009 11 01
*/

class PDOSelectQueryResult extends AbstractSelectQueryResult
{
    /**
     * @var PDOStatement
     */
    private $statement = null;

    /**
     * @var int
     */
    private $fetch_mode = null;

    /**
     * @var ArrayIterator
     */
    private $iterator = null;

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

    public function __construct($query, array $parameters, PDOStatement $statement, $fetch_mode = self::FETCH_ASSOC)
    {
        $this->statement = $statement;
        $this->fetch_mode = $fetch_mode;
        parent::__construct($query, $parameters);
    }

    public function __destruct()
    {
        $this->dispose();
    }

    public function set_fetch_mode($fetch_mode)
    {
        $this->fetch_mode = $fetch_mode;
    }

    public function get_rows_count()
    {
        return $this->statement->rowCount();
    }

    public function rewind()
    {
        if ($this->iterator === null)
        {
            $pdo_fetch_mode = PDO::FETCH_ASSOC;
            switch ($this->fetch_mode)
            {
                case self::FETCH_NUM:
                    $pdo_fetch_mode = PDO::FETCH_NUM;
                    break;
                case self::FETCH_ASSOC:
                default:
                    $pdo_fetch_mode = PDO::FETCH_ASSOC;
                    break;
            }
            $this->iterator = new ArrayIterator($this->statement->fetchAll($pdo_fetch_mode));
        }
        $this->iterator->rewind();
    }

    public function valid()
    {
        if ($this->iterator === null)
        {
            $this->rewind();
        }
        return  $this->iterator->valid();
    }

    public function current()
    {
        return $this->iterator->current();
    }

    public function key()
    {
        return $this->iterator->key();
    }

    public function next()
    {
        $this->iterator->next();
    }

    public function dispose()
    {
        if (!$this->is_disposed)
        {
            $this->statement->closeCursor();
            $this->is_disposed = true;
        }
    }

    protected function needs_rewind()
    {
        return $this->iterator === null;
    }
}
?>