블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Category

Recent Post

Recent Comment

Archive

2009. 10. 27. 02:25 카테고리 없음
반응형

목표 : 답변형(Reply) 게시판 만들기
          - WebReply 솔루션
          - Website : 웹 사이트
          - Website/Bin/
  
 Reply.Dsl.dll
 Reply.Bsl.dll
 Reply.Entity.dll
 Reply.Common.dll

   - Library : 클래스 라이브러리
   - Reply.Dsl : 클래스 라이브러리
   - Reply.Bsl : 클래스 라이브러리
   - Reply.Entity : 클래스 라이브러리
   - Reply.Common : 클래스 라이브러리 

 --------------------------------------------------------
--답변형 게시판 응용 프로그램
--작성날짜 : 2009년 10월 14일
--작성자 : 김용원
--------------------------------------------------------

-- 답변형 게시판(Reply)용 테이블 설계
Create Table dbo.Reply
(
   Num Int Identity(1,1) Not Null Primary Key, --번호
   Name VarChar(25) Not Null, --이름
   Email VarChar(100) Null, --이메일
   Title VarChar(150) Not Null, --제목
   PostDate DateTime Default GetDate() Not Null, --작성일
   PostIP VarChar(15) Not Null, --작성IP
   Content Text Not Null,  -- 내용
   Password VarChar(20) Not Null, --비밀번호
   ReadCount Int Default 0,  --조회수
   Encoding VarChar(10) Not Null, --인코딩(text/html)
   Homepage VarChar(100) Null, --홈페이지
   ModifyDate DateTime Null,  --수정일
   ModifyIP VarChar(15) Null, --수정IP
 ---------------------------
   Ref Int Not Null, --참조(부모글)
   Step Int Default 0, --답변길이(레벨)
   RefOrder Int Default 0 --답변순서 
)
Go

--글을 작성하는 저장 프로시저 만들기 : WriteReply
Create Proc dbo.WriteReply
   @Name VarChar(25),
   @Email VarChar(100),
   @Title VarChar(150),
   @PostIP VarChar(15),
   @Content Text,
   @Password VarChar(20),
   @Encoding Varchar(10),
   @Homepage VarChar(100)
 
AS
   Declare @MaxRef Int
   Select @MaxRef = Max(Ref) From Reply
 
 If @MaxRef is Null
    Set @MaxRef = 1
 Else
    Set @MaxRef = @MaxRef + 1 

 Insert Reply
 (
    Name, Email, Title, PostIP, Content,
    Password, Encoding, Homepage, Ref 
 )
 Values
 (
    @Name, @Email, @Title, @PostIP, @Content,
    @Password, @Encoding, @Homepage, @MaxRef 
 )

Go 

--데이터를 읽어오는 저장 프로시저 : ListReply
Create Procedure dbo.ListReply
As
 Select *From Reply Order By Ref Desc, RefOrder Asc
Go

--해당 글을 세부적으로 읽어오는 저장 프로시저 만들기 : ViewReply
Create Procedure dbo.ViewReply
   @Num Int
As
   Update Reply Set ReadCount = ReadCount + 1
   Where Num = @Num
 
   Select *From Reply Where Num = @Num

Go

--해당 글을 수정하는 저장 프로시저 : ModifyReply
Create Procedure dbo.ModifyReply

   @Name VarChar(25),
   @Email VarChar(100),
   @Title Varchar(150),
   @ModifyIP VarChar(15),
   @Content Text,
   @Encoding Varchar(10),
   @Homepage VarChar(100),
   @Password VarChar(20),
   @Num Int
As
   Declare @cnt Int
   Select @cnt = COUNT(*) From Reply
   Where Num = @Num And Password = @Password
 
 If @cnt > 0 --넘겨준 번호와 암호가 맞는 데이터가 있다면
  Update Reply
  Set
    Name = @Name, Email=@Email,
    Title = @Title, ModifyIP = @ModifyIP,
    ModifyDate = Getdate(), Content = @Content,
    Encoding = @Encoding, Homepage = @Homepage
   Where Num = @Num And Password = @Password
 Else
   Return -1 --암호가 틀리면 -1을 반환하자
Go 

--해당 글을 지우는 저장 프로시저 : DeleteReply
Create Procedure dbo.DeleteReply
   @Password VarChar(20),
   @Num Int
As
 Declare @cnt Int 
 
--암호와 번호가 맞으면 1을 반환
 Select @cnt = COUNT(*) From Reply
 Where Num = @Num And Password = @Password
 
 If @cnt > 0
    Delete Reply Where Num = @Num And Password = @Password
 Else
    Return -1 --삭제가 되지 않으면 -1반환
Go

--검색 저장 프로시저 : 정적 쿼리문
Create Procedure dbo.SearchReply
   @SearchField VarChar(25),
   @SearchQuery VarChar(25)
As
   Set @SearchQuery = '%' + @SearchQuery + '%'
   Select * From Reply
 Where
 (
    Case @SearchField
    When 'Name' Then Name
    When 'Content' Then Content
    When 'Title' Then Title
  Else
    @SearchQuery
  End   
 )
 Like @SearchQuery Order By Num Desc
Go

--글을 답변하는 저장 프로시저 : ReplyReply
Create Procedure dbo.ReplyReply
   @Name VarChar(25),
   @Email VarChar(100),
   @Title VarChar(150),
   @PostIP VarChar(15),
   @Content Text,
   @Password VarChar(20),
   @Encoding VarChar(10),
   @Homepage VarChar(100),
   @ParentNumber Int --부모글의 Number 필드값 받기 -> Ref, Step, RefOrder 
