summaryrefslogtreecommitdiff
path: root/avx512-0037785/packages/fcl-db/src/sqldb/interbase/fbadmin.pp
blob: 3dafa32e94cd04b456a0ccd3474994fc7807f41c (plain)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
unit FBAdmin;

{ Interbase/Firebird Administration using the service manager

  Copyright (C) 2012 Ludo Brands

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:

  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
}

{$mode objfpc}{$H+}

{$Define LinkDynamically}

interface

uses
  Classes, SysUtils,
{$IfDef LinkDynamically}
  ibase60dyn,
{$Else}
  ibase60,
{$EndIf}
  IBConnection;

type
  TIBBackupOption=(IBBkpVerbose,IBBkpIgnoreChecksums,IBBkpIgnoreLimbo,IBBkpMetadataOnly,
     IBBkpNoGarbageCollect,IBBkpOldDescriptions,IBBkpNonTransportable,IBBkpConvert,IBBkpWait);
  TIBBackupOptions= set of TIBBackupOption;
  TIBRestoreOption=(IBResVerbose,IBResDeactivateIdx,IBResNoShadow,IBResNoValidity,
     IBResOneAtaTime,IBResReplace,IBResCreate,IBResUseAllSpace,IBResAMReadOnly,IBResAMReadWrite,
     IBFixFssData, IBFixFssMeta,IBResWait);
  TIBRestoreOptions= set of TIBRestoreOption;
  TServiceProtocol=(IBSPLOCAL,IBSPTCPIP,IBSPNETBEUI,IBSPNAMEDPIPE);
  TIBOnOutput= procedure(Sender: TObject; msg: string; IBAdminAction: string) of object;
  TIBStatOption = (IBDataPages, IBDbLog, IBHeaderPages, IBIndexPages, IBSystemRelations,
    IBRecordVersions, IBStatTables);
  TIBStatOptions = set of TIBStatOption;

  { TFBAdmin }

  TFBAdmin=class(TComponent)
  private
    FErrorCode: longint;
    FErrorMsg: string;
    FFixFssDataCharSet: String;
    FHost: string;
    FOnOutput: TIBOnOutput;
    FOutput: TStringList;
    FPassword: string;
    FPort: word;
    FProtocol: TServiceProtocol;
    FServerImplementation: string;
    FServerLockDir: string;
    FServerMsgDir: string;
    FServerRootDir: string;
    FServerSecDBDir: string;
    FServerVersion: string;
    FStatus: array [0..19] of ISC_STATUS;
    FSvcHandle: isc_svc_handle;
    FUseExceptions: boolean;
    FUser: string;
    FWaitInterval: Integer;
    function CheckConnected(ProcName: string):boolean;
    procedure CheckError(ProcName : string; Status : PISC_STATUS);
    function GetDBInfo:boolean;
    function GetIBLongint(buffer:string; var bufptr:integer):longint;overload;
    function GetIBString(buffer:string; var bufptr:integer):string;overload;
    function GetOutput(IBAdminAction:string):boolean;
    function IBParamSerialize(isccode:byte;value:string):string;
    procedure IBRaiseError(GDSErrorCode:Longint; const msg : string; const args : array of const);
    function IBSPBParamSerialize(isccode:byte;value:string):string;
    function IBSPBParamSerialize(isccode:byte;value:longint):string;
    function MakeBackupOptions(options:TIBBackupOptions):longint;
    function MakeRestoreOptions(options:TIBRestoreOptions):longint;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    //Connect to service manager. Specify User,Password and, for remote databases,
    //Host and, if not standard, Port
    function Connect:boolean;
    //Disconnect from service manager. Done automatically when destroying component
    function DisConnect:boolean;
    //Backup database to a single file on the server.
    //Include IBBkpVerbose in Options to get progress feedback through the OnOutput Handler
    function Backup(Database,Filename:string;Options:TIBBackupOptions;RoleName:string=''):boolean;
    //Backup database to multiple files with length FileSize on the server.
    //Filenames is the list of filenames to use. The last file specified has no size limit.
    //Include IBBkpVerbose in Options to get progress feedback through the OnOutput Handler
    function BackupMultiFile(Database:string;Filenames:TStrings;FileSize:longint;
      Options:TIBBackupOptions;RoleName:string=''):boolean;
    //Restore database from a single file on the server.
    //Include IBResReplace to restore in and existing database or IBResCreate
    //to create a a new one.
    //Include IBResVerbose in Options to get progress feedback through the OnOutput Handler
    function Restore(Database,Filename:string;Options:TIBRestoreOptions;RoleName:string=''):boolean;
    //Restore database from multiple files on the server.
    //Filenames is the list of files to use.
    //Include IBResReplace to restore in and existing database or IBResCreate
    //to create a a new one.
    //Include IBResVerbose in Options to get progress feedback through the OnOutput Handler
    function RestoreMultiFile(Database:string;Filenames:TStrings;
      Options:TIBRestoreOptions;RoleName:string=''):boolean;
    //Add a new user.
    function AddUser(UserName,Password:string;RoleName:string='';
      GroupName:string='';FirstName:string='';MiddleName:string='';
      LastName:string='';UserID: longint = 0; GroupID: longint = 0):boolean;
    //Modify an existing user.
    function ModifyUser(UserName,Password:string;RoleName:string='';
      GroupName:string='';FirstName:string='';MiddleName:string='';
      LastName:string='';UserID: longint = 0; GroupID: longint = 0):boolean;
    //Delete an existing user.
    function DeleteUser(UserName:string;RoleName:string=''):boolean;
    //Get the details of an existing user.
    function GetUser(UserName:string;var GroupName,FirstName,MiddleName,
      LastName:string;var UserID, GroupID: longint):boolean;
    //Get the list of all users
    function GetUsers(Users:TStrings):boolean;
    //Get database server log file
    function GetDatabaseLog:boolean;
    // For Backup, Restore this will check if the service call is still running.
    function ServiceRunning: Boolean;
    // Wait till the service stops running, or until aTimeout (in milliseconds) is reached.
    // Return true if the service stopped, false if timeout reached.
    // WaitInterval is the interval (in milliseconds) between ServiceRunning calls.
    function WaitForServiceCompletion(aTimeOut: Integer): Boolean;
    //Get database statistics
    function GetDatabaseStats(Database:string;Options:TIBStatOptions;TableNames:String = ''): boolean;
    //Database server version
    property ServerVersion:string read FServerVersion;
    //Implementation string of the database server
    property ServerImplementation:string read FServerImplementation;
    //Setting of $FIREBIRD or $INTERBASE
    property ServerRootDir:string read FServerRootDir;
    //Setting of $FIREBIRD_LCK or $INTERBASE_LCK
    property ServerLockDir:string read FServerLockDir;
    //Setting of $FIREBIRD_MSG or $INTERBASE_MSG
    property ServerMsgDir:string read FServerMsgDir;
    //Path to the security database in use by the server
    property ServerSecDBDir:string read FServerSecDBDir;
    // FixFxxData/FixFxxMetaData code page
    property FixFssDataCharSet: String read FFixFssDataCharSet write FFixFssDataCharSet;
  published
    //User name to connect to service manager
    property User: string read FUser write FUser;
    //User name to connect to service manager
    property Password: string read FPassword write FPassword;
    //Database Host
    property Host: string read FHost write FHost;
    //Database Port, Default:3050
    property Port: word read FPort write FPort default 3050;
    //Protocol used to connect to service manager. One of:
    //IBSPLOCAL: Host and port ignored
    //IBSPTCPIP: Connect to Host:Port
    //IBSPNETBEUI: Connect to \\Host\
    //IBSPNAMEDPIPE: Connect to //Host/
    property Protocol: TServiceProtocol read FProtocol write FProtocol;
    //Errorcode returned in status vector or 0 for TFBAdmin errors
    property ErrorCode:longint read FErrorCode;
    //Errormsg returned in status vector or by TFBAdmin
    property ErrorMsg:string read FErrorMsg;
    //Raise exceptions when error encounterd. Default: false
    property UseExceptions:boolean read FUseExceptions write FUseExceptions;
    //Service output messages
    //Result from Backup and Restore operations and GetLog
    property Output:TStringList read FOutput;
    //Event handler for Service output messages
    //Used in Backup and Restore operations and GetLog
    property OnOutput: TIBOnOutput read FOnOutput write FOnOutput;
    // Interval (in milliseconds) to sleep while waiting for the service operation to end.
    Property WaitInterval : Integer Read FWaitInterval Write FWaitInterval;
  end;


