U_Orders.pas
上传用户:etonglee
上传日期:2014-03-01
资源大小:698k
文件大小:9k
源码类别:

Internet/IE编程

开发平台:

Delphi

  1. {
  2.  ToDo:      数据模块  
  3. }
  4. unit U_Orders;
  5. {$WARN SYMBOL_PLATFORM OFF}
  6. interface
  7. uses
  8.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  9.   ComServ, ComObj, VCLCom, StdVcl, bdemts, DataBkr, DBClient,
  10.   MtsRdm, Mtx, SvrObj_Orders_TLB, Provider, DB, ADODB,Variants;
  11. type
  12.   TOrders = class(TMtsDataModule, IOrders)
  13.     ADO_CNT: TADOConnection;
  14.     QRY_ORDER: TADOQuery;
  15.     DSPRD_ORDER: TDataSetProvider;
  16.     QRY_ORDER_DETAIL: TADOQuery;
  17.     DSPRD_ORDER_DETAIL: TDataSetProvider;
  18.     procedure MtsDataModuleActivate(Sender: TObject);
  19.     procedure MtsDataModuleDeactivate(Sender: TObject);
  20.     procedure MtsDataModuleCreate(Sender: TObject);
  21.     procedure DSPRD_ORDERUpdateError(Sender: TObject;
  22.       DataSet: TCustomClientDataSet; E: EUpdateError;
  23.       UpdateKind: TUpdateKind; var Response: TResolverResponse);
  24.     procedure DSPRD_ORDER_DETAILUpdateError(Sender: TObject;
  25.       DataSet: TCustomClientDataSet; E: EUpdateError;
  26.       UpdateKind: TUpdateKind; var Response: TResolverResponse);
  27.   private
  28.     { Private declarations }
  29.     FErrorMsg: WideString;
  30.   protected
  31.     class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
  32.     procedure GetOrder(OrderID: Integer; var RetData: OleVariant); safecall;
  33.     procedure GetOrderDetail(OrderID: Integer; var RetData: OleVariant);
  34.       safecall;
  35.     procedure GetOrders(BeginDate, EndDate: TDateTime;
  36.       var RetData: OleVariant); safecall;
  37.     procedure GetOtherData(var RetData: OleVariant); safecall;
  38.     procedure UpdateOrder(OrderID: Integer; OrderDelta,
  39.       OrderDetailDelta: OleVariant; var IsOk: WordBool;
  40.       var ErrorMsg: WideString); safecall;
  41.   public
  42.     { Public declarations }
  43.   end;
  44. var
  45.   Orders: TOrders;
  46. implementation
  47. {$R *.DFM}
  48. class procedure TOrders.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
  49. begin
  50.   if Register then
  51.   begin
  52.     inherited UpdateRegistry(Register, ClassID, ProgID);
  53.     EnableSocketTransport(ClassID);
  54.     EnableWebTransport(ClassID);
  55.   end else
  56.   begin
  57.     DisableSocketTransport(ClassID);
  58.     DisableWebTransport(ClassID);
  59.     inherited UpdateRegistry(Register, ClassID, ProgID);
  60.   end;
  61. end;
  62. procedure TOrders.GetOrder(OrderID: Integer; var RetData: OleVariant);
  63. begin
  64.   try
  65.     //***************************对ADOQuery初始化*******************************
  66.     QRY_ORDER.Close;
  67.     QRY_ORDER.Connection := ADO_CNT;
  68.     QRY_ORDER.SQL.Clear;
  69.     QRY_ORDER.SQL.Add('select * from Orders where OrderID='+inttostr(OrderID));
  70.     DSPRD_ORDER.DataSet := QRY_ORDER;
  71.     //**************************************************************************
  72.     QRY_ORDER.Open;
  73.     RetData := DSPRD_ORDER.Data;
  74.     SetComplete();
  75.     //提交事务
  76.   except
  77.     SetAbort();
  78.     //回滚事务
  79.   end;
  80. end;
  81. procedure TOrders.GetOrderDetail(OrderID: Integer;
  82.   var RetData: OleVariant);
  83. begin
  84.   try
  85.     //**************************对ADOQuery初始化********************************
  86.     QRY_ORDER_DETAIL.Close();
  87.     QRY_ORDER_DETAIL.Connection := ADO_CNT;
  88.     QRY_ORDER_DETAIL.SQL.Clear();
  89.     QRY_ORDER_DETAIL.SQL.Add('select * from OrderDetails where OrderID='+inttostr(OrderID));
  90.     DSPRD_ORDER_DETAIL.DataSet := QRY_ORDER_DETAIL;
  91.     //**************************************************************************
  92.     QRY_ORDER_DETAIL.Open();
  93.     RetData := DSPRD_ORDER_DETAIL.Data;
  94.     SetComplete();
  95.   except
  96.     SetAbort();
  97.   end;
  98. end;
  99. procedure TOrders.GetOrders(BeginDate, EndDate: TDateTime;
  100.   var RetData: OleVariant);
  101. begin
  102.   try
  103.     //***************************对ADOQuery初始化*******************************
  104.     QRY_ORDER.Close();
  105.     QRY_ORDER.Connection:=ADO_CNT;
  106.     QRY_ORDER.SQL.Clear();
  107.     QRY_ORDER.SQL.Add('SELECT top 100 Orders.OrderID, Customers.CompanyName, Orders.OrderDate, ');
  108.     QRY_ORDER.SQL.Add('      Orders.RequiredDate, Orders.ShippedDate, Orders.ShipName ');
  109.     QRY_ORDER.SQL.Add('FROM Orders INNER JOIN ');
  110.     QRY_ORDER.SQL.Add('      Customers ON Orders.CustomerID = Customers.CustomerID ');
  111.     QRY_ORDER.SQL.Add('where (Orders.OrderDate>='''+DateTimeToStr(BeginDate)+''')');
  112.     QRY_ORDER.SQL.Add('      and (Orders.OrderDate<='''+DateTimeToStr(EndDate)+''')');
  113.     QRY_ORDER.SQL.Add('ORDER BY Orders.OrderDate');
  114.     DSPRD_ORDER.DataSet:=QRY_ORDER;
  115.     //**************************************************************************
  116.     QRY_ORDER.Open();
  117.     RetData:=DSPRD_ORDER.Data;
  118.     SetComplete();
  119.     //调用了SetComplete相当于告诉组件已经任务完成可以做相应的处理了
  120.   except
  121.     SetAbort();
  122.   end;
  123. end;
  124. procedure TOrders.GetOtherData(var RetData: OleVariant);
  125. var
  126.   Query: TAdoQuery;
  127.   Dsprd: TDataSetProvider;
  128. begin
  129.   //**********通过使用动态数组实现数据打包的功能*************
  130.   RetData := VarArrayCreate([0,3], varVariant);
  131.   try
  132.     Query:=TAdoQuery.Create(nil);
  133.     Dsprd:=TDataSetProvider.Create(nil);
  134.     try
  135.       //******************获取员工信息************************
  136.       Dsprd.DataSet:=Query;
  137.       Query.Connection:=ADO_CNT;
  138.       Query.SQL.Add('SELECT EmployeeID,FirstName+ '' ''+LastName AS EmployeeName');
  139.       Query.SQL.Add('FROM Employees');
  140.       Query.Open();
  141.       RetData[0]:=Dsprd.Data;
  142.       //******************获取客户信息************************
  143.       Query.Close();
  144.       Query.SQL.Clear();
  145.       Query.SQL.Add('SELECT CustomerID, CompanyName');
  146.       Query.SQL.Add('FROM Customers');
  147.       Query.Open();
  148.       RetData[1]:=Dsprd.Data;
  149.       //******************获取运输公司信息************************
  150.       Query.Close();
  151.       Query.SQL.Clear();
  152.       Query.SQL.Add('SELECT ShipperID, CompanyName');
  153.       Query.SQL.Add('FROM Shippers');
  154.       Query.Open();
  155.       RetData[2]:=Dsprd.Data;
  156.       //******************获取货品信息************************
  157.       Query.Close();
  158.       Query.SQL.Clear();
  159.       Query.SQL.Add('SELECT ProductID, ProductName,UnitPrice');
  160.       Query.SQL.Add('FROM Products');
  161.       Query.Open();
  162.       RetData[3]:=Dsprd.Data;
  163.     finally
  164.       Query.Free();
  165.       Dsprd.Free();
  166.     end;
  167.     SetComplete();
  168.   except
  169.     SetAbort();
  170.   end;
  171. end;
  172. procedure TOrders.UpdateOrder(OrderID: Integer; OrderDelta,
  173.   OrderDetailDelta: OleVariant; var IsOk: WordBool;
  174.   var ErrorMsg: WideString);
  175. var
  176.   ErrorCount:integer;
  177.   Cltds:TClientDataSet;
  178. begin
  179.   ErrorCount:=0;
  180.   IsOk:=false;
  181.   ErrorMsg:='';
  182.   //*************************初始化ADOQUERY*******************************
  183.   DSPRD_ORDER.DataSet:=QRY_ORDER;
  184.   QRY_ORDER.Connection:=ADO_CNT;
  185.   QRY_ORDER.SQL.Clear();
  186.   QRY_ORDER.SQL.Add('select * from Orders where OrderID=-1');
  187.   //若要实现无状态,这一步是必须的,因为要根据以上语句生成更新语句
  188.   DSPRD_ORDER_DETAIL.DataSet:=QRY_ORDER_DETAIL;
  189.   QRY_ORDER_DETAIL.Connection:=ADO_CNT;
  190.   QRY_ORDER_DETAIL.SQL.Clear();
  191.   QRY_ORDER_DETAIL.SQL.Add('select * from OrderDetails where OrderID=-1');
  192.   //**********************************************************************
  193.   try
  194.     //更新Orders表
  195.     if not VarIsEmpty(OrderDelta) then
  196.       DSPRD_ORDER.ApplyUpdates(OrderDelta,-1,ErrorCount);
  197.       // -1 表示在更新的过程中不允许有错误发生
  198.     //更新Order Details表
  199.     if (not VarIsEmpty(OrderDetailDelta)) and (ErrorCount=0) then
  200.     begin
  201.       Cltds:=TClientDataSet.Create(nil);
  202.       try
  203.         Cltds.Data:=OrderDetailDelta;
  204.         Cltds.First();
  205.         while not Cltds.Eof do
  206.         begin
  207.           case cltds.UpdateStatus of
  208.             usInserted:
  209.             begin
  210.               //新添加的记录要给OrderID字段赋值
  211.               cltds.Edit();
  212.               cltds.FieldByName('OrderID').AsInteger:=OrderID;
  213.               cltds.Post();
  214.             end;
  215.             usUnmodified:
  216.             begin
  217.               //修改过的记录OrderID字段已有,无须赋值
  218.               cltds.Next();
  219.             end;
  220.           end;
  221.           Cltds.Next();
  222.         end;
  223.         DSPRD_ORDER_DETAIL.ApplyUpdates(cltds.Data,-1,ErrorCount);
  224.       finally
  225.         Cltds.Free();
  226.       end;
  227.     end;
  228.     if ErrorCount<>0 then
  229.     begin
  230.       SetAbort();
  231.       ErrorMsg:=FErrorMsg;
  232.       exit;
  233.     end;
  234.     IsOk:=true;
  235.     SetComplete();
  236.   except
  237.     on E:Exception do
  238.     begin
  239.       SetAbort();
  240.       ErrorMsg:=E.Message;
  241.     end;
  242.   end;
  243. end;
  244. procedure TOrders.MtsDataModuleActivate(Sender: TObject);
  245. const
  246.   DB_SERVER   =  'txxtrr';
  247.   DB_USERNAME =  'sa';
  248.   DB_PASSWORD =  '';
  249. begin
  250.   ADO_CNT.ConnectionString:='Provider=SQLOLEDB.1;Password='+DB_PASSWORD+
  251.     ';Persist Security Info=False;User ID='+DB_USERNAME+
  252.     ';Initial Catalog=Northwind;Data Source='+DB_SERVER;
  253.   ADO_CNT.Connected:=true;
  254.   FErrorMsg:='';
  255. end;
  256. procedure TOrders.MtsDataModuleDeactivate(Sender: TObject);
  257. begin
  258.   ADO_CNT.Connected:=false;
  259. end;
  260. procedure TOrders.MtsDataModuleCreate(Sender: TObject);
  261. begin
  262.   Pooled:=true;
  263. end;
  264. procedure TOrders.DSPRD_ORDERUpdateError(Sender: TObject;
  265.   DataSet: TCustomClientDataSet; E: EUpdateError; UpdateKind: TUpdateKind;
  266.   var Response: TResolverResponse);
  267. begin
  268.   FErrorMsg:=FErrorMsg+E.Message+#13;
  269. end;
  270. procedure TOrders.DSPRD_ORDER_DETAILUpdateError(Sender: TObject;
  271.   DataSet: TCustomClientDataSet; E: EUpdateError; UpdateKind: TUpdateKind;
  272.   var Response: TResolverResponse);
  273. begin
  274.   FErrorMsg:=FErrorMsg+E.Message+#13;
  275. end;
  276. initialization
  277.   TComponentFactory.Create(ComServer, TOrders,
  278.     Class_Orders, ciMultiInstance, tmApartment);
  279. end.