<?php

include_once './includes/config/Configuration.inc.php';

/**
 * This class allows easy data accesses.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class MySQLDatabase {

    /**
     *Contains the link with the database.
     * @var data
     */
    private $connection;

    private $counter;

    private static $_instance;

    private function  __construct() {

        $server = Configuration::SQLHost;
        $username = Configuration::SQLUsername;
        $password = Configuration::SQLPassword;
        $database = Configuration::SQLDatabase;

        $this->connection = @mysql_connect($server, $username, $password);

        if ($this->connection == false) {
            die(Localization::SQLdeath);
        }

        $result = @mysql_select_db($database, $this->connection);

        if ($result == false) {
            trigger_error("SQL Error : " . mysql_error(),  E_USER_ERROR);
            die(Localization::SQLdeath);
        }

        $this->counter = array();

    }

    public function  __destruct() {
        if ($this->connection != false) {
            @mysql_close($this->connection);
            $this->connection = false;
        }
        //TODO: Optimize a lot the requests pleaaaase !
    }

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

    public static function getInstance() {
        if (self::$_instance === NULL) {
            self::$_instance = new MySQLDatabase();
        }
        return self::$_instance;
    }

    public function getCount() {
        return count($this->counter);
    }

    /**
     * Runs the given request on the database and returns the results.
     *
     * @param string $sqlRequest          The request to run
     *
     * @return array                An array containing the results.
     */
    public function runRequest($sqlRequest) {
        $this->counter[] = $sqlRequest;
        if (strpos($sqlRequest,"SELECT") === false) {
            $message = "To run an operation, use the runOperation method\n";
            $message.= "Query: $sqlRequest\n";
            throw new Exception($message);
        }

        $result = mysql_query($sqlRequest, $this->connection);

        if ($result !== false) {

            $array = array();
            $count = 0;

            while($assoc = mysql_fetch_assoc($result)) {
                $array[$count] = $assoc;
                $count++;
            }

            return $array;
        } else {
            return false;
        }
    }

    /**
     * Runs the given request on the database and returns the results.
     *
     * @param string $sqlRequest  The request to run
     *
     * @return int/bool           The number of rows affected or false if an
     *                            error occured.
     */
    public function runOperation($sqlRequest) {
        $this->counter[] = $sqlRequest;
        if (strpos($sqlRequest,"SELECT") !== false) {
            $message = "To run a selection, use the runRequest method\n";
            $message.= "Query: $sqlRequest\n";
            throw new Exception($message);
        }

        $result = @mysql_query($sqlRequest, $this->connection);

        if ($result) {
            $result = mysql_affected_rows($this->connection);
        } else {
            $result = false;
        }

        return $result;
    }

    public function runOperations($sqlRequests) {
        $sqlRequestsArray = explode(";", $sqlRequests);
        $sqlReturnValues = array();
        foreach ($sqlRequestsArray as $sqlRequest) {
            $sqlReturnValues[] = $this->runOperation($sqlRequest);
        }
        return $sqlReturnValues;
    }

    //TODO: Some table creation function from a "export" SQL
}
?>