implementation

uses dateutils;

resourcestring
  SErrNotConnected = '%s : %s : Not connected.';
  SErrError = '%s : %s : %s';
  SErrConnected = '%s : Connect : Already connected.';
  SErrRestoreOptionsError = '%s : Restore : Nothing to do. Specify IBResReplace or IBResCreate in Options.';
  SErrRestoreMultiOptionsError = '%s : RestoreMultiFile : Nothing to do. Specify IBResReplace or IBResCreate in Options.';
  SErrUserDoesNotExist = '%s : GetUser : User does not exist.';
  SErrUserInvalidReply = '%s : GetUser : Invalid reply (%d).';
  SErrUsersInvalidReply = '%s : GetUsers : Invalid reply (%d).';

{ TFBAdmin }

function TFBAdmin.IBParamSerialize(isccode: byte; value: string): string;
begin
  result:=chr(isccode)+chr(Length(value))+value;
end;

procedure TFBAdmin.IBRaiseError(GDSErrorCode: Longint; const msg: string;
  const args: array of const);
begin
  FErrorMsg:=Format(msg,args);
  FErrorCode:=GDSErrorCode;
  if FUseExceptions then
    raise EIBDatabaseError.CreateFmt(msg,args,nil,GDSErrorCode,'');
end;

function TFBAdmin.IBSPBParamSerialize(isccode: byte; value: string): string;
begin
  result:=chr(isccode)+chr(Length(value) and $ff)+chr((Length(value)shr 8) and $ff)+value;
