Browse Source

* Merging revisions 576 from trunk:
------------------------------------------------------------------------
r576 | michael | 2019-08-26 20:36:19 +0200 (Mon, 26 Aug 2019) | 1 line

* Prevent calc fields spilling over from current record to edit buffer
------------------------------------------------------------------------

michael 6 years ago
parent
commit
22d6441e06
1 changed files with 32 additions and 3 deletions
  1. 32 3
      packages/fcl-db/jsondataset.pas

+ 32 - 3
packages/fcl-db/jsondataset.pas

@@ -35,6 +35,8 @@ type
   // This class is responsible for mapping the field objects of the records.
   // This class is responsible for mapping the field objects of the records.
   TJSONFieldMapper = Class(TObject)
   TJSONFieldMapper = Class(TObject)
   Public
   Public
+    // Remove a field from the
+    Procedure RemoveField(Const FieldName : String; FieldIndex : Integer; Row : JSValue); virtual; abstract;
     // Return row TJSONData instance with data for field 'FieldName' or 'FieldIndex'.
     // Return row TJSONData instance with data for field 'FieldName' or 'FieldIndex'.
     Function GetJSONDataForField(Const FieldName : String; FieldIndex : Integer; Row : JSValue) : JSValue; virtual; abstract;
     Function GetJSONDataForField(Const FieldName : String; FieldIndex : Integer; Row : JSValue) : JSValue; virtual; abstract;
     // Same, but now based on TField.
     // Same, but now based on TField.
@@ -276,6 +278,7 @@ type
     FRowType: TJSONRowType;
     FRowType: TJSONRowType;
     FFilterExpression : TFPExpressionParser;
     FFilterExpression : TFPExpressionParser;
     function GetFilterField(const AName: String): TFPExpressionResult;
     function GetFilterField(const AName: String): TFPExpressionResult;
+    procedure RemoveCalcFields(Buf: JSValue);
     procedure SetActiveIndex(AValue: String);
     procedure SetActiveIndex(AValue: String);
     procedure SetIndexes(AValue: TJSONIndexDefs);
     procedure SetIndexes(AValue: TJSONIndexDefs);
     procedure SetMetaData(AValue: TJSObject);
     procedure SetMetaData(AValue: TJSObject);
@@ -413,6 +416,7 @@ type
   // Fieldmapper to be used when the data is in an object
   // Fieldmapper to be used when the data is in an object
   TJSONObjectFieldMapper = Class(TJSONFieldMapper)
   TJSONObjectFieldMapper = Class(TJSONFieldMapper)
   Public
   Public
+    Procedure RemoveField(Const FieldName : String; FieldIndex : Integer; Row : JSValue); override;
     procedure SetJSONDataForField(Const FieldName : String; FieldIndex{%H-} : Integer; Row,Data : JSValue); override;
     procedure SetJSONDataForField(Const FieldName : String; FieldIndex{%H-} : Integer; Row,Data : JSValue); override;
     Function GetJSONDataForField(Const FieldName : String; FieldIndex{%H-} : Integer; Row : JSValue) : JSValue; override;
     Function GetJSONDataForField(Const FieldName : String; FieldIndex{%H-} : Integer; Row : JSValue) : JSValue; override;
     Function CreateRow : JSValue; override;
     Function CreateRow : JSValue; override;
@@ -422,6 +426,7 @@ type
   // Fieldmapper to be used when the data is in an array
   // Fieldmapper to be used when the data is in an array
   TJSONArrayFieldMapper = Class(TJSONFieldMapper)
   TJSONArrayFieldMapper = Class(TJSONFieldMapper)
   Public
   Public
+    Procedure RemoveField(Const FieldName : String; FieldIndex : Integer; Row : JSValue); override;
     procedure SetJSONDataForField(Const FieldName{%H-} : String; FieldIndex : Integer; Row,Data : JSValue); override;
     procedure SetJSONDataForField(Const FieldName{%H-} : String; FieldIndex : Integer; Row,Data : JSValue); override;
     Function GetJSONDataForField(Const FieldName{%H-} : String; FieldIndex : Integer; Row : JSValue) : JSValue; override;
     Function GetJSONDataForField(Const FieldName{%H-} : String; FieldIndex : Integer; Row : JSValue) : JSValue; override;
     Function CreateRow : JSValue; override;
     Function CreateRow : JSValue; override;
