-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
153 lines (111 loc) · 3.5 KB
/
database.php
File metadata and controls
153 lines (111 loc) · 3.5 KB
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
<?php
/**
* /db/database.php
*
* This is a database abstraction layer. It provides useful functions to avoid
* working directly with the mysqli API. To instanciate the Database class, the
* global constants for the session must be set. Otherwise an exception will be
* thrown. This is a necessary protection step to allow the live demo of the
* application.
*
*/
declare(strict_types=1);
class Database {
private $connector = null;
private $sql = "";
private $params = [];
private $response = null;
private $errors = [];
function __construct() {
if (
!defined("DB_HOST")
|| !defined("DB_USERNAME")
|| !defined("DB_PASSWORD")
|| !defined("DB_DBNAME")
) {
throw new Exception("Failed to connect to MySQL: invalid session constants", 1);
}
$this->connector = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DBNAME);
if ($this->connector->connect_error) {
throw new Exception("Failed to connect to MySQL: (" . $this->connector . ") " . $this->connector->connect_error, 1);
}
}
function clear(): void {
$this->sql = "";
$this->params = [];
$this->response = null;
$this->errors = [];
}
function setSql(string $sql): void {
$this->sql = $sql;
}
function getSql(): string {
return $this->sql;
}
function setParams(array $params): void {
$this->params = $params;
}
function getParams(): array {
return $this->params;
}
function query(): bool {
$statement = $this->connector->prepare($this->sql);
if (!empty($this->params)) {
$params = $this->parseParams();
$statement->bind_param($params["types"], ...$params["values"]);
}
$result = $statement->execute();
$this->errors = $statement->error_list;
$this->response = $statement->get_result();
$statement->close();
return $result;
}
private function parseParams(): array {
$parse = [
"types" => "",
"values" => []
];
foreach ($this->params as $param) {
$parse["types"] .= (array_key_exists("type", $param))
? $param["type"]
: "s";
$parse["values"][] = $param["value"];
}
return $parse;
}
/**
* @return mixed (either null or an array)
*/
function getRow() {
return $this->response->fetch_assoc();
}
function getAll(): array {
return $this->response->fetch_all(MYSQLI_ASSOC);
}
function getInsertId(): int {
return $this->connector->insert_id;
}
function getErrors(): array {
return $this->errors;
}
function generateUpdate(array $params): string {
$colVals = "";
foreach ($params["cols"] as $column) {
if ($colVals !== "") {
$colVals .= ", ";
}
$colVals .= $column["name"] . " = ?";
}
$conds = "";
foreach ($params["conditions"] as $condition) {
if ($conds !== "") {
if (!array_key_exists("cond_type", $condition)) {
$condition["cond_type"] = "AND";
}
$conds .= " " . $condition["cond_type"] . " ";
}
$conds .= $condition["name"] . " = ?";
}
return "UPDATE " . $params["table"] . " SET " . $colVals . " WHERE " . $conds;
}
}