end;

function TFBAdmin.IBSPBParamSerialize(isccode: byte; value: longint): string;
begin
  result:=chr(isccode)+chr(value and $ff)+chr((value shr 8) and $ff)
     +chr((value shr 16) and $ff)+chr((value shr 24) and $ff);
end;

function TFBAdmin.MakeBackupOptions(options: TIBBackupOptions): longint;
begin
  result:=0;
  if IBBkpConvert in Options then
    result:=result or isc_spb_bkp_convert;
  if IBBkpIgnoreChecksums in Options then
    result:=result or isc_spb_bkp_ignore_checksums;
  if IBBkpIgnoreLimbo in Options then
    result:=result or isc_spb_bkp_ignore_limbo;
  if IBBkpMetadataOnly  in Options then
    result:=result or isc_spb_bkp_metadata_only;
  if IBBkpNoGarbageCollect in Options then
    result:=result or isc_spb_bkp_no_garbage_collect;
  if IBBkpNonTransportable in Options then
    result:=result or isc_spb_bkp_non_transportable;
  if IBBkpOldDescriptions  in Options then
    result:=result or isc_spb_bkp_old_descriptions;
end;

function TFBAdmin.MakeRestoreOptions(options: TIBRestoreOptions): longint;
begin
  result:=0;
  if IBResCreate in Options then
    result:=result or isc_spb_res_create;
  if IBResDeactivateIdx in Options then
    result:=result or isc_spb_res_deactivate_idx;
  if IBResNoShadow in Options then
    result:=result or isc_spb_res_no_shadow;
  if IBResNoValidity in Options then
    result:=result or isc_spb_res_no_validity;
  if IBResOneAtaTime in Options then
    result:=result or isc_spb_res_one_at_a_time;
  if IBResReplace in Options then
    result:=result or isc_spb_res_replace;
  if IBResUseAllSpace in Options then
    result:=result or isc_spb_res_use_all_space;
end;


function TFBAdmin.CheckConnected(ProcName: string): boolean;
begin
  result:=false;
  if FSvcHandle=FB_API_NULLHANDLE then
    begin
    IBRaiseError(0,SErrNotConnected,[self.Name,ProcName]);
    exit;
    end;
  result:=true;
