60 |
|
{ } |
61 |
|
{************************************************************************} |
62 |
|
unit FB25ClientAPI; |
63 |
+ |
{$IFDEF MSWINDOWS} |
64 |
+ |
{$DEFINE WINDOWS} |
65 |
+ |
{$ENDIF} |
66 |
|
|
67 |
|
{$IFDEF FPC} |
68 |
|
{$mode delphi} |
95 |
|
FStatusIntf: IStatus; {Keep a reference to the interface - automatic destroy |
96 |
|
when this class is freed and last reference to IStatus |
97 |
|
goes out of scope.} |
95 |
– |
protected |
96 |
– |
{$IFDEF UNIX} |
97 |
– |
function GetFirebirdLibList: string; override; |
98 |
– |
{$ENDIF} |
99 |
– |
procedure LoadInterface; override; |
98 |
|
public |
99 |
< |
constructor Create; |
99 |
> |
constructor Create(aFBLibrary: TFBLibrary); |
100 |
|
destructor Destroy; override; |
101 |
|
function StatusVector: PISC_STATUS; |
102 |
+ |
function LoadInterface: boolean; override; |
103 |
+ |
function GetAPI: IFirebirdAPI; override; |
104 |
+ |
{$IFDEF UNIX} |
105 |
+ |
function GetFirebirdLibList: string; override; |
106 |
+ |
{$ENDIF} |
107 |
|
property IBServiceAPIPresent: boolean read FIBServiceAPIPresent; |
108 |
|
property Status: TFB25Status read FStatus; |
109 |
|
|
168 |
|
|
169 |
|
public |
170 |
|
{Helper Functions} |
171 |
< |
function DecodeInteger(bufptr: PChar; len: short): integer; override; |
172 |
< |
procedure SQLEncodeDate(aDate: TDateTime; bufptr: PChar); override; |
173 |
< |
function SQLDecodeDate(bufptr: PChar): TDateTime; override; |
174 |
< |
procedure SQLEncodeTime(aTime: TDateTime; bufptr: PChar); override; |
175 |
< |
function SQLDecodeTime(bufptr: PChar): TDateTime; override; |
176 |
< |
procedure SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PChar); override; |
177 |
< |
function SQLDecodeDateTime(bufptr: PChar): TDateTime; override; |
171 |
> |
function DecodeInteger(bufptr: PByte; len: short): integer; override; |
172 |
> |
procedure SQLEncodeDate(aDate: TDateTime; bufptr: PByte); override; |
173 |
> |
function SQLDecodeDate(bufptr: PByte): TDateTime; override; |
174 |
> |
procedure SQLEncodeTime(aTime: TDateTime; bufptr: PByte); override; |
175 |
> |
function SQLDecodeTime(bufptr: PByte): TDateTime; override; |
176 |
> |
procedure SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PByte); override; |
177 |
> |
function SQLDecodeDateTime(bufptr: PByte): TDateTime; override; |
178 |
|
|
179 |
|
public |
180 |
|
{IFirebirdAPI} |
181 |
|
|
182 |
|
{Database connections} |
183 |
|
function AllocateDPB: IDPB; |
184 |
< |
function OpenDatabase(DatabaseName: string; DPB: IDPB; RaiseExceptionOnConnectError: boolean=true): IAttachment; |
185 |
< |
function CreateDatabase(DatabaseName: string; DPB: IDPB; RaiseExceptionOnError: boolean=true): IAttachment; overload; |
186 |
< |
function CreateDatabase(sql: string; aSQLDialect: integer; RaiseExceptionOnError: boolean=true): IAttachment; overload; |
184 |
> |
function OpenDatabase(DatabaseName: AnsiString; DPB: IDPB; RaiseExceptionOnConnectError: boolean=true): IAttachment; |
185 |
> |
function CreateDatabase(DatabaseName: AnsiString; DPB: IDPB; RaiseExceptionOnError: boolean=true): IAttachment; overload; |
186 |
> |
function CreateDatabase(sql: AnsiString; aSQLDialect: integer; RaiseExceptionOnError: boolean=true): IAttachment; overload; |
187 |
|
|
188 |
|
{Start Transaction against multiple databases} |
189 |
|
function AllocateTPB: ITPB; |
195 |
|
{Service Manager} |
196 |
|
function AllocateSPB: ISPB; |
197 |
|
function HasServiceAPI: boolean; |
198 |
< |
function GetServiceManager(ServerName: string; Protocol: TProtocol; SPB: ISPB): IServiceManager; |
198 |
> |
function GetServiceManager(ServerName: AnsiString; Protocol: TProtocol; SPB: ISPB): IServiceManager; overload; |
199 |
> |
function GetServiceManager(ServerName: AnsiString; Port: AnsiString; Protocol: TProtocol; SPB: ISPB): IServiceManager; overload; |
200 |
|
|
201 |
|
{Information} |
202 |
|
function GetStatus: IStatus; override; |
203 |
|
function HasRollbackRetaining: boolean; |
204 |
|
function IsEmbeddedServer: boolean; override; |
205 |
< |
function GetImplementationVersion: string; |
205 |
> |
function GetImplementationVersion: AnsiString; |
206 |
|
|
207 |
|
{Firebird 3 API} |
208 |
|
function HasMasterIntf: boolean; |
210 |
|
|
211 |
|
end; |
212 |
|
|
209 |
– |
const |
210 |
– |
Firebird25ClientAPI: TFB25ClientAPI = nil; |
211 |
– |
|
213 |
|
implementation |
214 |
|
|
215 |
< |
uses FBMessages, dynlibs, FB25Attachment, FB25Transaction, FB25Services, FBParamBlock, |
215 |
> |
uses FBMessages, |
216 |
> |
{$IFDEF WINDOWS}Windows, {$ENDIF} |
217 |
> |
{$IFDEF FPC} Dynlibs, {$ENDIF} |
218 |
> |
FB25Attachment, FB25Transaction, FB25Services, FBParamBlock, |
219 |
|
IBUtils; |
220 |
|
|
221 |
|
{ Stubs for 6.0 only functions } |
229 |
|
|
230 |
|
function isc_service_attach_stub(status_vector : PISC_STATUS; |
231 |
|
isc_arg2 : UShort; |
232 |
< |
isc_arg3 : PChar; |
232 |
> |
isc_arg3 : PAnsiChar; |
233 |
|
service_handle : PISC_SVC_HANDLE; |
234 |
|
isc_arg5 : UShort; |
235 |
< |
isc_arg6 : PChar): |
235 |
> |
isc_arg6 : PAnsiChar): |
236 |
|
ISC_STATUS; {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} |
237 |
|
begin |
238 |
|
Result := 0; |
251 |
|
service_handle : PISC_SVC_HANDLE; |
252 |
|
recv_handle : PISC_SVC_HANDLE; |
253 |
|
isc_arg4 : UShort; |
254 |
< |
isc_arg5 : PChar; |
254 |
> |
isc_arg5 : PAnsiChar; |
255 |
|
isc_arg6 : UShort; |
256 |
< |
isc_arg7 : PChar; |
256 |
> |
isc_arg7 : PAnsiChar; |
257 |
|
isc_arg8 : UShort; |
258 |
< |
isc_arg9 : PChar): |
258 |
> |
isc_arg9 : PAnsiChar): |
259 |
|
ISC_STATUS; {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} |
260 |
|
begin |
261 |
|
Result := 0; |
266 |
|
service_handle : PISC_SVC_HANDLE; |
267 |
|
recv_handle : PISC_SVC_HANDLE; |
268 |
|
isc_arg4 : UShort; |
269 |
< |
isc_arg5 : PChar): |
269 |
> |
isc_arg5 : PAnsiChar): |
270 |
|
ISC_STATUS; {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF} |
271 |
|
begin |
272 |
|
Result := 0; |
320 |
|
threadvar |
321 |
|
FStatusVector: TStatusVector; |
322 |
|
|
323 |
+ |
{ TFB25ActivityReporter } |
324 |
+ |
|
325 |
|
function TFB25Status.StatusVector: PStatusVector; |
326 |
|
begin |
327 |
|
Result := @FStatusVector; |
337 |
|
end; |
338 |
|
{$ENDIF} |
339 |
|
|
340 |
< |
procedure TFB25ClientAPI.LoadInterface; |
340 |
> |
function TFB25ClientAPI.LoadInterface: boolean; |
341 |
|
begin |
342 |
< |
inherited LoadInterface; |
342 |
> |
Result := inherited LoadInterface; |
343 |
|
BLOB_get := GetProcAddr('BLOB_get'); {do not localize} |
344 |
|
BLOB_put := GetProcAddr('BLOB_put'); {do not localize} |
345 |
|
isc_wait_for_event := GetProcAddr('isc_wait_for_event'); {do not localize} |
386 |
|
isc_prepare_transaction := GetProcAddr('isc_prepare_transaction'); {do not localize} |
387 |
|
|
388 |
|
FIBServiceAPIPresent := true; |
389 |
< |
isc_rollback_retaining := GetProcAddress(IBLibrary, 'isc_rollback_retaining'); {do not localize} |
389 |
> |
isc_rollback_retaining := GetProcAddress(FFBLibrary.IBLibrary, 'isc_rollback_retaining'); {do not localize} |
390 |
|
if Assigned(isc_rollback_retaining) then |
391 |
|
begin |
392 |
|
isc_service_attach := GetProcAddr('isc_service_attach'); {do not localize} |
414 |
|
isc_encode_sql_time := @isc_encode_sql_time_stub; |
415 |
|
isc_encode_timestamp := @isc_encode_timestamp_stub; |
416 |
|
end; |
417 |
+ |
Result := Result and assigned(isc_attach_database); |
418 |
|
end; |
419 |
|
|
420 |
< |
constructor TFB25ClientAPI.Create; |
420 |
> |
function TFB25ClientAPI.GetAPI: IFirebirdAPI; |
421 |
|
begin |
422 |
< |
inherited; |
422 |
> |
Result := self; |
423 |
> |
end; |
424 |
> |
|
425 |
> |
constructor TFB25ClientAPI.Create(aFBLibrary: TFBLibrary); |
426 |
> |
begin |
427 |
> |
inherited Create(aFBLibrary); |
428 |
|
FStatus := TFB25Status.Create(self); |
429 |
|
FStatusIntf := FStatus; |
418 |
– |
Firebird25ClientAPI := self; |
430 |
|
end; |
431 |
|
|
432 |
|
destructor TFB25ClientAPI.Destroy; |
433 |
|
begin |
434 |
|
FStatusIntf := nil; |
424 |
– |
Firebird25ClientAPI := nil; |
435 |
|
inherited Destroy; |
436 |
|
end; |
437 |
|
|
448 |
|
|
449 |
|
function TFB25ClientAPI.AllocateDPB: IDPB; |
450 |
|
begin |
451 |
< |
Result := TDPB.Create; |
451 |
> |
Result := TDPB.Create(self); |
452 |
|
end; |
453 |
|
|
454 |
< |
function TFB25ClientAPI.OpenDatabase(DatabaseName: string; DPB: IDPB; |
454 |
> |
function TFB25ClientAPI.OpenDatabase(DatabaseName: AnsiString; DPB: IDPB; |
455 |
|
RaiseExceptionOnConnectError: boolean): IAttachment; |
456 |
|
begin |
457 |
< |
Result := TFB25Attachment.Create(DatabaseName,DPB,RaiseExceptionOnConnectError); |
457 |
> |
Result := TFB25Attachment.Create(self,DatabaseName,DPB,RaiseExceptionOnConnectError); |
458 |
|
if not Result.IsConnected then |
459 |
|
Result := nil; |
460 |
|
end; |
461 |
|
|
462 |
< |
function TFB25ClientAPI.CreateDatabase(DatabaseName: string; DPB: IDPB; |
462 |
> |
function TFB25ClientAPI.CreateDatabase(DatabaseName: AnsiString; DPB: IDPB; |
463 |
|
RaiseExceptionOnError: boolean): IAttachment; |
464 |
|
begin |
465 |
< |
Result := TFB25Attachment.CreateDatabase(DatabaseName, DPB, RaiseExceptionOnError ); |
465 |
> |
Result := TFB25Attachment.CreateDatabase(self,DatabaseName, DPB, RaiseExceptionOnError ); |
466 |
|
if (Result <> nil) and not Result.IsConnected then |
467 |
|
Result := nil; |
468 |
|
end; |
469 |
|
|
470 |
< |
function TFB25ClientAPI.CreateDatabase(sql: string; aSQLDialect: integer; |
470 |
> |
function TFB25ClientAPI.CreateDatabase(sql: AnsiString; aSQLDialect: integer; |
471 |
|
RaiseExceptionOnError: boolean): IAttachment; |
472 |
|
begin |
473 |
< |
Result := TFB25Attachment.CreateDatabase(sql,aSQLDialect, RaiseExceptionOnError ); |
473 |
> |
Result := TFB25Attachment.CreateDatabase(self,sql,aSQLDialect, RaiseExceptionOnError ); |
474 |
|
if (Result <> nil) and not Result.IsConnected then |
475 |
|
Result := nil; |
476 |
|
end; |
477 |
|
|
478 |
|
function TFB25ClientAPI.AllocateSPB: ISPB; |
479 |
|
begin |
480 |
< |
Result := TSPB.Create; |
480 |
> |
Result := TSPB.Create(self); |
481 |
|
end; |
482 |
|
|
483 |
|
function TFB25ClientAPI.AllocateTPB: ITPB; |
484 |
|
begin |
485 |
< |
Result := TTPB.Create; |
485 |
> |
Result := TTPB.Create(self); |
486 |
|
end; |
487 |
|
|
488 |
< |
function TFB25ClientAPI.GetServiceManager(ServerName: string; |
488 |
> |
function TFB25ClientAPI.GetServiceManager(ServerName: AnsiString; |
489 |
|
Protocol: TProtocol; SPB: ISPB): IServiceManager; |
490 |
|
begin |
491 |
|
if HasServiceAPI then |
492 |
< |
Result := TFB25ServiceManager.Create(ServerName,Protocol,SPB) |
492 |
> |
Result := TFB25ServiceManager.Create(self,ServerName,Protocol,SPB) |
493 |
> |
else |
494 |
> |
Result := nil; |
495 |
> |
end; |
496 |
> |
|
497 |
> |
function TFB25ClientAPI.GetServiceManager(ServerName: AnsiString; |
498 |
> |
Port: AnsiString; Protocol: TProtocol; SPB: ISPB): IServiceManager; |
499 |
> |
begin |
500 |
> |
if HasServiceAPI then |
501 |
> |
Result := TFB25ServiceManager.Create(self,ServerName,Protocol,SPB,Port) |
502 |
|
else |
503 |
|
Result := nil; |
504 |
|
end; |
506 |
|
function TFB25ClientAPI.StartTransaction(Attachments: array of IAttachment; |
507 |
|
TPB: array of byte; DefaultCompletion: TTransactionCompletion): ITransaction; |
508 |
|
begin |
509 |
< |
Result := TFB25Transaction.Create(Attachments,TPB,DefaultCompletion); |
509 |
> |
Result := TFB25Transaction.Create(self,Attachments,TPB,DefaultCompletion); |
510 |
|
end; |
511 |
|
|
512 |
|
function TFB25ClientAPI.StartTransaction(Attachments: array of IAttachment; |
513 |
|
TPB: ITPB; DefaultCompletion: TTransactionCompletion): ITransaction; |
514 |
|
begin |
515 |
< |
Result := TFB25Transaction.Create(Attachments,TPB,DefaultCompletion); |
515 |
> |
Result := TFB25Transaction.Create(self,Attachments,TPB,DefaultCompletion); |
516 |
|
end; |
517 |
|
|
518 |
|
function TFB25ClientAPI.HasServiceAPI: boolean; |
529 |
|
begin |
530 |
|
Result := false; |
531 |
|
{$IFDEF UNIX} |
532 |
< |
Result := Pos('libfbembed',FFBLibraryName) = 1; |
532 |
> |
Result := Pos('libfbembed',FFBLibrary.GetLibraryName) = 1; |
533 |
|
{$ENDIF} |
534 |
|
{$IFDEF WINDOWS} |
535 |
< |
Result := CompareText(FFBLibraryName,FIREBIRD_EMBEDDED) = 0; |
535 |
> |
Result := CompareText(FFBLibrary.GetLibraryName,FIREBIRD_EMBEDDED) = 0; |
536 |
|
{$ENDIF} |
537 |
|
end; |
538 |
|
|
546 |
|
Result := nil; |
547 |
|
end; |
548 |
|
|
549 |
< |
function TFB25ClientAPI.GetImplementationVersion: string; |
549 |
> |
function TFB25ClientAPI.GetImplementationVersion: AnsiString; |
550 |
|
begin |
551 |
|
Result := FBClientInterfaceVersion; |
552 |
|
end; |
553 |
|
|
554 |
< |
function TFB25ClientAPI.DecodeInteger(bufptr: PChar; len: short): integer; |
554 |
> |
function TFB25ClientAPI.DecodeInteger(bufptr: PByte; len: short): integer; |
555 |
|
begin |
556 |
|
Result := isc_portable_integer(bufptr,len); |
557 |
|
end; |
558 |
|
|
559 |
< |
procedure TFB25ClientAPI.SQLEncodeDate(aDate: TDateTime; bufptr: PChar); |
559 |
> |
procedure TFB25ClientAPI.SQLEncodeDate(aDate: TDateTime; bufptr: PByte); |
560 |
|
var |
561 |
|
tm_date: TCTimeStructure; |
562 |
|
Yr, Mn, Dy: Word; |
573 |
|
isc_encode_sql_date(@tm_date, PISC_DATE(bufptr)); |
574 |
|
end; |
575 |
|
|
576 |
< |
function TFB25ClientAPI.SQLDecodeDate(bufptr: PChar): TDateTime; |
576 |
> |
function TFB25ClientAPI.SQLDecodeDate(bufptr: PByte): TDateTime; |
577 |
|
var |
578 |
|
tm_date: TCTimeStructure; |
579 |
|
begin |
588 |
|
end; |
589 |
|
end; |
590 |
|
|
591 |
< |
procedure TFB25ClientAPI.SQLEncodeTime(aTime: TDateTime; bufptr: PChar); |
591 |
> |
procedure TFB25ClientAPI.SQLEncodeTime(aTime: TDateTime; bufptr: PByte); |
592 |
|
var |
593 |
|
tm_date: TCTimeStructure; |
594 |
|
Hr, Mt, S, Ms: Word; |
602 |
|
tm_mon := 0; |
603 |
|
tm_year := 0; |
604 |
|
end; |
605 |
< |
with Firebird25ClientAPI do |
587 |
< |
isc_encode_sql_time(@tm_date, PISC_TIME(bufptr)); |
605 |
> |
isc_encode_sql_time(@tm_date, PISC_TIME(bufptr)); |
606 |
|
if Ms > 0 then |
607 |
|
Inc(PISC_TIME(bufptr)^,Ms*10); |
608 |
|
end; |
609 |
|
|
610 |
< |
function TFB25ClientAPI.SQLDecodeTime(bufptr: PChar): TDateTime; |
610 |
> |
function TFB25ClientAPI.SQLDecodeTime(bufptr: PByte): TDateTime; |
611 |
|
var |
612 |
|
tm_date: TCTimeStructure; |
613 |
|
msecs: Word; |
624 |
|
end; |
625 |
|
end; |
626 |
|
|
627 |
< |
procedure TFB25ClientAPI.SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PChar); |
627 |
> |
procedure TFB25ClientAPI.SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PByte); |
628 |
|
var |
629 |
|
tm_date: TCTimeStructure; |
630 |
|
Yr, Mn, Dy, Hr, Mt, S, Ms: Word; |
644 |
|
Inc(PISC_TIMESTAMP(bufptr)^.timestamp_time,Ms*10); |
645 |
|
end; |
646 |
|
|
647 |
< |
function TFB25ClientAPI.SQLDecodeDateTime(bufptr: PChar): TDateTime; |
647 |
> |
function TFB25ClientAPI.SQLDecodeDateTime(bufptr: PByte): TDateTime; |
648 |
|
var |
649 |
|
tm_date: TCTimeStructure; |
650 |
|
msecs: Word; |
669 |
|
|
670 |
|
end. |
671 |
|
|
672 |
+ |
|