-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.php
159 lines (141 loc) · 2.79 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace alvin\phpmvc;
use alvin\phpmvc\Controller;
/**
* Base class for exposing database methods
*/
class Database extends Controller {
/**
* PDO instance.
* @var \PDO
*/
public $sqldb;
/**
* Type of datastore
* @var string
*/
public string $dbType;
/**
* database name
* @var string
*/
private string $DB_NAME;
/**
* database host name
* @var string
*/
private string $DB_HOST;
/**
* database port
* @var string
*/
private string $DB_PORT;
/**
* database user
* @var string
*/
private string $DB_USER;
/**
* database password
* @var string
*/
private string $DB_PASS;
/**
* data source name
* @var string
*/
private string $DB_DSN;
/**
* Creates a new Database instance.
*
* @param string $dbType type of database to instantiate
*/
public function __construct(string $dbType = 'sql') {
$this->dbType = $dbType;
switch ($dbType) {
case 'sql':{
$this->setupSQL();
}
}
}
/**
* Setup SQL database connection.
*
* Sets up sql database connection using environment variables and instantiates pdo
*
* @return void
*/
private function setupSQL() {
$this->DB_NAME = getenv('DB_NAME');
$this->DB_HOST = getenv('DB_HOST');
$this->DB_PORT = getenv('DB_PORT');
$this->DB_USER = getenv('DB_USER');
$this->DB_PASS = getenv('DB_PASS');
$this->DB_DSN = "mysql:host=$this->DB_HOST:$this->DB_PORT;dbname=$this->DB_NAME";
$this->sqldb = new \PDO($this->DB_DSN, $this->DB_USER, $this->DB_PASS);
$this->sqldb->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->sqldb->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
}
/**
* Query.
*
* Uses \PDO::query()
*
* @param string $string sql statement
* @return object|false
*/
public function query(string $string) {
return $this->sqldb->query($string);
}
/**
* Prepare an sql statement.
*
* Uses \PDO::prepare() for prepared statements
*
* @param string $string sql statement
* @return object|false
*/
public function prepare(string $sql) {
return $this->sqldb->prepare($sql);
}
/**
* Begin an sql transaction.
*
* Uses \PDO::beginTransaction() for beginning transactions
*
* @return boolean
*/
public function beginTransaction() {
$this->sqldb->beginTransaction();
}
/**
* Rollback an sql transaction.
*
* Uses \PDO::rollBack() for rolling back transactions
*
* @return boolean
*/
public function rollBack() {
$this->sqldb->rollBack();
}
/**
* Commit an sql transaction.
*
* Uses \PDO::commit() for commiting transactions
*
* @return boolean
*/
public function commit() {
$this->sqldb->commit();
}
/**
* Get id for last inserted record.
*
* Uses \PDO::lastInsertId()
*
* @return boolean
*/
public function lastInsertId() {
return $this->sqldb->lastInsertId();
}
}