end;

procedure TFBAdmin.CheckError(ProcName: string; Status: PISC_STATUS);
var
  buf : array [0..1023] of char;
  Msg : string;
  Err : longint;

begin
  if ((Status[0] = 1) and (Status[1] <> 0)) then
  begin
    Err := Status[1];
    msg := '';
    while isc_interprete(Buf, @Status) > 0 do
      Msg := Msg + LineEnding +' -' + StrPas(Buf);
    IBRaiseError(Err,SErrError,[self.Name,ProcName,Msg]);
  end;
end;

function TFBAdmin.GetDBInfo: boolean;

  function QueryInfo(isc:byte):string;
  var
    spb:string;
    len:integer;
  begin
    result:='';
    spb:=chr(isc);
    setlength(result,255);
    if (isc_service_query(@FStatus[0], @FSvcHandle, nil, 0, nil, length(spb),
      @spb[1],255,@result[1])=0) and (result[1]=chr(isc)) then
      begin
      len:=isc_vax_integer(@result[2],2);
      delete(result,1,3); // remove cmd and len
      setlength(result,len);
      end;
  end;

begin
  FServerImplementation:= QueryInfo(isc_info_svc_implementation);
  FServerLockDir:= QueryInfo(isc_info_svc_get_env_lock);
  FServerMsgDir:= QueryInfo(isc_info_svc_get_env_msg);
  FServerRootDir:= QueryInfo(isc_info_svc_get_env);
  FServerSecDBDir:= QueryInfo(isc_info_svc_user_dbpath);
  FServerVersion:= QueryInfo(isc_info_svc_server_version);
end;

function TFBAdmin.GetIBLongint(buffer: string; var bufptr: integer): longint;
begin
  bufptr:=bufptr+1;
  result:=isc_vax_integer(@Buffer[bufptr], 4);
  bufptr:=bufptr+4;
end;

function TFBAdmin.GetIBString(buffer: string; var bufptr: integer): string;
var
  len:integer;
begin
  bufptr:=bufptr+1;
  len:=isc_vax_integer(@buffer[bufptr], 2);
  bufptr:=bufptr+2;
  result:=copy(buffer,bufptr,len);
  bufptr:=bufptr+len;
end;

function TFBAdmin.GetOutput(IBAdminAction: string): boolean;
var
  len:integer;
  buffer:string;
  spb:string;
const
  BUFFERSIZE=1000;
begin
  len:=0;
  FOutput.Clear;
  spb:=chr(isc_info_svc_line);
  repeat
    setlength(buffer,BUFFERSIZE);
    result:=isc_service_query(@FStatus[0], @FSvcHandle, nil, 0, nil, length(spb),
      @spb[1],BUFFERSIZE,@buffer[1])=0;
    if not result then
      begin
      CheckError('GetOutput',FStatus);
      exit;
      end;
    if buffer[1]=chr(isc_info_svc_line) then
      begin
      len:=isc_vax_integer(@buffer[2],2);
      delete(buffer,1,3); // remove cmd and len
      setlength(buffer,len);
      FOutput.Add(buffer);
      if assigned(FOnOutput) then
        begin
        FOnOutput(Self,buffer,IBAdminAction);
        end;
      end;
  until len=0;
end;

constructor TFBAdmin.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FPort:= 3050;
  FOutput:=TStringList.Create;
  FFixFssDataCharSet:= '';
end;

destructor TFBAdmin.Destroy;
begin
  if FSvcHandle<>FB_API_NULLHANDLE then
  begin
    WaitInterval:=100;
    DisConnect;
  end;
  FOutput.Destroy;
  inherited Destroy;
end;

function TFBAdmin.Connect: boolean;
var
  Service:string;
  spb:string;