As
   Declare @ParentRef Int
   Declare @ParentStep Int
   Declare @ParentRefOrder Int
 
 Select @ParentRef = Ref, @ParentStep = Step,
   @ParentRefOrder = RefOrder From Reply
   Where Num = @ParentNumber
 Begin Tran 
 
--비집고 들어갈 자리 만들기
  Update Reply
    Set RefOrder = RefOrder + 1
  Where 
     Ref = @ParentRef
     And
   RefOrder > @ParentRefOrder
  
-- 부모글보다 Step을 1증가, 보여지는 순서도 1증가  
   Insert Reply
   (
    Name, Email, Title, PostIP, Content, Password,
    Encoding, Homepage, Ref, Step, RefOrder
   )
   Values
   (
    @Name, @Email, @Title, @PostIP, @Content, @Password,
    @Encoding, @Homepage, @ParentRef, @ParentStep + 1, @ParentRefOrder + 1
   ) 
 Commit Tran 
Go

----[0] 답변형 게시판 쿼리문 연습
----Drop Table dbo.ReplyTest
Create Table dbo.ReplyTest
(
   Num Int Identity(1, 1) Primary Key Not Null, -- 번호
   Title VarChar(150) Not Null, -- 제목

   Ref Int Default 0, --참조글(부모글;최상위글;답변이아닌글;그룹번호;Group)
   Step Int Default 0, --들여쓰기(한단계 답변:한단계들여쓰기;Level;Depth)
   RefOrder Int Default 0 --같은 그룹내에서의 정렬순서(Position)
)
Go

--[1] 처음으로 게시판 글쓰기
Insert Into ReplyTest(Title, Ref)
Values('첫번째 부모글', 1)

--[2] 새로운 글 입력 : Write.aspx.cs
Begin
 Declare @MaxRef Int
 Select @MaxRef = Max(Ref) From ReplyTest

 Insert Into ReplyTest(Title, Ref)
 Values('두번째 부모글', @MaxRef + 1)
End

--[3] 첫번째 부모글에 한단계 답변
Insert ReplyTest(Title, Ref, Step, RefOrder)
Values('>>첫번째 부모글에 답변', 1, 0+1, 0+1) 

--[4] 첫번째 부모글에 답변의 답변 : [3]번글의 답변
Insert ReplyTest(Title, Ref, Step, RefOrder)
Values('>>>>첫번째 부모글에 답변의 답변', 1, 2, 2) 

--[5] 두번째 부모글에 답변 : [2]번글의 답변
Insert ReplyTest(Title, Ref, Step, RefOrder)
Values('>>두번째 부모글에 답변', 2, 1, 1) 

--[6] 첫번째 부모글에 한단계 답변(나중에) :  [1]번 글에 답변
Update ReplyTest Set RefOrder = RefOrder + 1
Where Ref = 1 And RefOrder > 0 -- 부모글의 RefOrder(0)보다 큰 레코드 모드 업데이트
Insert ReplyTest(Title, Ref, Step, RefOrder)
Values('>>첫번째 부모글에 답변(나중에)', 1, 0+1, 0+1) 

--[7] [6]번 레코드에 답변
Update ReplyTest Set RefOrder = RefOrder + 1
Where Ref = 1 And RefOrder > 1 -- 부모글의 RefOrder(0)보다 큰 레코드 모드 업데이트
Insert ReplyTest(Title, Ref, Step, RefOrder)
Values('>>>>첫번째 부모글에 답변(나중에)의 답변', 1, 1+1, 1+1) 

--[!] 데이터 출력
Select * From ReplyTest Order By Ref Desc, RefOrder Asc

-- 저장 프로시저화
--[1] 입력 저장 프로시저
Create Procedure dbo.WriteReplyTest
 @Title VarChar(150)
As 

 Declare @MaxRef Int
 Select @MaxRef = Max(Ref) From ReplyTest
 If @MaxRef > 0
  Begin
    Insert Into ReplyTest(Title, Ref)
    Values(@Title, @MaxRef + 1)
  End
 Else --제일 첫번째 레코드 입력할때만 실행
  Begin
   Insert Into ReplyTest(Title, Ref)
   Values(@Title, 1)
  End
Go

--WriteReplyTest '첫번째 부모글'
--WriteReplyTest '두번째 부모글'

--[2] 출력 저장 프로시저
Create Proc dbo.ListReplyTest
As
 Select * From ReplyTest
 Order By Ref Desc, RefOrder Asc
Go

--ListReplyTest
--[3] 답변 저장 프로시저
--Drop Proc dbo.ReplyReplyTest
Create Proc dbo.ReplyReplyTest
   @Title VarChar(150),
   @ParentRef Int,   -- 부모글의 Ref
   @ParentStep Int,  -- 부모글의 Step
   @ParentRefOrder Int -- 부모글의 RefOrder
As
 Begin Tran
  Update ReplyTest
  Set RefOrder = RefOrder + 1
  Where
   Ref = @ParentRef
   And
   RefOrder > @ParentRefOrder

  Insert ReplyTest(Title, Ref, Step, RefOrder)
  Values(@Title, @ParentRef, @ParentStep+1, @ParentRefOrder + 1) 
 Commit Tran
Go







반응형
posted by Magic_kit