<?php

	if (boolval($_ENV[$_ENV["ENVIRONMENT"]]["MULTI_CONNECTIONS"]) == true) {
		class Connection{
			private $instance;

			public function __construct($key = 0){
				try{
					if (DB_CONNECTION_DATA["driver"][$key] == "MYSQL") {
						$this->instance = new PDO(
							"mysql:host=".DB_CONNECTION_DATA["host"][$key].";dbname=".DB_CONNECTION_DATA["database"][$key], 
							DB_CONNECTION_DATA["username"][$key], 
							DB_CONNECTION_DATA["password"][$key], 
							[
								PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
							]
						);
					}
					elseif (DB_CONNECTION_DATA["driver"][$key] == "POSTGRESQL") {
						$this->instance = new PDO(
							"pgsql:host=".DB_CONNECTION_DATA["host"][$key].";dbname=".DB_CONNECTION_DATA["database"][$key], 
							DB_CONNECTION_DATA["username"][$key], 
							DB_CONNECTION_DATA["password"][$key].";charset=utf8"
						);
					}
					elseif (DB_CONNECTION_DATA["driver"][$key] == "SQLSERVER") {
						$this->instance = new PDO(
							"sqlsrv:Server=".DB_CONNECTION_DATA["host"][$key].";Database=".DB_CONNECTION_DATA["database"][$key], 
							DB_CONNECTION_DATA["username"][$key], 
							DB_CONNECTION_DATA["password"][$key].";charset=utf8"
						);
					}
					elseif (DB_CONNECTION_DATA["driver"][$key] == "SQLITE") {
						$this->instance = new PDO("sqlite:host=".DB_CONNECTION_DATA["host"][$key]);
					}
					elseif (DB_CONNECTION_DATA["driver"][$key] == "ORACLE") {
						$tns = "(
									DESCRIPTION = (
										ADDRESS_LIST = (
											ADDRESS = (PROTOCOL = TCP) 
											(HOST = ".DB_CONNECTION_DATA["host"][$key].") 
											(PORT = ".DB_CONNECTION_DATA["port"][$key].")
										)
									)
									(CONNECT_DATA = (SID = ".DB_CONNECTION_DATA["sid"][$key]."))
								)";

						$this->instance = new PDO(
							"oci:dbname=".$tns.";charset=utf8", 
							DB_CONNECTION_DATA["username"][$key], 
							DB_CONNECTION_DATA["password"][$key]
						);
					}
					else{
						throw new Exception("ERROR: Database type not defined.", 500);
					}

					$this->instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
					$this->instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
				}
				catch (PDOException $e) {
					echo $e->getMessage();
				}
			}

			public function prepare($sql){
				return $this->instance->prepare($sql);
			}

			public function exec($sql){
				return $this->instance->exec($sql);
			}

			public function query($sql){
				return $this->instance->query($sql);
			}

			public function lastInsertId(){
				return $this->instance->lastInsertId();
			}

			public function quote($str){
				return $this->instance->quote($str);
			}

			public function beginTransaction(){
				return $this->instance->beginTransaction();
			}

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

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

		}
	}
	else{
		class Connection{
			private static $instance;

			public static function getInstance(){
				if (!isset(self::$instance)) { 
					try{
						if (DB_CONNECTION_DATA["driver"] == "MYSQL") {
							self::$instance = new PDO(
								"mysql:host=".DB_CONNECTION_DATA["host"].";dbname=".DB_CONNECTION_DATA["database"], 
								DB_CONNECTION_DATA["username"], 
								DB_CONNECTION_DATA["password"], 
								[
									PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
								]
							);
						}
						elseif (DB_CONNECTION_DATA["driver"] == "POSTGRESQL") {
							self::$instance = new PDO(
								"pgsql:host=".DB_CONNECTION_DATA["host"].";dbname=".DB_CONNECTION_DATA["database"], 
								DB_CONNECTION_DATA["username"], 
								DB_CONNECTION_DATA["password"].";charset=UTF-8"
							);
						}
						elseif (DB_CONNECTION_DATA["driver"] == "SQLSERVER") {
							self::$instance = new PDO(
								"sqlsrv:Server=".DB_CONNECTION_DATA["host"].";Database=".DB_CONNECTION_DATA["database"], 
								DB_CONNECTION_DATA["username"], 
								DB_CONNECTION_DATA["password"].";charset=UTF-8"
							);
						}
						elseif (DB_CONNECTION_DATA["driver"] == "SQLITE") {
							self::$instance = new PDO("sqlite:host=".DB_CONNECTION_DATA["host"]);
						}
						elseif (DB_CONNECTION_DATA["driver"] == "ORACLE") {
							$tns = "(
										DESCRIPTION = (
											ADDRESS_LIST = (
												ADDRESS = (PROTOCOL = TCP) 
												(HOST = ".DB_CONNECTION_DATA["host"].") 
												(PORT = ".DB_CONNECTION_DATA["port"].")
											)
										)
										(CONNECT_DATA = (SID = ".DB_CONNECTION_DATA["sid"]."))
									)";

							self::$instance = new PDO(
								"oci:dbname=".$tns.";charset=utf8", 
								DB_CONNECTION_DATA["username"], 
								DB_CONNECTION_DATA["password"]
							);
						}
						else{
							throw new Exception("ERROR: Database type not defined.", 500);
						}

						self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
						self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
					}
					catch (PDOException $e) {
						echo $e->getMessage();
					}
				}
				
				return self::$instance;
			}

			public static function prepare($sql){
				return self::getInstance()->prepare($sql);
			}

			public static function exec($sql){
				return self::getInstance()->exec($sql);
			}

			public static function query($sql){
				return self::getInstance()->query($sql);
			}

			public static function lastInsertId(){
				return self::getInstance()->lastInsertId();
			}

			public static function quote($str){
				return self::getInstance()->quote($str);
			}

			public static function beginTransaction(){
				return self::getInstance()->beginTransaction();
			}

			public static function rollBack(){
				return self::getInstance()->rollBack();
			}

			public static function commit(){
				return self::getInstance()->commit();
			}

		}
	}

?>