Browse Source

* Fixed batch processing, and multiple requests through 1 instance of an Adaptor

git-svn-id: trunk@16231 -
michael 15 years ago
parent
commit
a83037e7bc
2 changed files with 127 additions and 33 deletions
  1. 75 8
      packages/fcl-web/src/webdata/extjsjson.pp
  2. 52 25
      packages/fcl-web/src/webdata/fpwebdata.pp

+ 75 - 8
packages/fcl-web/src/webdata/extjsjson.pp

@@ -19,6 +19,7 @@ type
     FRowIndex : integer;
     FRowIndex : integer;
     function CheckData: Boolean;
     function CheckData: Boolean;
   Public
   Public
+    procedure reset; override;
     Function GetNextBatch : Boolean; override;
     Function GetNextBatch : Boolean; override;
     Function TryFieldValue(Const AFieldName : String; out AValue : String) : Boolean; override;
     Function TryFieldValue(Const AFieldName : String; out AValue : String) : Boolean; override;
     Destructor destroy; override;
     Destructor destroy; override;
@@ -39,8 +40,13 @@ type
     FBeforeRowToJSON: TJSONObjectEvent;
     FBeforeRowToJSON: TJSONObjectEvent;
     FOnErrorResponse: TJSONExceptionObjectEvent;
     FOnErrorResponse: TJSONExceptionObjectEvent;
     FOnMetaDataToJSON: TJSONObjectEvent;
     FOnMetaDataToJSON: TJSONObjectEvent;
-    procedure SendSuccess(ResponseContent: TStream; AddIDValue : Boolean = False; CallBack : TJSONObjectEvent = Nil);
+    FBatchResult : TJSONArray;
+    Function AddIdToBatch : TJSONObject;
+    procedure SendSuccess(ResponseContent: TStream; AddIDValue : Boolean = False);
   protected
   protected
+    Procedure StartBatch(ResponseContent : TStream); override;
+    Procedure NextBatchItem(ResponseContent : TStream); override;
+    Procedure EndBatch(ResponseContent : TStream); override;
     Function CreateAdaptor(ARequest : TRequest) : TCustomWebdataInputAdaptor; override;
     Function CreateAdaptor(ARequest : TRequest) : TCustomWebdataInputAdaptor; override;
     Function AddFieldToJSON(O: TJSONObject; AFieldName: String; F: TField): TJSONData;
     Function AddFieldToJSON(O: TJSONObject; AFieldName: String; F: TField): TJSONData;
     function GetDataContentType: String; override;
     function GetDataContentType: String; override;
@@ -56,6 +62,8 @@ type
     Procedure DoInsertRecord(ResponseContent : TStream); override;
     Procedure DoInsertRecord(ResponseContent : TStream); override;
     Procedure DoUpdateRecord(ResponseContent : TStream); override;
     Procedure DoUpdateRecord(ResponseContent : TStream); override;
     Procedure DoDeleteRecord(ResponseContent : TStream); override;
     Procedure DoDeleteRecord(ResponseContent : TStream); override;
+  Public
+    Destructor destroy; override;
   Published
   Published
     // Called before any fields are added to row object (passed to handler).
     // Called before any fields are added to row object (passed to handler).
     Property AfterRowToJSON : TJSONObjectEvent Read FAfterRowToJSON Write FAfterRowToJSON;
     Property AfterRowToJSON : TJSONObjectEvent Read FAfterRowToJSON Write FAfterRowToJSON;
@@ -369,6 +377,7 @@ begin
     L:=Resp.AsJSON;
     L:=Resp.AsJSON;
     If Length(L)>0 then
     If Length(L)>0 then
       ResponseContent.WriteBuffer(L[1],Length(L));
       ResponseContent.WriteBuffer(L[1],Length(L));
+    Resp.Add('root',RowsProperty);
     Resp.Add(RowsProperty,TJSONArray.Create());
     Resp.Add(RowsProperty,TJSONArray.Create());
     If Assigned(FOnErrorResponse) then
     If Assigned(FOnErrorResponse) then
       FOnErrorResponse(Self,E,Resp);
       FOnErrorResponse(Self,E,Resp);
@@ -377,7 +386,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-procedure TExtJSJSONDataFormatter.SendSuccess(ResponseContent: TStream; AddIDValue : Boolean = False; CallBack : TJSONObjectEvent = Nil);
+procedure TExtJSJSONDataFormatter.SendSuccess(ResponseContent: TStream; AddIDValue : Boolean = False);
 
 
 Var
 Var
    Resp : TJSonObject;
    Resp : TJSonObject;