begin
  result:=false;
  {$IfDef LinkDynamically}
  result:=InitialiseIBase60<>0;
  {$EndIf}
  if FSvcHandle<>FB_API_NULLHANDLE then
    raise EIBDatabaseError.CreateFmt(SErrConnected,[Self.Name],nil,0,'');
  Service:='service_mgr';
  case FProtocol of
    IBSPTCPIP:if FPort=3050 then
        service:=FHost+':'+service
      else
        service:=FHost+'/'+IntTostr(FPort)+':'+service;
    IBSPNETBEUI:service:='\\'+FHost+'\'+service;
    IBSPNAMEDPIPE:service:='//'+FHost+'/'+service;
  end;
  spb:=chr(isc_spb_version)+chr(isc_spb_current_version)+
    IBParamSerialize(isc_spb_user_name,FUser)+
    IBParamSerialize(isc_spb_password,FPassword);
  result:=isc_service_attach(@FStatus[0], 0,PChar(Service), @FSvcHandle,
      length(spb), @spb[1]) = 0;
  if not result then
    CheckError('Connect',FStatus)
  else
    GetDBInfo;
end;

function TFBAdmin.DisConnect: boolean;

begin
  result:=CheckConnected('DisConnect');
  result:= isc_service_detach(@FStatus[0], @FSvcHandle) = 0;
  if not result then
    CheckError('DisConnect',FStatus);
  FSvcHandle:=FB_API_NULLHANDLE;
  {$IfDef LinkDynamically}
  ReleaseIBase60;
  {$EndIf}
  result:=true;
end;

function TFBAdmin.Backup(Database, Filename: string; Options: TIBBackupOptions;
  RoleName: string): boolean;
var
  spb:string;
begin
  result:=CheckConnected('Backup');
  spb:=chr(isc_action_svc_backup)+IBSPBParamSerialize(isc_spb_dbname,Database)
    +IBSPBParamSerialize(isc_spb_bkp_file,Filename);
  if RoleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sql_role_name,copy(RoleName,1,31));
  if IBBkpVerbose in Options then
    spb:=spb+chr(isc_spb_verbose);
  spb:=spb+IBSPBParamSerialize(isc_spb_options,MakeBackupOptions(Options));
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    begin
    CheckError('Backup',FStatus);
    exit;
    end;
  if IBBkpVerbose in Options then
    result:=GetOutput('Backup')
  else if (IBBkpWait in Options) then
    WaitForServiceCompletion(0);
end;

function TFBAdmin.BackupMultiFile(Database: string; Filenames: TStrings;
  FileSize: longint; Options: TIBBackupOptions; RoleName: string): boolean;
var
  spb:string;
  i:integer;
begin
  result:=CheckConnected('BackupMultiFile');
  spb:=chr(isc_action_svc_backup)+IBSPBParamSerialize(isc_spb_dbname,Database);
  for i:=0 to Filenames.Count-1 do
    begin
    spb:=spb+IBSPBParamSerialize(isc_spb_bkp_file,Filenames[i]);
    spb:=spb+IBSPBParamSerialize(isc_spb_bkp_length,FileSize);
    end;
  if RoleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sql_role_name,copy(RoleName,1,31));
  if IBBkpVerbose in Options then
    spb:=spb+chr(isc_spb_verbose);
  spb:=spb+IBSPBParamSerialize(isc_spb_options,MakeBackupOptions(Options));
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    begin
    CheckError('BackupMultiFile',FStatus);
    exit;
    end;
  if IBBkpVerbose in Options then
    result:=GetOutput('BackupMultiFile')
  else if (IBBkpWait in Options) then
    WaitForServiceCompletion(0);
end;

Function TFBAdmin.ServiceRunning : Boolean;

const
  BUFFERSIZE=1000;

var
  res:integer;
  buffer: string;
  spb:string;

begin
  FOutput.Clear;
  spb:=chr(isc_info_svc_running);
  setlength(buffer,BUFFERSIZE);
  result:=isc_service_query(@FStatus[0], @FSvcHandle, nil, 0, nil, length(spb),
          @spb[1],BUFFERSIZE,@buffer[1])=0;
  if Not Result then
    CheckError('ServiceRunning',FSTatus);
  if (Buffer[1]=Char(isc_info_svc_running)) then
    begin
    res:=isc_vax_integer(@Buffer[2],4);
    Result:=res=1;
    end
  else
    IBRaiseError(0,'%s: Service status detection returned wrong result',[self.Name]);
