Location: PHPKode > projects > PWF > PWF_0.3.0/Data/Db.php
<?php

/**
 * _Data_Db is the database object of PWF. It inherits from PDO 
 * @copyright	Copyright (c) 2012, PWF
 * @license		http://phpwebframework.com/License 
 * @version PWF_0.3.0
 */
class _Data_Db extends PDO
{
	/**
	 * The PDO statements that have been used. 
	 * Keys of the array are the text of the statement. 
	 * That way PDO statements instantiates only once. 
	 * @var array of PDOStatement
	 */
	private $statements = array();

	/**
	 * The text of the last used statement (for debugging reasons) 
	 * @var string 
	 */
	private $lastUsedStatement; 
	
	/**
	 * The values of the last used statement (for debugging reasons) 
	 * @var array
	 */
	private $lastUsedValues = array(); 
	
	
	/**
	 * It takes a sql statement and replacement values for question marks placeholders (if there are any) executes the query by a prepared statement and 
	 * returns the result (if any) as an multidimentional array with the first key the index num in the row of results, the second the name of result column. 
	 * @param $statement The query statement 
	 * @param none,string,array $values The replacement values as array for question marks placeholders if there are any. If there is only one then this can be a string  
	 * @throws _Exception_Data
	 * @return null,array Returns the result (if any) as an multidimentional array with the first key the index num in the row of results, the second the name of result column and the value.  
	 */
	public function request($statement,$values = null)
	{

		$result=array();

		$statement = trim($statement);
			
		if(is_null($values))
		{
			$values = array(); 
		}
		if(!is_array($values))
		{
			$values = array($values);
		}
    $this->lastUsedValues = $values; 
    
		if(substr_count($statement,"?") != count($values))
		{
			throw new _Exception_Data(_Exception_Messages::$preparedStatementMismatch.$statement, 4561);
		}

		if(!isset($this->statements[$statement]))
		{
			try
			{
				$this->statements[$statement] = $this->prepare($statement);
			}
			catch(PDOException $e)
			{
        throw new _Exception_Data($e->getMessage(),$e->getCode());
			}
		}

		try
		{
			$this->lastUsedStatement = $statement;
			$this->statements[$statement]->execute($values);
			if($this->statements[$statement]->errorCode()!="00000")
			{
				$error = $this->statements[$statement]->errorInfo();
				throw new _Exception_Data($error[2],(int)$error[0]);
			}
		}
		catch(PDOException $e)
		{
			throw new _Exception_Data($e->getMessage(),$e->getCode());
		}
    
		$result = $this->statements[$statement]->fetchAll(PDO::FETCH_ASSOC);

		return $result;
	}
	
	/**
	 * Returns array of informations about the last executed statement (the text of the statement and the values for 
	 * question marks placeholders if any) 
	 * @return array
	 */
	public function debugLastStatement()
	{
		return  array_merge((array)$this->statements[$this->lastUsedStatement]->debugDumpParams(),$this->lastUsedValues);
	}
}
?>
Return current item: PWF