summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Bridge.php64
1 files changed, 23 insertions, 41 deletions
diff --git a/src/Bridge.php b/src/Bridge.php
index 81f80b6..1e37910 100644
--- a/src/Bridge.php
+++ b/src/Bridge.php
@@ -16,46 +16,16 @@ class Bridge {
$sql = 'SELECT post_id FROM posts ' .
'WHERE message_id = ' . $this->db->quote($messageId);
- $result = $this->db->query($sql);
-
- $rows = $result->fetchAll(PDO::FETCH_ASSOC);
- $result->closeCursor();
-
- switch (count($rows)) {
- case 0:
- trigger_error("Unknown message id: $messageId", E_USER_ERROR);
- break;
-
- case 1:
- return $rows[0]['post_id'];
-
- default:
- trigger_error("Too many rows returned: $messageId", E_USER_ERROR);
- break;
- }
+ $row = get_exactly_one_row($sql);
+ return $row['post_id'];
}
public function getMessageId($postId) {
$sql = 'SELECT message_id FROM posts ' .
'WHERE post_id = ' . $this->db->quote($postId);
- $result = $this->db->query($sql);
-
- $rows = $result->fetchAll(PDO::FETCH_ASSOC);
- $result->closeCursor();
-
- switch (count($rows)) {
- case 0:
- trigger_error("Unknown post id: $postId", E_USER_ERROR);
- break;
-
- case 1:
- return $rows[0]['message_id'];
-
- default:
- trigger_error("Too many rows returned: $postId", E_USER_ERROR);
- break;
- }
+ $row = get_exactly_one_row($sql);
+ return $row['message_id'];
}
public function registerMessage($msg, $parentId) {
@@ -76,19 +46,31 @@ class Bridge {
}
public function getDefaultForumId($list) {
- $sql = 'SELECT forum_id FROM bridge ' .
+ $sql = 'SELECT forum_id FROM forums ' .
'WHERE list_name = ' . $this->db->quote($list);
+ $row = get_exactly_one_row($sql);
+ return $row['forum_id'];
+ }
+
+ protected function get_exactly_one_row($sql) {
$result = $this->db->query($sql);
- if (!$result) {
- trigger_error("Unknown list name: $list", E_USER_ERROR);
- }
- // FIXME: what to do if more than one row is returned?
- $row = $result->fetch(PDO::FETCH_ASSOC);
+ $rows = $result->fetchAll(PDO::FETCH_ASSOC);
$result->closeCursor();
+
+ switch (count($rows)) {
+ case 0:
+ trigger_error("No rows returned: $sql", E_USER_ERROR);
+ break;
- return $row['forum_id'];
+ case 1:
+ return $rows[0];
+
+ default:
+ trigger_error("Too many rows returned: $sql", E_USER_ERROR);
+ break;
+ }
}
}