Skip to content

Commit 7514d2b

Browse files
committed
remove sql boilerplate
1 parent 796ee0d commit 7514d2b

File tree

1 file changed

+84
-137
lines changed

1 file changed

+84
-137
lines changed

resources/lib/UnitySQL.php

Lines changed: 84 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -35,106 +35,111 @@ public function getConn()
3535
return $this->conn;
3636
}
3737

38-
//
39-
// requests table methods
40-
//
41-
public function addRequest($requestor, $dest = self::REQUEST_BECOME_PI)
38+
private function search($table, $filters)
4239
{
43-
if ($this->requestExists($requestor, $dest)) {
44-
return;
45-
}
46-
4740
$stmt = $this->conn->prepare(
48-
"INSERT INTO " . self::TABLE_REQS . " (uid, request_for) VALUES (:uid, :request_for)"
41+
"SELECT * FROM $table WHERE " .
42+
implode(" and ", array_map(fn($x) => "$x=:$x", array_keys($filters)))
4943
);
50-
$stmt->bindParam(":uid", $requestor);
51-
$stmt->bindParam(":request_for", $dest);
52-
44+
foreach ($filters as $key => $val) {
45+
$stmt->bindParam(":$key", $val);
46+
}
5347
$stmt->execute();
48+
return $stmt->fetchAll();
5449
}
5550

56-
public function removeRequest($requestor, $dest = self::REQUEST_BECOME_PI)
51+
private function delete($table, $filters)
5752
{
58-
if (!$this->requestExists($requestor, $dest)) {
59-
return;
60-
}
61-
6253
$stmt = $this->conn->prepare(
63-
"DELETE FROM " . self::TABLE_REQS . " WHERE uid=:uid and request_for=:request_for"
54+
"DELETE FROM $table WHERE " .
55+
implode(" and ", array_map(fn($x) => "$x=:$x", array_keys($filters)))
6456
);
65-
$stmt->bindParam(":uid", $requestor);
66-
$stmt->bindParam(":request_for", $dest);
67-
57+
foreach ($filters as $key => $val) {
58+
$stmt->bindParam(":$key", $val);
59+
}
6860
$stmt->execute();
6961
}
7062

71-
public function removeRequests($dest = self::REQUEST_BECOME_PI)
63+
private function insert($table, $data)
7264
{
7365
$stmt = $this->conn->prepare(
74-
"DELETE FROM " . self::TABLE_REQS . " WHERE request_for=:request_for"
66+
"INSERT INTO $table " .
67+
"(" . implode(", ", array_keys($data)) . ") " .
68+
"VALUES " .
69+
"(" . implode(", ", array_map(fn($x) => ":$x", array_keys($data))) . ")"
7570
);
76-
$stmt->bindParam(":request_for", $dest);
77-
71+
foreach ($data as $key => $val) {
72+
$stmt->bindParam(":$key", $val);
73+
}
7874
$stmt->execute();
7975
}
8076

81-
public function requestExists($requestor, $dest = self::REQUEST_BECOME_PI)
77+
private function update($table, $filters, $data)
8278
{
79+
// "UPDATE " . self::TABLE_NOTICES . " SET date=:date, title=:title, message=:message WHERE id=:id"
8380
$stmt = $this->conn->prepare(
84-
"SELECT * FROM " . self::TABLE_REQS . " WHERE uid=:uid and request_for=:request_for"
81+
"UPDATE $table SET" .
82+
implode(", ", array_map(fn($x) => "$x=:$x", array_keys($filters))) .
83+
"WHERE " .
84+
implode(" and ", array_map(fn($x) => "$x=:$x", array_keys($filters)))
8585
);
86-
$stmt->bindParam(":uid", $requestor);
87-
$stmt->bindParam(":request_for", $dest);
88-
86+
foreach ($filters as $key => $val) {
87+
$stmt->bindParam(":$key", $val);
88+
}
89+
foreach ($data as $key => $val) {
90+
$stmt->bindParam(":$key", $val);
91+
}
8992
$stmt->execute();
90-
91-
return count($stmt->fetchAll()) > 0;
9293
}
9394

