* @package Mammut\DB\Adapter\PostgreSQL */ class Statement extends \Mammut\DB\Statement { private $stmt; public function __construct($database, $query, $limit = -1, $skip = 0) { $this->db = $database; $this->query = $query; $this->limit = $limit; $this->skip = $skip; } /** * executes the statement * * @return MFDBResult the result of the query */ public function execute(array $param = array()) { if(substr_count($this->query, '?') != count($param)) throw new IllegalStateException('wrong parameter count, needs to be ' . substr_count($this->query, '?')); $parts = explode('?', $this->query); $newquery = ''; $i = 1; foreach($param as $p) { $newquery .= current($parts); $newquery .= $this->db->escapeValue($p); next($parts); } $newquery .= current($parts); $this->result = $this->db->query($newquery, $this->limit, $this->skip); $this->executed = true; return true; } /** * * @return array a list of column names */ public function getColumns() { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->result->getColumns(); } /** * * @return MFDBColumnInfo a info object for $column, which could be a string or * integer */ public function getColumnInfo($column) { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->result->getColumnInfo($column); } /** * * @return int the number of fields */ public function getColumnCount() { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->result->getColumnCount(); } public function getRowNumber() { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->rownr; } public function getRowCount() { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->result->getRowCount(); } public function fetchRow() { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->result->fetchRow(); } public function fetchArray() { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->result->fetchArray(); } public function fetchObject($class = false, $param = array()) { if(!$this->executed) throw new IllegalStateException('statement was not executed'); return $this->result->fetchObject($class, $param); } /** * frees all resources */ public function free() { if(is_object($this->result)) $this->result->close(); $this->result = NULL; $this->executed = false; } /** * closes the statement and frees all resources. * after calling * this method, the statement cannot be used */ public function close() { $this->free(); } public function __destruct() { $this->close(); } }