//这是我写的一个简单bbs服务的代码
//用于教学的,基于com+/webservice/client,下面是com+的核心,所谓的业务逻辑封装,就是这些。如果感兴趣,可以给全套代码
抽象了几个主要方法:
//获得帖子内容
procedure GetDetail(id: Integer;
out vDetail: OleVariant);
safecall;
//获得主帖标题
procedure GetRootSubjects(PageNo: Integer;
out vData: OleVariant;
out EndofFile: WordBool);
safecall;
//获得某个主帖的所有子帖
procedure GetChildDetail(pId: Integer;
out vChildDetail: OleVariant;
PageNo: Integer;
out EndofFile: WordBool);
safecall;
//跟贴
procedure AddChild(pid: Integer;
const Subject, Content,
Submiter: WideString);
safecall;
//加主帖
procedure AddRoot(const Subject, Content, Submiter: WideString);
safecall;
procedure GetRootDetail(var vRootDetail: OleVariant;
PageNo: Integer;
out EndofFile: WordBool);
safecall;
unit BBSImpl;
{$WARN SYMBOL_PLATFORM OFF}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComServ, ComObj, VCLCom, StdVcl, bdemts, DataBkr, DBClient,
MtsRdm, Mtx, PMtsBBS_TLB, Provider, DB, DBTables;
type
TBBS = class(TMtsDataModule, IBBS)
qryDetailById: TQuery;
Session1: TSession;
Database1: TDatabase;
dspDetailByID: TDataSetProvider;
qryRootSubjects: TQuery;
dspRootSubjects: TDataSetProvider;
cdsRootSubjects: TClientDataSet;
qryChildDetail: TQuery;
dspChildDetail: TDataSetProvider;
cdsChildDetail: TClientDataSet;
qryInsertRec: TQuery;
qryUpdateFollowTime: TQuery;
qryRootDetail: TQuery;
dspRootDetail: TDataSetProvider;
cdsRootDetail: TClientDataSet;
private
{ Private declarations }
procedure UpdateFollowTime(pId:integer;dTime:TDateTime);
protected
class procedure UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
override;
procedure GetDetail(id: Integer;
out vDetail: OleVariant);
safecall;
procedure GetRootSubjects(PageNo: Integer;
out vData: OleVariant;
out EndofFile: WordBool);
safecall;
procedure GetChildDetail(pId: Integer;
out vChildDetail: OleVariant;
PageNo: Integer;
out EndofFile: WordBool);
safecall;
procedure AddChild(pid: Integer;
const Subject, Content,
Submiter: WideString);
safecall;
procedure AddRoot(const Subject, Content, Submiter: WideString);
safecall;
procedure GetRootDetail(var vRootDetail: OleVariant;
PageNo: Integer;
out EndofFile: WordBool);
safecall;
public
{ Public declarations }
end;
var
BBS: TBBS;
implementation
{$R *.DFM}
uses Math;
class procedure TBBS.UpdateRegistry(Register: Boolean;
const ClassID, ProgID: string);
begin
if Register then
begin
inherited UpdateRegistry(Register, ClassID, ProgID);
EnableSocketTransport(ClassID);
EnableWebTransport(ClassID);
end else
begin
DisableSocketTransport(ClassID);
DisableWebTransport(ClassID);
inherited UpdateRegistry(Register, ClassID, ProgID);
end;
end;
procedure TBBS.GetDetail(id: Integer;
out vDetail: OleVariant);
begin
// qryDetailByID.ParamByName('id').AsInteger:=id;
// qryDetailByID.Open;
qryDetailByID.Params[0].AsInteger:=id;
qryDetailByID.Open;
vDetail:=dspDetailByID.Data ;
qryDetailByID.Close;
end;
procedure TBBS.GetRootSubjects(PageNo: Integer;
out vData: OleVariant;
out EndofFile: WordBool);
var FirstRec,i,j:integer;
FieldsArray:Array[0..3] of variant;
begin
EndofFile:=false;
cdsRootSubjects.Open;
FirstRec:= max(0,(PageNo-1)*10);
cdsRootSubjects.MoveBy(FirstRec);
with TClientDataSet.Create(nil)do
try
FieldDefs:=cdsRootSubjects.FieldDefs;
CreateDataSet;
for i:=0 to 9do
begin
with cdsRootSubjectsdo
for j:=0 to FieldCount -1do
FieldsArray[j]:=Fields[j].value;
InsertRecord([FieldsArray[0],FieldsArray[1],FieldsArray[2],FieldsArray[3]]);
cdsRootSubjects.Next;
if cdsRootSubjects.Eof then
begin
EndofFile:=true;
Break;
end;
end;
vData:=Data;
finally
Free;
cdsRootSubjects.Close;
end;
end;
procedure TBBS.GetChildDetail(pId: Integer;
out vChildDetail: OleVariant;
PageNo: Integer;
out EndofFile: WordBool);
var FirstRec,i,j:integer;
FieldsArray:array[0..6] of variant;
begin
EndofFile:=false;
qryChildDetail.Params[0].AsInteger:=pId;
cdsChildDetail.Open;
FirstRec:= max(0,(PageNo-1)*10);
cdsChildDetail.MoveBy(FirstRec);
with TClientDataSet.Create(nil)do
try
FieldDefs:=cdsChildDetail.FieldDefs;
CreateDataSet;
if cdsChildDetail.Eof then
EndofFile:=true
else
begin
for i:=0 to 9do
begin
with cdsChildDetaildo
for j:=0 to FieldCount -1do
FieldsArray[j]:=Fields[j].value;
InsertRecord([FieldsArray[0],FieldsArray[1],FieldsArray[2],FieldsArray[3],FieldsArray[4],FieldsArray[5],FieldsArray[6]]);
cdsChildDetail.Next;
if cdsChildDetail.Eof then
begin
EndofFile:=true;
Break;
end;
end;
end;
vChildDetail:=Data;
finally
Free;
cdsChildDetail.Close;
end;
end;
procedure TBBS.AddChild(pid: Integer;
const Subject, Content,
Submiter: WideString);
var dTime:TDateTime;
begin
{pid,subject,content,submiter,SubmitTime}
dTime:=now;
with qryInsertRecdo
begin
params[0].AsInteger:=pid;
params[1].AsString:=Subject;
params[2].AsString:=Content;
params[3].AsString:=Submiter;
params[4].AsDateTime:=dTime;
params[5].AsDateTime:=dTime;
ExecSql;
end;
UpdateFollowTime(pid,dTime);
end;
procedure TBBS.AddRoot(const Subject, Content, Submiter: WideString);
var dTime:TDateTime;
begin
{pid,subject,content,submiter,SubmitTime}
dTime:=now;
with qryInsertRecdo
begin
params[0].AsInteger:=0;
params[1].AsString:=Subject;
params[2].AsString:=Content;
params[3].AsString:=Submiter;
params[4].AsDateTime:=dTime;
params[5].AsDateTime:=dTime;
ExecSql;
end;
end;
procedure TBBS.UpdateFollowTime(pId:integer;dTime: TDateTime);
begin
qryUpdateFollowTime.ParamByName('id').asInteger:=pId;
qryUpdateFollowTime.ParamByName('LastFollowTime').asDateTime:=dTime;
qryUpdateFollowTime.ExecSQL;
if pId<=0 then
Exit;
with qryDetailByIDdo
begin
Close;
Params[0].AsInteger:=pId;
Open;
if not qryDetailByID.eof then
if (fieldByName('Pid').AsInteger>0) and (fieldByName('Pid').AsInteger<>fieldByName('id').AsInteger) then
UpdateFollowTime(fieldByName('Pid').AsInteger,dTime);
Close;
end;
end;
procedure TBBS.GetRootDetail(var vRootDetail: OleVariant;
PageNo: Integer;
out EndofFile: WordBool);
var FirstRec,i,j:integer;
FieldsArray:array[0..6] of variant;
begin
EndofFile:=false;
cdsRootDetail.Open;
FirstRec:= max(0,(PageNo-1)*10);
cdsRootDetail.MoveBy(FirstRec);
with TClientDataSet.Create(nil)do
try
FieldDefs:=cdsRootDetail.FieldDefs;
CreateDataSet;
for i:=0 to 9do
begin
with cdsRootDetaildo
for j:=0 to FieldCount -1do
FieldsArray[j]:=Fields[j].value;
InsertRecord([FieldsArray[0],FieldsArray[1],FieldsArray[2],FieldsArray[3],FieldsArray[4],FieldsArray[5],FieldsArray[6]]);
cdsRootDetail.Next;
if cdsRootDetail.Eof then
begin
EndofFile:=true;
Break;
end;
end;
vRootDetail:=Data;
finally
Free;
cdsRootDetail.Close;
end;
end;
initialization
TComponentFactory.Create(ComServer, TBBS,
Class_BBS, ciMultiInstance, tmApartment);
end.