end;

Function TFBAdmin.WaitForServiceCompletion(aTimeOut : Integer) : Boolean;

Var
  N : TDateTime;

begin
  N:=Now;
  Repeat
    Sleep(WaitInterval);
    Result:=not ServiceRunning;
  until Result or ((aTimeOut<>0) and (MilliSecondsBetween(Now,N)>aTimeOut*WaitInterval));
end;


function TFBAdmin.Restore(Database, Filename: string;
  Options: TIBRestoreOptions; RoleName: string): boolean;
var
  spb:string;
begin
  result:=CheckConnected('Restore');
  if not ((IBResReplace in Options) or (IBResCreate in Options)) then
    begin
    result:=false;
    IBRaiseError(0,SErrRestoreOptionsError,[self.Name]);
    exit;
    end;
  spb:=chr(isc_action_svc_restore)+IBSPBParamSerialize(isc_spb_dbname,Database)
    +IBSPBParamSerialize(isc_spb_bkp_file,Filename);
  if RoleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sql_role_name,copy(RoleName,1,31));
  if IBResVerbose in Options then
    spb:=spb+chr(isc_spb_verbose);
  if (IBResAMReadOnly in Options) or (IBResAMReadWrite in Options) then
    begin
    if (IBResAMReadOnly in Options) then   //ReadOnly overrides ReadWrite
      spb:=spb+chr(isc_spb_res_access_mode)+chr(isc_spb_res_am_readonly)
    else
      spb:=spb+chr(isc_spb_res_access_mode)+chr(isc_spb_res_am_readwrite);
    end;
  if (IBFixFssData in Options) and (FixFssDataCharSet > ' ') then
    spb:=spb+IBSPBParamSerialize(isc_spb_res_fix_fss_data, FixFssDataCharSet);
  if (IBFixFssMeta in Options) and (FixFssDataCharSet > ' ') then
    spb:=spb+IBSPBParamSerialize(isc_spb_res_fix_fss_metadata, FixFssDataCharSet);
  spb:=spb+IBSPBParamSerialize(isc_spb_options,MakeRestoreOptions(Options));
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    begin
    CheckError('Restore',FStatus);
    exit;
    end;
  if IBResVerbose in Options then
    result:=GetOutput('Restore')
  else if IBResWait in Options then
    WaitForServiceCompletion(0);
end;


function TFBAdmin.RestoreMultiFile(Database: string; Filenames: TStrings;
  Options: TIBRestoreOptions; RoleName: string): boolean;
var
  spb:string;
  i:integer;
begin
  result:=CheckConnected('RestoreMultiFile');
  if not ((IBResReplace in Options) or (IBResCreate in Options)) then
    begin
    result:=false;
    IBRaiseError(0,SErrRestoreMultiOptionsError,[self.Name]);
    exit;
    end;
  spb:=chr(isc_action_svc_restore)+IBSPBParamSerialize(isc_spb_dbname,Database);
  for i:=0 to Filenames.Count-1 do
    spb:=spb+IBSPBParamSerialize(isc_spb_bkp_file,Filenames[i]);
  if RoleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sql_role_name,copy(RoleName,1,31));
  if IBResVerbose in Options then
    spb:=spb+chr(isc_spb_verbose);
  if (IBResAMReadOnly in Options) or (IBResAMReadWrite in Options) then
    begin
    if (IBResAMReadOnly in Options) then   //ReadOnly overrides ReadWrite
      spb:=spb+chr(isc_spb_res_access_mode)+chr(isc_spb_res_am_readonly)
    else
      spb:=spb+chr(isc_spb_res_access_mode)+chr(isc_spb_res_am_readwrite);
    end;
  spb:=spb+IBSPBParamSerialize(isc_spb_options,MakeRestoreOptions(Options));
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    begin
    CheckError('RestoreMultiFile',FStatus);
    exit;
    end;
  if IBResVerbose in Options then
    result:=GetOutput('RestoreMultiFile');
end;

function TFBAdmin.AddUser(UserName, Password: string; RoleName: string;
  GroupName: string; FirstName: string; MiddleName: string; LastName: string;
  UserID: longint; GroupID: longint): boolean;
