<?php
// ModelBase contains all of the base functions necessary for
//   each model class to execute queries as easily as
//   possible
class ModelBase {    
    private function __construct() {
    }        
}


class MyPDO extends PDO{
  
    const DB_HOST=DATABSE_HOST;
    const DB_PORT=DATABSE_PORT;
    const DB_NAME=DATABSE_NAME;
    const DB_USER=DATABSE_USER;
    const DB_PASS=DATABSE_PASSWROD;

    public function __construct($options=null){
		    try {
				parent::__construct('mysql:host='.MyPDO::DB_HOST.';port='.MyPDO::DB_PORT.';dbname='.MyPDO::DB_NAME,
				MyPDO::DB_USER,
				MyPDO::DB_PASS,$options);
				$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			}
			catch(PDOException $e){
				Logger::newMessage($e);
				logger::customErrorMsg();
			}
    }
	
	
	public function query($query){
		$args = func_get_args();
        array_shift($args); //first element is not an argument but the query itself, should removed

        $reponse = parent::prepare($query);
        $reponse->execute($args);
		
		$result = array();
		$i = 0;
		while ($row = $reponse->fetch(PDO::FETCH_OBJ)) {
			$result[$i] = $row;
			$i++;
		}
						
        return $result;
	}
	
	public function queryFirst($sql){ //secured query with prepare and execute       		
 	    $result = $this->query($sql);        
        return $result[0];
    }
	
}

class MyPDOStatement extends PDOStatement {

  public function execute($input_parameters = null) {
    if (is_array($input_parameters)) {
      $i = 1;
      foreach ($input_parameters as $p) {
        // default to string datatype
        $parameterType = PDO::PARAM_STR;
        // now let's see if there is something more appropriate
        if (is_bool($p)) {
          $parameterType = PDO::PARAM_BOOL;
        } elseif (is_null($p)) {
          $parameterType = PDO::PARAM_NULL;
        } elseif (is_int($p)) {
          $parameterType = PDO::PARAM_INT;
        }
        // parameters passed to execute() are input-only parameters, so use
        // bindValue()
        $this->bindValue($i, $p, $parameterType);
        $i++;
      }
    }
    return parent::execute();
  }

}
?>