|
||
| Inside Technique : Adding Discussion Forums : Stored Procedures The discussion forum uses a collection of stored procedures for posting and editing messages.
We will now take you through each stored procedure. Listing Forums and MessagesThe stored procedures sp_gettopicsforforum and sp_getmessagesfortopic are used to retrieve individual messages and the messages within each forum. (we create the list of forums directly from the t_forums table so no stored procedure is used). sp_gettopicsforforumThis stored procedure returns two recordsets given a forum id: one containing the forum information and the other
containing the list of topics for the specified forum. The forum information allows you to access
meta-information (eg., forum name and description). The list of topics is based on all messages in the forum that
have a topic_parent of -1. This defines that the message is the first message in the topic. All replies have
a topic_parent pointing to the originating message.
sp_getmessagesfortopicThis stored procedure returns two recordsets representing a topic and all its replies.
The first recordset returns the original message with information about the forum the message is contained
within. The second recordset returns all the replies to the original message. Message replies are easily
retrieved by finding messages with the topic_parent equal to the starting topic's ID.
sp_getforumThis stored procedure is very simple and just returns all the meta-information for a particular forum.
Message ManipulationThe remaining stored procedures are used to post, edit, and delete messages. sp_deletemessageThis stored procedure is used to delete an individual message from the message board.
This stored procedure is very simple and only deletes a single message.
By itself, this stored procedure is inadequate. For example, when you delete a message with replies you most likely want the replies to also be deleted. This does happen, but it happens inside the t_topics table where through a delete trigger. Triggers are specialized stored procedures that are automatically executed when a table is updated either through an insert, edit, or delete operation. If you delete the first message in a topic, we go ahead and delete all replies. If the message you are
deleting is a reply, then we decrement the counter that tracks how many replies have been posted.
sp_editmessageThis stored procedure is used by the administration screens to allow you to update an existing message.
sp_starttopicThis stored procedure is used to post new messages to a specific forum. By specifying a topic_parent, you can
use this procedure to reply to a specific message.
Whenever a message is posted, we call an update trigger on the t_topics table. This trigger
updates the message counter and last update information:
These are all the stored procedures used by the message forum. If you are looking for a challenge, try extending the stored procedures and table to support a threaded message forum (hint - think about how you can use the topic_parent field). If you choose to add threading, be sure to update the delete stored procedure and update and delete triggers. They are not currently designed to work correctly with a threaded system. We are now ready to take you through the ASP Scripts that interact with the database and expose a simple user-interface. Page 1:Adding Discussion Forums © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |