快驴ORM工具生成的三层架构下的主从复合型实体类代码(50)

  • 主题发起人 主题发起人 wpy020327
  • 开始时间 开始时间
W

wpy020327

Unregistered / Unconfirmed
GUEST, unregistred user!
1、数据结构:============= 主表MyOrder,关键字段OrderId 从表MyOrderDetail,关键字段OrderId 两者通过关键字段关联,一对多关系2、生成的从表实体类单元代码===========================//// ------------------------------------------------------------------------------------//// Entity_MyOrderDetail.pas -- This entity class unit for table "MyOrderDetail". //// Generated by QuickBurro V3.2.1.0 Entity Classes Generator//// Generate time: 2010-05-23 20:18:04//// Copyright (C) Jopher Software Studio, 2006-2010 //// ------------------------------------------------------------------------------------//unit Entity_MyOrderDetail;interfaceuses SysUtils, Classes, db, DBClient, SyncBurro, QBParcel;type TMyOrderDetail = class(TPersistent) Private ff_OrderId: Integer; ff_GoodsId: Integer; ff_GoodsName: String; ff_Price: Double; ff_Number: Double; ff_Money: Double; class function CreateCds: TClientDataset; public Property f_OrderId: Integer read ff_OrderId write ff_OrderId; Property f_GoodsId: Integer read ff_GoodsId write ff_GoodsId; Property f_GoodsName: String read ff_GoodsName write ff_GoodsName; Property f_Price: Double read ff_Price write ff_Price; Property f_Number: Double read ff_Number write ff_Number; Property f_Money: Double read ff_Money write ff_Money; constructor Create; destructor Destroy; override; class function ReadFromCds(aCds: TClientDataset): TMyOrderDetail; function SaveToCds(Cds: TClientDataset): Boolean; class function ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: String): TMyOrderDetail; function DeleteFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): boolean; function InsertToDB(NodeId: String; SBurro: TSyncBurro; var NewKeyValue: integer): boolean; function UpdateToDB(NodeId: String; SBurro: TSyncBurro; Condition: string): boolean; end; TMyOrderDetailList = class(TList) public constructor Create; destructor Destroy; override; class function ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string = ''; OrderBy: string = ''): TMyOrderDetailList; class function ReadFromCDS(Cds: TClientDataset): TMyOrderDetailList; function SaveToCds(aCds: TClientDataset): boolean; function SaveToFile(FileName: string; DataFormat: TDataPacketFormat): boolean; function LoadFromFile(FileName: string): boolean; function SaveToStream(aStream: TMemoryStream; DataFormat: TDataPacketFormat): boolean; function LoadFromStream(aStream: TMemoryStream): boolean; function SaveToParcel(aParcel: TQBParcel; GoodsName: string): boolean; function LoadFromParcel(aParcel: TQBParcel; GoodsName: string): boolean; end;implementation{ TMyOrderDetail }//// Create client dataset for the table...class function TMyOrderDetail.CreateCds: TClientDataset;begin Result:=TClientDataset.Create(nil); with Result do begin with FieldDefs.AddFieldDef do begin Name:='OrderId'; DataType:=ftInteger; end; with FieldDefs.AddFieldDef do begin Name:='GoodsId'; DataType:=ftInteger; end; with FieldDefs.AddFieldDef do begin Name:='GoodsName'; DataType:=ftString; Size:=32; end; with FieldDefs.AddFieldDef do begin Name:='Price'; DataType:=ftFloat; end; with FieldDefs.AddFieldDef do begin Name:='Number'; DataType:=ftFloat; end; with FieldDefs.AddFieldDef do begin Name:='Money'; DataType:=ftFloat; end; CreateDataSet; end; Result.Open;end;//// Create an instance...constructor TMyOrderDetail.Create;begin ff_OrderId:=0; ff_GoodsId:=0; ff_GoodsName:=''; ff_Price:=0.0; ff_Number:=0.0; ff_Money:=0.0;end;//// Destroy an instance...destructor TMyOrderDetail.Destroy;begin inheritedend;//// Current Record to an object...class function TMyOrderDetail.ReadFromCds(aCds: TClientDataset): TMyOrderDetail;begin if (not aCds.active) or (aCds.RecordCount<=0) then begin Result:=nil; exit; end; Result := TMyOrderDetail.Create; with Result do begin f_OrderId := aCds.FieldByName('OrderId').AsInteger; f_GoodsId := aCds.FieldByName('GoodsId').AsInteger; f_GoodsName := Trim(aCds.FieldByName('GoodsName').AsString); f_Price := aCds.FieldByName('Price').AsFloat; f_Number := aCds.FieldByName('Number').AsFloat; f_Money := aCds.FieldByName('Money').AsFloat; end;end;//// Object to Current Record...function TMyOrderDetail.SaveToCds(Cds: TClientDataset): Boolean;begin try Cds.edit; Cds.FieldValues['OrderId']:=ff_OrderId; Cds.FieldValues['GoodsId']:=ff_GoodsId; Cds.FieldValues['GoodsName']:=ff_GoodsName; Cds.FieldValues['Price']:=ff_Price; Cds.FieldValues['Number']:=ff_Number; Cds.FieldValues['Money']:=ff_Money; Cds.post; result:=true; except result:=false; end;end;//// Export class function...class function TMyOrderDetail.ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): TMyOrderDetail;var SqlCommand: string; Cds: TClientDataset;begin Result := nil; Cds:=TClientDataset.Create(nil); SqlCommand := 'SELECT * FROM [MyOrderDetail] WHERE '+condition; if not SBurro.ReadDataset(NodeId,SqlCommand,Cds) then begin Cds.Free; exit; end; Result:=ReadFromCds(Cds); Cds.Free;end;//// Insert to database method...function TMyOrderDetail.InsertToDB(NodeId: String; SBurro: TSyncBurro; var NewKeyValue: integer): boolean;var Cds: TClientDataset;begin Cds:=TMyOrderDetail.CreateCds; try Cds.Append; Cds.FieldValues['OrderId']:=ff_OrderId; Cds.FieldValues['GoodsId']:=ff_GoodsId; Cds.FieldValues['GoodsName']:=ff_GoodsName; Cds.FieldValues['Price']:=ff_Price; Cds.FieldValues['Number']:=ff_Number; Cds.FieldValues['Money']:=ff_Money; Cds.Post; except Cds.Free; Result:=false; exit; end; Result:=SBurro.AppendRecord(NodeId,'[MyOrderDetail]',Cds,NewKeyValue); Cds.Free;end;//// Update to database method...function TMyOrderDetail.UpdateToDB(NodeId: String; SBurro: TSyncBurro; Condition: String): boolean;var Cds: TClientDataset;begin Cds:=TMyOrderDetail.CreateCds; try Cds.Append; Cds.FieldValues['OrderId']:=ff_OrderId; Cds.FieldValues['GoodsId']:=ff_GoodsId; Cds.FieldValues['GoodsName']:=ff_GoodsName; Cds.FieldValues['Price']:=ff_Price; Cds.FieldValues['Number']:=ff_Number; Cds.FieldValues['Money']:=ff_Money; Cds.Post; except Cds.Free; Result:=false; exit; end; Result:=SBurro.ModifyRecord(NodeId,'[MyOrderDetail]',Condition,Cds); Cds.Free;end;//// Delete from database method...function TMyOrderDetail.DeleteFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): boolean;var SqlCommand,tmpstr: string;begin SqlCommand:='DELETE FROM [MyOrderDetail] WHERE '+condition; result:=SBurro.ExecSQL(NodeId,SqlCommand,0,tmpstr);end;{ TMyOrderDetailList }//// Create an instance...constructor TMyOrderDetailList.Create;begin inherited Create;end;//// Destroy a TMyOrderDetailList instance...destructor TMyOrderDetailList.Destroy;var I: Integer; Obj: TObject;begin for I := 0 to Count - 1 do begin Obj := Items; Obj.Free; end; inherited;end;//// Create a TMyOrderDetailList instance from remote table...class function TMyOrderDetailList.ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string; Orderby: string): TMyOrderDetailList;var SqlCommand: string; Cds: TClientDataset; TmpObj: TMyOrderDetail;begin Result := TMyOrderDetailList.Create; Cds:=TClientDataset.Create(nil); if Condition='' then begin SqlCommand := 'SELECT ' +'[OrderId],' +'[GoodsId],' +'[GoodsName],' +'[Price],' +'[Number],' +'[Money]' +' FROM [MyOrderDetail]' end else begin SqlCommand := 'SELECT ' +'[OrderId],' +'[GoodsId],' +'[GoodsName],' +'[Price],' +'[Number],' +'[Money]' +' FROM [MyOrderDetail] WHERE '+Condition; end; if Orderby<>'' then SqlCommand:=SqlCommand+' ORDER BY '+Orderby; if not SBurro.ReadDataset(NodeId,SqlCommand,Cds) then begin Cds.Free; exit; end; Cds.First; while not Cds.Eof do begin TmpObj := TMyOrderDetail.ReadFromCDS(Cds); Result.Add(TmpObj); Cds.Next; end; Cds.Free;end;//// Create a TMyOrderDetailList instance from CDS...class function TMyOrderDetailList.ReadFromCDS(Cds: TClientDataset): TMyOrderDetailList;var TmpObj: TMyOrderDetail;begin Result := TMyOrderDetailList.Create; Cds.First; while not Cds.Eof do begin TmpObj := TMyOrderDetail.ReadFromCDS(Cds); Result.Add(TmpObj); Cds.Next; end;end;//// Save to ClientDataset...function TMyOrderDetailList.SaveToCds(aCds: TClientDataset): boolean;var TmpObj: TMyOrderDetail; i: integer; ok: boolean; Cds: TClientDataset;begin Cds:=TMyOrderDetail.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['GoodsId']:=TmpObj.f_GoodsId; Cds.FieldValues['GoodsName']:=TmpObj.f_GoodsName; Cds.FieldValues['Price']:=TmpObj.f_Price; Cds.FieldValues['Number']:=TmpObj.f_Number; Cds.FieldValues['Money']:=TmpObj.f_Money; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin aCds.data:=Cds.Data; Result:=true; end else Result:=false; Cds.Free; end;//// Save data to a file...function TMyOrderDetailList.SaveToFile(FileName: string; DataFormat: TDataPacketFormat): boolean;var Cds: TClientDataset; TmpObj: TMyOrderDetail; i: integer; ok: boolean;begin Cds:=TMyOrderDetail.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['GoodsId']:=TmpObj.f_GoodsId; Cds.FieldValues['GoodsName']:=TmpObj.f_GoodsName; Cds.FieldValues['Price']:=TmpObj.f_Price; Cds.FieldValues['Number']:=TmpObj.f_Number; Cds.FieldValues['Money']:=TmpObj.f_Money; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin try Cds.SaveToFile(FileName,DataFormat); except ok:=false; end; end; Cds.Free; result:=ok;end;//// Load data from a file...function TMyOrderDetailList.LoadFromFile(FileName: string): boolean;var Cds: TClientDataset; TmpObj: TMyOrderDetail; ok: boolean;begin self.Clear; Cds:=TClientDataset.Create(nil); try Cds.LoadFromFile(FileName); ok:=Cds.Active; except ok:=false; end; if not ok then begin cds.Free; result:=false; exit; end; cds.First; while not cds.Eof do begin TmpObj := TMyOrderDetail.ReadFromCDS(Cds); Self.Add(TmpObj); Cds.Next; end; Cds.Free; result:=true;end;//// Save data to a memorystream...function TMyOrderDetailList.SaveToStream(aStream: TMemoryStream; DataFormat: TDataPacketFormat): boolean;var Cds: TClientDataset; TmpObj: TMyOrderDetail; i: integer; ok: boolean;begin Cds:=TMyOrderDetail.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['GoodsId']:=TmpObj.f_GoodsId; Cds.FieldValues['GoodsName']:=TmpObj.f_GoodsName; Cds.FieldValues['Price']:=TmpObj.f_Price; Cds.FieldValues['Number']:=TmpObj.f_Number; Cds.FieldValues['Money']:=TmpObj.f_Money; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin try aStream.Clear; Cds.SaveToStream(aStream,DataFormat); except ok:=false; end; end; Cds.Free; result:=ok;end;//// Load data from a memorystream...function TMyOrderDetailList.LoadFromStream(aStream: TMemoryStream): boolean;var Cds: TClientDataset; TmpObj: TMyOrderDetail; ok: boolean;begin self.Clear; Cds:=TClientDataset.Create(nil); try aStream.Position:=0; Cds.LoadFromStream(aStream); ok:=Cds.Active; except ok:=false; end; if not ok then begin cds.Free; result:=false; exit; end; cds.First; while not cds.Eof do begin TmpObj := TMyOrderDetail.ReadFromCDS(Cds); Self.Add(TmpObj); Cds.Next; end; Cds.Free; result:=true;end;//// Save data to a QBParcel...function TMyOrderDetailList.SaveToParcel(aParcel: TQBParcel; GoodsName: string): boolean;var Cds: TClientDataset; TmpObj: TMyOrderDetail; i: integer; ok: boolean;begin Cds:=TMyOrderDetail.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['GoodsId']:=TmpObj.f_GoodsId; Cds.FieldValues['GoodsName']:=TmpObj.f_GoodsName; Cds.FieldValues['Price']:=TmpObj.f_Price; Cds.FieldValues['Number']:=TmpObj.f_Number; Cds.FieldValues['Money']:=TmpObj.f_Money; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin try aParcel.PutCDSGoods(GoodsName,Cds); ok:=true; except ok:=false; end; end; Cds.Free; result:=ok;end;//// Load data from a QBparcel...function TMyOrderDetailList.LoadFromParcel(aParcel: TQBParcel; GoodsName: string): boolean;var Cds: TClientDataset; TmpObj: TMyOrderDetail; ok: boolean;begin self.Clear; Cds:=TClientDataset.Create(nil); try aParcel.GetCDSGoods(GoodsName,Cds); ok:=Cds.Active; except ok:=false; end; if not ok then begin cds.Free; result:=false; exit; end; cds.First; while not cds.Eof do begin TmpObj := TMyOrderDetail.ReadFromCDS(Cds); Self.Add(TmpObj); Cds.Next; end; Cds.Free; result:=true;end;end.3、生成的主表实体类代码=======================//// ------------------------------------------------------------------------------------//// Entity_MyOrder.pas -- This entity class unit for table "MyOrder". //// Generated by QuickBurro V3.2.1.0 Entity Classes Generator//// Generate time: 2010-05-24 09:43:28//// Copyright (C) Jopher Software Studio, 2006-2010//// ------------------------------------------------------------------------------------//unit Entity_MyOrder;interfaceuses SysUtils, Classes, db, DBClient, SyncBurro, QBParcel, Entity_MyOrderDetail;type TMyOrder = class(TPersistent) Private ff_OrderId: Integer; ff_OrderDate: String; ff_CustomerName: String; ff_Saler: String; ff_AllMoney: Double; ff_MyOrderDetail_List: TMyOrderDetailList; class function CreateCds: TClientDataset; public Property f_OrderId: Integer read ff_OrderId write ff_OrderId; Property f_OrderDate: String read ff_OrderDate write ff_OrderDate; Property f_CustomerName: String read ff_CustomerName write ff_CustomerName; Property f_Saler: String read ff_Saler write ff_Saler; Property f_AllMoney: Double read ff_AllMoney write ff_AllMoney; Property f_MyOrderDetail_List: TMyOrderDetailList read ff_MyOrderDetail_List write ff_MyOrderDetail_List; constructor Create; destructor Destroy; override; class function ReadFromCds(aCds: TClientDataset): TMyOrder; procedure LoadAppertains(NodeId: String; SBurro: TSyncBurro); function SaveToCds(Cds: TClientDataset): Boolean; class function ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: String): TMyOrder; function DeleteFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): boolean; function InsertToDB(NodeId: String; SBurro: TSyncBurro; var NewKeyValue: integer): boolean; function UpdateToDB(NodeId: String; SBurro: TSyncBurro; Condition: string): boolean; end; TMyOrderList = class(TList) public constructor Create; destructor Destroy; override; class function ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string = ''; OrderBy: string = ''): TMyOrderList; class function ReadFromCDS(Cds: TClientDataset): TMyOrderList; function SaveToCds(aCds: TClientDataset): boolean; function SaveToFile(FileName: string; DataFormat: TDataPacketFormat): boolean; function LoadFromFile(FileName: string): boolean; function SaveToStream(aStream: TMemoryStream; DataFormat: TDataPacketFormat): boolean; function LoadFromStream(aStream: TMemoryStream): boolean; function SaveToParcel(aParcel: TQBParcel; GoodsName: string): boolean; function LoadFromParcel(aParcel: TQBParcel; GoodsName: string): boolean; end;implementation{ TMyOrder }//// Create client dataset for the table...class function TMyOrder.CreateCds: TClientDataset;begin Result:=TClientDataset.Create(nil); with Result do begin with FieldDefs.AddFieldDef do begin Name:='OrderId'; DataType:=ftInteger; end; with FieldDefs.AddFieldDef do begin Name:='OrderDate'; DataType:=ftString; Size:=10; end; with FieldDefs.AddFieldDef do begin Name:='CustomerName'; DataType:=ftString; Size:=32; end; with FieldDefs.AddFieldDef do begin Name:='Saler'; DataType:=ftString; Size:=16; end; with FieldDefs.AddFieldDef do begin Name:='AllMoney'; DataType:=ftFloat; end; CreateDataSet; end; Result.Open;end;//// Create an instance...constructor TMyOrder.Create;begin ff_OrderId:=0; ff_OrderDate:=''; ff_CustomerName:=''; ff_Saler:=''; ff_AllMoney:=0.0; ff_MyOrderDetail_List:=TMyOrderDetailList.Create;end;//// Destroy an instance...destructor TMyOrder.Destroy;begin If ff_MyOrderDetail_List<>nil then ff_MyOrderDetail_List.free; inherited;end;//// Current Record to an object...class function TMyOrder.ReadFromCds(aCds: TClientDataset): TMyOrder;begin if (not aCds.active) or (aCds.RecordCount<=0) then begin Result:=nil; exit; end; Result := TMyOrder.Create; with Result do begin f_OrderId := aCds.FieldByName('OrderId').AsInteger; f_OrderDate := Trim(aCds.FieldByName('OrderDate').AsString); f_CustomerName := Trim(aCds.FieldByName('CustomerName').AsString); f_Saler := Trim(aCds.FieldByName('Saler').AsString); f_AllMoney := aCds.FieldByName('AllMoney').AsFloat; end;end;//// Load the appertain properties...procedure TMyOrder.LoadAppertains(NodeId: String; SBurro: TSyncBurro);begin if ff_MyOrderDetail_List<>nil then ff_MyOrderDetail_List.free; ff_MyOrderDetail_List:=TMyOrderDetailList.ReadFromDb(NodeId,SBurro,'OrderId='+Inttostr(ff_OrderId),'');end;//// Object to Current Record...function TMyOrder.SaveToCds(Cds: TClientDataset): Boolean;begin try Cds.edit; Cds.FieldValues['OrderId']:=ff_OrderId; Cds.FieldValues['OrderDate']:=ff_OrderDate; Cds.FieldValues['CustomerName']:=ff_CustomerName; Cds.FieldValues['Saler']:=ff_Saler; Cds.FieldValues['AllMoney']:=ff_AllMoney; Cds.post; result:=true; except result:=false; end;end;//// Export class function...class function TMyOrder.ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): TMyOrder;var SqlCommand: string; Cds: TClientDataset;begin Result := nil; Cds:=TClientDataset.Create(nil); SqlCommand := 'SELECT * FROM [MyOrder] WHERE '+condition; if not SBurro.ReadDataset(NodeId,SqlCommand,Cds) then begin Cds.Free; exit; end; Result:=ReadFromCds(Cds); Result.f_MyOrderDetail_List:=TMyOrderDetailList.ReadFromDb(NodeId,SBurro,'OrderId='+Inttostr(Result.f_OrderId),''); Cds.Free;end;//// Insert to database method...function TMyOrder.InsertToDB(NodeId: String; SBurro: TSyncBurro; var NewKeyValue: integer): boolean;var Cds: TClientDataset; i,j: integer;begin Cds:=TMyOrder.CreateCds; try Cds.Append; Cds.FieldValues['OrderId']:=ff_OrderId; Cds.FieldValues['OrderDate']:=ff_OrderDate; Cds.FieldValues['CustomerName']:=ff_CustomerName; Cds.FieldValues['Saler']:=ff_Saler; Cds.FieldValues['AllMoney']:=ff_AllMoney; Cds.Post; except Cds.Free; Result:=false; exit; end; Result:=SBurro.AppendRecord(NodeId,'[MyOrder]',Cds,NewKeyValue); if not result then Raise Exception.Create('Error on commit task!'); for i:=0 to ff_MyOrderDetail_List.Count-1 do begin if not TMyOrderDetail(ff_MyOrderDetail_List.items).InsertToDB(NodeId,SBurro,j) then Raise Exception.Create('Error on insert to sub tables!'); end; Cds.Free;end;//// Update to database method...function TMyOrder.UpdateToDB(NodeId: String; SBurro: TSyncBurro; Condition: String): boolean;var Cds: TClientDataset; tmpstr: string; i,j: integer; begin Cds:=TMyOrder.CreateCds; try Cds.Append; Cds.FieldValues['OrderId']:=ff_OrderId; Cds.FieldValues['OrderDate']:=ff_OrderDate; Cds.FieldValues['CustomerName']:=ff_CustomerName; Cds.FieldValues['Saler']:=ff_Saler; Cds.FieldValues['AllMoney']:=ff_AllMoney; Cds.Post; except Cds.Free; Result:=false; exit; end; Result:=SBurro.ModifyRecord(NodeId,'[MyOrder]',Condition,Cds); if not result then Raise Exception.Create('Error on commit task!'); tmpstr:=trim(Condition); delete(tmpstr,1,length('OrderId')+1); if ff_MyOrderDetail_List.Count>0 then begin if not TMyOrderDetail(ff_MyOrderDetail_List.items[0]).DeleteFromDB(NodeId,SBurro,'OrderId='+tmpstr) then Raise Exception.Create('Error on update sub tables!'); end; for i:=0 to ff_MyOrderDetail_List.Count-1 do begin if not TMyOrderDetail(ff_MyOrderDetail_List.items).InsertToDB(NodeId,SBurro,j) then Raise Exception.Create('Error on update sub tables!'); end; Cds.Free;end;//// Delete from database method...function TMyOrder.DeleteFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string): boolean;var SqlCommand,tmpstr: string; i: integer; begin SqlCommand:='DELETE FROM [MyOrder] WHERE '+condition; result:=SBurro.ExecSQL(NodeId,SqlCommand,0,tmpstr); if not result then Raise Exception.Create('Error on commit task!'); tmpstr:=trim(Condition); delete(tmpstr,1,length('OrderId')+1); for i:=0 to ff_MyOrderDetail_List.Count-1 do begin if not TMyOrderDetail(ff_MyOrderDetail_List.items).DeleteFromDB(NodeId,SBurro,'OrderId='+tmpstr) then Raise Exception.Create('Error on update sub tables!'); end;end;{ TMyOrderList }//// Create an instance...constructor TMyOrderList.Create;begin inherited Create;end;//// Destroy a TMyOrderList instance...destructor TMyOrderList.Destroy;var I: Integer; Obj: TObject;begin for I := 0 to Count - 1 do begin Obj := Items; Obj.Free; end; inherited;end;//// Create a TMyOrderList instance from remote table...class function TMyOrderList.ReadFromDB(NodeId: String; SBurro: TSyncBurro; Condition: string; Orderby: string): TMyOrderList;var SqlCommand: string; Cds: TClientDataset; TmpObj: TMyOrder;begin Result := TMyOrderList.Create; Cds:=TClientDataset.Create(nil); if Condition='' then begin SqlCommand := 'SELECT ' +'[OrderId],' +'[OrderDate],' +'[CustomerName],' +'[Saler],' +'[AllMoney]' +' FROM [MyOrder]' end else begin SqlCommand := 'SELECT ' +'[OrderId],' +'[OrderDate],' +'[CustomerName],' +'[Saler],' +'[AllMoney]' +' FROM [MyOrder] WHERE '+Condition; end; if Orderby<>'' then SqlCommand:=SqlCommand+' ORDER BY '+Orderby; if not SBurro.ReadDataset(NodeId,SqlCommand,Cds) then begin Cds.Free; exit; end; Cds.First; while not Cds.Eof do begin TmpObj := TMyOrder.ReadFromCDS(Cds); Result.Add(TmpObj); Cds.Next; end; Cds.Free;end;//// Create a TMyOrderList instance from CDS...class function TMyOrderList.ReadFromCDS(Cds: TClientDataset): TMyOrderList;var TmpObj: TMyOrder;begin Result := TMyOrderList.Create; Cds.First; while not Cds.Eof do begin TmpObj := TMyOrder.ReadFromCDS(Cds); Result.Add(TmpObj); Cds.Next; end;end;//// Save to ClientDataset...function TMyOrderList.SaveToCds(aCds: TClientDataset): boolean;var TmpObj: TMyOrder; i: integer; ok: boolean; Cds: TClientDataset;begin Cds:=TMyOrder.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate; Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName; Cds.FieldValues['Saler']:=TmpObj.f_Saler; Cds.FieldValues['AllMoney']:=TmpObj.f_AllMoney; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin aCds.data:=Cds.Data; Result:=true; end else Result:=false; Cds.Free; end;//// Save data to a file...function TMyOrderList.SaveToFile(FileName: string; DataFormat: TDataPacketFormat): boolean;var Cds: TClientDataset; TmpObj: TMyOrder; i: integer; ok: boolean;begin Cds:=TMyOrder.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate; Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName; Cds.FieldValues['Saler']:=TmpObj.f_Saler; Cds.FieldValues['AllMoney']:=TmpObj.f_AllMoney; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin try Cds.SaveToFile(FileName,DataFormat); except ok:=false; end; end; Cds.Free; result:=ok;end;//// Load data from a file...function TMyOrderList.LoadFromFile(FileName: string): boolean;var Cds: TClientDataset; TmpObj: TMyOrder; ok: boolean;begin self.Clear; Cds:=TClientDataset.Create(nil); try Cds.LoadFromFile(FileName); ok:=Cds.Active; except ok:=false; end; if not ok then begin cds.Free; result:=false; exit; end; cds.First; while not cds.Eof do begin TmpObj := TMyOrder.ReadFromCDS(Cds); Self.Add(TmpObj); Cds.Next; end; Cds.Free; result:=true;end;//// Save data to a memorystream...function TMyOrderList.SaveToStream(aStream: TMemoryStream; DataFormat: TDataPacketFormat): boolean;var Cds: TClientDataset; TmpObj: TMyOrder; i: integer; ok: boolean;begin Cds:=TMyOrder.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate; Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName; Cds.FieldValues['Saler']:=TmpObj.f_Saler; Cds.FieldValues['AllMoney']:=TmpObj.f_AllMoney; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin try aStream.Clear; Cds.SaveToStream(aStream,DataFormat); except ok:=false; end; end; Cds.Free; result:=ok;end;//// Load data from a memorystream...function TMyOrderList.LoadFromStream(aStream: TMemoryStream): boolean;var Cds: TClientDataset; TmpObj: TMyOrder; ok: boolean;begin self.Clear; Cds:=TClientDataset.Create(nil); try aStream.Position:=0; Cds.LoadFromStream(aStream); ok:=Cds.Active; except ok:=false; end; if not ok then begin cds.Free; result:=false; exit; end; cds.First; while not cds.Eof do begin TmpObj := TMyOrder.ReadFromCDS(Cds); Self.Add(TmpObj); Cds.Next; end; Cds.Free; result:=true;end;//// Save data to a QBParcel...function TMyOrderList.SaveToParcel(aParcel: TQBParcel; GoodsName: string): boolean;var Cds: TClientDataset; TmpObj: TMyOrder; i: integer; ok: boolean;begin Cds:=TMyOrder.CreateCds; ok:=true; for i:=0 to Self.Count-1 do begin TmpObj:=Self.items; try Cds.Append; Cds.FieldValues['OrderId']:=TmpObj.f_OrderId; Cds.FieldValues['OrderDate']:=TmpObj.f_OrderDate; Cds.FieldValues['CustomerName']:=TmpObj.f_CustomerName; Cds.FieldValues['Saler']:=TmpObj.f_Saler; Cds.FieldValues['AllMoney']:=TmpObj.f_AllMoney; Cds.Post; except ok:=false; end; if not ok then break; end; if ok then begin try aParcel.PutCDSGoods(GoodsName,Cds); ok:=true; except ok:=false; end; end; Cds.Free; result:=ok;end;//// Load data from a QBparcel...function TMyOrderList.LoadFromParcel(aParcel: TQBParcel; GoodsName: string): boolean;var Cds: TClientDataset; TmpObj: TMyOrder; ok: boolean;begin self.Clear; Cds:=TClientDataset.Create(nil); try aParcel.GetCDSGoods(GoodsName,Cds); ok:=Cds.Active; except ok:=false; end; if not ok then begin cds.Free; result:=false; exit; end; cds.First; while not cds.Eof do begin TmpObj := TMyOrder.ReadFromCDS(Cds); Self.Add(TmpObj); Cds.Next; end; Cds.Free; result:=true;end;end.4、说明=======实体类代码自动生成,充分提高开发效率,特别是此类代码能适应于三层架构应用,效果更佳。以上代码可直接在各Delphi版本、QuickBurro组件包支持下编译通过。
 
后退
顶部