Page 1 of 1

How BeforeOpen is implemented in SDAC ?

Posted: Wed 30 Nov 2011 11:41
by Petya
Hello folks,

Recently I replaced ZeosLib with SDAC in a 6 years old project.
ZeosLib didn't execute TDataSet.BeforeOpen on an ExecSQL call; just for Open.
Therefore I wrote a wrapper class for its query component, and now I wanna implement it for SDAC.

My question is that should I leave the marked (<- XXX) BeforeOpen call in my derived class (TMyMSQuery), or SDAC does it properly ?

Code: Select all

  TMyMSQuery	= class(TMSQuery)
  private
    procedure BeforeOpenHandler(DataSet: TDataSet);
  public
    constructor Create(AOwner: TComponent); override;
    procedure ExecSQL;
    procedure Open;
  end;

// *********************************************

constructor TMyMSQuery.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  BeforeOpen:=BeforeOpenHandler;
end;

procedure TMyMSQuery.Open;
begin
  if IniParams.Database.ProtectQueries then
  try
     DatabaseRWCriticalSection.Enter;
     inherited Open;
  finally
     DatabaseRWCriticalSection.Leave;
  end
  else inherited Open;
end;

procedure TMyMSQuery.ExecSQL;
begin
  if Assigned(BeforeOpen) then BeforeOpen(Self);       // <- XXX - leave it or not ?
  if IniParams.Database.ProtectQueries then
  try
     DatabaseRWCriticalSection.Enter;
     inherited ExecSQL;
  finally
     DatabaseRWCriticalSection.Leave;
  end
  else inherited ExecSQL;
end;

{$B-}
procedure TMyMSQuery.BeforeOpenHandler(DataSet: TDataSet);
begin
  if IniParams.Log.Database and (DataSet.ClassName='TMyMSQuery') then FileLog(DatabaseAll,Linearize((DataSet as TMSQuery).SQL.Text));
end;
Thanks, Peter

Posted: Thu 01 Dec 2011 11:12
by AndreyZ
Hello,

If SQL statement is a query, ExecSQL calls the Open method. That's why the following code will call the BeforeOpen event:

Code: Select all

MSQuery.SQL.Text := 'select * from table';
MSQuery.ExecSQL;
, but this code will not call it:

Code: Select all

MSQuery.SQL.Text := 'update table set fieldname=''test'' where id=1';
MSQuery.ExecSQL;
The BeforeOpen event is called when SQL statement returns result set. You can use the BeforeExecute event instead of BeforeOpen. The BeforeExecute is called always despite of SQL statement returns result set or not.

Posted: Thu 01 Dec 2011 13:38
by Petya
Hello AndreyZ,

The help file I got in my package does not contain TMSQuery.BeforeExecute event declaration, neither of its ancestor classes, not even TDataSet. It does not accept BeforeOpen's syntax.
Where can I find the correct parameter list ?

Thanks, Peter

Ooops ... :D

Posted: Thu 01 Dec 2011 14:05
by Petya
Okay, I found it ... TNotifyEvent ... :lol:

Sometimes it happens you waste time overcomplicating things ... :P
http://www.petyamaster.hu/dxn/blog/2011 ... r_life.jpg

Okay, it worx 8)
Thanks AndreyZ.

Posted: Fri 02 Dec 2011 08:14
by AndreyZ
Feel free to contact us if you have any further questions about SDAC.