<?php
	// namespace
	namespace sven\sys\mysql;

	// required library

	$dir = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, dirname(__FILE__)."/../sources/envms-fluentpdo/src/");

	if(file_exists($dir)) {
		//prebuilt directory files to serve classes faster
		$files = [
	$dir.'Query.php', $dir.'Literal.php', $dir.'Structure.php', $dir.'Utilities.php', $dir.'Queries\Base.php', $dir.'Queries\Common.php', $dir.'Queries\Delete.php', $dir.'Queries\Insert.php', $dir.'Queries\Select.php', $dir.'Queries\Update.php'
		];
		foreach ($files as $file) {
			if(file_exists($file))
				require_once($file);
			else
				return \sven\sys\core::addException(new \sven\sys\Exception("ERROR: Files not found", "SQL Query Builder was not found ({$file})", 404));
		}
	}
	else return \sven\sys\core::addException(new \sven\sys\Exception("ERROR: Files not found", "SQL Query Builder was not found", 404));
	unset($dir);

	/**
	 * mysql
	 *
	 * mysql.class uses PDO to connect to our database and handle requests
	 * and a SQL Query Builder to get some handsome code
	 *
	 * Example usage:
	 * $db = new \sven\sys\mysql();
	 * $output = $db->select('*', 'users', 'ID=4');
	 * var_dump($output);
	 *
	 * @package sven\sys\mysql
	 * @copyright 2018 Ruben Meyer
	 * @author Ruben Meyer <contact@rxbn.de>
	 * @version 0.1.0
	 * @see https://github.com/envms/fluentpdo		 	<query builder>
	 * @see http://php.net/manual/de/class.pdo.php 		<PDO>
	 *
	 */
	class mysql {

		/**
		 * instance
		 *
		 * Instance
		 *
		 * @access private
		 * @static
		 * @var resource (new self();)
		 */
		private static $instance = null;

		/**
		 * connection
		 *
		 * PDO Connection
		 *
		 * @access private
		 * @static
		 * @var object|null Should contain the database connection
		 */
		private static $connection = null;

		/**
		 * __construct
		 *
		 * Class constructor
		 *
		 * @access public
		 * @return void
		 */
		public function __construct() {
			self::$instance = $this;
			$this->connect();
		}

		/**
		 * __destruct
		 *
		 * Class destructor
		 *
		 * @access public
		 * @return void
		 */
		public function __destruct() { $this->disconnect(); }

		/**
		 * getInstance
		 *
		 * returns Instance
		 *
		 * @access public
		 * @return object
		 */
		public static function getInstance() {
			return self::$instance;
		}

		/**
		 * getBuilder
		 *
		 * returns SQL Builder to query
		 *
		 * @access public
		 * @return \Envms\FluentPDO\Query|\sven\sys\sven\uncallable
		 */
		public function getBuilder() {
			if(self::$connection !== null) return new \Envms\FluentPDO\Query(self::$connection);
			else {
				\sven\sys\core::addException(new \sven\sys\Exception("Exception", "Error: MySQL \$connection is NULL", 2002));
				return new \sven\sys\sven\uncallable;
			}
		}

		/**
		 * connect
		 *
		 * connects to our database
		 *
		 * @access private
		 * @return void
		 */
		private function connect() {
			if(self::$connection) return;
			if(php_uname('r') === "6.1") {
			//if(false) {
				// development place @ school
				$data = (object) [
					'host' => '102-012',
					'user' => 'intabi19',
					'pass' => 'hallo',
					'db' => 'sven'
				];
			} else {
				// development place @ home
				$data = (object) [
					'host' => '127.0.0.1',
					'user' => 'root',
					'pass' => '',
					'db' => 'sven'
				];
			}
			try {
				if(mysqli_connect($data->host, $data->user, $data->pass)) {
					self::$connection = new \PDO('mysql:host='.$data->host.';dbname='.$data->db.';charset=utf8mb4', $data->user, $data->pass);
					self::$connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
					self::$connection->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
				}
			}
			catch (\Exception $e) {
				\sven\sys\core::addException(new \sven\sys\Exception($e));
			}
		}

		/**
		 * disconnect
		 *
		 * closes connection
		 *
		 * @access private
		 * @return void
		 */
		private function disconnect() {
			if(gettype(self::$connection) == "object") self::$connection = null;
		}
	};

?>
