存储过程实现BBS树形结构

BBS的树形结构一直是大家讨论的话题,以前我做都是利用命名规则来实现,这样的好处是表的冗余字段少,结构清楚,容易理解,但其局限性也很明显。感谢廖家远提供算法(实话说,当年算法就没有学好),我决定采用一下这种算法来实现bbs的树形结构。基本思路如下:
    bbs文章表中有这样几个字段:
    RootID :   根ID , 新发贴子及其所有子贴都相同。
    FatherID:  父ID , 父贴子ID
    Layer:     层数 , 贴子在树中的深度。
    OrderNum:  排序基数,关键所在,根据它来排序。 数据挖掘研究院

基本算法举例如下:

根16(拿个小的举例)
id    ordernum              Layer               
1     16                     0
2     16+16/2                1   回复第1贴
3     16+16/(2^2)            1   回复第1贴
4     16+16/2+16/(2^3)       2   回复第2贴
5     16+16/(2^2)+16/(2^4)   2   回复第3贴

数据挖掘工具

然后,根据排序的结果是(加上回复的深度,就成了树状结构)
id      ordernum                  深度
1       16                          0
3       16+16/(2^2)                 1
5       16+16/(2^2)+16/(2^4)        2
2       16+16/2                     1
4       16+16/2+16/(2^3)            2 数据挖掘工具

成了这样的树:
1
  3
     5
  2
     4

根据以上思路,我们设计表如下:

/*BBS文章表*/
if exists (select * from sysobjects where ID = object_id("BBS"))
   drop table BBS
go

create table BBS
             (
        ID        int primary key identity        not null ,
                RootID        int        default 0        not null , 
        FatherID    int         default 0        not null ,
        Layer        tinyint        default 0        not null ,
                ForumID         int             default 0        not null , 数据挖掘工具
        UserID        int        default 0        not null ,
        Title        varchar(255)    default ""        not null ,
        Content        text        default ""             ,
        PostTime    datetime    default getdate()    not null ,
        FaceID        tinyint        default 0        not null ,
                TotalChilds     int             default 0               not null , 数据挖掘工具
                OrderNum        float           default power(2,30)     not null ,
        Hits        int        default 0        not null ,
                selected        bit             default 0               not null ,
                closed          bit             default 0               not null ,


        IfEmail        bit         default 0        not null ,
        IfSignature    bit        default 0        not null
           )
go
                 

/*BBS注册用户表*/
if exists(select * from sysobjects where ID = object_id("BBSUser"))
   drop table BBSUser
go

数据挖掘实验室

create table BBSUser
             (
               ID       int        Primary key identity    not null ,
               UserName    varchar(20)    default ""              not null ,
               Password    varchar(10)  default ""              not null ,
               UserType    tinyint      default 0               not null , --用户类型,1为斑竹

数据挖掘论坛


               Email       varchar(100) default ""              not null ,
               HomePage    varchar(100) default ""              not null ,
               ICQ         varchar(20)  default ""              not null ,
               Signature   varchar(255) default ""              not null , --签名
               Point       int          default 0               not null , --用户积分 数据挖掘工具
              )
go   


    表结构定了,剩下的就是怎样实现。我把所有相关的功能集成在一个存储过程中,包括贴子本身的存储,其关键是排序基数的生成;父贴子相关字段的更新 ; 根贴相关字段的更新,这些都放到一个事务中,以保持数据的一致性,另外如果父贴要求有回复用email通知,在存储过程中实现发回复email的功能,而不必借助任何asp或其他的组件。这样就使所有任务在一个存储过程中实现。

数据挖掘交友

 

  数据挖掘交友


--------------------------------------------------------------------------------

数据挖掘工具


下面是上篇文章所说的存储过程,其作用已经说过,在这里就不再赘述了。请大家自己看代码吧。这个存储过程只是存储数据的过程,以后如果有时间我将讲一下读取数据。 数据挖掘实验室

/**********************************************************************/
/*                                                                    */
/*  Stored Procudure :  up_PostTopic                                  */
/*                                                                    */

数据挖掘研究院


