<?php
namespace nur\m\pgsql;

use nur\func;
use nur\m\base\AbstractRowIterator;
use nur\m\IRowIncarnation;

class PgsqlRowIterator extends AbstractRowIterator {
  /** @var PgsqlConn */
  protected $conn;

  /** @var string|null */
  protected $sql;

  /** @var IRowIncarnation|null */
  protected $incarnation;

  /**
   * @var resource|null statement permettant de récupérer les lignes résultat
   */
  protected $stmt;

  /**
   * @var callable|null fonction permettant de (re)créer $stmt. par défaut,
   * $stmt est créé à partir de $sql et $bindings
   */
  protected $createStmtFunc;

  function __construct(PgsqlConn $conn, ?string $sql=null, ?array &$bindings=null, ?IRowIncarnation $incarnation=null) {
    parent::__construct();
    $this->conn = $conn;
    $this->reset($sql, $bindings, $incarnation);
  }

  function reset(?string $sql, ?array &$bindings=null, ?IRowIncarnation $incarnation=null): self {
    if ($bindings === null) $bindings = [];
    $this->sql = $sql;
    $this->data =& $bindings;
    $this->incarnation = $incarnation;
    return $this;
  }

  function initStmt($stmt=null, ?callable $createStmtFunc=null): self {
    $this->stmt = $stmt;
    $this->createStmtFunc = $createStmtFunc;
    return $this;
  }

  /** créer une instance de statement */
  protected function createStmt() {
    $createStmtFunc = $this->createStmtFunc;
    if ($createStmtFunc !== null) {
      return func::call($createStmtFunc, $this->conn);
    } else {
      ["stmt" => $stmt
      ] = $this->conn->_execute($this->sql, $this->data, ["stmt" => true]);
      return $stmt;
    }
  }

  protected function _setup(): void {
    if ($this->stmt === null) {
      $incarnation = $this->incarnation;
      if ($incarnation !== null) {
        $incarnation->prepareBindings($this->data);
      }
      $this->stmt = $this->createStmt();
    }
  }

  protected function _next() {
    return pg_fetch_assoc($this->stmt);
  }

  protected function _cook(&$row, &$key): void {
    if ($this->incarnation !== null) {
      $this->incarnation->loadRow($row, $key);
    }
  }

  protected function _teardown(): void {
    if ($this->stmt !== null) {
      pg_free_result($this->stmt);
      $this->stmt = null;
    }
  }

  function key() { return $this->_key(); }
  function current() { return $this->_current(); }
}
