From 15ddec7e5e81c290a780076ef5520e402a147cae Mon Sep 17 00:00:00 2001 From: uckelman Date: Wed, 7 Apr 2010 20:41:24 +0000 Subject: Refactored. git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@6631 67b53d14-2c14-4ace-a08f-0dab2b34000c --- src/Bridge.php | 64 +++++++++++++++++++++------------------------------------- 1 file 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; + } } } -- cgit v1.2.3