<?php
declare(strict_types=1);


require_once("config.inc.php");

class DBConnector
{

	private $server = '';
	private $userName = '';
	private $password = '';
	private $database = '';

	private $con = null;

	// Object construction with database connectivity - Default Constructor
	function __construct()
	{
		$this->server = DB_SERVER;
		$this->userName = DB_USER_NAME;
		$this->password = DB_PASSWORD;
		$this->database = DB_DATABSE;
	}

	/*
	 * DBConnector::OpenConnection()
	 * 
	 * Open mysql database connection
	 * 
	 * @access public
	 * 
	 * @return void
	 */
	public function OpenConnection()
	{
		if ($this->con == null) {
			$dsn = "mysql:host=" . $this->server . ";dbname=" . $this->database . ";charset=utf8mb4";
			$options = [
				PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
				PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
				PDO::ATTR_EMULATE_PREPARES => false,
				PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci, FILE_USAGE 'None', SQL_MODE='STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"
			];

			try {
				$this->con = new PDO($dsn, $this->userName, $this->password, $options);
			} catch (PDOException $e) {
				die('Connection failed: ' . $e->getMessage());
			}
		}
	}

	/*
	 * DBConnector::RunQuery()
	 * 
	 * Use to execute select statements
	 *
	 * @param String $SQL : The SQL statement to be executed
	 * 
	 * @access public
	 * 
	 * @return PDOStatement
	 */
	public function RunQuery($SQL, $params = [])
	{
		$this->OpenConnection();
		try {
			$stmt = $this->con->prepare($SQL);
			$stmt->execute($params);
			return $stmt;
		} catch (PDOException $e) {
			// Log error or rethrow
			die("Query Failed: " . $e->getMessage() . " SQL: " . $SQL);
		}
	}

	/*
	 * DBConnector::ExecuteQuery()
	 * 
	 * Use to execute insert, update and delete statements
	 *
	 * @param String $SQL : The SQL statement to be executed
	 * 
	 * @access public
	 * 
	 * @return boolean
	 */
	public function ExecuteQuery($SQL, $params = [])
	{
		$this->OpenConnection();
		try {
			$stmt = $this->con->prepare($SQL);
			return $stmt->execute($params);
		} catch (PDOException $e) {
			die("Query Failed: " . $e->getMessage() . " SQL: " . $SQL);
			return false;
		}
	}

	/*
	 * DBConnector::CheckRecordAvailability()
	 * 
	 * Checks whtether records available or not for the given SQL statement
	 *
	 * @param String $SQL : The SQL statement to be executed
	 * 
	 * @access public
	 * 
	 * @return boolean
	 */
	public function CheckRecordAvailability($SQL, $params = [])
	{
		$this->OpenConnection();
		try {
			$stmt = $this->con->prepare($SQL);
			$stmt->execute($params);
			return $stmt->rowCount() > 0;
		} catch (PDOException $e) {
			die("Query Failed: " . $e->getMessage());
		}
	}

	/*
	 * DBConnector::AutoIncrementExecuteQuery()
	 * 
	 * Get generated auto increment key/ID, after the execution of given SQL statement
	 *
	 * @param String $SQL : The SQL statement to be executed
	 * 
	 * @access public
	 * 
	 * @return Integer
	 */
	public function AutoIncrementExecuteQuery($SQL, $params = [])
	{
		$this->OpenConnection();
		try {
			$stmt = $this->con->prepare($SQL);
			$stmt->execute($params);
			return $this->con->lastInsertId();
		} catch (PDOException $e) {
			die("Query Failed: " . $e->getMessage());
		}
	}

	/*
	 * DBConnector::CloseConnection()
	 * 
	 * Close MySQL Connection
	 * 
	 * @access public
	 * 
	 * @return void
	 */
	public function CloseConnection()
	{
		$this->con = null;
	}

	public function GetConnection()
	{
		$this->OpenConnection();
		return $this->con;
	}
}

?>