var
  spb:string;
begin
  result:=CheckConnected('AddUser');
  spb:=chr(isc_action_svc_add_user)+IBSPBParamSerialize(isc_spb_sec_username,copy(UserName,1,31))+
    IBSPBParamSerialize(isc_spb_sec_password,copy(Password,1,8));
  if RoleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sql_role_name,copy(RoleName,1,31));
  if GroupName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_groupname,copy(GroupName,1,31));
  if FirstName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_firstname,copy(FirstName,1,255));
  if MiddleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_middlename,copy(MiddleName,1,255));
  if LastName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_lastname,copy(LastName,1,255));
  if UserID<>0 then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_userid,UserID);
  if GroupID<>0 then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_groupid,GroupID);
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    CheckError('AddUser',FStatus);
end;

function TFBAdmin.ModifyUser(UserName, Password: string; RoleName: string;
  GroupName: string; FirstName: string; MiddleName: string; LastName: string;
  UserID: longint; GroupID: longint): boolean;
var
  spb:string;
begin
  result:=CheckConnected('ModifyUser');
  spb:=chr(isc_action_svc_modify_user)+IBSPBParamSerialize(isc_spb_sec_username,copy(UserName,1,31))+
    IBSPBParamSerialize(isc_spb_sec_password,copy(Password,1,8));
  if RoleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sql_role_name,copy(RoleName,1,31));
  if GroupName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_groupname,copy(GroupName,1,31));
  if FirstName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_firstname,copy(FirstName,1,255));
  if MiddleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_middlename,copy(MiddleName,1,255));
  if LastName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_lastname,copy(LastName,1,255));
  if UserID<>0 then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_userid,UserID);
  if GroupID<>0 then
    spb:=spb+IBSPBParamSerialize(isc_spb_sec_groupid,GroupID);
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    CheckError('ModifyUser',FStatus);
end;

function TFBAdmin.DeleteUser(UserName: string; RoleName: string): boolean;
var
  spb:string;
begin
  result:=CheckConnected('DeleteUser');
  spb:=chr(isc_action_svc_delete_user)+IBSPBParamSerialize(isc_spb_sec_username,copy(UserName,1,31));
  if RoleName<>'' then
    spb:=spb+IBSPBParamSerialize(isc_spb_sql_role_name,copy(RoleName,1,31));
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    CheckError('DeleteUser',FStatus);
end;

function TFBAdmin.GetUser(UserName: string; var GroupName, FirstName,
  MiddleName, LastName: string; var UserID, GroupID: longint): boolean;
var
  spb:string;
  buffer:string;
  bufptr:integer;
const
  BUFFERSIZE=1000;
begin
  result:=CheckConnected('GetUser');
  spb:=chr(isc_action_svc_display_user)+IBSPBParamSerialize(isc_spb_sec_username,copy(UserName,1,31));
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    begin
    CheckError('GetUser',FStatus);
    exit;
    end;
  //retrieve result
  spb:=chr(isc_info_svc_get_users);
  setlength(buffer,BUFFERSIZE);
  result:=isc_service_query(@FStatus[0], @FSvcHandle, nil, 0, nil, length(spb),
    @spb[1],BUFFERSIZE,@buffer[1])=0;
  if not result then
    begin
    CheckError('GetUser',FStatus);
    exit;
    end;
  bufptr:=4;
  if buffer[1]=chr(isc_info_svc_get_users) then
    begin
    if buffer[bufptr]=chr(isc_info_end) then
      begin
      result:=false;
      IBRaiseError(0,SErrUserDoesNotExist,[self.Name]);
      exit;
      end;
    while buffer[bufptr]<>chr(isc_info_end) do
      begin
      case buffer[bufptr] of
        chr(isc_spb_sec_username):GetIBString(buffer,bufptr);  //trash result
        chr(isc_spb_sec_groupname):GroupName:=GetIBString(buffer,bufptr);
        chr(isc_spb_sec_firstname):FirstName:=GetIBString(buffer,bufptr);
        chr(isc_spb_sec_middlename):MiddleName:=GetIBString(buffer,bufptr);
        chr(isc_spb_sec_lastname):LastName:=GetIBString(buffer,bufptr);
        chr(isc_spb_sec_userid):UserID:=GetIBLongint(buffer,bufptr);
        chr(isc_spb_sec_groupid):GroupID:=GetIBLongint(buffer,bufptr);
        else
          begin
          result:=false;
          IBRaiseError(0,SErrUserInvalidReply,[self.Name,ord(buffer[bufptr])]);
          exit;
          end;
        end;
      end;
    end;
