Skip to content

Commit 3c60915

Browse files
committed
add logging, fix bug
1 parent 7e8fa59 commit 3c60915

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

resources/lib/UnitySQL.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace UnityWebPortal\lib;
44

55
use PDO;
6+
use PDOException;
67

78
class UnitySQL
89
{
@@ -35,16 +36,28 @@ public function getConn()
3536
return $this->conn;
3637
}
3738

39+
private function execute($statement)
40+
{
41+
try {
42+
$statement->execute();
43+
} catch (PDOException $e) {
44+
ob_start();
45+
$statement->debugDumpParams();
46+
$sql_debug_dump = ob_get_clean();
47+
throw new PDOException($sql_debug_dump, 0, $e);
48+
}
49+
}
50+
3851
private function search($table, $filters)
3952
{
4053
$stmt = $this->conn->prepare(
4154
"SELECT * FROM $table WHERE " .
4255
implode(" and ", array_map(fn($x) => "$x=:$x", array_keys($filters)))
4356
);
4457
foreach ($filters as $key => $val) {
45-
$stmt->bindParam(":$key", $val);
58+
$stmt->bindValue(":$key", $val);
4659
}
47-
$stmt->execute();
60+
$this->execute($stmt);
4861
return $stmt->fetchAll();
4962
}
5063

@@ -55,9 +68,9 @@ private function delete($table, $filters)
5568
implode(" and ", array_map(fn($x) => "$x=:$x", array_keys($filters)))
5669
);
5770
foreach ($filters as $key => $val) {
58-
$stmt->bindParam(":$key", $val);
71+
$stmt->bindValue(":$key", $val);
5972
}
60-
$stmt->execute();
73+
$this->execute($stmt);
6174
}
6275

6376
private function insert($table, $data)
@@ -69,9 +82,9 @@ private function insert($table, $data)
6982
"(" . implode(", ", array_map(fn($x) => ":$x", array_keys($data))) . ")"
7083
);
7184
foreach ($data as $key => $val) {
72-
$stmt->bindParam(":$key", $val);
85+
$stmt->bindValue(":$key", $val);
7386
}
74-
$stmt->execute();
87+
$this->execute($stmt);
7588
}
7689

7790
private function update($table, $filters, $data)
@@ -84,11 +97,12 @@ private function update($table, $filters, $data)
8497
implode(" and ", array_map(fn($x) => "$x=:$x", array_keys($filters)))
8598
);
8699
foreach ($filters as $key => $val) {
87-
$stmt->bindParam(":$key", $val);
100+
$stmt->bindValue(":$key", $val);
88101
}
89102
foreach ($data as $key => $val) {
90-
$stmt->bindParam(":$key", $val);
103+
$stmt->bindValue(":$key", $val);
91104
}
105+
$this->execute($stmt);
92106
$stmt->execute();
93107
}
94108

@@ -108,7 +122,7 @@ public function removeRequest($requestor, $dest = self::REQUEST_BECOME_PI)
108122
if (!$this->requestExists($requestor, $dest)) {
109123
return;
110124
}
111-
$this->delete(self::TABLE_REQS, ["uid" => $uid, "request_for" => $dest]);
125+
$this->delete(self::TABLE_REQS, ["uid" => $requestor, "request_for" => $dest]);
112126
}
113127

114128
public function removeRequests($dest = self::REQUEST_BECOME_PI)
@@ -250,8 +264,8 @@ public function getRole($uid, $group)
250264
$stmt = $this->conn->prepare(
251265
"SELECT * FROM " . self::TABLE_GROUP_ROLE_ASSIGNMENTS . " WHERE user=:uid AND `group`=:group"
252266
);
253-
$stmt->bindParam(":uid", $uid);
254-
$stmt->bindParam(":group", $group);
267+
$stmt->bindValue(":uid", $uid);
268+
$stmt->bindValue(":group", $group);
255269

256270
$stmt->execute();
257271

@@ -263,7 +277,7 @@ public function hasPerm($role, $perm)
263277
$stmt = $this->conn->prepare(
264278
"SELECT * FROM " . self::TABLE_GROUP_ROLES . " WHERE slug=:role"
265279
);
266-
$stmt->bindParam(":role", $role);
280+
$stmt->bindValue(":role", $role);
267281

268282
$stmt->execute();
269283

@@ -277,7 +291,7 @@ public function getPriority($role)
277291
$stmt = $this->conn->prepare(
278292
"SELECT * FROM " . self::TABLE_GROUP_ROLES . " WHERE slug=:role"
279293
);
280-
$stmt->bindParam(":role", $role);
294+
$stmt->bindValue(":role", $role);
281295

282296
$stmt->execute();
283297

@@ -290,8 +304,8 @@ public function roleAvailableInGroup($uid, $group, $role)
290304
$stmt = $this->conn->prepare(
291305
"SELECT * FROM " . self::TABLE_GROUP_ROLE_ASSIGNMENTS . " WHERE user=:uid AND `group`=:group"
292306
);
293-
$stmt->bindParam(":uid", $uid);
294-
$stmt->bindParam(":group", $group);
307+
$stmt->bindValue(":uid", $uid);
308+
$stmt->bindValue(":group", $group);
295309

296310
$stmt->execute();
297311
$row = $stmt->fetchAll()[0];
@@ -302,7 +316,7 @@ public function roleAvailableInGroup($uid, $group, $role)
302316
"SELECT * FROM " . self::TABLE_GROUP_TYPES . " WHERE slug=:slug"
303317
);
304318

305-
$stmt->bindParam(":slug", $group_slug);
319+
$stmt->bindValue(":slug", $group_slug);
306320
$stmt->execute();
307321

308322
$row = $stmt->fetchAll()[0];

0 commit comments

Comments
 (0)