ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/examples/services/ShutdownDatabaseDlgUnit.pas
Revision: 143
Committed: Fri Feb 23 12:11:21 2018 UTC (6 years, 9 months ago) by tony
Content type: text/x-pascal
File size: 5698 byte(s)
Log Message:
Fixes Merged

File Contents

# User Rev Content
1 tony 143 (*
2     * IBX For Lazarus (Firebird Express)
3     *
4     * The contents of this file are subject to the Initial Developer's
5     * Public License Version 1.0 (the "License"); you may not use this
6     * file except in compliance with the License. You may obtain a copy
7     * of the License here:
8     *
9     * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10     *
11     * Software distributed under the License is distributed on an "AS
12     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
13     * implied. See the License for the specific language governing rights
14     * and limitations under the License.
15     *
16     * The Initial Developer of the Original Code is Tony Whyman.
17     *
18     * The Original Code is (C) 2015 Tony Whyman, MWA Software
19     * (http://www.mwasoftware.co.uk).
20     *
21     * All Rights Reserved.
22     *
23     * Contributor(s): ______________________________________.
24     *
25     *)
26    
27     unit ShutdownDatabaseDlgUnit;
28    
29     {$mode objfpc}{$H+}
30    
31     interface
32    
33     uses
34     Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
35     ComCtrls, ExtCtrls, IBServices, IB;
36    
37     type
38    
39     { TShutdownDatabaseDlg }
40    
41     TShutdownDatabaseDlg = class(TForm)
42     Bevel1: TBevel;
43     CloseBtn: TButton;
44     IBConfigService: TIBConfigService;
45     ProgressBar1: TProgressBar;
46     StatusMsg: TLabel;
47     procedure CloseBtnClick(Sender: TObject);
48     procedure FormShow(Sender: TObject);
49     private
50     FShutDownmode: TShutdownMode;
51     FDelay: integer;
52     FAborting: boolean;
53     FSecContextError: boolean;
54     FShutdownWaitThread: TThread;
55     procedure OnWaitCompleted(Sender: TObject);
56     public
57     procedure Shutdown(aShutDownmode: TShutdownMode; aDelay: integer);
58     property Aborting: boolean read FAborting;
59     end;
60    
61     var
62     ShutdownDatabaseDlg: TShutdownDatabaseDlg;
63    
64     implementation
65    
66     {$R *.lfm}
67    
68     uses IBErrorCodes;
69    
70     resourcestring
71     sWaitStatusMsg = 'Waiting for %s to shutdown';
72     sDatabaseShutdown = 'Database has been successfully shutdown';
73     sOnCompleted = 'Shutdown of %s completed with response: %s';
74    
75     type
76     { TShutdownWaitThread }
77    
78     TShutdownWaitThread = class(TThread)
79     private
80     FErrorMessage: string;
81     FIBConfigService: TIBConfigService;
82     FOptions: TShutdownMode;
83     FSecContextError: boolean;
84     FSuccess: boolean;
85     FWait: integer;
86     FOnCompleted: TNotifyEvent;
87     procedure DoCallback;
88     protected
89     procedure Execute; override;
90     public
91     constructor Create(aService: TIBConfigService; Options: TShutdownMode;
92     Wait: Integer; OnCompleted: TNotifyEvent);
93     destructor Destroy; override;
94     procedure Abort;
95     property Success: boolean read FSuccess;
96     property SecContextError: boolean read FSecContextError;
97     property ErrorMessage: string read FErrorMessage;
98     end;
99    
100    
101    
102     { TShutdownWaitThread }
103    
104     procedure TShutdownWaitThread.DoCallback;
105     begin
106     if assigned(FOnCompleted) then
107     FOnCompleted(self);
108     end;
109    
110     procedure TShutdownWaitThread.Execute;
111     begin
112     FSuccess := false;
113     FIBConfigService.Active := true;
114     try
115     try
116     FIBConfigService.ShutDownDatabase(FOptions,FWait);
117     FErrorMessage := 'Completed without error';
118     FSuccess := true;
119     except
120     on E: EIBInterBaseError do
121     if E.IBErrorCode = isc_sec_context then
122     FSecContextError := true
123     else
124     FErrorMessage := E.Message;
125    
126     on E: Exception do
127     FErrorMessage := E.Message;
128     end;
129     finally
130     if not FSecContextError then
131     while FIBConfigService.IsServiceRunning do;
132     if Terminated and FSuccess then
133     FIBConfigService.BringDatabaseOnline;
134     FIBConfigService.Active := false;
135     end;
136     Synchronize(@DoCallback);
137     end;
138    
139     constructor TShutdownWaitThread.Create(aService: TIBConfigService;
140     Options: TShutdownMode; Wait: Integer; OnCompleted: TNotifyEvent);
141     var Password: string;
142     begin
143     inherited Create(false);
144     FOptions := Options;
145     FWait := Wait;
146     FOnCompleted := OnCompleted;
147     FreeOnTerminate := true;
148     FIBConfigService := TIBConfigService.Create(nil);
149     FIBConfigService.Assign(aService);
150     FIBConfigService.DatabaseName := aService.DatabaseNAme;
151     end;
152    
153     destructor TShutdownWaitThread.Destroy;
154     begin
155     if FIBConfigService <> nil then FIBConfigService.Free;
156     inherited Destroy;
157     end;
158    
159     procedure TShutdownWaitThread.Abort;
160     begin
161     Terminate;
162     end;
163    
164     { TShutdownDatabaseDlg }
165    
166     procedure TShutdownDatabaseDlg.FormShow(Sender: TObject);
167     begin
168     FAborting := false;
169     StatusMsg.Caption := Format(sWaitStatusMsg,[IBConfigService.DatabaseName]);
170     FShutdownWaitThread := TShutdownWaitThread.Create(IBConfigService,FShutDownMode,FDelay,@OnWaitCompleted);
171     end;
172    
173     procedure TShutdownDatabaseDlg.CloseBtnClick(Sender: TObject);
174     begin
175     FAborting := true;
176     FShutdownWaitThread.Terminate;
177     Close;
178     end;
179    
180     procedure TShutdownDatabaseDlg.OnWaitCompleted(Sender: TObject);
181     begin
182     with TShutdownWaitThread(Sender) do
183     if not Success and SecContextError then
184     self.FSecContextError := true
185     else
186     if not FAborting then
187     MessageDlg(Format(sOnCompleted,[IBConfigService.DatabaseName,ErrorMessage]),
188     mtInformation,[mbOK],0);
189     FAborting := false;
190     Close;
191     end;
192    
193     procedure TShutdownDatabaseDlg.Shutdown(aShutDownmode: TShutdownMode; aDelay: integer);
194     begin
195     FShutDownmode := aShutDownmode;
196     FDelay := aDelay;
197     FSecContextError := false;
198     if aDelay <= 0 then
199     begin
200     IBConfigService.Active := true;
201     try
202     IBConfigService.ShutDownDatabase(aShutDownmode,0);
203     while IBConfigService.IsServiceRunning do;
204     if aDelay = 0 then
205     MessageDlg(sDatabaseShutdown,mtInformation,[mbOK],0);
206     finally
207     IBConfigService.Active := false;
208     end
209     end
210     else
211     begin
212     ShowModal;
213     if FSecContextError then
214     raise EIBInterBaseError.Create(FirebirdAPI.getStatus); {re-raise the error}
215     end;
216     end;
217    
218     end.
219