Examples Delphi

Title: Query Activator
Question: If we forget to activate all the necessary TQueris at design time, it results in errors during execution: Cannot perform operation on closed dataset.
Answer:
We frequently make the dataset inactive during design and foreget to make it active again, which results in error during execution.
To overcome this, I developed TQueryActivator and TEnhQuery and just activate the QueryActivator to make all the registered EnhQueries active.
You can have more than one TQueryActivator on a form and activate/deactivate at appropriate instances. You can register more than one Query to a ueryActivator.
This can be enhanced to append, edit, save / applyupdates to a group of Queries,in a loop.Let me work on that.
The components get installed in Enh_Db pallet.
Here is the source for TQueryActivator and TEnhQuery..Enjoy.
//================================================================
unit QueryActivator;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DBTables, DB ;
type
TQueryActivator = class(TComponent)
private
{ Private declarations }
pQueries: TList;
bActive: Boolean;
procedure SetActive(bValue: Boolean);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure RegisterQuery(pValue: TQuery);
procedure UnregisterQuery(pValue: TQuery);
published
{ Published declarations }
property Active: Boolean read bActive write SetActive;
end;
procedure Register;
implementation
uses Enh_Query ;
//********
constructor TQueryActivator.Create(AOwner: TComponent);
begin
inherited;
pQueries := TList.Create;
bActive := False ;
//bConnected := True;
end;
destructor TQueryActivator.Destroy;
var
i: integer;
begin
pQueries.Free;
inherited;
end;
procedure TQueryActivator.RegisterQuery(pValue: TQuery);
begin
if(not (pValue is TEnh_Query))then
exit;
if(pQueries.IndexOf(pValue) = -1)then
begin
pValue.CachedUpdates := True;
pQueries.Add(pValue);
end;
end;
procedure TQueryActivator.UnregisterQuery(pValue: TQuery);
var
i: integer;
begin
if(not (pValue is TEnh_Query))then
exit;
i := pQueries.IndexOf(pValue);
if(i -1)then
pQueries.Delete(i)
else
raise Exception.Create('The Enh_Query ' + pValue.Name + ' not found in the managed query list.');
end;
Procedure TQueryActivator.SetActive(bValue: Boolean);
var i : smallint ;
begin
bActive := bValue;
for i:=0 to pQueries.Count-1 do
if bValue then
TEnh_Query(pQueries.Items[i]).Active := bValue;
{if(Assigned(pOpenTables))then
pOpenTables;}
end;
//********
procedure Register;
begin
RegisterComponents('Enh_DB', [TQueryActivator]);
end;
end.
//==========================================================================
//Code For TEnhQuery
//==========================================================================
unit Enh_Query;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, QueryActivator;
type
TEnh_Query = class(TQuery)
private
{ Private declarations }
pActivator: TQueryActivator;
procedure SetActivator(pValue: TQueryActivator);
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
Property QueryActivator: TQueryActivator read pActivator write SetActivator;
end;
procedure Register;
implementation
//****************
procedure TEnh_Query.SetActivator(pValue: TQueryActivator);
begin
if(pActivator pValue)then
begin
if(pActivator NIL)then
pActivator.UnregisterQuery(self);
pActivator := pValue;
if(pActivator NIL)then
begin
pActivator.RegisterQuery(self);
Active := QueryActivator.Active;
end;
end;
end;
//****************
procedure Register;
begin
RegisterComponents('Enh_DB', [TEnh_Query]);
end;
end.