How BeforeOpen is implemented in SDAC ?

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Petya
Posts: 21
Joined: Wed 28 Jul 2010 11:24

How BeforeOpen is implemented in SDAC ?

Post by Petya » Wed 30 Nov 2011 11:41

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

AndreyZ

Post by AndreyZ » Thu 01 Dec 2011 11:12

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.

Petya
Posts: 21
Joined: Wed 28 Jul 2010 11:24

Post by Petya » Thu 01 Dec 2011 13:38

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

Petya
Posts: 21
Joined: Wed 28 Jul 2010 11:24

Ooops ... :D

Post by Petya » Thu 01 Dec 2011 14:05

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.

AndreyZ

Post by AndreyZ » Fri 02 Dec 2011 08:14

Feel free to contact us if you have any further questions about SDAC.

Post Reply