diff options
Diffstat (limited to 'src/Bridge.php')
-rw-r--r-- | src/Bridge.php | 64 |
1 files changed, 48 insertions, 16 deletions
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) { |