From 3b888ef4cddf97d2f51953b4dd33e1d35eb02930 Mon Sep 17 00:00:00 2001 From: uckelman Date: Tue, 23 Mar 2010 22:38:14 +0000 Subject: * Added getMessageId(), getPostId(). * Build posts table for tests. git-svn-id: https://vassalengine.svn.sourceforge.net/svnroot/vassalengine/site-src/trunk@6629 67b53d14-2c14-4ace-a08f-0dab2b34000c --- src/Bridge.php | 64 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/Bridge.php b/src/Bridge.php index ab627d7..81f80b6 100644 --- a/src/Bridge.php +++ b/src/Bridge.php @@ -8,31 +8,63 @@ class Bridge { public function __construct($db = FALSE) { $this->db = $db ? $db : new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB, DB_USER, DB_PASS); + + $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } public function getPostId($messageId) { - $sql = 'SELECT post_id FROM posts' . - 'WHERE message_id = "' . $this->db->quote($message_id) . '"'; + $sql = 'SELECT post_id FROM posts ' . + 'WHERE message_id = ' . $this->db->quote($messageId); $result = $this->db->query($sql); - if (!$result) { - trigger_error("Unknown message id: $message_id", 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(); - return $row['post_id']; + 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; + } + } + + 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; + } } public function registerMessage($msg, $parentId) { - $sql = 'INSERT INTO posts' . - '(post_id, message_id, parent_message_id, references)' . - 'VALUES(' . $msg->getPostId() . ', ' - '"' . $this->db->quote($msg->getMessageId()) . '", ' - '"' . $this->db->quote() . '", ' - '"' . $this->db->quote($msg->getReferences()) . '")'; + $sql = 'INSERT INTO posts ' . + '(post_id, message_id, in_reply_to, refs) ' . + 'VALUES (' . $msg->getPostId() . ', ' + . $this->db->quote($msg->getMessageId()) . ', ' + . $this->db->quote() . ', ' + . $this->db->quote($msg->getReferences()) . ')'; $count = $this->db->exec($sql); @@ -44,8 +76,8 @@ class Bridge { } public function getDefaultForumId($list) { - $sql = 'SELECT forum_id FROM bridge' . - 'WHERE list_name = "' . $this->db->quote($list) . '"'; + $sql = 'SELECT forum_id FROM bridge ' . + 'WHERE list_name = ' . $this->db->quote($list); $result = $this->db->query($sql); if (!$result) { -- cgit v1.2.3