@@ -387,9 +396,14 @@ begin
   try
   try
     Resp:=TJsonObject.Create;
     Resp:=TJsonObject.Create;
     Resp.Add(SuccessProperty,True);
     Resp.Add(SuccessProperty,True);
-    Resp.Add(Provider.IDFieldName,Provider.IDFieldValue);
-    If Assigned(CallBack) then
-      CallBack(Self,Resp);
+    Resp.Add('root',Self.RowsProperty);
+    If Assigned(FBatchResult) and (FBatchResult.Count>0) then
+      begin
+      Resp.Add(Self.RowsProperty,FBatchResult);
+      FBatchResult:=Nil;
+      end
+    else
+      Resp.Add(Self.RowsProperty,TJSONNull.Create());
     L:=Resp.AsJSON;
     L:=Resp.AsJSON;
     ResponseContent.WriteBuffer(L[1],Length(L));
     ResponseContent.WriteBuffer(L[1],Length(L));
   finally
   finally
@@ -397,23 +411,65 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure TExtJSJSONDataFormatter.StartBatch(ResponseContent: TStream);
+begin
+  If Assigned(FBatchResult) then
+    FBatchResult.Clear
+  else
+    FBatchResult:=TJSONArray.Create();
+end;
+
+procedure TExtJSJSONDataFormatter.NextBatchItem(ResponseContent: TStream);
+begin
+end;
+
+procedure TExtJSJSONDataFormatter.EndBatch(ResponseContent: TStream);
+begin
+  SendSuccess(Responsecontent,True);
+end;
+
+Function TExtJSJSONDataFormatter.AddIdToBatch : TJSONObject;
+
+begin
+  Result:=TJSONObject.Create([Provider.IDFieldName,Provider.IDFieldValue]);
+  FBatchResult.Add(Result);
+end;
+
 procedure TExtJSJSONDataFormatter.DoInsertRecord(ResponseContent: TStream);
 procedure TExtJSJSONDataFormatter.DoInsertRecord(ResponseContent: TStream);
 
 
+Var
+  D : TJSONObject;
+
 begin
 begin
   Inherited;
   Inherited;
-  SendSuccess(ResponseContent,True,FAfterInsert);
+  D:=AddIDToBatch;
+  If Assigned(FAfterInsert) then
+    FAfterInsert(Self,D);
 end;
 end;
 
 
 procedure TExtJSJSONDataFormatter.DoUpdateRecord(ResponseContent: TStream);
 procedure TExtJSJSONDataFormatter.DoUpdateRecord(ResponseContent: TStream);
+
+Var
+  D : TJSONObject;
+
 begin
 begin
   inherited DoUpdateRecord(ResponseContent);
   inherited DoUpdateRecord(ResponseContent);
-  SendSuccess(ResponseContent,False,FAfterUpdate);
+  D:=AddIDToBatch;
+  If Assigned(FAfterUpdate) then
+    FAfterUpdate(Self,D);
 end;
 end;
 
 
 procedure TExtJSJSONDataFormatter.DoDeleteRecord(ResponseContent: TStream);
 procedure TExtJSJSONDataFormatter.DoDeleteRecord(ResponseContent: TStream);
 begin
 begin
   inherited DoDeleteRecord(ResponseContent);
   inherited DoDeleteRecord(ResponseContent);
-  SendSuccess(ResponseContent,False,FAfterDelete);
+  If Assigned(FAfterDelete) then
+    FAfterDelete(Self,Nil);
+end;
+
+destructor TExtJSJSONDataFormatter.destroy;
+begin
+  FreeAndNil(FBatchResult);
+  inherited destroy;
 end;
 end;
 
 
 { TExtJSJSonWebdataInputAdaptor }
 { TExtJSJSonWebdataInputAdaptor }
@@ -459,6 +515,17 @@ begin
     end;
     end;
 end;
 end;
 
 
+procedure TExtJSJSonWebdataInputAdaptor.reset;
+begin
+  If (FRows=Nil) then
+    FreeAndNil(FCurrentRow)
+  else
+    FreeAndNil(FRows);
+  FRowIndex:=0;
+  FreeAndNil(FIDValue);
+  inherited reset;
+end;
+
 function TExtJSJSonWebdataInputAdaptor.GetNextBatch: Boolean;
 function TExtJSJSonWebdataInputAdaptor.GetNextBatch: Boolean;
 begin
 begin
   If (FRows=Nil) then
   If (FRows=Nil) then

+ 52 - 25
packages/fcl-web/src/webdata/fpwebdata.pp

@@ -144,6 +144,9 @@ type
     procedure SetAdaptor(const AValue: TCustomWebDataInputAdaptor);
     procedure SetAdaptor(const AValue: TCustomWebDataInputAdaptor);
     procedure SetDataProvider(const AValue: TFPCustomWebDataProvider);
     procedure SetDataProvider(const AValue: TFPCustomWebDataProvider);
   Protected
   Protected
