-
Notifications
You must be signed in to change notification settings - Fork 9
/
dbConnection.php
69 lines (59 loc) · 1.83 KB
/
dbConnection.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* A singleton designed to hold the database connection.
*
* @author Lauge Rud Knudsen <laugerudknudsen@gmail.com>
* @version V2
*/
class dbConnection {
private static $instance;
private $driver = 'pgsql';
private $user;
private $password;
private $host;
private $port;
private $dbname;
private $persistent = false;
private $db;
function __construct() {
$helper = Helper::getInstance();
$this->user = $helper->setting('DATABASE_USER');
$this->password = $helper->setting('DATABASE_PASSWORD');
$this->host = $helper->setting('DATABASE_HOST');
$this->port = $helper->setting('DATABASE_PORT');
$this->dbname = $helper->setting('DATABASE_NAME');
$this->driver = $helper->setting('DATABASE_DRIVER');
$this->persistent = $helper->setting('DATABASE_PERSISTENT');
}
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new self;
}
return self::$instance;
}
private function __clone() { }
public function getDB() {
return $this->db;
}
public function start() {
/*
$credentials = "user = " . Helper::getInstance()->setting('DATABASE_USER') . " password = " . Helper::getInstance()->setting('DATABASE_PASSWORD');
$this->db = pg_connect("$this->host $this->port dbname = " . Helper::getInstance()->setting('DATABASE_NAME') . " $credentials");
unset($credentials);
*/
try {
$this->db = new PDO($this->driver . ':host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname, $this->user, $this->password, [
PDO::ATTR_PERSISTENT => $this->persistent
]);
} catch (PDOException $ex) {
die();
}
}
public function close() {
$this->db = null;
// pg_close($this->db);
}
public function prepare($query) {
return $this->db->prepare($query);
}
}