<?php

require_once('config.php');
require_once('DB.php');

class Database {
   private $conn;

   function __construct($dsn = null) {
      global $conn;

      // Domylnie uywa wartoci z pliku konfiguracyjnego
      if($dsn == null) {
         $dsn = $cfg['db']['dsn'];
      }

      // Nawizuje poczenie z baz odpowiadajc $dsn
      $this->conn = DB::connect($dsn);

      if(DB::isError($this->conn)) {
         // Poczenie nieudane, zgasza wyjtek
         throw new Exception($this->conn->getMessage(), $this->conn->getCode());
      }

      // Dane pobierane jako tablica asocjacyjna
      $this->conn->setFetchMode(DB_FETCHMODE_ASSOC);
   }

   function __destruct() {
      $this->conn->disconnect();
   }
}
?>