end;

function TFBAdmin.GetUsers(Users: TStrings): boolean;
var
  spb:string;
  buffer:string;
  bufptr:integer;
const
  BUFFERSIZE=1000;
begin
  result:=CheckConnected('GetUsers');
  spb:=chr(isc_action_svc_display_user);
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    begin
    CheckError('GetUsers',FStatus);
    exit;
    end;
  //retrieve result
  spb:=chr(isc_info_svc_get_users);
  setlength(buffer,BUFFERSIZE);
  result:=isc_service_query(@FStatus[0], @FSvcHandle, nil, 0, nil, length(spb),
    @spb[1],BUFFERSIZE,@buffer[1])=0;
  if not result then
    begin
    CheckError('GetUsers',FStatus);
    exit;
    end;
  bufptr:=4;
  Users.Clear;
  if buffer[1]=chr(isc_info_svc_get_users) then
    begin
    while buffer[bufptr]<>chr(isc_info_end) do
      begin
      case buffer[bufptr] of
        chr(isc_spb_sec_username):Users.Add(GetIBString(buffer,bufptr));
        chr(isc_spb_sec_groupname),
        chr(isc_spb_sec_firstname),
        chr(isc_spb_sec_middlename),
        chr(isc_spb_sec_lastname):GetIBString(buffer,bufptr);       //trash result
        chr(isc_spb_sec_userid),
        chr(isc_spb_sec_groupid):GetIBLongint(buffer,bufptr);       //trash result
        else
          begin
          result:=false;
          IBRaiseError(0,SErrUsersInvalidReply,[self.Name,ord(buffer[bufptr])]);
          exit;
          end;
        end;
      end;
    end;
end;


function TFBAdmin.GetDatabaseLog: boolean;
var
  spb:string;
begin
  result:=CheckConnected('GetLogFile');
  spb:=chr(isc_action_svc_get_ib_log);
  result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not result then
    begin
    CheckError('GetLogFile',FStatus);
    exit;
    end;
  result:=GetOutput('GetLogFile');
end;

function TFBAdmin.GetDatabaseStats(Database:string;Options: TIBStatOptions; TableNames: String
  ): boolean;
var
  spb:string;
  param: Integer;
begin
  Result:=CheckConnected('GetDatabaseStats');
  param := 0;
  if (IBDataPages in Options) then
    param := param or isc_spb_sts_data_pages;
  if (IBDbLog in Options) then
    param := param or isc_spb_sts_db_log;
  if (IBHeaderPages in Options) then
    param := param or isc_spb_sts_hdr_pages;
  if (IBIndexPages in Options) then
    param := param or isc_spb_sts_idx_pages;
  if (IBSystemRelations in Options) then
    param := param or isc_spb_sts_sys_relations;
  if (IBRecordVersions in Options) then
    param := param or isc_spb_sts_record_versions;
  if (IBStatTables in Options) then
    param := param or isc_spb_sts_table;
  spb  := Char(isc_action_svc_db_stats)+IBSPBParamSerialize(isc_spb_dbname,Database)+
    IBSPBParamSerialize(isc_spb_options, param);
  if (IBStatTables in Options) and (TableNames <> '') then
    spb := spb+IBSPBParamSerialize(isc_spb_command_line, TableNames);
  Result:=isc_service_start(@FStatus[0], @FSvcHandle, nil, length(spb),
    @spb[1])=0;
  if not Result then
    begin
    CheckError('GetDatabaseStats',FStatus);
    exit;
    end;
  Result:=GetOutput('GetDatabaseStats');
end;

end.