看了http://www.cnblogs.com/Heroman/archive/2005/01/20/94806.html后我感觉有必要介绍一下CodeSmith,通常追求挑战的程序员认为设计、开发都是充满乐趣,但重复的开发相同的底层工作却是无味的,我接触过也试用过很多代码生成软件,但不是生成出的代码太复杂,就是有些方面不合自己的胃口,尤其是针对Web开发体会更是如此,通常的代码生成器生成的代码直接支持net.remoting,并且均是对dataset直接操作的,而在通常的小规模的web开发中net.reromting不一定用,而直接对dataset操作要求在session来保存临时编辑的数据,而通常对于web开发来说只要一条一条的对数据库编辑,还有,web开发通常都要有分页设计功能,而自动生成的代码也不是尽如人意。所以我一直寻求一种全方位的自定义的生成工具,哪怕初期开发复杂些,但只要开发一次,以后就可以生成合自己胃口的代码。后来一不小心发现了codesmith终于发现他正是我想要的模板生成工具,以下就以下面这个最典型的数据库结构向大家展示一下我作的模板生成的代码,来说明codesmith能达到的功能。 另外可以在“生成工具-CodeSmith-享受编程的乐趣(三)”中可以下载我写的模板
这个数据库中是面对标准的文章发布功能的主要包括两个表一个是articleClass 另一个是article,在aritclass中是一个标准的树状结构(parentid 和id有一个自联接)而articlass中有一个对于ArticleClassID的外键引用。图中除了parentID字段以外均不能为空,这样开发起来方便些:)
对于数据库的操作通常都是用多层来实现的,本篇四层为例,但目前我只作了下三层的模板,因为界面层变化因素太多通常针对不同的项目有不同的要求,大家可以通常这里介绍的方式根据具体项目来自己实现界面层。
第一层数据库层,最通常的工作就是作一些存贮过程供下一层调用
生成的代码如下:
由于我通常作的开发都是基于Web的所以同时整合了分页操作,另外针对树状结构专门提供了一些贮存过程和函数。
这个数据库中是面对标准的文章发布功能的主要包括两个表一个是articleClass 另一个是article,在aritclass中是一个标准的树状结构(parentid 和id有一个自联接)而articlass中有一个对于ArticleClassID的外键引用。图中除了parentID字段以外均不能为空,这样开发起来方便些:)
对于数据库的操作通常都是用多层来实现的,本篇四层为例,但目前我只作了下三层的模板,因为界面层变化因素太多通常针对不同的项目有不同的要求,大家可以通常这里介绍的方式根据具体项目来自己实现界面层。
第一层数据库层,最通常的工作就是作一些存贮过程供下一层调用
生成的代码如下:
由于我通常作的开发都是基于Web的所以同时整合了分页操作,另外针对树状结构专门提供了一些贮存过程和函数。
------------------------------------------------------------------------------------------------------------------------
-- the sprocs returns various error/success codes
-- a return value of 0 means success
-- a return value of 1 means a dup A_ArticleClass
-- a return value of 2 means A_ArticleClass doesn't exist
------------------------------------------------------------------------------------------------------------------------
/****** Object: Stored Procedure [dbo].A_ArticleClass_InsertDeleteUpdate Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_InsertDeleteUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_InsertDeleteUpdate]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_InsertDeleteUpdate]
(
@ID uniqueidentifier = null,
@ParentID uniqueidentifier = null,
@Name nvarchar(128) = null,
@Description ntext = null,
@OrderBy int = null,
@ImgUrl nvarchar(128) = null,
@IconUrl nvarchar(128) = null,
@Action int
)
AS
BEGIN
IF @Action = 0
-- Insert
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
)
RETURN 1
IF EXISTS(SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ParentID = @ParentID) And (Name = @Name)
)
RETURN 1
INSERT INTO [dbo].[A_ArticleClass](ID, ParentID, Name, Description, OrderBy, ImgUrl, IconUrl)
VALUES (@ID, @ParentID, @Name, @Description, @OrderBy, @ImgUrl, @IconUrl)
SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
RETURN
END
IF @Action = 1
-- Delete
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
)
BEGIN
Delete
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
IF @Action = 2
-- Update
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
)
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ParentID = @ParentID) And (Name = @Name)
AND (NOT
((ID = @ID))
))
RETURN 1
UPDATE [dbo].[A_ArticleClass]
SET
ParentID = @ParentID, Name = @Name, Description = @Description, OrderBy = @OrderBy, ImgUrl = @ImgUrl, IconUrl = @IconUrl
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetPageData Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetPageData]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetPageData]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetPageData]
(
@FieldsName nvarchar(512) = 'A.*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL,
@RecordCount int = 0 output ,
@PageIndex int = 0 output,
@PageSize int = 10
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = 'A.*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
CREATE TABLE #Pager
(
ID uniqueidentifier,
IndexID int IDENTITY (1, 1) NOT NULL
)
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'INSERT INTO #Pager (ID)'
+ ' SELECT ID'
+ ' FROM [dbo].[A_ArticleClass]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC(@SelectSql)
SELECT @RecordCount = COUNT(*) FROM #Pager
IF(@RecordCount < (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @RecordCount / @PageSize
END
IF(@RecordCount = (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @PageIndex - 1
IF @PageIndex < 0
BEGIN
SET @PageIndex = 0
END
END
DECLARE @PageUpperBound int
DECLARE @PageLowerBound int
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
SET @SelectSql = 'SELECT '+ @FieldsName
+ ' FROM [dbo].[A_ArticleClass] AS A INNER JOIN #Pager AS B'
+ ' ON A.ID = B.ID '
+ ' WHERE'
+ ' B.IndexID > ' + RTRIM(LTRIM(CAST(@PageLowerBound AS varchar(128))))
+ ' AND B.IndexID < ' + RTRIM(LTRIM(CAST(@PageUpperBound AS varchar(128))))
+ ' ORDER BY B.IndexID'
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetAll Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetAll]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetAll]
(
@FieldsName nvarchar(512) = '*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = '*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'SELECT ' + @FieldsName
+ ' FROM [dbo].[A_ArticleClass]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetLevel Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetLevel')
drop function [dbo].[A_ArticleClass_GetLevel]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetLevel]
(
@ID uniqueidentifier
)
RETURNS int
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ReturnValue int
SELECT @ReturnValue = -1
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ReturnValue = @ReturnValue + 1
SELECT @ParentID = ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetMaxLevel Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetMaxLevel')
drop function [dbo].[A_ArticleClass_GetMaxLevel]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetMaxLevel]
(
)
RETURNS int
AS
BEGIN
DECLARE @ReturnValue int
SELECT @ReturnValue = Max([dbo].A_ArticleClass_GetLevel(ID))
from [dbo].A_ArticleClass
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetFullName Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetFullName')
drop function [dbo].[A_ArticleClass_GetFullName]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetFullName]
(
@ID uniqueidentifier
)
RETURNS nvarchar(4000)
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ReturnValue nvarchar(4000)
SELECT @ReturnValue = ''
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ReturnValue = '/' + isnull([Name],'No Name') + @ReturnValue
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetTopID Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetTopID')
drop function [dbo].[A_ArticleClass_GetTopID]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetTopID]
(
@ID uniqueidentifier
)
RETURNS uniqueidentifier
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ResultID uniqueidentifier
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ResultID = @ParentID
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ResultID
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetFullID Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetFullID')
drop function [dbo].[A_ArticleClass_GetFullID]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetFullID]
(
@ID uniqueidentifier
)
RETURNS nvarchar(4000)
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ReturnValue nvarchar(4000)
SELECT @ReturnValue = ''
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ReturnValue = '/' + Cast([ID]as varchar(128)) + @ReturnValue
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetChildren Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetChildren]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetChildren]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetChildren]
(
@ID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier,
[Level] int
)
insert into #TmpPaths(ID,[Level])
select ID,[dbo].A_ArticleClass_GetLevel(ID)
from [dbo].[A_ArticleClass]
where (ID = @ID)
while(exists(
select * from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
)
)
begin
insert into #TmpPaths(ID,[Level])
select ID,[dbo].A_ArticleClass_GetLevel(ID)
from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
end
delete #TmpPaths
where ID = @ID
select A.*, B.Level
from [dbo].[A_ArticleClass] A inner join #TmpPaths B
on A.ID = B.ID
order by B.Level desc
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetNonChildren Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetNonChildren]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetNonChildren]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetNonChildren]
(
@ID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier
)
insert into #TmpPaths(ID)
select ID
from [dbo].[A_ArticleClass]
where (ID = @ID)
while(exists(
select * from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
)
)
begin
insert into #TmpPaths(ID)
select ID
from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
end
Select * from [dbo].[A_ArticleClass]
where ID not in (select ID from #TmpPaths)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetParents Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetParents]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetParents]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetParents]
(
@ID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier,
[Level] int
)
DECLARE @ParentID uniqueidentifier
SELECT @ParentID = ParentID
from [dbo].[A_ArticleClass]
where (ID = @ID)
WHILE (not (@ParentID is null))
BEGIN
insert into #TmpPaths(ID,[Level])
select ID,[dbo].A_ArticleClass_GetLevel(ID)
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
select A.*, B.Level
from [dbo].[A_ArticleClass] A inner join #TmpPaths B
on A.ID = B.ID
order by B.Level desc
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_MoveTo Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_MoveTo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_MoveTo]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_MoveTo]
(
@ID uniqueidentifier,
@NewParentID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @NewParentID))
BEGIN
RETURN 2
END
Update [dbo].A_ArticleClass
Set ParentID = @NewParentID
where (ID = @ID)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_CopyChildren Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_CopyChildren]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_CopyChildren]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_CopyChildren]
(
@ID uniqueidentifier,
@FromID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @FromID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier,
ParentID uniqueidentifier null,
NID uniqueidentifier,
NParentID uniqueidentifier null
)
insert into #TmpPaths(ID,ParentID,NID, NParentID)
select ID,ParentID, @ID, null
from [dbo].[A_ArticleClass]
where (ID = @FromID)
while(exists(
select * from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
)
)
begin
insert into #TmpPaths(ID,ParentID,NID, NParentID)
select ID,ParentID, NewID(), null
from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
end
update #TmpPaths
set #TmpPaths.NParentID = b.NID
from #TmpPaths, #tmpPaths b
where #TmpPaths.ParentID = b.ID
delete #TmpPaths
where ID = @FromID
insert into [dbo].[A_ArticleClass](Name,Description,OrderBy,ImgUrl,IconUrl,ParentID,ID)
select B.Name,B.Description,B.OrderBy,B.ImgUrl,B.IconUrl,a.NParentID, a.NID
from #TmpPaths a inner join [dbo].[A_ArticleClass] b
on a.ID = b.ID
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
------------------------------------------------------------------------------------------------------------------------
-- the sprocs returns various error/success codes
-- a return value of 0 means success
-- a return value of 1 means a dup A_Article
-- a return value of 2 means A_Article doesn't exist
------------------------------------------------------------------------------------------------------------------------
/****** Object: Stored Procedure [dbo].A_Article_InsertDeleteUpdate Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_Article_InsertDeleteUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_Article_InsertDeleteUpdate]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_Article_InsertDeleteUpdate]
(
@ID uniqueidentifier = null,
@ArticleClassID uniqueidentifier = null,
@Title nvarchar(256) = null,
@Content ntext = null,
@IsNew bit = null,
@IsTop bit = null,
@OrderBy int = null,
@ImgUrl nvarchar(128) = null,
@CreateTime datetime = null,
@UserName nvarchar(64) = null,
@Author nvarchar(16) = null,
@Keywords nvarchar(256) = null,
@Action int
)
AS
BEGIN
IF @Action = 0
-- Insert
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
)
RETURN 1
IF EXISTS(SELECT *
FROM [dbo].[A_Article]
WHERE (ArticleClassID = @ArticleClassID) And (Title = @Title)
)
RETURN 1
INSERT INTO [dbo].[A_Article](ID, ArticleClassID, Title, Content, IsNew, IsTop, OrderBy, ImgUrl, CreateTime, UserName, Author, Keywords)
VALUES (@ID, @ArticleClassID, @Title, @Content, @IsNew, @IsTop, @OrderBy, @ImgUrl, @CreateTime, @UserName, @Author, @Keywords)
SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
RETURN
END
IF @Action = 1
-- Delete
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
)
BEGIN
Delete
FROM [dbo].[A_Article]
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
IF @Action = 2
-- Update
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
)
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_Article]
WHERE (ArticleClassID = @ArticleClassID) And (Title = @Title)
AND (NOT
((ID = @ID))
))
RETURN 1
UPDATE [dbo].[A_Article]
SET
ArticleClassID = @ArticleClassID, Title = @Title, Content = @Content, IsNew = @IsNew, IsTop = @IsTop, OrderBy = @OrderBy, ImgUrl = @ImgUrl, CreateTime = @CreateTime, UserName = @UserName, Author = @Author, Keywords = @Keywords
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_Article_GetPageData Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_Article_GetPageData]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_Article_GetPageData]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_Article_GetPageData]
(
@FieldsName nvarchar(512) = 'A.*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL,
@RecordCount int = 0 output ,
@PageIndex int = 0 output,
@PageSize int = 10
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = 'A.*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
CREATE TABLE #Pager
(
ID uniqueidentifier,
IndexID int IDENTITY (1, 1) NOT NULL
)
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'INSERT INTO #Pager (ID)'
+ ' SELECT ID'
+ ' FROM [dbo].[A_Article]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC(@SelectSql)
SELECT @RecordCount = COUNT(*) FROM #Pager
IF(@RecordCount < (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @RecordCount / @PageSize
END
IF(@RecordCount = (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @PageIndex - 1
IF @PageIndex < 0
BEGIN
SET @PageIndex = 0
END
END
DECLARE @PageUpperBound int
DECLARE @PageLowerBound int
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
SET @SelectSql = 'SELECT '+ @FieldsName
+ ' FROM [dbo].[A_Article] AS A INNER JOIN #Pager AS B'
+ ' ON A.ID = B.ID '
+ ' WHERE'
+ ' B.IndexID > ' + RTRIM(LTRIM(CAST(@PageLowerBound AS varchar(128))))
+ ' AND B.IndexID < ' + RTRIM(LTRIM(CAST(@PageUpperBound AS varchar(128))))
+ ' ORDER BY B.IndexID'
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_Article_GetAll Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_Article_GetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_Article_GetAll]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_Article_GetAll]
(
@FieldsName nvarchar(512) = '*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = '*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'SELECT ' + @FieldsName
+ ' FROM [dbo].[A_Article]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
-- the sprocs returns various error/success codes
-- a return value of 0 means success
-- a return value of 1 means a dup A_ArticleClass
-- a return value of 2 means A_ArticleClass doesn't exist
------------------------------------------------------------------------------------------------------------------------
/****** Object: Stored Procedure [dbo].A_ArticleClass_InsertDeleteUpdate Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_InsertDeleteUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_InsertDeleteUpdate]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_InsertDeleteUpdate]
(
@ID uniqueidentifier = null,
@ParentID uniqueidentifier = null,
@Name nvarchar(128) = null,
@Description ntext = null,
@OrderBy int = null,
@ImgUrl nvarchar(128) = null,
@IconUrl nvarchar(128) = null,
@Action int
)
AS
BEGIN
IF @Action = 0
-- Insert
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
)
RETURN 1
IF EXISTS(SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ParentID = @ParentID) And (Name = @Name)
)
RETURN 1
INSERT INTO [dbo].[A_ArticleClass](ID, ParentID, Name, Description, OrderBy, ImgUrl, IconUrl)
VALUES (@ID, @ParentID, @Name, @Description, @OrderBy, @ImgUrl, @IconUrl)
SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
RETURN
END
IF @Action = 1
-- Delete
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
)
BEGIN
Delete
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
IF @Action = 2
-- Update
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ID)
)
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_ArticleClass]
WHERE (ParentID = @ParentID) And (Name = @Name)
AND (NOT
((ID = @ID))
))
RETURN 1
UPDATE [dbo].[A_ArticleClass]
SET
ParentID = @ParentID, Name = @Name, Description = @Description, OrderBy = @OrderBy, ImgUrl = @ImgUrl, IconUrl = @IconUrl
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetPageData Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetPageData]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetPageData]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetPageData]
(
@FieldsName nvarchar(512) = 'A.*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL,
@RecordCount int = 0 output ,
@PageIndex int = 0 output,
@PageSize int = 10
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = 'A.*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
CREATE TABLE #Pager
(
ID uniqueidentifier,
IndexID int IDENTITY (1, 1) NOT NULL
)
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'INSERT INTO #Pager (ID)'
+ ' SELECT ID'
+ ' FROM [dbo].[A_ArticleClass]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC(@SelectSql)
SELECT @RecordCount = COUNT(*) FROM #Pager
IF(@RecordCount < (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @RecordCount / @PageSize
END
IF(@RecordCount = (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @PageIndex - 1
IF @PageIndex < 0
BEGIN
SET @PageIndex = 0
END
END
DECLARE @PageUpperBound int
DECLARE @PageLowerBound int
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
SET @SelectSql = 'SELECT '+ @FieldsName
+ ' FROM [dbo].[A_ArticleClass] AS A INNER JOIN #Pager AS B'
+ ' ON A.ID = B.ID '
+ ' WHERE'
+ ' B.IndexID > ' + RTRIM(LTRIM(CAST(@PageLowerBound AS varchar(128))))
+ ' AND B.IndexID < ' + RTRIM(LTRIM(CAST(@PageUpperBound AS varchar(128))))
+ ' ORDER BY B.IndexID'
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetAll Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetAll]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetAll]
(
@FieldsName nvarchar(512) = '*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = '*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'SELECT ' + @FieldsName
+ ' FROM [dbo].[A_ArticleClass]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetLevel Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetLevel')
drop function [dbo].[A_ArticleClass_GetLevel]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetLevel]
(
@ID uniqueidentifier
)
RETURNS int
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ReturnValue int
SELECT @ReturnValue = -1
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ReturnValue = @ReturnValue + 1
SELECT @ParentID = ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetMaxLevel Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetMaxLevel')
drop function [dbo].[A_ArticleClass_GetMaxLevel]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetMaxLevel]
(
)
RETURNS int
AS
BEGIN
DECLARE @ReturnValue int
SELECT @ReturnValue = Max([dbo].A_ArticleClass_GetLevel(ID))
from [dbo].A_ArticleClass
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetFullName Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetFullName')
drop function [dbo].[A_ArticleClass_GetFullName]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetFullName]
(
@ID uniqueidentifier
)
RETURNS nvarchar(4000)
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ReturnValue nvarchar(4000)
SELECT @ReturnValue = ''
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ReturnValue = '/' + isnull([Name],'No Name') + @ReturnValue
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetTopID Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetTopID')
drop function [dbo].[A_ArticleClass_GetTopID]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetTopID]
(
@ID uniqueidentifier
)
RETURNS uniqueidentifier
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ResultID uniqueidentifier
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ResultID = @ParentID
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ResultID
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetFullID Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where type = 'FN' AND name = 'A_ArticleClass_GetFullID')
drop function [dbo].[A_ArticleClass_GetFullID]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[A_ArticleClass_GetFullID]
(
@ID uniqueidentifier
)
RETURNS nvarchar(4000)
AS
BEGIN
DECLARE @ParentID uniqueidentifier
DECLARE @ReturnValue nvarchar(4000)
SELECT @ReturnValue = ''
SELECT @ParentID = @ID
WHILE (not (@ParentID is null))
BEGIN
SELECT @ReturnValue = '/' + Cast([ID]as varchar(128)) + @ReturnValue
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
RETURN @ReturnValue
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetChildren Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetChildren]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetChildren]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetChildren]
(
@ID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier,
[Level] int
)
insert into #TmpPaths(ID,[Level])
select ID,[dbo].A_ArticleClass_GetLevel(ID)
from [dbo].[A_ArticleClass]
where (ID = @ID)
while(exists(
select * from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
)
)
begin
insert into #TmpPaths(ID,[Level])
select ID,[dbo].A_ArticleClass_GetLevel(ID)
from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
end
delete #TmpPaths
where ID = @ID
select A.*, B.Level
from [dbo].[A_ArticleClass] A inner join #TmpPaths B
on A.ID = B.ID
order by B.Level desc
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetNonChildren Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetNonChildren]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetNonChildren]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetNonChildren]
(
@ID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier
)
insert into #TmpPaths(ID)
select ID
from [dbo].[A_ArticleClass]
where (ID = @ID)
while(exists(
select * from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
)
)
begin
insert into #TmpPaths(ID)
select ID
from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
end
Select * from [dbo].[A_ArticleClass]
where ID not in (select ID from #TmpPaths)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_GetParents Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_GetParents]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_GetParents]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_GetParents]
(
@ID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier,
[Level] int
)
DECLARE @ParentID uniqueidentifier
SELECT @ParentID = ParentID
from [dbo].[A_ArticleClass]
where (ID = @ID)
WHILE (not (@ParentID is null))
BEGIN
insert into #TmpPaths(ID,[Level])
select ID,[dbo].A_ArticleClass_GetLevel(ID)
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
SELECT @ParentID=ParentID
FROM [dbo].[A_ArticleClass]
WHERE (ID = @ParentID)
END
select A.*, B.Level
from [dbo].[A_ArticleClass] A inner join #TmpPaths B
on A.ID = B.ID
order by B.Level desc
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_MoveTo Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_MoveTo]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_MoveTo]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_MoveTo]
(
@ID uniqueidentifier,
@NewParentID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @NewParentID))
BEGIN
RETURN 2
END
Update [dbo].A_ArticleClass
Set ParentID = @NewParentID
where (ID = @ID)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_ArticleClass_CopyChildren Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_ArticleClass_CopyChildren]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_ArticleClass_CopyChildren]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_ArticleClass_CopyChildren]
(
@ID uniqueidentifier,
@FromID uniqueidentifier
)
AS
BEGIN
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @ID))
BEGIN
RETURN 2
END
IF NOT exists(SELECT * FROM [dbo].A_ArticleClass WHERE (ID = @FromID))
BEGIN
RETURN 2
END
CREATE TABLE #TmpPaths
(
ID uniqueidentifier,
ParentID uniqueidentifier null,
NID uniqueidentifier,
NParentID uniqueidentifier null
)
insert into #TmpPaths(ID,ParentID,NID, NParentID)
select ID,ParentID, @ID, null
from [dbo].[A_ArticleClass]
where (ID = @FromID)
while(exists(
select * from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
)
)
begin
insert into #TmpPaths(ID,ParentID,NID, NParentID)
select ID,ParentID, NewID(), null
from [dbo].[A_ArticleClass]
where ParentID in (select ID from #TmpPaths)
and ID not in(select ID from #TmpPaths)
end
update #TmpPaths
set #TmpPaths.NParentID = b.NID
from #TmpPaths, #tmpPaths b
where #TmpPaths.ParentID = b.ID
delete #TmpPaths
where ID = @FromID
insert into [dbo].[A_ArticleClass](Name,Description,OrderBy,ImgUrl,IconUrl,ParentID,ID)
select B.Name,B.Description,B.OrderBy,B.ImgUrl,B.IconUrl,a.NParentID, a.NID
from #TmpPaths a inner join [dbo].[A_ArticleClass] b
on a.ID = b.ID
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
------------------------------------------------------------------------------------------------------------------------
-- the sprocs returns various error/success codes
-- a return value of 0 means success
-- a return value of 1 means a dup A_Article
-- a return value of 2 means A_Article doesn't exist
------------------------------------------------------------------------------------------------------------------------
/****** Object: Stored Procedure [dbo].A_Article_InsertDeleteUpdate Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_Article_InsertDeleteUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_Article_InsertDeleteUpdate]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_Article_InsertDeleteUpdate]
(
@ID uniqueidentifier = null,
@ArticleClassID uniqueidentifier = null,
@Title nvarchar(256) = null,
@Content ntext = null,
@IsNew bit = null,
@IsTop bit = null,
@OrderBy int = null,
@ImgUrl nvarchar(128) = null,
@CreateTime datetime = null,
@UserName nvarchar(64) = null,
@Author nvarchar(16) = null,
@Keywords nvarchar(256) = null,
@Action int
)
AS
BEGIN
IF @Action = 0
-- Insert
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
)
RETURN 1
IF EXISTS(SELECT *
FROM [dbo].[A_Article]
WHERE (ArticleClassID = @ArticleClassID) And (Title = @Title)
)
RETURN 1
INSERT INTO [dbo].[A_Article](ID, ArticleClassID, Title, Content, IsNew, IsTop, OrderBy, ImgUrl, CreateTime, UserName, Author, Keywords)
VALUES (@ID, @ArticleClassID, @Title, @Content, @IsNew, @IsTop, @OrderBy, @ImgUrl, @CreateTime, @UserName, @Author, @Keywords)
SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
RETURN
END
IF @Action = 1
-- Delete
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
)
BEGIN
Delete
FROM [dbo].[A_Article]
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
IF @Action = 2
-- Update
BEGIN
IF EXISTS(
SELECT *
FROM [dbo].[A_Article]
WHERE (ID = @ID)
)
BEGIN
IF EXISTS(SELECT *
FROM [dbo].[A_Article]
WHERE (ArticleClassID = @ArticleClassID) And (Title = @Title)
AND (NOT
((ID = @ID))
))
RETURN 1
UPDATE [dbo].[A_Article]
SET
ArticleClassID = @ArticleClassID, Title = @Title, Content = @Content, IsNew = @IsNew, IsTop = @IsTop, OrderBy = @OrderBy, ImgUrl = @ImgUrl, CreateTime = @CreateTime, UserName = @UserName, Author = @Author, Keywords = @Keywords
WHERE (ID = @ID)
RETURN
END
ELSE
BEGIN
RETURN 2
END
END
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_Article_GetPageData Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_Article_GetPageData]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_Article_GetPageData]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_Article_GetPageData]
(
@FieldsName nvarchar(512) = 'A.*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL,
@RecordCount int = 0 output ,
@PageIndex int = 0 output,
@PageSize int = 10
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = 'A.*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
CREATE TABLE #Pager
(
ID uniqueidentifier,
IndexID int IDENTITY (1, 1) NOT NULL
)
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'INSERT INTO #Pager (ID)'
+ ' SELECT ID'
+ ' FROM [dbo].[A_Article]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC(@SelectSql)
SELECT @RecordCount = COUNT(*) FROM #Pager
IF(@RecordCount < (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @RecordCount / @PageSize
END
IF(@RecordCount = (@PageSize * @PageIndex))
BEGIN
SET @PageIndex = @PageIndex - 1
IF @PageIndex < 0
BEGIN
SET @PageIndex = 0
END
END
DECLARE @PageUpperBound int
DECLARE @PageLowerBound int
SET @PageLowerBound = @PageSize * @PageIndex
SET @PageUpperBound = @PageLowerBound + @PageSize + 1
SET @SelectSql = 'SELECT '+ @FieldsName
+ ' FROM [dbo].[A_Article] AS A INNER JOIN #Pager AS B'
+ ' ON A.ID = B.ID '
+ ' WHERE'
+ ' B.IndexID > ' + RTRIM(LTRIM(CAST(@PageLowerBound AS varchar(128))))
+ ' AND B.IndexID < ' + RTRIM(LTRIM(CAST(@PageUpperBound AS varchar(128))))
+ ' ORDER BY B.IndexID'
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
/****** Object: Stored Procedure [dbo].A_Article_GetAll Script Date: 2005年1月22日 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[A_Article_GetAll]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[A_Article_GetAll]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
------------------------------------------------------------------------------------------------------------------------
-- Date Created: 2005年1月22日
-- Created By: Generated by Keyss
------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[A_Article_GetAll]
(
@FieldsName nvarchar(512) = '*',
@Where nvarchar(512) = NULL,
@OrderBy nvarchar(128) = NULL
)
AS
BEGIN
IF @Where IS NULL
BEGIN
Set @Where = ''
END
IF (@FieldsName IS NULL) or (@FieldsName = '')
BEGIN
Set @FieldsName = '*'
END
IF @OrderBy IS NULL
BEGIN
Set @OrderBy = ''
END
Set @FieldsName = LTRIM(RTRIM(@FieldsName))
Set @Where = LTRIM(RTRIM(@Where))
Set @OrderBy = LTRIM(RTRIM(@OrderBy))
Declare @SelectSql nvarchar(4000)
SET @SelectSql = 'SELECT ' + @FieldsName
+ ' FROM [dbo].[A_Article]'
IF @Where <>''
BEGIN
SET @SelectSql = @SelectSql + ' Where ' + @Where
END
IF @OrderBy <>''
BEGIN
SET @SelectSql = @SelectSql + ' Order By ' + @OrderBy
END
EXEC (@SelectSql)
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO