forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IB_Services.pas
2472 lines (2241 loc) · 72.1 KB
/
IB_Services.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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:[email protected] }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy ([email protected]) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit IB_Services;
interface
{$I FIBPlus.inc}
uses
SysUtils, Classes,
ibase, IB_Intf, IB_Externals {$IFDEF D6+}, Variants {$ELSE} ,StdFuncs {$ENDIF};
const
DefaultBufferSize = 32000;
SPBPrefix = 'isc_spb_';
SPBConstantNames: array[1..isc_spb_last_spb_constant] of AnsiString = (
'user_name',
'sys_user_name',
'sys_user_name_enc',
'password',
'password_enc',
'command_line',
'db_name',
'verbose',
'options',
'connect_timeout',
'dummy_packet_interval',
'sql_role_name',
'instance_name'
);
SPBConstantValues: array[1..isc_spb_last_spb_constant] of Integer = (
isc_spb_user_name_mapped_to_server,
isc_spb_sys_user_name_mapped_to_server,
isc_spb_sys_user_name_enc_mapped_to_server,
isc_spb_password_mapped_to_server,
isc_spb_password_enc_mapped_to_server,
isc_spb_command_line_mapped_to_server,
isc_spb_dbname_mapped_to_server,
isc_spb_verbose_mapped_to_server,
isc_spb_options_mapped_to_server,
isc_spb_connect_timeout_mapped_to_server,
isc_spb_dummy_packet_interval_mapped_to_server,
isc_spb_sql_role_name_mapped_to_server ,
isc_spb_instance_name_mapped_to_server
);
{$IFDEF INC_SERVICE_SUPPORT}
type
TProtocol = (TCP, SPX, NamedPipe, Local);
TOutputBufferOption = (ByLine, ByChunk);
TpFIBCustomService = class;
TLoginEvent = procedure(Database: TpFIBCustomService;
LoginParams: TStrings) of object;
TpFIBCustomService = class(TComponent)
private
FClientLibrary: IIBClientLibrary;
FLibraryName: string;
procedure LoadLibrary;
private
FIBLoaded: Boolean;
FParamsChanged : Boolean;
FSPB, FQuerySPB : PAnsiChar;
FSPBLength, FQuerySPBLength : Short;
// FTraceFlags: TTraceFlags;
FOnLogin: TLoginEvent;
FLoginPrompt: Boolean;
FBufferSize: Integer;
FOutputBuffer: PAnsiChar;
FQueryParams: AnsiString;
FServerName: AnsiString;
FHandle: TISC_SVC_HANDLE;
FStreamedActive : Boolean;
FOnAttach: TNotifyEvent;
FOutputBufferOption: TOutputBufferOption;
FProtocol: TProtocol;
FParams: TStrings;
// FGDSLibrary : IGDSLibrary;
function GetActive: Boolean;
function GetServiceParamBySPB(const Idx: Integer): String;
procedure SetActive(const Value: Boolean);
procedure SetBufferSize(const Value: Integer);
procedure SetParams(const Value: TStrings);
procedure SetServerName(const Value: Ansistring);
procedure SetProtocol(const Value: TProtocol);
procedure SetServiceParamBySPB(const Idx: Integer;
const Value: String);
function IndexOfSPBConst(st: String): Integer;
procedure ParamsChange(Sender: TObject);
procedure ParamsChanging(Sender: TObject);
procedure CheckServerName;
function Call(ErrCode: ISC_STATUS; RaiseError: Boolean): ISC_STATUS;
function ParseString(var RunLen: Integer): Ansistring;
function ParseInteger(var RunLen: Integer): Integer;
procedure GenerateSPB(sl: TStrings; var SPB: AnsiString; var SPBLength: Short);
procedure SetLibraryName(const Value: string);
function StoredLibraryName: Boolean;
protected
procedure Loaded; override;
function Login: Boolean;
procedure CheckActive;
procedure CheckInactive;
property OutputBuffer : PAnsiChar read FOutputBuffer;
property OutputBufferOption : TOutputBufferOption read FOutputBufferOption write FOutputBufferOption;
property BufferSize : Integer read FBufferSize write SetBufferSize default DefaultBufferSize;
procedure InternalServiceQuery;
property ServiceQueryParams: AnsiString read FQueryParams write FQueryParams;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Attach;
procedure Detach;
property Handle: TISC_SVC_HANDLE read FHandle;
property ServiceParamBySPB[const Idx: Integer]: String read GetServiceParamBySPB
write SetServiceParamBySPB;
property ClientLibrary: IIBClientLibrary read FClientLibrary;
property SSPB:PAnsiChar read FSPB;
published
property Active: Boolean read GetActive write SetActive default False;
property ServerName: Ansistring read FServerName write SetServerName;
property LibraryName:string read FLibraryName write SetLibraryName stored StoredLibraryName;
property Protocol: TProtocol read FProtocol write SetProtocol default Local;
property Params: TStrings read FParams write SetParams;
property LoginPrompt: Boolean read FLoginPrompt write FLoginPrompt default True;
// property TraceFlags: TTraceFlags read FTraceFlags write FTraceFlags;
property OnAttach: TNotifyEvent read FOnAttach write FOnAttach;
property OnLogin: TLoginEvent read FOnLogin write FOnLogin;
end;
TDatabaseInfo = class
public
NoOfAttachments: Integer;
NoOfDatabases: Integer;
DbName: array of string;
constructor Create;
destructor Destroy; override;
end;
TLicenseInfo = class
public
Key: array of string;
Id: array of string;
Desc: array of string;
LicensedUsers: Integer;
constructor Create;
destructor Destroy; override;
end;
TLicenseMaskInfo = class
public
LicenseMask: Integer;
CapabilityMask: Integer;
end;
TConfigFileData = class
public
ConfigFileValue: array of integer;
ConfigFileKey: array of integer;
constructor Create;
destructor Destroy; override;
end;
TConfigParams = class
public
ConfigFileData: TConfigFileData;
ConfigFileParams: array of string;
BaseLocation: string;
LockFileLocation: string;
MessageFileLocation: string;
SecurityDatabaseLocation: string;
constructor Create;
destructor Destroy; override;
end;
TVersionInfo = class
public
ServerVersion: String;
ServerImplementation: string;
ServiceVersion: Integer;
end;
TPropertyOption = (Database, License, LicenseMask, ConfigParameters, Version);
TPropertyOptions = set of TPropertyOption;
TpFIBServerProperties = class(TpFIBCustomService)
private
FOptions: TPropertyOptions;
FDatabaseInfo: TDatabaseInfo;
FLicenseInfo: TLicenseInfo;
FLicenseMaskInfo: TLicenseMaskInfo;
FVersionInfo: TVersionInfo;
FConfigParams: TConfigParams;
procedure ParseConfigFileData(var RunLen: Integer);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Fetch;
procedure FetchDatabaseInfo;
procedure FetchLicenseInfo;
procedure FetchLicenseMaskInfo;
procedure FetchConfigParams;
procedure FetchVersionInfo;
property DatabaseInfo: TDatabaseInfo read FDatabaseInfo;
property LicenseInfo: TLicenseInfo read FLicenseInfo;
property LicenseMaskInfo: TLicenseMaskInfo read FLicenseMaskInfo;
property VersionInfo: TVersionInfo read FVersionInfo;
property ConfigParams: TConfigParams read FConfigParams;
published
property Options : TPropertyOptions read FOptions write FOptions;
end;
TpFIBControlService = class (TpFIBCustomService)
private
FStartParams: AnsiString;
FStartSPB: PAnsiChar;
FStartSPBLength: Integer;
function GetIsServiceRunning: Boolean;
protected
property ServiceStartParams: AnsiString read FStartParams write FStartParams;
procedure SetServiceStartOptions; virtual;
procedure ServiceStartAddParam (Value: Ansistring; param: Integer); overload;
procedure ServiceStartAddParam (Value: Integer; param: Integer); overload;
procedure InternalServiceStart;
public
constructor Create(AOwner: TComponent); override;
procedure ServiceStart; virtual;
property IsServiceRunning : Boolean read GetIsServiceRunning;
end;
TServiceGetTextNotify = procedure (Sender: TObject; const Text: string) of object;
TpFIBControlAndQueryService = class (TpFIBControlService)
private
FEof: Boolean;
FAction: Integer;
FOnTextNotify: TServiceGetTextNotify;
procedure SetAction(Value: Integer);
protected
property Action: Integer read FAction write SetAction;
property OnTextNotify: TServiceGetTextNotify read FOnTextNotify write FOnTextNotify;
public
constructor Create (AOwner: TComponent); override;
function GetNextLine : String;
function GetNextChunk : String;
procedure ServiceStart; override;
function GetNextBuf: AnsiString;
property Eof: boolean read FEof;
published
property BufferSize;
end;
TShutdownMode = (Forced, DenyTransaction, DenyAttachment);
TpFIBConfigService = class(TpFIBControlService)
private
FDatabaseName: string;
procedure SetDatabaseName(const Value: string);
protected
public
procedure ServiceStart; override;
procedure ShutdownDatabase (Options: TShutdownMode; Wait: Integer);
procedure SetSweepInterval (Value: Integer);
procedure SetDBSqlDialect (Value: Integer);
procedure SetPageBuffers (Value: Integer);
procedure ActivateShadow;
procedure BringDatabaseOnline;
procedure SetReserveSpace (Value: Boolean);
procedure SetAsyncMode (Value: Boolean);
procedure SetReadOnly (Value: Boolean);
published
property DatabaseName: string read FDatabaseName write SetDatabaseName;
end;
TLicensingAction = (LicenseAdd, LicenseRemove);
TpFIBLicensingService = class(TpFIBControlService)
private
FID: String;
FKey: String;
FAction: TLicensingAction;
procedure SetAction(Value: TLicensingAction);
protected
procedure SetServiceStartOptions; override;
public
procedure AddLicense;
procedure RemoveLicense;
published
property Action: TLicensingAction read FAction write SetAction default LicenseAdd;
property Key: String read FKey write FKey;
property ID: String read FID write FID;
end;
TpFIBLogService = class(TpFIBControlAndQueryService)
private
protected
procedure SetServiceStartOptions; override;
public
published
property OnTextNotify;
end;
TStatOption = (DataPages, DbLog, HeaderPages, IndexPages, SystemRelations,
RecordVersions, StatTables);
TStatOptions = set of TStatOption;
TpFIBStatisticalService = class(TpFIBControlAndQueryService)
private
FDatabaseName : string;
FOptions : TStatOptions;
FTableNames : String;
procedure SetDatabaseName(const Value: string);
protected
procedure SetServiceStartOptions; override;
public
published
property DatabaseName: string read FDatabaseName write SetDatabaseName;
property Options : TStatOptions read FOptions write FOptions;
property TableNames : String read FTableNames write FTableNames;
property OnTextNotify;
end;
TpFIBBackupRestoreService = class(TpFIBControlAndQueryService)
private
FVerbose: Boolean;
protected
public
published
property Verbose : Boolean read FVerbose write FVerbose default False;
property OnTextNotify;
end;
TBackupOption = (IgnoreChecksums, IgnoreLimbo, MetadataOnly, NoGarbageCollection,
OldMetadataDesc, NonTransportable, ConvertExtTables);
TBackupOptions = set of TBackupOption;
TpFIBBackupService = class (TpFIBBackupRestoreService)
private
FDatabaseName: string;
FOptions: TBackupOptions;
FBackupFile: TStrings;
FBlockingFactor: Integer;
procedure SetBackupFile(const Value: TStrings);
protected
procedure SetServiceStartOptions; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ a name=value pair of filename and length }
property BackupFile: TStrings read FBackupFile write SetBackupFile;
property BlockingFactor: Integer read FBlockingFactor write FBlockingFactor;
property DatabaseName: string read FDatabaseName write FDatabaseName;
property Options : TBackupOptions read FOptions write FOptions;
end;
TRestoreOption = (DeactivateIndexes, NoShadow, NoValidityCheck, OneRelationAtATime,
Replace, CreateNewDB, UseAllSpace, ValidationCheck, OnlyMetadata,
FixFssMetadata,FixFssData
);
TRestoreOptions = set of TRestoreOption;
TpFIBRestoreService = class (TpFIBBackupRestoreService)
private
FDatabaseName: TStrings;
FBackupFile: TStrings;
FOptions: TRestoreOptions;
FPageSize: Integer;
FPageBuffers: Integer;
FFixCharset :Ansistring;
procedure SetBackupFile(const Value: TStrings);
procedure SetDatabaseName(const Value: TStrings);
protected
procedure SetServiceStartOptions; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ a name=value pair of filename and length }
property DatabaseName: TStrings read FDatabaseName write SetDatabaseName;
property BackupFile: TStrings read FBackupFile write SetBackupFile;
property PageSize: Integer read FPageSize write FPageSize default 4096;
property PageBuffers: Integer read FPageBuffers write FPageBuffers;
property FixCharset :Ansistring read FFixCharset write FFixCharset;
property Options : TRestoreOptions read FOptions write FOptions default [CreateNewDB];
end;
// Firebird 2.5 only
TpFIBNBackupService = class (TpFIBControlAndQueryService)
private
FDatabaseName: String;
FLevel: Integer;
FBackupFile: string;
protected
procedure SetServiceStartOptions; override;
public
constructor Create(AOwner: TComponent); override;
procedure BackUp(const DBName, BackupName:string; aLevel:integer);
published
property BackupFile: string read FBackupFile write FBackupFile;
property DatabaseName: String read FDatabaseName write FDatabaseName;
property Level: Integer read FLevel write FLevel default 0;
end;
// Firebird 2.5 only
TpFIBNRestoreService = class (TpFIBControlAndQueryService)
private
FDatabaseName: string;
FBackupFiles: TStrings;
procedure SetBackupFiles(const Value: TStrings);
protected
procedure SetServiceStartOptions; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Restore(const aBackUpFiles: array of string; const DBName:string);
published
property BackupFiles: TStrings read FBackupFiles write SetBackupFiles;
property DatabaseName: String read FDatabaseName write FDatabaseName;
end;
TValidateOption = (LimboTransactions, CheckDB, IgnoreChecksum, KillShadows, MendDB,
SweepDB, ValidateDB, ValidateFull);
TValidateOptions = set of TValidateOption;
TTransactionGlobalAction = (CommitGlobal, RollbackGlobal, RecoverTwoPhaseGlobal,
NoGlobalAction);
TTransactionState = (LimboState, CommitState, RollbackState, UnknownState);
TTransactionAdvise = (CommitAdvise, RollbackAdvise, UnknownAdvise);
TServiceTransactionAction = (CommitAction, RollbackAction);
TLimboTransactionInfo = class
public
MultiDatabase: Boolean;
ID: Integer;
HostSite: String;
RemoteSite: String;
RemoteDatabasePath: String;
State: TTransactionState;
Advise: TTransactionAdvise;
Action: TServiceTransactionAction;
end;
TpFIBValidationService = class(TpFIBControlAndQueryService)
private
FDatabaseName: string;
FOptions: TValidateOptions;
FLimboTransactionInfo: array of TLimboTransactionInfo;
FGlobalAction: TTransactionGlobalAction;
procedure SetDatabaseName(const Value: string);
function GetLimboTransactionInfo(index: integer): TLimboTransactionInfo;
function GetLimboTransactionInfoCount: integer;
protected
procedure SetServiceStartOptions; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure FetchLimboTransactionInfo;
procedure FixLimboTransactionErrors;
property LimboTransactionInfo[Index: integer]: TLimboTransactionInfo read GetLimboTransactionInfo;
property LimboTransactionInfoCount: Integer read GetLimboTransactionInfoCount;
published
property DatabaseName: string read FDatabaseName write SetDatabaseName;
property Options: TValidateOptions read FOptions write FOptions;
property GlobalAction: TTransactionGlobalAction read FGlobalAction
write FGlobalAction;
property OnTextNotify;
end;
TUserInfo = class
public
UserName: string;
FirstName: string;
MiddleName: string;
LastName: string;
GroupID: Integer;
UserID: Integer;
end;
TSecurityAction = (ActionAddUser, ActionDeleteUser, ActionModifyUser, ActionDisplayUser);
TSecurityModifyParam = (ModifyFirstName, ModifyMiddleName, ModifyLastName, ModifyUserId,
ModifyGroupId, ModifyPassword,ModifySecAdmin);
TSecurityModifyParams = set of TSecurityModifyParam;
TpFIBSecurityService = class(TpFIBControlAndQueryService)
private
FUserID: Integer;
FGroupID: Integer;
FSecAdmin:boolean;
FFirstName: string;
FUserName: string;
FPassword: string;
FSQLRole: string;
FLastName: string;
FMiddleName: string;
FUserInfo: array of TUserInfo;
FSecurityAction: TSecurityAction;
FModifyParams: TSecurityModifyParams;
procedure ClearParams;
procedure SetSecurityAction (Value: TSecurityAction);
procedure SetFirstName (Value: String);
procedure SetMiddleName (Value: String);
procedure SetLastName (Value: String);
procedure SetPassword (Value: String);
procedure SetUserId (Value: Integer);
procedure SetGroupId (Value: Integer);
procedure SetSecAdmin(Value:boolean);
procedure FetchUserInfo;
function GetUserInfo(Index: Integer): TUserInfo;
function GetUserInfoCount: Integer;
protected
procedure Loaded; override;
procedure SetServiceStartOptions; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DisplayUsers;
procedure DisplayUser(UserName: string);
procedure AddUser;
procedure DeleteUser;
procedure ModifyUser;
property UserInfo[Index: Integer]: TUserInfo read GetUserInfo;
property UserInfoCount: Integer read GetUserInfoCount;
published
property SecurityAction: TSecurityAction read FSecurityAction
write SetSecurityAction;
property SQlRole : string read FSQLRole write FSQLrole;
property UserName : string read FUserName write FUserName;
property FirstName : string read FFirstName write SetFirstName;
property MiddleName : string read FMiddleName write SetMiddleName;
property LastName : string read FLastName write SetLastName;
property UserID : Integer read FUserID write SetUserID;
property GroupID : Integer read FGroupID write SetGroupID;
property Password : string read FPassword write SetPassword;
property SecAdmin:boolean read FSecAdmin write SetSecAdmin default False;
end;
{$ENDIF}
implementation
{$IFDEF INC_SERVICE_SUPPORT}
uses
StrUtil,
{$IFNDEF NO_MONITOR}
FIBSQLMonitor,
{$ENDIF}
fib;
{ TpFIBCustomService }
procedure TpFIBCustomService.Attach;
var
SPB: AnsiString;
ConnectString: AnsiString;
begin
CheckInactive;
CheckServerName;
if FLoginPrompt and not Login then
FIBError(feOperationCancelled, [nil]);
{ Generate a new SPB if necessary }
if FParamsChanged then
begin
FParamsChanged := False;
GenerateSPB(FParams, SPB, FSPBLength);
FIBAlloc(FSPB, 0, FsPBLength);
Move(SPB[1], FSPB[0], FSPBLength);
end;
case FProtocol of
TCP: ConnectString := FServerName + ':service_mgr'; {do not localize}
SPX: ConnectString := FServerName + '@service_mgr'; {do not localize}
NamedPipe: ConnectString := '\\' + FServerName + '\service_mgr'; {do not localize}
Local: ConnectString := 'service_mgr'; {do not localize}
end;
LoadLibrary;
if Call(FClientLibrary.isc_service_attach(StatusVector, Length(ConnectString),
PAnsiChar(ConnectString), @FHandle,
FSPBLength, FSPB), False) > 0 then
begin
FHandle := nil;
IBError(FClientLibrary, Self);
end;
if Assigned(FOnAttach) then
FOnAttach(Self);
{$IFNDEF NO_MONITOR}
MonitorHook.ServiceAttach(Self);
{$ENDIF}
end;
procedure TpFIBCustomService.Loaded;
begin
inherited Loaded;
try
if FStreamedActive and (not Active) then
Attach;
except
on E:Exception do
if csDesigning in ComponentState then
begin
ShowException(E,nil);
end
else
raise;
end;
end;
function TpFIBCustomService.Login: Boolean;
var
IndexOfUser, IndexOfPassword, IndexOfRole: Integer;
Username, Password, RoleName: String;
LoginParams: TStrings;
begin
if Assigned(FOnLogin) then begin
result := True;
LoginParams := TStringList.Create;
try
LoginParams.Assign(Params);
FOnLogin(Self, LoginParams);
Params.Assign (LoginParams);
finally
LoginParams.Free;
end;
end
else
begin
IndexOfUser := IndexOfSPBConst(SPBConstantNames[isc_spb_user_name]);
if IndexOfUser <> -1 then
Username := Copy(Params[IndexOfUser],
Pos('=', Params[IndexOfUser]) + 1, {mbcs ok}
Length(Params[IndexOfUser]));
IndexOfPassword := IndexOfSPBConst(SPBConstantNames[isc_spb_password]);
if IndexOfPassword <> -1 then
Password := Copy(Params[IndexOfPassword],
Pos('=', Params[IndexOfPassword]) + 1, {mbcs ok}
Length(Params[IndexOfPassword]));
IndexOfRole:= IndexOfSPBConst(SPBConstantNames[isc_spb_sql_role_name]);
if IndexOfRole<>-1 then
RoleName := Copy(Params[IndexOfRole],
Pos('=', Params[IndexOfRole]) + 1,
Length(Params[IndexOfRole]));
// if Assigned(LoginDialogExProc) then
// result := LoginDialogExProc(serverName, Username, Password, false)
// else
// Result := false;
if not Assigned(pFIBLoginDialog) then
Result := False
else
Result := pFIBLoginDialog(ServerName, Username, Password,RoleName);
if result then
begin
IndexOfPassword := IndexOfSPBConst(SPBConstantNames[isc_spb_password]);
if IndexOfUser = -1 then
Params.Add(SPBConstantNames[isc_spb_user_name] + '=' + Username)
else
Params[IndexOfUser] := SPBConstantNames[isc_spb_user_name] +
'=' + Username;
if IndexOfPassword = -1 then
Params.Add(SPBConstantNames[isc_spb_password] + '=' + Password)
else
Params[IndexOfPassword] := SPBConstantNames[isc_spb_password] +
'=' + Password;
if IndexOfRole=-1 then
Params.Add(SPBConstantNames[isc_spb_sql_role_name] + '=' + RoleName)
else
Params[IndexOfRole] := SPBConstantNames[isc_spb_sql_role_name] +
'=' + RoleName;
end;
end;
end;
procedure TpFIBCustomService.CheckActive;
begin
if FStreamedActive and (not Active) then
Loaded;
if FHandle = nil then
FIBError(feServiceActive, [nil]);
end;
procedure TpFIBCustomService.CheckInactive;
begin
if FHandle <> nil then
FIBError(feServiceInActive, [nil]);
end;
constructor TpFIBCustomService.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLibraryName := IBASE_DLL;
// FGDSLibrary := GetGDSLibrary;
FIBLoaded := False;
// FGDSLibrary.CheckIBLoaded;
FIBLoaded := True;
FProtocol := local;
FserverName := '';
FParams := TStringList.Create;
FParamsChanged := True;
TStringList(FParams).OnChange := ParamsChange;
TStringList(FParams).OnChanging := ParamsChanging;
FSPB := nil;
FQuerySPB := nil;
FBufferSize := DefaultBufferSize;
FHandle := nil;
FLoginPrompt := True;
// FTraceFlags := [];
FOutputbuffer := nil;
// FGDSLibrary := GetGDSLibrary;
end;
destructor TpFIBCustomService.Destroy;
begin
if FIBLoaded and Assigned(FClientLibrary) then
begin
if FHandle <> nil then
Detach;
FreeMem(FSPB);
FSPB := nil;
end;
FreeMem(FOutputBuffer);
FParams.Free;
FClientLibrary:=nil;
// FGDSLibrary := nil;
inherited Destroy;
end;
procedure TpFIBCustomService.Detach;
begin
CheckActive;
LoadLibrary;
if (Call(FClientLibrary.isc_service_detach(StatusVector, @FHandle), False) > 0) then
begin
FHandle := nil;
IBError(FClientLibrary, Self);
end
else
FHandle := nil;
{$IFNDEF NO_MONITOR}
MonitorHook.ServiceDetach(Self);
{$ENDIF}
end;
procedure TpFIBCustomService.LoadLibrary;
begin
if not Assigned(FClientLibrary) then
FClientLibrary := IB_Intf.GetClientLibrary(FLibraryName);
end;
function TpFIBCustomService.GetActive: Boolean;
begin
result := FHandle <> nil;
end;
function TpFIBCustomService.GetServiceParamBySPB(const Idx: Integer): String;
var
ConstIdx, EqualsIdx: Integer;
begin
if (Idx > 0) and (Idx <= isc_spb_last_spb_constant) then
begin
ConstIdx := IndexOfSPBConst(SPBConstantNames[Idx]);
if ConstIdx = -1 then
result := ''
else
begin
result := Params[ConstIdx];
EqualsIdx := Pos('=', result); {mbcs ok}
if EqualsIdx = 0 then
result := ''
else
result := Copy(result, EqualsIdx + 1, Length(result));
end;
end
else
result := '';
end;
procedure TpFIBCustomService.InternalServiceQuery;
begin
FQuerySPBLength := Length(FQueryParams);
if FQuerySPBLength = 0 then
FIBError(feQueryParamsError, [nil]);
FIBAlloc(FQuerySPB, 0, FQuerySPBLength);
Move(FQueryParams[1], FQuerySPB[0], FQuerySPBLength);
if (FOutputBuffer = nil) then
FIBAlloc(FOutputBuffer, 0, FBufferSize);
try
LoadLibrary;
if call(FClientLibrary.isc_service_query(StatusVector, @FHandle, nil, 0, nil,
FQuerySPBLength, FQuerySPB,
FBufferSize, FOutputBuffer), False) > 0 then
begin
FHandle := nil;
IBError(FClientLibrary, Self);
end;
finally
FreeMem(FQuerySPB);
FQuerySPB := nil;
FQuerySPBLength := 0;
FQueryParams := '';
end;
{$IFNDEF NO_MONITOR}
MonitorHook.ServiceQuery(Self);
{$ENDIF}
end;
procedure TpFIBCustomService.SetActive(const Value: Boolean);
begin
if csReading in ComponentState then
FStreamedActive := Value
else
if Value <> Active then
if Value then
Attach
else
Detach;
end;
procedure TpFIBCustomService.SetBufferSize(const Value: Integer);
begin
if (Value <> FBufferSize) then
begin
FBufferSize := Value;
if FOutputBuffer <> nil then
FIBAlloc(FOutputBuffer, 0, FBufferSize);
end;
end;
procedure TpFIBCustomService.SetParams(const Value: TStrings);
begin
FParams.Assign(Value);
end;
procedure TpFIBCustomService.SetServerName(const Value: AnsiString);
begin
if FServerName <> Value then
begin
CheckInactive;
FServerName := Value;
if (FProtocol = Local) and (FServerName <> '') then
FProtocol := TCP
else
if (FProtocol <> Local) and (FServerName = '') then
FProtocol := Local;
end;
end;
procedure TpFIBCustomService.SetProtocol(const Value: TProtocol);
begin
if FProtocol <> Value then
begin
CheckInactive;
FProtocol := Value;
if (Value = Local) then
FServerName := '';
end;
end;
procedure TpFIBCustomService.SetServiceParamBySPB(const Idx: Integer;
const Value: String);
var
ConstIdx: Integer;
begin
ConstIdx := IndexOfSPBConst(SPBConstantNames[Idx]);
if (Value = '') then
begin
if ConstIdx <> -1 then
Params.Delete(ConstIdx);
end
else
begin
if (ConstIdx = -1) then
Params.Add(SPBConstantNames[Idx] + '=' + Value)
else
Params[ConstIdx] := SPBConstantNames[Idx] + '=' + Value;
end;
end;
function TpFIBCustomService.IndexOfSPBConst(st: String): Integer;
var
i, pos_of_str: Integer;
begin
result := -1;
for i := 0 to Params.Count - 1 do
begin
pos_of_str := Pos(st, Params[i]); {mbcs ok}
if (pos_of_str = 1) or (pos_of_str = Length(SPBPrefix) + 1) then
begin
result := i;
break;
end;
end;
end;
procedure TpFIBCustomService.ParamsChange(Sender: TObject);
begin
FParamsChanged := True;
end;
procedure TpFIBCustomService.ParamsChanging(Sender: TObject);
begin
CheckInactive;
end;
procedure TpFIBCustomService.CheckServerName;
begin
if (FServerName = '') and (FProtocol <> Local) then
FIBError(feServerNameMissing, [nil]);
end;
function TpFIBCustomService.Call(ErrCode: ISC_STATUS;
RaiseError: Boolean): ISC_STATUS;
begin
result := ErrCode;
if RaiseError and (ErrCode > 0) then
IBError(FClientLibrary, Self);
end;
function TpFIBCustomService.ParseString(var RunLen: Integer): Ansistring;
var
Len: UShort;
tmp: AnsiChar;
begin
LoadLibrary;
Len := FClientLibrary.isc_vax_integer(OutputBuffer + RunLen, 2);
RunLen := RunLen + 2;
if (Len <> 0) then
begin
tmp := OutputBuffer[RunLen + Len];
OutputBuffer[RunLen + Len] := #0;
result := AnsiString(PAnsiChar(@OutputBuffer[RunLen]));
OutputBuffer[RunLen + Len] := tmp;
RunLen := RunLen + Len;
end
else
result := '';
end;
function TpFIBCustomService.ParseInteger(var RunLen: Integer): Integer;
begin
LoadLibrary;
Result := FClientLibrary.isc_vax_integer(OutputBuffer + RunLen, 4);
RunLen := RunLen + 4;
end;
{
* GenerateSPB -
* Given a string containing a textual representation
* of the Service parameters, generate a service
* parameter buffer, and return it and its length
* in SPB and SPBLength, respectively.
}
procedure TpFIBCustomService.GenerateSPB(sl: TStrings; var SPB: AnsiString;
var SPBLength: Short);
var
i, j : Integer;
SPBVal, SPBServerVal: UShort;
param_name, param_value: String;
pval: Integer;
begin
{ The SPB is initially empty, with the exception that
the SPB version must be the first byte of the string.
}
SPBLength := 2;