unit XData.Web.JsonDataset;

{$I XData.inc}

interface

uses
  Types, DB, Classes, SysUtils;

type
  JSValue = type Variant;

  TBaseJSONDataSet = class(TDataSet)
  end;

  TUpdateStatus = (usUnmodified, usModified, usInserted, usDeleted, usResolved, usResolveFailed);
  TBookmark = record
    Data : JSValue;
    Flag : TBookmarkFlag;
  end; // Bookmark is always the index in the data array.

  TResolveInfo = record
    Data : JSValue;
    Status : TUpdateStatus;
    Error : String; // Only filled on error.
    BookMark : TBookmark;
    _private : JSValue; // for use by descendents of TDataset
  end;
  TResolveInfoArray = array of TResolveInfo;

  TResolveResults = record
    Records : TResolveInfoArray;
  end;

  TApplyUpdatesEvent = Procedure(Sender: TDataset; Info: TResolveResults) of object;

  {$IFDEF DELPHIXE4_LVL}
  TXDataRecordBuffer = TRecBuf;
  {$ELSE}
  TXDataRecordBuffer = TRecordBuffer;
  {$ENDIF}

  TXDataWebEntityField = class(TVariantField)
  end;

  TXDataWebDatasetField = class(TVariantField)
  end;

//  TXDataWebGuidField = class(TStringField)
//  end;

  TXDataJsonDataset = class(TBaseJsonDataset)
  private
    FAfterApplyUpdates: TApplyUpdatesEvent;
  protected
    function GetRecord(Buffer: TXDataRecordBuffer; GetMode: TGetMode; DoCheck: boolean): TGetResult; override;
    function IsCursorOpen: Boolean; override;
    procedure InternalClose; override;
    procedure InternalOpen; override;
    procedure InternalHandleException; override;
  protected
    function GetFieldClass(FieldType: TFieldType): TFieldClass; override;
    procedure InternalInitFieldDefs; override;
  public
    procedure SetJsonData(Data: JSValue);
  published
    // redeclared data set properties
//    property Active;
    property AfterApplyUpdates: TApplyUpdatesEvent read FAfterApplyUpdates write FAfterApplyUpdates;
    property BeforeOpen;
    property AfterOpen;
    property BeforeClose;
    property AfterClose;
    property BeforeInsert;
    property AfterInsert;
    property BeforeEdit;
    property AfterEdit;
    property BeforePost;
    property AfterPost;
    property BeforeCancel;
    property AfterCancel;
    property BeforeDelete;
    property AfterDelete;
    property BeforeScroll;
    property AfterScroll;
    property OnCalcFields;
    property OnDeleteError;
    property OnEditError;
    property OnFilterRecord;
    property OnNewRecord;
    property OnPostError;
  end;

implementation

{ TXDataJsonDataset }

function TXDataJsonDataset.GetFieldClass(FieldType: TFieldType): TFieldClass;
begin
  case FieldType of
    ftVariant: Result := TXDataWebEntityField;
    ftDataset: Result := TXDataWebDatasetField;
    ftCurrency: Result := TFloatField;
    ftGuid: Result := TStringField;
  else
    result := inherited GetFieldClass(FieldType);
  end;
end;

function TXDataJsonDataset.GetRecord(Buffer: TXDataRecordBuffer; GetMode: TGetMode;
  DoCheck: Boolean): TGetResult;
begin
  Result := TGetResult.grEOF;
end;

procedure TXDataJsonDataset.InternalClose;
begin
end;

procedure TXDataJsonDataset.InternalHandleException;
begin
  if Assigned(Classes.ApplicationHandleException) then
    ApplicationHandleException(Self);
end;

procedure TXDataJsonDataset.InternalInitFieldDefs;
begin
end;

procedure TXDataJsonDataset.InternalOpen;
begin
end;

function TXDataJsonDataset.IsCursorOpen: Boolean;
begin
  Result := False;
end;

procedure TXDataJsonDataset.SetJsonData(Data: JSValue);
begin
end;

end.
