<?php
require_once 'web_constant.php';
class DB {
	private $conn;
	private $host;
	private $user;
	private $password;
	private $baseName;
	private $port;
	private $debug;
	private $status_fatal;
 
	function __construct($params=array()) {
		$this->conn = false;
		
		$this->host = web_constant::HOST;
		$this->user = web_constant::USER;		
		$this->password = web_constant::PASSWORD;
		$this->baseName = web_constant::DATABASE;
		$this->port = web_constant::PORT;
		
		$this->debug = true;
		$this->connect();
	}
 
	function __destruct() {
		$this->disconnect();
	}
	
	function connect() {
		if (!$this->conn) {
			try {
				//$this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->baseName.'', $this->user, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));  
				$this->conn = new PDO('mysql:host='.$this->host.';port='.$this->port.';dbname='.$this->baseName.'', $this->user, $this->password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
			}
			catch (Exception $e) {
				die('Erreur : ' . $e->getMessage());
			}
 
			if (!$this->conn) {
				$this->status_fatal = true;
				echo 'Connection BDD failed';
				die();
			} 
			else {
				$this->status_fatal = false;
			}
		}
 
		return $this->conn;
	}
 
	function disconnect() {
		if ($this->conn) {
			$this->conn = null;
		}
	}
	
	// TRANSACTION SUPPORT
	function beginTransaction() {
		return $this->conn->beginTransaction();
	}

	function commit() {
		return $this->conn->commit();
	}

	function rollBack() {
		return $this->conn->rollBack();
	}


	function getOne($query,$values) {
		$result = $this->conn->prepare($query);
		$ret = $result->execute($values);
		if (!$ret) {
		   echo 'PDO::errorInfo():';
		   echo '<br />';
		   echo 'error SQL: '.$query;
		   die();
		}
		$result->setFetchMode(PDO::FETCH_ASSOC);
		$reponse = $result->fetch();
		
		return $reponse;
	}
	
	function getAll($query,$values) {
		$result = $this->conn->prepare($query);
		$ret = $result->execute($values);
		if (!$ret) {
		   echo 'PDO::errorInfo():';
		   echo '<br />';
		   echo 'error SQL: '.$query;
		   die();
		}
		$result->setFetchMode(PDO::FETCH_ASSOC);
		$reponse = $result->fetchAll();
		
		return $reponse;
	}
	
	function execute($query,$values) {
		$result = $this->conn->prepare($query);
		$ret = $result->execute($values);
		if (!$ret) {
		   echo 'PDO::errorInfo():';
		   echo '<br />';
		   echo 'error SQL: '.$query;
		   //print_r($result->errorInfo());
		   die();
		}
		return $ret;
	}
	
	function execute1($query) {
		$result = $this->conn->prepare($query);
		$ret = $result->execute();
		if (!$ret) {
			//print_r($result->errorInfo());
		   echo 'PDO::errorInfo():';
		   echo '<br />';
		   echo 'error SQL: '.$query;
		   die();
		}
		return $ret;
	}
	
	function num_rows($query,$values) {
		$result = $this->conn->prepare($query);
		$ret = $result->execute($values);
		if (!$ret) {
		   echo 'PDO::errorInfo():';
		   echo '<br />';
		   echo 'error SQL: '.$query;
		   die();
		}
		$response=$result->rowCount();
		return $response;
	}
	
	function lastId($query,$values) {
		$result = $this->conn->prepare($query);
		$ret = $result->execute($values);
		if (!$ret) {
		   echo 'PDO::errorInfo():';
		   echo '<br />';
		   echo 'error SQL: '.$query;
		   //print_r($result->errorInfo());
		   die();
		}
		$last_id = $this->conn->lastInsertId();
		return $last_id;
	}
}