2015-02-08 16:29:39 +00:00
|
|
|
<?php
|
|
|
|
|
2015-04-30 19:19:07 +00:00
|
|
|
namespace Module\Database;
|
2015-04-29 15:18:33 +00:00
|
|
|
use \FuzeWorks\Module;
|
|
|
|
use \PDO;
|
2015-05-06 16:26:19 +00:00
|
|
|
use \FuzeWorks\DatabaseException;
|
2015-04-29 15:18:33 +00:00
|
|
|
|
2015-04-30 19:19:07 +00:00
|
|
|
class Main extends Module {
|
2015-02-08 16:29:39 +00:00
|
|
|
|
2015-04-30 19:19:07 +00:00
|
|
|
/**
|
|
|
|
* The default database connection
|
|
|
|
* @access private
|
|
|
|
* @var PDO Class
|
|
|
|
*/
|
2015-02-08 16:29:39 +00:00
|
|
|
private $DBH;
|
|
|
|
public $prefix;
|
|
|
|
|
|
|
|
public function __construct(&$core) {
|
|
|
|
parent::__construct($core);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onLoad() {
|
|
|
|
$this->config->dbActive = true;
|
|
|
|
}
|
|
|
|
|
2015-05-06 16:26:19 +00:00
|
|
|
/**
|
|
|
|
* Connect to a database
|
|
|
|
* @access public
|
|
|
|
* @param StdObject Config, like the database config in Application/Config
|
|
|
|
*/
|
2015-04-30 19:19:07 +00:00
|
|
|
public function connect($config = null) {
|
|
|
|
// If nothing is given, connect to database from the main config, otherwise use the served configuration
|
|
|
|
if (is_null($config)) {
|
|
|
|
$db = $this->mods->config->database;
|
2015-02-08 16:29:39 +00:00
|
|
|
} else {
|
2015-04-30 19:19:07 +00:00
|
|
|
$db = $config;
|
2015-02-08 16:29:39 +00:00
|
|
|
}
|
2015-05-06 16:26:19 +00:00
|
|
|
|
|
|
|
if (empty($db->type) || empty($db->host)) {
|
|
|
|
throw (new DatabaseException('Database is not configured!'));
|
|
|
|
}
|
2015-04-30 19:19:07 +00:00
|
|
|
|
|
|
|
// Get the DSN for popular types of databases or a custom DSN
|
|
|
|
switch (strtolower($db->type)) {
|
|
|
|
case 'mysql':
|
|
|
|
$dsn = "mysql:host=".$db->host.";";
|
|
|
|
$dsn .= (!empty($db->database) ? "dbname=".$db->database.";" : "");
|
2015-02-08 16:29:39 +00:00
|
|
|
break;
|
2015-04-30 19:19:07 +00:00
|
|
|
case 'custom':
|
|
|
|
$dsn = $db->dsn;
|
2015-02-08 16:29:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:19:07 +00:00
|
|
|
try {
|
|
|
|
$this->mods->logger->logInfo("Connecting to '".$dsn."'", "Database");
|
|
|
|
// And create the connection
|
|
|
|
$this->DBH = new PDO($dsn, $db->username, $db->password, (isset($db->options) ? $db->options : null));
|
|
|
|
$this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
$this->mods->logger->logInfo("Connected to database", "Database");
|
|
|
|
|
|
|
|
// And set the prefix
|
|
|
|
$this->prefix = $db->prefix;
|
|
|
|
} catch (Exception $e) {
|
2015-05-06 16:26:19 +00:00
|
|
|
throw (new DatabaseException('Could not connect to the database: "'. $e->getMessage() . '"'));
|
2015-04-30 19:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPrefix() {
|
|
|
|
return $this->prefix;
|
|
|
|
}
|
2015-02-08 16:29:39 +00:00
|
|
|
|
2015-04-30 19:19:07 +00:00
|
|
|
public function is_active() {
|
|
|
|
if($this->DBH === null){
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
2015-02-08 16:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __call($name, $params) {
|
|
|
|
if ($this->is_active()) {
|
|
|
|
return call_user_func_array(array($this->DBH, $name), $params);
|
|
|
|
} else {
|
2015-04-30 19:19:07 +00:00
|
|
|
$this->connect();
|
2015-02-08 16:29:39 +00:00
|
|
|
return call_user_func_array(array($this->DBH, $name), $params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __get($name) {
|
|
|
|
if ($this->is_active()) {
|
|
|
|
return $this->DBH->$name;
|
|
|
|
} else {
|
2015-04-30 19:19:07 +00:00
|
|
|
$this->connect();
|
2015-02-08 16:29:39 +00:00
|
|
|
return $this->DBH->$name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __set($name, $value) {
|
|
|
|
if ($this->is_active()) {
|
|
|
|
$this->DBH->$name = $value;
|
|
|
|
} else {
|
2015-04-30 19:19:07 +00:00
|
|
|
$this->connect();
|
2015-02-08 16:29:39 +00:00
|
|
|
$this->DBH->$name = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|