-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.php
50 lines (40 loc) · 1.55 KB
/
database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
* This class connects to the MySQL-Database and provides a database connection object
*/
class Database {
/** @var null|PDO the database connection object */
private $dbConnection = null;
/**
* Database constructor.
* Connects to the MySQL-Database and creates a database-object
*/
function __construct() {
// if the connection was already made, return the connection-object
if ($this->dbConnection != null) {
return $this->dbConnection;
}
$this->dbConnection = new PDO('mysql:host=' . Config::$dbConfig['host'] . ';dbname=' . Config::$dbConfig['dbname'] . ';port=' . Config::$dbConfig['port'],
Config::$dbConfig['user'], Config::$dbConfig['pass']);
$this->dbConnection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$this->dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->dbConnection;
}
/**
* Prepares the given query
* @param $query the query
* @return PDOStatement a prepared PDO-query
*/
function prepare($query) : PDOStatement {
return $this->dbConnection->prepare($query);
}
/**
* prints the debug-log
* @codeCoverageIgnore
*/
function printLog() {
if (get_class($this->dbConnection) === "LoggedPDO") {
$this->dbConnection->printLog();
}
}
}