Skip to content

Commit

Permalink
Don't export an unvisbile dataset
Browse files Browse the repository at this point in the history
When a dataset has all fields unvisible it cannot be exported.
  • Loading branch information
mateusvicente100 committed Jul 9, 2021
1 parent aead608 commit 4d7cf49
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/DataSet.Serialize.Export.pas
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ TDataSetSerialize = class
/// </summary>
function HasChildModification(const ADataSet: TDataSet): Boolean;
{$ENDIF}
/// <summary>
/// Verifify if a DataSet has at least one visible field.
/// </summary>
function HasVisibleFields(const ADataSet: TDataSet): Boolean;
public
/// <summary>
/// Responsible for creating a new instance of TDataSetSerialize class.
Expand Down Expand Up @@ -277,13 +281,19 @@ function TDataSetSerialize.DataSetToJSONObject(const ADataSet: TDataSet; const A
begin
if FOnlyUpdatedRecords then
TFDDataSet(LNestedDataSet).FilterChanges := [rtInserted, rtModified, rtDeleted, rtUnmodified];
if TDataSetSerializeConfig.GetInstance.Export.ExportEmptyDataSet or (LNestedDataSet.RecordCount > 0) then
try
if (not TDataSetSerializeConfig.GetInstance.Export.ExportEmptyDataSet) and (LNestedDataSet.RecordCount = 0) then
Continue;
if TDataSetSerializeConfig.GetInstance.Export.ExportOnlyFieldsVisible and (not HasVisibleFields(LNestedDataSet)) then
Continue;
if TDataSetSerializeConfig.GetInstance.Export.ExportChildDataSetAsJsonObject and (LNestedDataSet.RecordCount = 1) then
Result.AddPair(TDataSetSerializeUtils.FormatDataSetName(LNestedDataSet.Name), DataSetToJsonObject(LNestedDataSet))
else
Result.AddPair(TDataSetSerializeUtils.FormatDataSetName(LNestedDataSet.Name), DataSetToJSONArray(LNestedDataSet, True));
if FOnlyUpdatedRecords then
TFDDataSet(LNestedDataSet).FilterChanges := [rtInserted, rtModified, rtUnmodified];
finally
if FOnlyUpdatedRecords then
TFDDataSet(LNestedDataSet).FilterChanges := [rtInserted, rtModified, rtUnmodified];
end;
end;
finally
LDataSetDetails.Free;
Expand Down Expand Up @@ -323,6 +333,18 @@ function TDataSetSerialize.EncodingBlobField(const AField: TField): string;
end;
end;

function TDataSetSerialize.HasVisibleFields(const ADataSet: TDataSet): Boolean;
var
LField: TField;
begin
Result := False;
for LField in ADataSet.Fields do
begin
if LField.Visible then
Exit(True);
end;
end;

{$IF NOT DEFINED(FPC)}
function TDataSetSerialize.HasChildModification(const ADataSet: TDataSet): Boolean;
var
Expand Down

0 comments on commit 4d7cf49

Please sign in to comment.