home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 February / Chip_2003-02_cd1.bin / zkuste / delphi / kompon / d34567 / KADAO77.ZIP / KADaoConnectionCheck.pas < prev    next >
Pascal/Delphi Source File  |  2001-08-21  |  7KB  |  236 lines

  1. unit KADaoConnectionCheck;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, DB, Extctrls, Dialogs, KDaoDatabase;
  7.  
  8.  
  9. type
  10.   TErrAction=(CloseDatabase,RaiseException);
  11.   TErrActionSet=Set of TErrAction;
  12.   TKADaoConnectionCheck = class(TComponent)
  13.   private
  14.     { Private declarations }
  15.     Timer              : TTimer;
  16.     InTimer            : Boolean;
  17.     Procedure            TimerProcedure(Sender: TObject);
  18.   protected
  19.     { Protected declarations }
  20.     F_Active           : Boolean;
  21.     F_CheckInterval    : Integer;
  22.     F_Database         : TKADaoDatabase;
  23.     F_ErrorCode        : Integer;
  24.     F_ExceptionText    : String;
  25.     F_OnErrorAction    : TErrActionSet;
  26.     F_OnNoConnection   : TNotifyEvent;
  27.     F_OnConnectionAgain: TNotifyEvent;
  28.  
  29.     Procedure            F_Set_Active(Value:Boolean);
  30.     Procedure            F_Set_CheckInterval(Value:Integer);
  31.     Procedure            F_Set_Database(Value:TKADaoDatabase);
  32.     Procedure            F_Set_ErrorCode(Value:Integer);
  33.     Procedure            F_Set_ExceptionText(Value: String);
  34.     Procedure            F_Set_OnErrorAction(Value:TErrActionSet);
  35.     Procedure            Activate(Value:Boolean);
  36.     Procedure            Notification(AComponent: TComponent; Operation: TOperation); override;
  37.     Procedure            Loaded; override;
  38.   public
  39.     { Public declarations }
  40.  
  41.      Constructor         Create(AOwner : TComponent); override;
  42.      Destructor          Destroy; override;
  43.   published
  44.     { Published declarations }
  45.     Property Active                : Boolean         Read F_Active            Write F_Set_Active;
  46.     Property Interval              : Integer         Read F_CheckInterval     Write F_Set_CheckInterval;
  47.     Property Database              : TKADaoDatabase  Read F_Database          Write F_Set_Database;
  48.     Property ErrorCode             : Integer         Read F_ErrorCode         Write F_Set_ErrorCode;
  49.     Property ExceptionText         : String          Read F_ExceptionText     Write F_Set_ExceptionText;
  50.     Property ErrorAction           : TErrActionSet   Read F_OnErrorAction     Write F_Set_OnErrorAction;
  51.     Property OnNoConnection        : TNotifyEvent    Read F_OnNoConnection    Write F_OnNoConnection;
  52.     Property OnConnectionRestored  : TNotifyEvent    Read F_OnConnectionAgain Write F_OnConnectionAgain;
  53.   end;
  54.  
  55. procedure Register;
  56.  
  57. implementation
  58.  
  59.  
  60. Constructor TKADaoConnectionCheck.Create(AOwner : TComponent);
  61. Begin
  62.  F_Active           := False;
  63.  F_CheckInterval    := 1000;
  64.  F_Database         := Nil;
  65.  F_ExceptionText    := 'Connection to Database %s is broken!';
  66.  F_OnErrorAction    := [];
  67.  F_ErrorCode        := 0;
  68.  InTimer            := False;
  69.  Timer              := TTimer.Create(Self);
  70.  Timer.Enabled      := False;
  71.  Timer.Interval     := F_CheckInterval;
  72.  Timer.OnTimer      := TimerProcedure;
  73.  Inherited Create(AOwner);
  74. End;
  75.  
  76. Destructor TKADaoConnectionCheck.Destroy;
  77. Begin
  78.  if F_Active Then Active := False;
  79.  Timer.OnTimer:=Nil;
  80.  Timer.Free;
  81.  Inherited Destroy;
  82. End;
  83.  
  84. Procedure TKADaoConnectionCheck.TimerProcedure(Sender: TObject);
  85. Var
  86.  Res : Integer;
  87.  Dir : TSearchRec;
  88.  S   : String;
  89. Begin
  90.  if InTimer Then Exit;
  91.  if Not Assigned(F_Database) Then Exit;
  92.  InTimer := True;
  93.  Try
  94.   Res:=FindFirst(F_Database.Database,faAnyFile,Dir);
  95.   FindClose(Dir);
  96.   if (Res <> 0) And (F_ErrorCode=0) Then
  97.      Begin
  98.        F_ErrorCode := Res;
  99.        if F_Database.Connected Then
  100.           Begin
  101.            if Assigned(F_OnNoConnection) Then F_OnNoConnection(Self);
  102.            if CloseDatabase  in F_OnErrorAction Then F_Database.Close;
  103.            if RaiseException in F_OnErrorAction Then
  104.               Begin
  105.                if Pos('%s',AnsiLowerCase(F_ExceptionText)) <> 0  Then
  106.                   S := Format(F_ExceptionText,[F_Database.Database])
  107.                Else
  108.                  S := F_ExceptionText;
  109.               DatabaseError(S);
  110.              End;
  111.           End;
  112.      End
  113.   Else
  114.      Begin
  115.        F_ErrorCode:=Res;
  116.        if (F_ErrorCode=0) And Assigned(F_OnConnectionAgain) Then F_OnConnectionAgain(Self);
  117.      End;
  118.  Finally
  119.   InTimer := False;
  120.  End;
  121.  if Not F_Database.Connected Then Exit;
  122. End;
  123.  
  124. Procedure TKADaoConnectionCheck.Activate(Value:Boolean);
  125. Begin
  126.  If Value Then
  127.     Begin
  128.       Timer.Enabled := True;
  129.     End
  130.  Else
  131.     Begin
  132.      Timer.Enabled := False;
  133.     End;
  134. End;
  135.  
  136. Procedure TKADaoConnectionCheck.Loaded;
  137. begin
  138.   Try
  139.     inherited Loaded;
  140.     if F_Active Then Activate(F_Active);
  141.   Except
  142.   End;
  143. end;
  144.  
  145. Procedure TKADaoConnectionCheck.F_Set_Active(Value:Boolean);
  146. Begin
  147.  if F_Active=Value Then Exit;
  148.  F_Active := Value;
  149.  if csLoading in ComponentState Then Exit;
  150.  if (F_Active) And (Not Assigned(F_Database)) Then
  151.      Begin
  152.        F_Active:=False;
  153.        DatabaseError('Database property is not set!');
  154.      End;
  155.  if (F_Active) And (Not F_Database.Connected) Then
  156.     Begin
  157.        F_Active:=False;
  158.        F_Active:=False;DatabaseError('Database is not connected!');
  159.     End;
  160.  Activate(F_Active);
  161. End;
  162.  
  163. Procedure TKADaoConnectionCheck.F_Set_CheckInterval(Value:Integer);
  164. Begin
  165.  if csLoading in ComponentState Then
  166.     Begin
  167.       F_CheckInterval := Value;
  168.       Exit;
  169.     End
  170.  Else
  171.     Begin
  172.      if F_Active Then DatabaseError('Cannot set CheckInterval while Active property is true!');
  173.      F_CheckInterval := Value;
  174.     End;
  175. End;
  176.  
  177. Procedure TKADaoConnectionCheck.F_Set_Database(Value:TKADaoDatabase);
  178. Begin
  179.  if csLoading in ComponentState Then
  180.     Begin
  181.       F_Database := Value;
  182.       Exit;
  183.     End
  184.  Else
  185.     Begin
  186.      if F_Active Then DatabaseError('Cannot set Database while Active property is true!');
  187.      F_Database := Value;
  188.     End;
  189. End;
  190.  
  191. Procedure TKADaoConnectionCheck.F_Set_ErrorCode(Value:Integer);
  192. Begin
  193.  //*************************************************** ReadOnly
  194. End;
  195.  
  196. Procedure TKADaoConnectionCheck.F_Set_ExceptionText(Value: String);
  197. Begin
  198.   if csLoading in ComponentState Then
  199.     Begin
  200.       F_ExceptionText := Value;
  201.       Exit;
  202.     End
  203.  Else
  204.     Begin
  205.      if F_Active Then DatabaseError('Cannot set ExceptionText while Active property is true!');
  206.      F_ExceptionText := Value;
  207.     End;
  208. End;
  209.  
  210. Procedure TKADaoConnectionCheck.F_Set_OnErrorAction(Value:TErrActionSet);
  211. Begin
  212.    if csLoading in ComponentState Then
  213.     Begin
  214.       F_OnErrorAction := Value;
  215.       Exit;
  216.     End
  217.  Else
  218.     Begin
  219.      if F_Active Then DatabaseError('Cannot set OnErrorAction while Active property is true!');
  220.      F_OnErrorAction:=Value;
  221.     End;
  222. End;
  223.  
  224. Procedure TKADaoConnectionCheck.Notification(AComponent: TComponent; Operation: TOperation);
  225. begin
  226.   inherited Notification(AComponent, Operation);
  227.   if (Operation = opRemove) and (F_Database <> nil) and (AComponent = F_Database) then F_Database := nil;
  228. end;
  229.  
  230. procedure Register;
  231. begin
  232.   RegisterComponents('KA Dao', [TKADaoConnectionCheck]);
  233. end;
  234.  
  235. end.
  236.