@@ -977,14 +982,18 @@ end;
 
 
 { TJSONArrayFieldMapper }
 { TJSONArrayFieldMapper }
 
 
+procedure TJSONArrayFieldMapper.RemoveField(const FieldName: String; FieldIndex: Integer; Row: JSValue);
+begin
+  TJSArray(Row).Splice(FieldIndex,1);
+end;
+
 procedure TJSONArrayFieldMapper.SetJSONDataForField(const FieldName: String;
 procedure TJSONArrayFieldMapper.SetJSONDataForField(const FieldName: String;
   FieldIndex: Integer; Row, Data: JSValue);
   FieldIndex: Integer; Row, Data: JSValue);
 begin
 begin
   TJSValueDynArray(Row)[FieldIndex]:=Data;
   TJSValueDynArray(Row)[FieldIndex]:=Data;
 end;
 end;
 
 
-function TJSONArrayFieldMapper.GetJSONDataForField(Const FieldName: String;
-  FieldIndex: Integer; Row: JSValue): JSValue;
+function TJSONArrayFieldMapper.GetJSONDataForField(const FieldName: String; FieldIndex: Integer; Row: JSValue): JSValue;
 begin
 begin
   Result:=TJSValueDynArray(Row)[FieldIndex];
   Result:=TJSValueDynArray(Row)[FieldIndex];
 end;
 end;
@@ -997,6 +1006,11 @@ end;
 
 
 { TJSONObjectFieldMapper }
 { TJSONObjectFieldMapper }
 
 
+procedure TJSONObjectFieldMapper.RemoveField(const FieldName: String; FieldIndex: Integer; Row: JSValue);
+begin
+  jsDelete(Row,FieldName);
+end;
+
 procedure TJSONObjectFieldMapper.SetJSONDataForField(const FieldName: String;
 procedure TJSONObjectFieldMapper.SetJSONDataForField(const FieldName: String;
   FieldIndex: Integer; Row, Data: JSValue);
   FieldIndex: Integer; Row, Data: JSValue);
 begin
 begin
@@ -1374,13 +1388,28 @@ begin
     end;
     end;
 end;
 end;
 
 
+procedure TBaseJSONDataSet.RemoveCalcFields(Buf : JSValue);
+
+Var
+  i : integer;
+
+begin
+  For I:=0 to Fields.Count-1 do
+    if Fields[i].FieldKind in [fkCalculated,fkInternalCalc] then
+      FieldMapper.RemoveField(FIelds[i].FieldName,FIelds[i].Index,Buf);
+
+end;
+
 procedure TBaseJSONDataSet.InternalEdit;
 procedure TBaseJSONDataSet.InternalEdit;
 
 
 begin
 begin
 //  Writeln('TBaseJSONDataSet.InternalEdit:  ');
 //  Writeln('TBaseJSONDataSet.InternalEdit:  ');
   FEditIdx:=FCurrentIndex.RecordIndex[FCurrent];
   FEditIdx:=FCurrentIndex.RecordIndex[FCurrent];
   if not isUndefined(Rows[FEditIdx]) then
   if not isUndefined(Rows[FEditIdx]) then
-    FEditRow:=TJSJSON.parse(TJSJSON.stringify(Rows[FEditIdx]))
+    begin
+    FEditRow:=TJSJSON.parse(TJSJSON.stringify(Rows[FEditIdx]));
+    RemoveCalcFields(FEditRow);
+    end
   else
   else
     FEditRow:=TJSObject.new;
     FEditRow:=TJSObject.new;
 //  Writeln('TBaseJSONDataSet.InternalEdit: ',FEditRow);
 //  Writeln('TBaseJSONDataSet.InternalEdit: ',FEditRow);