Jump to content
Sign in to follow this  
WalkingAway

TFDDataSet descendant, overriding FetchOptions.Mode from design time value

Recommended Posts

I have such a test classes (copy of TFDAdaptedDataSet, TFDCustomMemTable and TFDMemTable):

unit xDataSet;

interface

uses
  System.SysUtils, System.Classes, Data.DB,
  FireDAC.Comp.DataSet,
  Firedac.Stan.Def,
  Firedac.Stan.Param,
  Firedac.Stan.Option,
  Firedac.DatS,
  Firedac.Phys.Intf;

type
  TxBaseDataSet = class(TFDDataSet)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }

    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    { TFDDataSet }
    property FetchOptions;
    property FormatOptions;
    property ResourceOptions;
    property SecurityOptions;
    property UpdateOptions;
  published
    { Published declarations }
  end;

  TxCustomDataSet = class(TxBaseDataSet)
  private
    FOptionsIntf: IFDStanOptions;
  protected
    FStoreDefs: Boolean;

    property StoreDefs: Boolean read FStoreDefs write FStoreDefs default False;
    // IFDStanOptions
    function GetOptionsIntf: IFDStanOptions; override;
    procedure GetParentOptions(var AOpts: IFDStanOptions);
 public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

  TxDataSet = class(TxCustomDataSet)
  published
    property ActiveStoredUsage;
    { TDataSet }
    property Active;
    property AutoCalcFields;
    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 BeforeRefresh;
    property AfterRefresh;
    property OnCalcFields;
    property OnDeleteError;
    property OnEditError;
    property OnNewRecord;
    property OnPostError;
    property FieldOptions;
    property Filtered;
    property FilterOptions;
    property Filter;
    property OnFilterRecord;
    property ObjectView default True;
    property Constraints;
    property DataSetField;
    property FieldDefs stored FStoreDefs;
    { TFDDataSet }
    property CachedUpdates;
    property FilterChanges;
    property IndexDefs stored FStoreDefs;
    property Indexes;
    property IndexesActive;
    property IndexName;
    property IndexFieldNames;
    property Aggregates;
    property AggregatesActive;
    property ConstraintsEnabled;
    property MasterSource;
    property MasterFields;
    property DetailFields;
    property OnUpdateRecord;
    property OnUpdateError;
    property OnReconcileError;
    property BeforeApplyUpdates;
    property AfterApplyUpdates;
    property BeforeGetRecords;
    property AfterGetRecords;
    property AfterGetRecord;
    property BeforeRowRequest;
    property AfterRowRequest;
    property BeforeExecute;
    property AfterExecute;
    property FetchOptions;
    property FormatOptions;
    property ResourceOptions;
    property SecurityOptions;
    property UpdateOptions;

    { TxCustomCustomDataset }
    property StoreDefs;
 public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TxDataSet]);
end;

{ TxBaseDataSet }

constructor TxBaseDataSet.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

destructor TxBaseDataSet.Destroy;
begin
  inherited Destroy;
end;

{ TxustomCustomDataset }

constructor TxCustomDataSet.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

destructor TxCustomDataSet.Destroy;
begin
  Close;
  Destroying;
  inherited Destroy;
  if FOptionsIntf <> nil then begin
    FOptionsIntf.ObjectDestroyed(Self);
    FOptionsIntf := nil;
  end;
end;

function TxCustomDataSet.GetOptionsIntf: IFDStanOptions;
begin
  if FOptionsIntf = nil then
    FOptionsIntf := TFDOptionsContainer.Create(Self, TFDFetchOptions,
      TFDBottomUpdateOptions, TFDBottomResourceOptions, GetParentOptions);
  Result := FOptionsIntf;
end;

procedure TxCustomDataSet.GetParentOptions(var AOpts: IFDStanOptions);
begin
  AOpts := nil;
end;

{ TxDataSet }

constructor TxDataSet.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

destructor TxDataSet.Destroy;
begin
  inherited Destroy;
end;

end.

Nothing wrong here. But I can not add quite simple thing - I want to override FetchOptions.Mode (let say, it alway has to be fmAll - no metter what was set in design time).

What I see - value from design time is always som how written in the end. So, when I put 

FetchOptions.Mode = fmAll

to all three available Create methods - constractors,  nothing changes.

How to do it?

 

Also, if I put component to form and ask its mode, it is always what was set in design time.

procedure TForm22.Button1Click(Sender: TObject);
begin
  case xDataSet1.FetchOptions.Mode of
    fmAll: begin
      ShowMessage('fmAll');
    end;
    fmExactRecsMax: begin
      ShowMessage('fmExactRecsMax');
    end;
    fmManual: begin
      ShowMessage('fmManual');
    end;
    fmOnDemand: begin
      ShowMessage('fmOnDemand');
    end;
  end;
end;

Thank you.

Edited by WalkingAway
mistakes

Share this post


Link to post

Components dropped at design time load their properties from the dfm file after the constructor has executed. To set a property to a different value you need to override the Loaded method and do the deed there.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×