94-
public function getRequests($dest = self::REQUEST_BECOME_PI)
95+
//
96+
// requests table methods
97+
//
98+
public function addRequest($requestor, $dest = self::REQUEST_BECOME_PI)
9599
{
96-
$stmt = $this->conn->prepare(
97-
"SELECT * FROM " . self::TABLE_REQS . " WHERE request_for=:request_for"
98-
);
99-
$stmt->bindParam(":request_for", $dest);
100+
if ($this->requestExists($requestor, $dest)) {
101+
return;
102+
}
103+
$this->insert(self::TABLE_REQS, ["uid" => $requestor, "request_for" => $dest]);
104+
}
100105

101-
$stmt->execute();
106+
public function removeRequest($requestor, $dest = self::REQUEST_BECOME_PI)
107+
{
108+
if (!$this->requestExists($requestor, $dest)) {
109+
return;
110+
}
111+
$this->delete(self::TABLE_REQS, ["uid" => $uid, "request_for" => $dest]);
112+
}
102113

103-
return $stmt->fetchAll();
114+
public function removeRequests($dest = self::REQUEST_BECOME_PI)
115+
{
116+
$this->delete(self::TABLE_REQS, ["request_for" => $dest]);
104117
}
105118

106-
public function getRequestsByUser($user)
119+
public function requestExists($requestor, $dest = self::REQUEST_BECOME_PI)
107120
{
108-
$stmt = $this->conn->prepare(
109-
"SELECT * FROM " . self::TABLE_REQS . " WHERE uid=:uid"
110-
);
111-
$stmt->bindParam(":uid", $user);
121+
$results = $this->search(self::TABLE_REQS, ["request_for" => $dest]);
122+
return count($results) > 0;
123+
}
112124

113-
$stmt->execute();
125+
public function getRequests($dest = self::REQUEST_BECOME_PI)
126+
{
127+
return $this->search(self::TABLE_REQS, ["request_for" => $dest]);
128+
}
114129

115-
return $stmt->fetchAll();
130+
public function getRequestsByUser($uid)
131+
{
132+
return $this->search(self::TABLE_REQS, ["uid" => $uid]);
116133
}
117134

118135
public function deleteRequestsByUser($user)
119136
{
120-
$stmt = $this->conn->prepare(
121-
"DELETE FROM " . self::TABLE_REQS . " WHERE uid=:uid"
122-
);
123-
$stmt->bindParam(":uid", $user);
124-
125-
$stmt->execute();
137+
$this->delete(self::TABLE_REQS, ["uid" => $uid]);
126138
}
127139