+    Procedure StartBatch(ResponseContent : TStream); virtual;
+    Procedure NextBatchItem(ResponseContent : TStream); virtual;
+    Procedure EndBatch(ResponseContent : TStream); virtual;
     Function GetDataContentType : String; virtual;
     Function GetDataContentType : String; virtual;
     procedure DatasetToStream(Stream: TStream); virtual;abstract;
     procedure DatasetToStream(Stream: TStream); virtual;abstract;
     Function CreateAdaptor(ARequest : TRequest) : TCustomWebdataInputAdaptor; virtual;
     Function CreateAdaptor(ARequest : TRequest) : TCustomWebdataInputAdaptor; virtual;
@@ -869,6 +872,22 @@ begin
     FDataProvider.FreeNotification(Self);
     FDataProvider.FreeNotification(Self);
 end;
 end;
 
 
+procedure TCustomHTTPDataContentProducer.StartBatch(ResponseContent: TStream);
+begin
+  // Do nothing
+end;
+
+procedure TCustomHTTPDataContentProducer.NextBatchItem(ResponseContent: TStream
+  );
+begin
+  // do nothing
+end;
+
+procedure TCustomHTTPDataContentProducer.EndBatch(ResponseContent: TStream);
+begin
+  // do nothing
+end;
+
 function TCustomHTTPDataContentProducer.GetDataContentType: String;
 function TCustomHTTPDataContentProducer.GetDataContentType: String;
 begin
 begin
   Result:='';
   Result:='';
@@ -888,6 +907,7 @@ Var
   A : TCustomWebdataInputAdaptor;
   A : TCustomWebdataInputAdaptor;
 
 
 begin
 begin
+  {$ifdef wmdebug}SendDebugFmt('Request content %s',[ARequest.Content]);{$endif}
   B:=(Adaptor=Nil);
   B:=(Adaptor=Nil);
   if B then
   if B then
     begin
     begin
@@ -896,36 +916,43 @@ begin
     end;
     end;
   try
   try
     try
     try
-      While Adaptor.GetNextBatch do
-        begin
-        {$ifdef wmdebug}SendDebug('Starting batch Loop');{$endif}
-        Case Adaptor.Action of
-          wdaInsert  : DoInsertRecord(Content);
-          wdaUpdate  : begin
-                      {$ifdef wmdebug}SendDebug('Aha1');{$endif}
-                      DoUpdateRecord(Content);
-                      {$ifdef wmdebug}SendDebug('Aha2');{$endif}
-                      end;
-          wdaDelete  : DoDeleteRecord(Content);
-          wdaRead    : DoReadRecords(Content);
-          wdaUnknown : Raise EFPHTTPError.Create(SErrNoAction);
-        else
-          inherited DoGetContent(ARequest, Content,Handled);
-        end;
-        if (Adaptor.Action in [wdaInsert,wdaUpdate,wdaDelete,wdaRead]) then
-          Handled:=true;
+      Case Adaptor.Action of
+        wdaRead : DoReadRecords(Content);
+        wdaInsert,
+        wdaUpdate,
+        wdaDelete :
+          begin
+          {$ifdef wmdebug}SendDebug('Starting batch Loop');{$endif}
+          StartBatch(Content);
+          While Adaptor.GetNextBatch do
+            begin
+            {$ifdef wmdebug}SendDebug('Next batch item');{$endif}
+            NextBatchItem(Content);
+            Case Adaptor.Action of
+              wdaInsert  : DoInsertRecord(Content);
+              wdaUpdate  : DoUpdateRecord(Content);
+              wdaDelete  : DoDeleteRecord(Content);
+            else
+              inherited DoGetContent(ARequest, Content,Handled);
+            end;
+          end;
+         EndBatch(Content);
         {$ifdef wmdebug}SendDebug('Ended batch Loop');{$endif}
         {$ifdef wmdebug}SendDebug('Ended batch Loop');{$endif}
-        end;
-    except
-    On E : Exception do
-      begin
-      DoExceptionToStream(E,Content);
-      Handled:=True;
+         end;
+      else
+        Raise EFPHTTPError.Create(SErrNoAction);
       end;
       end;
+      Handled:=true;
+    except
+      On E : Exception do
+        begin
+        DoExceptionToStream(E,Content);
+        Handled:=True;
+        end;
     end;
     end;
   finally
   finally
     If B then
     If B then
-     FreeAndNil(A);
+      FreeAndNil(A);
   end;
   end;
 end;
 end;