<?php

/**
 * class databaseStatement
 *
 * Description for class databaseStatement
 *
 * @author:
*/
namespace core;

use PDO;

class databaseStatement extends \PDOStatement {
	protected $pdo;
	protected $boundParams = array();
	protected $boundShortParams = array();

	protected function __construct($pdo) {
		$this->pdo = $pdo;
		
		$this->boundParams = array();
		$this->boundShortParams = array();
	}

	public function bindParam($param, &$value, $datatype = PDO::PARAM_STR, $length = 0, $driverOptions = false) {
		$this->boundParams[$param] = array("value" => &$value, "datatype" => $datatype);
		$this->boundShortParams[$param] = &$value;
		
		return parent::bindParam($param, $value, $datatype, $length, $driverOptions);
	}

	public function execute($args = null) {
		$success = parent::execute($args); 
		
		if($args != null) {
			$this->boundShortParams = $args;
		}
		
		return $success;
	}

	public function __debug($exit = true) {
		$string = (string)$this->pdo->query;
		$data = $this->boundShortParams;
		
		$indexed = $data == array_values($data);
		foreach($data as $k => $v) {
			if(is_string($v)) {
				$v = "'$v'";
			}
			
			if($indexed) {
				$string=preg_replace('/\?/', $v, $string, 1);
			} else {
				$string = str_replace($k, $v, $string);
			}
		}
		
		__debug($string, $exit);
	}

	public function fetch($fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) {
		return parent::fetch($fetch_style, $cursor_orientation, $cursor_offset);
	}
}
?>