128140
public function addNotice($title, $date, $content, $operator)
129141
{
130-
$stmt = $this->conn->prepare(
131-
"INSERT INTO " . self::TABLE_NOTICES . " (date, title, message) VALUES (:date, :title, :message)"
132-
);
133-
$stmt->bindParam(":date", $date);
134-
$stmt->bindParam(":title", $title);
135-
$stmt->bindParam(":message", $content);
136-
137-
$stmt->execute();
142+
$this->insert(self::TABLE_NOTICES, ["date" => $date, "title" => $title, "message" => $content]);
138143

139144
$operator = $operator->getUID();
140145

@@ -161,56 +166,27 @@ public function editNotice($id, $title, $date, $content)
161166

162167
public function deleteNotice($id)
163168
{
164-
$stmt = $this->conn->prepare(
165-
"DELETE FROM " . self::TABLE_NOTICES . " WHERE id=:id"
166-
);
167-
$stmt->bindParam(":id", $id);
168-
169-
$stmt->execute();
169+
$this->delete(self::TABLE_NOTICES, ["id" => $id]);
170170
}
171171

172172
public function getNotice($id)
173173
{
174-
$stmt = $this->conn->prepare(
175-
"SELECT * FROM " . self::TABLE_NOTICES . " WHERE id=:id"
176-
);
177-
$stmt->bindParam(":id", $id);
178-
179-
$stmt->execute();
180-
181-
return $stmt->fetchAll()[0];
174+
return $this->search(self::TABLE_NOTICES, ["id" => $id]);
182175
}
183176

184177
public function getNotices()
185178
{
186-
$stmt = $this->conn->prepare(
187-
"SELECT * FROM " . self::TABLE_NOTICES . " ORDER BY date DESC"
188-
);
189-
$stmt->execute();
190-
191-
return $stmt->fetchAll();
179+
return $this->search(self::TABLE_NOTICES, []);
192180
}
193181

194182
public function getPages()
195183
{
196-
$stmt = $this->conn->prepare(
197-
"SELECT * FROM " . self::TABLE_PAGES
198-
);
199-
$stmt->execute();
200-
201-
return $stmt->fetchAll();
184+
return $this->search(self::TABLE_PAGES, []);
202185
}
203186

204187
public function getPage($id)
205188
{
206-
$stmt = $this->conn->prepare(
207-
"SELECT * FROM " . self::TABLE_PAGES . " WHERE page=:id"
208-
);
209-
$stmt->bindParam(":id", $id);
210-
211-
$stmt->execute();
212-
213-
return $stmt->fetchAll()[0];
189+
return $this->search(self::TABLE_PAGES, ["page" => $id]);
214190
}
215191

216192
public function editPage($id, $content, $operator)
@@ -236,73 +212,44 @@ public function editPage($id, $content, $operator)
236212
// audit log table methods
237213
public function addLog($operator, $operator_ip, $action_type, $recipient)
238214
{
239-
$stmt = $this->conn->prepare(
240-
"INSERT INTO " . self::TABLE_AUDIT_LOG . " (operator, operator_ip, action_type, recipient)
241-
VALUE (:operator, :operator_ip, :action_type, :recipient)"
215+
$this->insert(
216+
self::TABLE_REQS,
217+
[
218+
"operator" => $operator,
219+
"operator_ip" => $operator_ip,
220+
"action_type" => $action_type,
221+
"recipient" => $recipient
222+
]
242223
);
243-
$stmt->bindParam(":operator", $operator);
244-
$stmt->bindParam(":operator_ip", $operator_ip);
245-
$stmt->bindParam(":action_type", $action_type);
246-
$stmt->bindParam(":recipient", $recipient);
247-
248-
$stmt->execute();
249224
}
250225

251226
public function addAccountDeletionRequest($uid)
252227
{
253-
$stmt = $this->conn->prepare(
254-
"INSERT INTO " . self::TABLE_ACCOUNT_DELETION_REQUESTS . " (uid) VALUE (:uid)"
255-
);
256-
$stmt->bindParam(":uid", $uid);
257-
258-
$stmt->execute();
228+
$this->insert(self::TABLE_ACCOUNT_DELETION_REQUESTS, ["uid" => $uid]);
259229
}
260230

261231
public function accDeletionRequestExists($uid)
262232
{
263-
$stmt = $this->conn->prepare(
264-
"SELECT * FROM " . self::TABLE_ACCOUNT_DELETION_REQUESTS . " WHERE uid=:uid"
265-
);
266-
$stmt->bindParam(":uid", $uid);
267-
268-
$stmt->execute();
269-
270-
return count($stmt->fetchAll()) > 0;
233+
$results = $this->search(self::TABLE_ACCOUNT_DELETION_REQUESTS, ["uid" => $uid]);
234+
return count($results) > 0;
271235
}
272236

273237
public function deleteAccountDeletionRequest($uid)
274238
{
275239
if (!$this->accDeletionRequestExists($uid)) {
276240
return;
277241
}
278-
$stmt = $this->conn->prepare(
279-
"DELETE FROM " . self::TABLE_ACCOUNT_DELETION_REQUESTS . " WHERE uid=:uid"
280-
);
281-
$stmt->bindParam(":uid", $uid);
282-
$stmt->execute();
242+
$this->delete(self::TABLE_ACCOUNT_DELETION_REQUESTS, ["uid" => $uid]);
283243
}
284244

285245
public function getSiteVar($name)
286246
{
287-
$stmt = $this->conn->prepare(
288-
"SELECT * FROM " . self::TABLE_SITEVARS . " WHERE name=:name"
289-
);
290-
$stmt->bindParam(":name", $name);
291-
292-
$stmt->execute();
293-
294-
return $stmt->fetchAll()[0]['value'];
247+
return $this->search(self::TABLE_SITEVARS, ["name" => $name]);
295248
}
296249

297250
public function updateSiteVar($name, $value)
298251
{
299-
$stmt = $this->conn->prepare(
300-
"UPDATE " . self::TABLE_SITEVARS . " SET value=:value WHERE name=:name"
301-
);
302-
$stmt->bindParam(":name", $name);
303-
$stmt->bindParam(":value", $value);
304-
305-
$stmt->execute();
252+
$this->update(self::TABLE_SITEVARS, ["value" => $value, "name" => $name]);
306253
}
307254

308255
public function getRole($uid, $group)

0 commit comments

Comments
 (0)