Skip to content

Commit

Permalink
Create fields def
Browse files Browse the repository at this point in the history
I can not use an disable dataset. If is a FDMemTable I can create the fields by the JSON and open. Otherwise, I must cancel the process, because can be a query with database connection.
  • Loading branch information
mateusvicente100 committed Aug 20, 2019
1 parent 4351398 commit 79387e3
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/core/DataSet.Serialize.JSON.Impl.pas
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ TJSONSerialize = class
/// The key values of the ADataSet parameter.
/// </returns>
function GetKeyValuesDataSet(const ADataSet: TDataSet; const AJSONObject: TJSONObject): TKeyValues;
/// <summary>
/// Load the fields into the dataset.
/// </summary>
procedure LoadFieldsFromJSON(const ADataSet: TDataSet; const AJSONObject: TJSONObject);
public
/// <summary>
/// Responsible for creating a new isntância of TDataSetSerialize class.
Expand Down Expand Up @@ -165,7 +169,8 @@ TJSONSerialize = class
implementation

uses System.Classes, System.SysUtils, System.NetEncoding, System.TypInfo, System.DateUtils, Providers.DataSet.Serialize.Constants,
System.Generics.Collections, System.Variants, UpdatedStatus.Types;
System.Generics.Collections, System.Variants, UpdatedStatus.Types,
FireDAC.Comp.Client;

{ TJSONSerialize }

Expand All @@ -180,6 +185,16 @@ procedure TJSONSerialize.JSONObjectToDataSet(const AJSONObject: TJSONObject; con
begin
if (not Assigned(AJSONObject)) or (not Assigned(ADataSet)) then
Exit;

if not(ADataSet.Active) then
begin
if not(ADataSet is TFDMemTable) then
Exit;
if ADataSet.FieldCount = 0 then
LoadFieldsFromJSON(ADataSet, AJSONObject);
ADataSet.Open;
end;

if AJSONObject.TryGetValue(OBJECT_STATE, LObjectState) then
begin
if TUpdateStatus.usInserted.ToString.Equals(LObjectState) then
Expand Down Expand Up @@ -318,6 +333,21 @@ procedure TJSONSerialize.LoadBlobFieldFromStream(const AField: TField; const AJS
end;
end;

procedure TJSONSerialize.LoadFieldsFromJSON(const ADataSet: TDataSet; const AJSONObject: TJSONObject);
var
JSONPair: TJSONPair;
begin
for JSONPair in AJSONObject do
begin
with ADataSet.FieldDefs.AddFieldDef do
begin
Name := JSONPair.JsonString.Value;
DataType := ftString;
Size := 4096;
end;
end;
end;

function TJSONSerialize.LoadFieldStructure(const AJSONValue: TJSONValue): TFieldStructure;
begin
Result.FieldType := TFieldType(GetEnumValue(TypeInfo(TFieldType), AJSONValue.GetValue<string>('DataType')));
Expand Down

0 comments on commit 79387e3

Please sign in to comment.