unit XData.Web.Dataset;

interface

uses
  Classes, DB,
  XData.Web.Connection,
  XData.Web.DatasetCommon,
  XData.Web.JsonDataset,
  XData.Model.Classes;

const
{$IF COMPILERVERSION > 28}
  pidWeb = $10000;
{$ELSE}
  pidWeb = $1000;
{$IFEND}
  TMSWebPlatform = pidWin32 + pidWin64 + pidWeb;

type
  TUpdateStatus = XData.Web.JsonDataset.TUpdateStatus;
  TBookmark = XData.Web.JsonDataset.TBookmark;
  TServerRecordCountMode = (smNone, smInlineCount);

  [ComponentPlatforms(TMSWebPlatform)]
  TXDataWebDataSet = class(TXDataJsonDataset)
  private
    FEntitySetName: string;
    FConnection: TXDataWebConnection;
    FSubpropsDepth: Integer;
    FQueryString: string;
    FServerRecordCountMode: TServerRecordCountMode;
    FQuerySkip: Integer;
    FQueryTop: Integer;
    FServerRecordCount: Integer;
    procedure SetConnection(const Value: TXDataWebConnection);
    function GetCurrentData: JSValue;
    procedure CheckConnection;
    procedure CheckEntitySetName;
    procedure SetEntitySetName(const Value: string);
    procedure SetSubpropsDepth(const Value: Integer);
  protected
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure InternalInitFieldDefs; override;
    procedure InitializeFieldDefinitions;
  public
    procedure ApplyUpdates;
    procedure Load;
    property CurrentData: JSValue read GetCurrentData;
    property ServerRecordCount: Integer read FServerRecordCount;
  published
    property EntitySetName: string read FEntitySetName write SetEntitySetName;
    property Connection: TXDataWebConnection read FConnection write SetConnection;
    property SubpropsDepth: Integer read FSubpropsDepth write SetSubpropsDepth default 0;
    property QueryString: string read FQueryString write FQueryString;
    property QueryTop: Integer read FQueryTop write FQueryTop default 0;
    property QuerySkip: Integer read FQuerySkip write FQuerySkip default 0;
    property ServerRecordCountMode: TServerRecordCountMode read FServerRecordCountMode write FServerRecordCountMode default smNone;
  end;

implementation

{ TXDataDataSet }

procedure TXDataWebDataSet.ApplyUpdates;
begin
end;

procedure TXDataWebDataSet.CheckConnection;
begin
  if Connection = nil then
    DatabaseError('Connection not assigned');
end;

procedure TXDataWebDataSet.CheckEntitySetName;
begin
  if EntitySetName = '' then
    DatabaseError('EntitySetName not specified.');
end;

function TXDataWebDataSet.GetCurrentData: JSValue;
begin
  Result := 0;
end;

procedure TXDataWebDataSet.InitializeFieldDefinitions;
var
  Model: TXDataModel;
begin
  CheckConnection;
  CheckEntitySetName;

  Model := Connection.Model;
  if Model = nil then
    Model := Connection.RetrieveModel;
  try
    InitFieldDefsFromEntityType(Self, Model.GetEntitySet(EntitySetName).EntityType, SubPropsDepth);
  finally
    if Model <> Connection.Model then
      Model.Free;
  end;
end;

procedure TXDataWebDataSet.InternalInitFieldDefs;
begin
  FieldDefs.Clear;
  if not (csDesigning in ComponentState) and (FieldCount > 0) then
  begin
    InitFieldDefsFromFields;
    Exit;
  end;

  InitializeFieldDefinitions;
end;

procedure TXDataWebDataSet.Load;
begin
end;

procedure TXDataWebDataSet.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited;
  if (Operation = opRemove) and (AComponent = FConnection) then
    Connection := nil;
end;

procedure TXDataWebDataSet.SetConnection(const Value: TXDataWebConnection);
begin
  if Value <> FConnection then
  begin
    Active := false;
    if FConnection <> nil then
      FConnection.RemoveFreeNotification(Self);
    FConnection := Value;
    if FConnection <> nil then
      FConnection.FreeNotification(Self);
    FieldDefs.Updated := False;
  end;
end;

procedure TXDataWebDataSet.SetEntitySetName(const Value: string);
begin
  if FEntitySetName <> Value then
  begin
    FEntitySetName := Value;
    FieldDefs.Updated := False;
  end;
end;

procedure TXDataWebDataSet.SetSubpropsDepth(const Value: Integer);
begin
  if FSubpropsDepth <> Value then
  begin
    FSubpropsDepth := Value;
    FieldDefs.Updated := False;
  end;
end;

end.
