<?php 

error_reporting(1);
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);

class Sql {
    
	private static $conn;

	public function __construct()
	{
        self::getConnection();
	}

	public static function getConnection() {
        
        try {
        
	        if (empty(self::$conn)) {
				$conexao = parse_ini_file(__DIR__.DIRECTORY_SEPARATOR.'Lib/env.ini');
		    	$driver  = $conexao['driver']; 
		    	$host    = $conexao['host'];
		    	$name    = $conexao['name'];
		    	$user    = $conexao['user'];
		    	$pass    = $conexao['pass'];
		    	//$port    = $conexao['port'];

				self::$conn = new PDO("{$driver}:host={$host};port=7999;dbname={$name}",$user,$pass);
				self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			}
			
			return self::$conn;	
        
        }   
		catch (Exception $e) {
        	print $e->getMessage();
        }
	}

	private function setParams($statement, $parameters = array())
	{

		foreach ($parameters as $key => $value) {
			
			$this->bindParam($statement, $key, $value);

		}

	}

	private function bindParam($statement, $key, $value)
	{

		$statement->bindParam($key, $value);

	}

	public function query($rawQuery, $params = array())
	{

		$stmt = self::$conn->prepare($rawQuery);

		$this->setParams($stmt, $params);

		$stmt->execute();

	}

	public function select($rawQuery, $params = array()):array
	{

		$stmt = self::$conn->prepare($rawQuery);

		$this->setParams($stmt, $params);

		$stmt->execute();

		return $stmt->fetchAll(PDO::FETCH_ASSOC);

	}

}

 ?>