/*  Description:        贴子存储及回复Email                           */
/*                                                                    */
/*  Author:             Bigeagle                                      */
/*                                                                    */
/*  date:               2000/7/25 凌晨                                */
/*                                                                    */
/*  History:            version 1.0 by BigEagle , 2000/7/25           */
/*                                                                    */
/**********************************************************************/
if exists (select * from sysobjects where id = object_id("up_PostTopic"))
   drop proc up_PostTopic
go

create proc up_PostTopic @a_intID int OUTPUT ,
       @a_intFatherID int , @a_intForumID int , @a_intUserID int ,
       @a_strTitle varchar(255) , @a_strContent text , @a_intFaceID tinyint ,
       @a_bIfEmail bit , @a_bIfSignature bit
   as
       declare @m_intTopicID int
       declare @m_intLayer   tinyint
       declare @m_intRootID  int
       declare @m_fOrderNum  float

       select @m_fOrderNum = power(2 , 30)      --初始化排序基数       数据挖掘研究院

       /*首先判断是否有这个论坛,没有则退出*/
       if not exists (select * from BBSCategory where CategoryID = @a_intForumID)
          begin
                select @a_intID = 0
                return(0)
          end
      
       /*判断是新发贴子还是回应主题*/
       if @a_intFatherID = 0                    --没有父贴子,说明是新发贴子
               select @m_intLayer = 1 , @m_intRootID = 0
       else 数据挖掘交友
          begin
               if not exists(select * from BBS where ID = @a_intFatherID)   --如果没发现父贴子
                  begin             
                       select "TopicID" = 0
               return (0)
                  end
               else                                --如果发现父贴子,则取出层数和根ID


                  select @m_intLayer = Layer + 1 ,@m_intRootID = RootID ,@m_fOrderNum = OrderNum 
                         from BBS where ID = @a_intFatherID
          end

       /*更新表,因为要对多个表操作,所以放到事务里*/
       begin transaction
            
             /*插入表BBS*/
             insert into BBS (FatherID , Layer , ForumID , UserID , Title ,
                              Content , PostTime , FaceID , Hits , selected ,
                              closed , IfEmail , IfSignature , OrderNum)
                         values(@a_intFatherID , @m_intLayer , @a_intForumID , @a_intUserID , @a_strTitle ,


                                @a_strContent , getdate() , @a_intFaceID , 0 , 0 ,
                                0 , @a_bIfEmail , @a_bIfSignature , default)
             if (@@error <> 0) goto On_Error       --如果出错转向错误处理部分
             select @m_intTopicID = @@identity     --取出刚刚插入纪录的ID

数据挖掘交友

             /*如果是新发贴子则取ID为RootID*/

             if @m_intRootID = 0                       --新发贴子
                begin
                     select @m_intRootID = @m_intTopicID
                end 数据挖掘交友

             else                                     --不是新发贴子则更新根纪录的TotalCounts
                begin
                     update BBS set TotalChilds = TotalChilds + 1  --更新根的子贴数
                            where  ID = @m_intRootID
                     if (@@error <> 0) goto On_Error       --如果更新失败则转向错误处理部分


                end

             select @m_fOrderNum = @m_fOrderNum + power(2,30)/power(2,TotalChilds)
                    from BBS where ID = @m_intRootID
             select @m_fOrderNum 数据挖掘工具


             /*更新RootID , OrderNum*/ 数据挖掘工具

             update BBS set OrderNum = @m_fOrderNum , RootID = @m_intRootID
                    where ID = @m_intTopicID
             if (@@error <> 0) goto On_Error       --如果更新失败则转向错误处理部分 数据挖掘交友

            /*更新BBSCategory表*/
            update BBSCategory set TopicCounts = TopicCounts + 1 , LastReplyTime = getDate()
                   where CategoryID = @a_intForumID
             if (@@error <>0) goto On_Error        --如果更新失败则转向错误处理部分

           /*更新BBSUser表,将用户分数加一*/
           update BBSUser set Point = Point + 1
                  where ID = @a_intUserID 数据挖掘实验室

       /*如果全部成功则完成事务*/
       commit transaction 数据挖掘实验室


       /*如果要求回复则发邮件*/
       declare @m_strEmail varchar(100) , @m_bIfEmail bit
       declare @m_strName  varchar(20) , @m_strSubject varchar(50)
       declare @m_strMessage varchar(255)
       select @m_bIfEmail = a.IfEmail , @m_strEmail = IsNull(b.Email , ""),
              @m_strName = b.UserName
              from BBS as a
                   left join BBSUser as b on a.UserID = b.ID
              where a.ID = @a_intFatherID
       select @m_strSubject = "来自eMatter Board : 您有回复"
       select @m_strMessage = "您发表在eMatter Board的贴子现在有人回复:" 数据挖掘研究院
                              + " http://server1/bbs/showtopic.asp?ID="
                              + convert(varchar,@a_intFatherID)
                                
       if @m_StrEmail <> ""  and @m_bIfEmail = 1
          exec master..xp_sendmail @recipients = @m_strEmail , @subject = @m_strSubject,
                                   @message = @m_strMessage 数据挖掘交友
      
       select @a_intID = @m_intTopicID                        --返回贴子ID
       return (0)
      
       On_error:                                  --错误处理部分
                rollback transaction
                select @a_intID = 0                          --贴子ID返回0,代表失败
                return (-1)


