<?php

/**
 * Classe d'accès à une Base de données Mysql
 * @author heron
 * @version 1.0.0.2
 *
 */
class Database {
	private $serverName;
	private $port;
	private $dbName;
	private $user;
	private $password;
	private $pdoObject;

	/**
	 * Constructeur
	 * @param string $dbName
	 * @param string $serverName
	 * @param string $port
	 * @param string $user
	 * @param string $password
	 */
	public function Database($dbName, $serverName = "localhost", $port = "3306",
			$user = "root", $password = "") {
		$this->dbName = $dbName;
		$this->serverName = $serverName;
		$this->port = $port;
		$this->user = $user;
		$this->password = $password;
	}

	public function connect() {
		try {
			$this->pdoObject = new PDO(
					'mysql:host=' . $this->serverName . ';dbname='
							. $this->dbName . ';port:' . $this->port,
					$this->user, $this->password);
			$this->pdoObject->exec("SET CHARACTER SET utf8");
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}

	/**
	 * Exécute l'instruction SQL passée en paramètre et retourne un statement
	 * @param string $sql
	 * @return PDOStatement
	 */
	public function query($sql) {
		return $this->pdoObject->query($sql);
	}
	/*
	 * Exécute une instruction sql de mise à jour (INSERT, UPDATE ou DELETE)
	 */
	public function execute($sql) {
		return $this->pdoObject->exec($sql);
	}

	public function getServerName() {
		return $this->serverName;
	}

	public function setServerName($serverName) {
		$this->serverName = $serverName;
	}
	
	public function prepareStatement($sql){
		return $this->pdoObject->prepare($sql);
	}
	
	public function bindValueFromStatement(PDOStatement $statement,$parameter,$value){
		return $statement->bindValue(":".$parameter, $value);
	}
	public function lastInserId(){
		return $this->pdoObject->lastInsertId();
	}

}
