forked from motaz/turbobird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitfirebirdservices.pas
378 lines (308 loc) · 9.22 KB
/
unitfirebirdservices.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{ Written by Zoran }
unit UnitFirebirdServices;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ibase60dyn, LazUTF8;
type
EFBServiceError = class(Exception);
{ TFirebirdServices }
TFirebirdServices = class(TObject)
private
FBackupFile: String;
FDBName: String;
FHostName: String;
FPassword: String;
FUserName: String;
FVerboseOutput: Boolean;
FPServiceHandle: pisc_svc_handle;
FArrIStatus: array [0..19] of ISC_STATUS;
procedure SetBkpName(const AValue: String);
procedure SetDBName(const AValue: String);
procedure SetHostName(const AValue: String);
procedure SetPassword(const AValue: String);
procedure SetUserName(const AValue: String);
// Raise exception if we cannot attach to service
procedure RaiseIfNotAttachedService(const S: String);
// Run backup or restore depending on BR
procedure StartBackupRestore(BR: Byte);
procedure RaiseServiceErr;
public
constructor Create;
destructor Destroy; override;
function AttachService: Boolean;
function DetachService: Boolean;
// Runs a backup on a database
procedure StartBackup;
// Restores a backup file to database
procedure StartRestore;
function ServiceQuery(out S: String): Boolean; //Returns True if there is more output.
// Runs sweep on database
procedure StartSweep;
function ServiceAttached: Boolean;
// Name/IP address of host for database connection
property HostName: String read FHostName write SetHostName;
// Database name for connection
property DBName: String read FDBName write SetDBName;
// File that contains a Firebird .fbk file
property BackupFile: String read FBackupFile write SetBkpName;
// Username for database connection
property UserName: String read FUserName write SetUserName;
// Password for database connection
property Password: String read FPassword write SetPassword;
// If true, give verbose output when running services functions
property VerboseOutput: Boolean read FVerboseOutput write FVerboseOutput;
end;
implementation
{ TFirebirdServices }
procedure TFirebirdServices.SetDBName(const AValue: String);
begin
if FDBName <> AValue then begin
if ServiceAttached then
raise EFBServiceError.Create('You cannot change database file name when service is attached!!!');
FDBName := AValue;
end;
end;
procedure TFirebirdServices.SetHostName(const AValue: String);
begin
if FHostName <> AValue then begin
if ServiceAttached then
raise EFBServiceError.Create('You cannot change host name when service is already attached!!!');
FHostName := AValue;
end;
end;
procedure TFirebirdServices.SetBkpName(const AValue: String);
begin
if FBackupFile <> AValue then begin
if ServiceAttached then
raise EFBServiceError.Create('You cannot change backup file name when service is attached!!!');
FBackupFile := AValue;
end;
end;
procedure TFirebirdServices.SetPassword(const AValue: String);
begin
if FPassword <> AValue then
FPassword := AValue;
end;
procedure TFirebirdServices.SetUserName(const AValue: String);
begin
if FUserName <> AValue then begin
if ServiceAttached then
raise EFBServiceError.Create('You cannot change user when service is already attached!!!');
FUserName := AValue;
end;
end;
procedure TFirebirdServices.RaiseIfNotAttachedService(const S: String);
begin
if not ServiceAttached then
raise EFBServiceError.Create('Cannot start ' + S + ' process, as service is not attached!');
end;
procedure TFirebirdServices.StartBackupRestore(BR: Byte);
var
W: Word;
Msg: String;
B: Byte;
Buff: array [0..1023] of Char;
N: Cardinal;
S: String;
begin
case BR of
isc_action_svc_backup: Msg := 'BACKUP';
isc_action_svc_restore: Msg := 'RESTORE';
else
raise EFBServiceError.CreateFmt('Invalid parameter value %d passed to StartBackupRestore',[BR]);
end;
RaiseIfNotAttachedService(Msg);
S := Char(BR); // Either isc_action_svc_backup or isc_action_svc_restore
S := S + Char(isc_spb_dbname); // this cluster describes db name.
W := Length(FDBName);
B := W mod 256;
S := S + Char(B);
B := W div 256;
S := S + Char(B);
S := S + FDBName;
B := isc_spb_bkp_file; // this cluster describes backup file
S := S + Char(B);
W := Length(FBackupFile);
B := W mod 256;
S := S + Char(B);
B := W div 256;
S := S + Char(B);
S := S + FBackupFile;
if FVerboseOutput then begin
B := isc_spb_verbose; // verbose output
S := S + Char(B);
end;
if BR = isc_action_svc_restore then begin
B := isc_spb_options; // options
S := S + Char(B);
// followed now by for-byte bitmask
N := isc_spb_res_replace; // this says replace db file if it exists.
B := N mod 256; // Now, the bytes order in number must be inverted!
S := S + Char(B);
N := N div 256;
B := N mod 256;
S := S + Char(B);
N := N div 256;
B := N mod 256;
S := S + Char(B);
N := N div 256;
B := N mod 256;
S := S + Char(B);
end;
W := Length(S);
Buff := S;
if isc_service_start(@FArrIStatus, @FPServiceHandle, nil, W, @Buff) <> 0 then
RaiseServiceErr;
end;
procedure TFirebirdServices.RaiseServiceErr;
var
Msg: array [0..1023] of Char;
Ps: PISC_STATUS;
begin
Ps := @FArrIStatus;
isc_interprete(@Msg, @Ps); // Firebird interpretes the error code and
// turns it into a human-readable error message.
raise EFBServiceError.Create(
'Error: ' + IntToStr(FArrIStatus[1]) + LineEnding + Msg);
end;
function TFirebirdServices.ServiceQuery(out S: String): Boolean;
var
S2: String;
RequestBuff, ResultBuff: array [0..1023] of Char;
W: Word;
I: Integer;
begin
S2 := Char(isc_info_svc_line);
RequestBuff := S2;
for I := Low(ResultBuff) to High(ResultBuff) do
ResultBuff[I] := #0;
isc_service_query(@FArrIStatus, @FPServiceHandle, nil, 0, nil,
Length(S2), @RequestBuff, Length(ResultBuff), @ResultBuff);
Result := False;
if ResultBuff[0] = Char(isc_info_svc_line) then begin
W := Byte(ResultBuff[2]);
W := 256 * W + Byte(ResultBuff[1]) + 2;
if W > High(ResultBuff) then
W := High(ResultBuff);
S := '';
I := 3;
while I <= W do begin
S := S + ResultBuff[I];
Inc(I);
end;
Result := S > '';
end;
end;
procedure TFirebirdServices.StartSweep;
var
Msg, S: String;
N: Cardinal;
W: Word;
B: Byte;
Buff: array [0..1023] of Char;
begin
Msg := 'SWEEP';
RaiseIfNotAttachedService(Msg);
S := Char(isc_action_svc_repair);
S := S + Char(isc_spb_dbname);
W := Length(FDBName);
B := W mod 256;
S := S + Char(B);
B := W div 256;
S := S + Char(B);
S := S + FDBName;
B := isc_spb_options;
S := S + Char(B);
N := isc_spb_rpr_sweep_db;
B := N mod 256;
S := S + Char(B);
N := N div 256;
B := N mod 256;
S := S + Char(B);
N := N div 256;
B := N mod 256;
S := S + Char(B);
N := N div 256;
B := N mod 256;
S := S + Char(B);
W := Length(S);
Buff := S;
if isc_service_start(@FArrIStatus, @FPServiceHandle, nil, W, @Buff) <> 0 then
RaiseServiceErr;
end;
function TFirebirdServices.ServiceAttached: Boolean;
begin
Result := FPServiceHandle <> nil;
end;
constructor TFirebirdServices.Create;
begin
inherited Create;
FPServiceHandle := nil;
FVerboseOutput := True;
FHostName := '';
FBackupFile := '';
FDBName := '';
FUserName := '';
FPassword := '';
end;
destructor TFirebirdServices.Destroy;
begin
DetachService;
inherited Destroy;
end;
function TFirebirdServices.AttachService: Boolean;
var
S: String;
ServiceName, Buff: array [0..255] of Char;
W1, W2: Word;
B: Byte;
begin
if ServiceAttached then
raise EFBServiceError.Create('Service already attached!!!');
S := Trim(FHostName);
if (Length(S) > 0) and (UTF8UpperCase(S) <> 'LOCALHOST') then
S := FHostName + ':service_mgr'
else
S := 'service_mgr';
W1 := Length(S);
ServiceName := S;
B := isc_spb_version;
S := Char(B);
B := isc_spb_current_version;
S := S + Char(B);
B := isc_spb_user_name;
S := S + Char(B);
B := Length(FUserName);
S := S + Char(B) + FUserName;
B := isc_spb_password;
S := S + Char(B);
B := Length(FPassword);
S := S + Char(B) + FPassword;
W2 := Length(S);
Buff := S;
Result := isc_service_attach(@FArrIStatus, W1, @ServiceName, @FPServiceHandle, W2, @Buff) = 0;
if not Result then
RaiseServiceErr;
end;
function TFirebirdServices.DetachService: Boolean;
begin
Result := True;
if FPServiceHandle <> nil then begin
Result := isc_service_detach(@FArrIStatus, @FPServiceHandle) = 0;
if not Result then
RaiseServiceErr;
FPServiceHandle := nil;
end;
end;
procedure TFirebirdServices.StartBackup;
begin
StartBackupRestore(isc_action_svc_backup);
end;
procedure TFirebirdServices.StartRestore;
begin
StartBackupRestore(isc_action_svc_restore);
end;
initialization
finalization
end.