go

数据挖掘论坛

[数据挖掘专家] [数据挖掘研究院] [数据挖掘论坛] [数据挖掘实验室]
上一篇:RSA加解密算法源代码及程序说明...关于RSA给20万奖金的问题嘛~
下一篇:◆关于TCP/IP序列号生成方法的研究(一)
最新评论共有 0 位网友发表了评论 , 查看所有评论
发表评论( 不能超过250字,需审核,请自觉遵守互联网相关政策法规。 )
匿名?
数据挖掘网站导航 数据挖掘论坛导航
  • 数据挖掘工具
  • 数据挖掘论坛
  • DataCruncher - Cognos
  • MineSet - MathSoft
  • Intelligent Miner - GainSmarts
  • Sqlserver - SAS - Clementine
  • CART - Weka - WizSoft
  • NeuroShell - ModelQuest
  • data mining tools - Darwin
  • 数据挖掘交友
  • 数据挖掘博客
  • 数据挖掘工具
  • 数据挖掘资源
  • 数据挖掘技术算法
  • 数据挖掘相关期刊、会议
  • 研究院联盟合作专区
  • 数据挖掘基础与相关技术
  • 数据挖掘厂商与就业
  • 数据挖掘研究者乐园
  • 知名厂商数据挖掘工具资料
  • 国内数据挖掘实验室
  • Foreign Data Mining Lab
  • 热点关注
  • Internet控制信息协议(ICMP)
  • 微软公司软件开发模式简介
  • http1.1
  • TCP协议规范(中文版)
  • linux端口列表
  • 语音识别进入IVR系统
  • Api函数列表——与文件相关
  • RVP:存在和即时消息传送协议(3)
  • Win32环境下动态链接库(DLL)编程原理
  • PPPInternet协议控制协议(中文版)
  • 论坛最新话题
  • Foundations of Statistical Natural Langu
  • Game Theory meet Data Mining: A Recent P
  • System Building: How does it help or hin
  • 数据挖掘与Clementine培训
  • 新手报到
  • 求 SASEM 客户流失预测分析
  • 数据挖掘工程师/搜索研究院—北京——无线
  • 数据挖掘入门介绍(如何着手数据挖掘)
  • Information Overload Survey Results
  • The INEX 2005 Workshop on Element Retrie
  • 相关资讯
  • Internet控制信息协议(ICMP)
  • 中文RFC文档远程COM选项(四)
  • Api函数列表——与文件相关
  • RVP:存在和即时消息传送协议(3)
  • 微软公司软件开发模式简介
  • MMXInstructions
  • TCP协议规范(中文版)
  • PPPInternet协议控制协议(中文版)
  • 语音识别进入IVR系统
  • http1.1
  • 数据挖掘实验室资料
  • 数据挖掘博客地址
  • 数据挖掘实验室网站地址
  • Prepare for Medicare audits by using dat
  • 注册成为SAS用户与爱好者俱乐部会员
  • 水南梅
  • 明日烟
  • 新人报道
  • 下载
  • 厦门服务器托管,450元/月—0592-5177319 高
  • 买空间送域名--0592-5177319 高静