多层数据库应用基于Delphi DataSnap方法调用的实现.doc_第1页
多层数据库应用基于Delphi DataSnap方法调用的实现.doc_第2页
多层数据库应用基于Delphi DataSnap方法调用的实现.doc_第3页
多层数据库应用基于Delphi DataSnap方法调用的实现.doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

多层数据库应用基于Delphi DataSnap方法调用的实现(二)更新数据集 收藏 传统的数据集的读取和更新,是通过中间层的TDataSetProvider来完成的。TDataSetProvider负责从它上游的数据集读取数据生成Data包,再传给客户端;另一方面,在客户端提交更新时(TClientDataSet.ApplyUpdates),TDataSetProvider还负责解析上传的Delta包,并最终实现数据库的更新。现在在我们当前的方法调用方式下,不能再通过TDataSetProvider.ApplyUpdates来自动完成更新了,但是,我们还可以借用TDataSetProvider解析Delta数据包的功能,手动生成SQL语句来完成数据库的更新。手动更新看似复杂,实际上编码并不多,而且这种方式具有很大的灵活性,同时还解决了在传统方式下,多表联合查询不能完全自动更新的软肋。下面来看看示例代码:中间层代码.interfaceuses SysUtils, Classes, DSServer, DBXOracle, FMTBcd, DB, SqlExpr, WideStrings, Provider, CodeSiteLogging, DBClient;type$METHODINFO ON TServerMethods1 = class(TDataModule) SQLConnection1: TSQLConnection; SQLDataSet1: TSQLDataSet; DataSetProvider1: TDataSetProvider; procedure DataSetProvider1BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean); private Private declarations FOnDeltaRecordUpdate: TBeforeUpdateRecordEvent; procedure UpdateEmployeesDelta(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean); public Public declarations function GetEmployeeFullName(EmployeeId: Integer): string; function GetEmployees: TDataSet; function UpdateEmployees(EMPDelta: OleVariant): Boolean; end;$METHODINFO OFF.cedure TServerMethods1.DataSetProvider1BeforeUpdateRecord(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean);begin if Assigned(FOnDeltaRecordUpdate) then FOnDeltaRecordUpdate(Sender, SourceDS, DeltaDS, UpdateKind, Applied);end;procedure TServerMethods1.UpdateEmployeesDelta(Sender: TObject; SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var Applied: Boolean);var SQLStr, Clause1, Clause2: string; I: Integer;begin Applied := False; case UpdateKind of $REGION Process ukModify ukModify: begin with DeltaDS do begin for I := 0 to Fields.Count - 1 do if not FieldsI.IsNull then Clause1 := Clause1 + , + FieldsI.FieldName + =: + FieldsI.FieldName; Clause1 := System.Copy(Clause1, 2, Length(Clause1) - 1); SQLStr := UPDATE EMPLOYEES SET + Clause1 + WHERE EMPLOYEE_ID = :EMPLOYEE_ID; end; with SQLDataSet1 do begin Close; CommandText := SQLStr; for I := 0 to DeltaDS.Fields.Count - 1 do if not DeltaDS.FieldsI.IsNull then ParamByName(DeltaDS.FieldsI.FieldName).Value := DeltaDS.FieldsI.Value; ParamByName(EMPLOYEE_ID).AsInteger := DeltaDS.FieldByName(EMPLOYEE_ID).OldValue; if (ExecSQL() = 0) then Abort; end; end; $ENDREGION $REGION Process ukInsert ukInsert: begin for I := 0 to DeltaDS.Fields.Count - 1 do begin if not DeltaDS.FieldsI.IsNull then begin Clause1 := Clause1 + , + DeltaDS.FieldsI.FieldName; Clause2 := Clause2 + , : + DeltaDS.FieldsI.FieldName; end; end; Clause1 := System.Copy(Clause1, 2, Length(Clause1) - 1); Clause2 := System.Copy(Clause2, 2, Length(Clause2) - 1); SQLStr := INSERT INTO EMPLOYEES( + Clause1 + ) VALUES( + Clause2 + ); with SQLDataSet1 do begin Close; CommandText := SQLStr; for I := 0 to DeltaDS.Fields.Count - 1 do if not DeltaDS.FieldsI.IsNull then ParamByName(DeltaDS.FieldsI.FieldName).Value := DeltaDS.FieldsI.Value; if (ExecSQL() = 0) then Abort; end; end; $ENDREGION $REGION Process ukDelete ukDelete: begin SQLStr := DELETE EMPLOYEES WHERE EMPLOYEE_ID = :EMPLOYEE_ID; with SQLDataSet1 do begin Close; CommandText := SQLStr; ParamByName(EMPLOYEE_ID).AsInteger := DeltaDS.FieldByName(EMPLOYEE_ID).OldValue; if (ExecSQL() = 0) then Abort; end; end; $ENDREGION end; Applied := True;end;function TServerMethods1.UpdateEmployees(EMPDelta: OleVariant): Boolean;var ErrorCount: Integer;begin /指定Employees的专有更新过程 FOnDeltaRecordUpdate := UpdateEmployeesDelta; DataSetProvider1.ApplyUpdates(EMPDelta, 0, ErrorCount); Result := ErrorCount = 0;end;1、增加一个DataSetProvider1控件,让它与SQLDataSet1控件关联。2、申明一个事件句柄FOnDeltaRecordUpdate: TBeforeUpdateRecordEvent,然后在DataSetProvider1的BeforeUpdateRecord事件过程中,再嵌套一个TBeforeUpdateRecordEvent事件。3、申明和定义一个私有过程UpdateEmployeesDelta,其参数定义遵循TBeforeUpdateRecordEvent事件过程的定义,此过程专门用来处理Employees的更新。实际上,所有表的更新只有Update、Insert和Delete三种类型,针对当前传入的Delta包,可以手动构造出SQL语句。这里只有具有的表不一样,但处理流程都是完全一样的,所以,若有其他新表的更新,可以直接套用相关代码段,甚至可以做成一个专门的一个通用过程。4、申明和定义UpdateEmployees函数,此方法是暴露给客户端进行调用的。若客户端有多个表需要在一个事务中完成更新,则可以在此过程中手动启动事务,分别完成多个表Delta数据包的更新,最后提交或rollback。若有新的方法暴露给客户端来更新其他表的时候,只需要相应地再创建一个私有更新过程,并把此过程指针赋给FOnDeltaRecordUpdate即可。客户端代码: 通过在客户端中的TSQLConection控件,可以生成DataSnap client class:. TServerMethods1Client = class private FDBXConnection: TDBXConnection; FInstanceOwner: Boolean; FEchoStringCommand: TDBXCommand; FReverseStringCommand: TDBXCommand; FGetEmployeeFullNameCommand: TDBXCommand; FGetEmployeesCommand: TDBXCommand; FGetJobsCommand: TDBXCommand; FUpdateEmployeesCommand: TDBXCommand; public constructor Create(ADBXConnection: TDBXConnection); overload; constructor Create(ADBXConnection: TDBXConnection; AInstanceOwner: Boolean); overload; destructor Destroy; override; function GetEmployeeFullName(EmployeeId: Integer): string; function GetEmployees: TDataSet; function UpdateEmployees(EMPDelta: OleVariant): Boolean; end;. 在调用模块中,比如一个按钮事件中完成Employees被更新数据的提交,其中FMethodProxy为TServerMethods1Client的实例:.procedure TForm2.Button2Click(Sender: TObject);begin if ClientDataSet1.ChangeCount 0 then begin FMethodProxy.UpdateEmployees(ClientDataSet1.Delta); end;end;.通过上述方式,中间层业务类模块,只需要三个控件,就可以完成现在和今后所有的数据读取和更新操作。更重要的是,由于每次方法调用之间是不需要客户端到中间层、中间层到数据库的两个连接始终保持并专用的,在实际调用方法的时候才需要连接。但在上述例子中,我们的类工厂(TD

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论