ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/udr/client/FBClientAPI.pas
(Generate patch)

Comparing:
ibx/trunk/fbintf/client/FBClientAPI.pas (file contents), Revision 47 by tony, Mon Jan 9 15:31:51 2017 UTC vs.
ibx/branches/udr/client/FBClientAPI.pas (file contents), Revision 387 by tony, Wed Jan 19 13:34:42 2022 UTC

# Line 60 | Line 60
60   {                                                                        }
61   {************************************************************************}
62   unit FBClientAPI;
63 + {$IFDEF MSWINDOWS}
64 + {$DEFINE WINDOWS}
65 + {$ENDIF}
66  
67   {$IFDEF FPC}
68   {$mode delphi}
# Line 70 | Line 73 | unit FBClientAPI;
73   interface
74  
75   uses
76 <  Classes,  Dynlibs, IB, IBHeader, FBActivityMonitor, FBMessages, IBExternals;
76 >  Classes,
77 >    {$IFDEF WINDOWS}Windows, {$ENDIF}
78 >    {$IFDEF FPC} Dynlibs, {$ENDIF}
79 >   IB, IBHeader, FBActivityMonitor, FBMessages, IBExternals, FmtBCD;
80  
81 < {For Linux see result of GetFirebirdLibList method}
81 > {For Linux see result of GetFirebirdLibListruntime/nongui/winipc.inc method}
82   {$IFDEF DARWIN}
83   const
84   FIREBIRD_SO2 = 'libfbclient.dylib';
# Line 84 | Line 90 | FIREBIRD_CLIENT = 'fbclient.dll'; {do no
90   FIREBIRD_EMBEDDED = 'fbembed.dll';
91   {$ENDIF}
92  
93 + const
94 +  {fb_shutdown reasons}
95 +  fb_shutrsn_svc_stopped          = -1;
96 +  fb_shutrsn_no_connection        = -2;
97 +  fb_shutrsn_app_stopped          = -3;
98 +  fb_shutrsn_signal               = -5;
99 +  fb_shutrsn_services             = -6;
100 +  fb_shutrsn_exit_called          = -7;
101 +
102 + const
103 +    DefaultTimeZoneFile = '/etc/timezone';
104 +
105 + const
106 +  IBLocalBufferLength = 512;
107 +  IBBigLocalBufferLength = IBLocalBufferLength * 2;
108 +  IBHugeLocalBufferLength = IBBigLocalBufferLength * 20;
109 +
110   type
111    TStatusVector              = array[0..19] of NativeInt;
112    PStatusVector              = ^TStatusVector;
# Line 92 | Line 115 | type
115  
116    { TFBStatus }
117  
118 <  TFBStatus = class(TFBInterfacedObject)
118 >  TFBStatus = class(TFBInterfacedObject, IStatus)
119    private
97    FIBCS: TRTLCriticalSection; static;
120      FIBDataBaseErrorMessages: TIBDataBaseErrorMessages;
121 +    FPrefix: AnsiString;
122 +    function SQLCodeSupported: boolean;
123    protected
124      FOwner: TFBClientAPI;
125 +    function GetIBMessage: Ansistring; virtual; abstract;
126 +    function GetSQLMessage: Ansistring;
127    public
128 <    constructor Create(aOwner: TFBClientAPI);
128 >    constructor Create(aOwner: TFBClientAPI; prefix: AnsiString='');
129      function StatusVector: PStatusVector; virtual; abstract;
130 +    procedure Assign(src: TFBStatus); virtual;
131 +    function Clone: IStatus; virtual; abstract;
132  
133      {IStatus}
134 <    function GetIBErrorCode: Long;
135 <    function Getsqlcode: Long;
136 <    function GetMessage: string;
134 >    function GetIBErrorCode: TStatusCode;
135 >    function Getsqlcode: TStatusCode;
136 >    function GetMessage: AnsiString;
137      function CheckStatusVector(ErrorCodes: array of TFBStatusCode): Boolean;
138      function GetIBDataBaseErrorMessages: TIBDataBaseErrorMessages;
139      procedure SetIBDataBaseErrorMessages(Value: TIBDataBaseErrorMessages);
140    end;
141  
142 +  { TFBLibrary }
143 +
144 +  TFBLibrary = class(TFBInterfacedObject,IFirebirdLibrary)
145 +  private
146 +    class var FEnvSetupDone: boolean;
147 +    class var FLibraryList: array of IFirebirdLibrary;
148 +  private
149 +    FFirebirdAPI: IFirebirdAPI;
150 +    FRequestedLibName: string;
151 +    function LoadIBLibrary: boolean;
152 +  protected
153 +    FFBLibraryName: string;
154 +    FIBLibrary: TLibHandle;
155 +    procedure FreeFBLibrary;
156 +    function GetOverrideLibName: string;
157 +    class procedure SetupEnvironment;
158 +  protected
159 +    function GetFirebird3API: IFirebirdAPI; virtual; abstract;
160 +    function GetLegacyFirebirdAPI: IFirebirdAPI; virtual; abstract;
161 +  public
162 +    constructor Create(aLibPathName: string='');
163 +    destructor Destroy; override;
164 +    class function GetFBLibrary(aLibPathName: string): IFirebirdLibrary;
165 +    class procedure FreeLibraries;
166 +    function SameLibrary(aLibName: string): boolean;
167 +
168 +  public
169 +    {IFirebirdLibrary}
170 +    function GetHandle: TLibHandle;
171 +    function GetLibraryName: string;
172 +    function GetLibraryFilePath: string;
173 +    function GetFirebirdAPI: IFirebirdAPI;
174 +    property IBLibrary: TLibHandle read FIBLibrary;
175 +  end;
176 +
177    { TFBClientAPI }
178  
179    TFBClientAPI = class(TFBInterfacedObject)
180    private
181 <    FOwnsIBLibrary: boolean;
182 <    procedure LoadIBLibrary;
181 >    FLocalTimeZoneName: AnsiString; {Informal Time Zone Name from tzname e.g. GMT or BST}
182 >    FTZDataTimeZoneID: AnsiString; {TZData DB ID e.g. Europe/London}
183 >    FLocalTimeOffset: integer;
184 >    FIsDaylightSavingsTime: boolean;
185 >    class var FIBCS: TRTLCriticalSection;
186 >    function FBTimeStampToDateTime(aDate, aTime: longint): TDateTime;
187 >    procedure GetTZDataSettings;
188    protected
189 <    FFBLibraryName: string; static;
190 <    FFBLibraryPath: string; static;
191 <    IBLibrary: TLibHandle; static;
192 <    function GetProcAddr(ProcName: PChar): Pointer;
193 <    function GetOverrideLibName: string;
194 <    {$IFDEF UNIX}
195 <    function GetFirebirdLibList: string; virtual; abstract;
196 <    {$ENDIF}
197 <    procedure LoadInterface; virtual;
189 >    FFBLibrary: TFBLibrary;
190 >    function GetProcAddr(ProcName: PAnsiChar): Pointer;
191 >
192 >  protected type
193 >    Tfb_shutdown = function (timeout: uint;
194 >                                 const reason: int): int;
195 >                   {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
196 >  protected
197 >    {FB Shutdown API}
198 >    fb_shutdown: Tfb_shutdown;
199 >
200    public
201      {Taken from legacy API}
132    isc_sqlcode: Tisc_sqlcode;
202      isc_sql_interprete: Tisc_sql_interprete;
203 <    isc_interprete: Tisc_interprete;
135 <    isc_event_counts: Tisc_event_counts;
136 <    isc_event_block: Tisc_event_block;
137 <    isc_free: Tisc_free;
203 >    isc_sqlcode: Tisc_sqlcode;
204  
205 <    constructor Create;
140 <    destructor Destroy; override;
205 >    constructor Create(aFBLibrary: TFBLibrary);
206      procedure IBAlloc(var P; OldSize, NewSize: Integer);
207      procedure IBDataBaseError;
208 <    procedure SetupEnvironment;
208 >    function LoadInterface: boolean; virtual;
209 >    procedure FBShutdown; virtual;
210 >    function GetAPI: IFirebirdAPI; virtual; abstract;
211 >    {$IFDEF UNIX}
212 >    function GetFirebirdLibList: string; virtual; abstract;
213 >    {$ENDIF}
214 >    function HasDecFloatSupport: boolean;
215 >    function HasInt128Support: boolean; virtual;
216 >    function HasLocalTZDB: boolean; virtual;
217 >    function HasExtendedTZSupport: boolean; virtual;
218 >    function HasTimeZoneSupport: boolean; virtual;
219  
220 +  public
221 +    property LocalTimeZoneName: AnsiString read FLocalTimeZoneName;
222 +    property TZDataTimeZoneID: AnsiString read FTZDataTimeZoneID;
223 +    property LocalTimeOffset: integer read FLocalTimeOffset;
224 +  public
225      {Encode/Decode}
226 <    procedure EncodeInteger(aValue: integer; len: integer; buffer: PChar);
227 <    function DecodeInteger(bufptr: PChar; len: short): integer; virtual; abstract;
228 <    procedure SQLEncodeDate(aDate: TDateTime; bufptr: PChar); virtual; abstract;
229 <    function SQLDecodeDate(byfptr: PChar): TDateTime; virtual; abstract;
230 <    procedure SQLEncodeTime(aTime: TDateTime; bufptr: PChar); virtual; abstract;
231 <    function SQLDecodeTime(bufptr: PChar): TDateTime;  virtual; abstract;
232 <    procedure SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PChar); virtual; abstract;
233 <    function SQLDecodeDateTime(bufptr: PChar): TDateTime; virtual; abstract;
234 <
226 >    procedure EncodeInteger(aValue: int64; len: integer; buffer: PByte);
227 >    function DecodeInteger(bufptr: PByte; len: short): int64;
228 >    procedure SQLEncodeDate(aDate: TDateTime; bufptr: PByte);  virtual; abstract;
229 >    function SQLDecodeDate(byfptr: PByte): TDateTime;  virtual; abstract;
230 >    procedure SQLEncodeTime(aTime: TDateTime; bufptr: PByte);  virtual; abstract;
231 >    function SQLDecodeTime(bufptr: PByte): TDateTime;  virtual; abstract;
232 >    procedure SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PByte); virtual; abstract;
233 >    function  SQLDecodeDateTime(bufptr: PByte): TDateTime; virtual; abstract;
234 >    function Int128ToStr(bufptr: PByte; scale: integer): AnsiString; virtual;
235 >    procedure StrToInt128(scale: integer; aValue: AnsiString; bufptr: PByte);
236 >      virtual;
237 >    procedure SQLDecFloatEncode(aValue: tBCD; SQLType: cardinal; bufptr: PByte); virtual;
238 >    function SQLDecFloatDecode(SQLType: cardinal;  bufptr: PByte): tBCD; virtual;
239  
240      {IFirebirdAPI}
241      function GetStatus: IStatus; virtual; abstract;
242      function IsLibraryLoaded: boolean;
243      function IsEmbeddedServer: boolean; virtual; abstract;
244 <    function GetLibraryName: string;
245 <    function GetCharsetName(CharSetID: integer): string;
246 <    function CharSetID2CodePage(CharSetID: integer; var CodePage: TSystemCodePage): boolean;
247 <    function CodePage2CharSetID(CodePage: TSystemCodePage; var CharSetID: integer): boolean;
164 <    function CharSetName2CharSetID(CharSetName: string; var CharSetID: integer): boolean;
165 <    function CharSetWidth(CharSetID: integer; var Width: integer): boolean;
244 >    function GetFBLibrary: IFirebirdLibrary;
245 >    function GetImplementationVersion: AnsiString;
246 >    function GetClientMajor: integer;  virtual; abstract;
247 >    function GetClientMinor: integer;  virtual; abstract;
248    end;
249  
250 < const FirebirdClientAPI: TFBClientAPI = nil;
250 >    IJournallingHook = interface
251 >      ['{7d3e45e0-3628-416a-9e22-c20474825031}']
252 >      procedure TransactionStart(Tr: ITransaction);
253 >      function TransactionEnd(TransactionID: integer; Completion: TTrCompletionState): boolean;
254 >      procedure TransactionRetained(Tr: ITransaction; OldTransactionID: integer; Action: TTransactionAction);
255 >      procedure ExecQuery(Stmt: IStatement);
256 >      procedure ExecImmediateJnl(sql: AnsiString; tr: ITransaction);
257 >    end;
258  
259   implementation
260  
261 < uses IBUtils, {$IFDEF Unix} initc, {$ENDIF}
261 > uses IBUtils, Registry,
262 >  {$IFDEF Unix} unix, initc, dl, {$ENDIF}
263 > {$IFDEF FPC}
264   {$IFDEF WINDOWS }
265 < Windows,Registry, WinDirs,
265 > WinDirs,
266 > {$ENDIF}
267 > {$ELSE}
268 > ShlObj,
269   {$ENDIF}
270   SysUtils;
271  
272   {$IFDEF UNIX}
273 < {$I uloadlibrary.inc}
273 > {$I 'include/uloadlibrary.inc'}
274   {$ELSE}
275 < {$I wloadlibrary.inc}
275 > {$I 'include/wloadlibrary.inc'}
276   {$ENDIF}
277  
278 < type
279 <  TCharsetMap = record
280 <    CharsetID: integer;
281 <    CharSetName: string;
282 <    CharSetWidth: integer;
283 <    CodePage: TSystemCodePage;
278 >
279 > { TFBLibrary }
280 >
281 > function TFBLibrary.GetOverrideLibName: string;
282 > begin
283 >  Result := FFBLibraryName;
284 >  if (Result = '') and AllowUseOfFBLIB then
285 >    Result := GetEnvironmentVariable('FBLIB');
286 >  if Result = '' then
287 >  begin
288 >    if assigned(OnGetLibraryName) then
289 >      OnGetLibraryName(Result)
290    end;
291 + end;
292  
293 < const
294 <  CharSetMap: array [0..69] of TCharsetMap = (
295 <  (CharsetID: 0; CharSetName: 'NONE'; CharSetWidth: 1; CodePage: CP_ACP),
296 <  (CharsetID: 1; CharSetName: 'OCTETS'; CharSetWidth: 1; CodePage: CP_NONE),
297 <  (CharsetID: 2; CharSetName: 'ASCII'; CharSetWidth: 1; CodePage: CP_ASCII),
298 <  (CharsetID: 3; CharSetName: 'UNICODE_FSS'; CharSetWidth: 3; CodePage: CP_UTF8),
299 <  (CharsetID: 4; CharSetName: 'UTF8'; CharSetWidth: 4; CodePage: CP_UTF8),
300 <  (CharsetID: 5; CharSetName: 'SJIS_0208'; CharSetWidth: 2; CodePage: 20932),
301 <  (CharsetID: 6; CharSetName: 'EUCJ_0208'; CharSetWidth: 2; CodePage: 20932),
302 <  (CharsetID: 7; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
303 <  (CharsetID: 8; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
304 <  (CharsetID: 9; CharSetName: 'DOS737'; CharSetWidth: 1; CodePage: 737),
305 <  (CharsetID: 10; CharSetName: 'DOS437'; CharSetWidth: 1; CodePage: 437),
306 <  (CharsetID: 11; CharSetName: 'DOS850'; CharSetWidth: 1; CodePage: 850),
307 <  (CharsetID: 12; CharSetName: 'DOS865'; CharSetWidth: 1; CodePage: 865),
308 <  (CharsetID: 13; CharSetName: 'DOS860'; CharSetWidth: 1; CodePage: 860),
309 <  (CharsetID: 14; CharSetName: 'DOS863'; CharSetWidth: 1; CodePage: 863),
310 <  (CharsetID: 15; CharSetName: 'DOS775'; CharSetWidth: 1; CodePage: 775),
311 <  (CharsetID: 16; CharSetName: 'DOS858'; CharSetWidth: 1; CodePage: 858),
312 <  (CharsetID: 17; CharSetName: 'DOS862'; CharSetWidth: 1; CodePage: 862),
313 <  (CharsetID: 18; CharSetName: 'DOS864'; CharSetWidth: 1; CodePage: 864),
314 <  (CharsetID: 19; CharSetName: 'NEXT'; CharSetWidth: 1; CodePage: CP_NONE),
315 <  (CharsetID: 20; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
316 <  (CharsetID: 21; CharSetName: 'ISO8859_1'; CharSetWidth: 1; CodePage: 28591),
317 <  (CharsetID: 22; CharSetName: 'ISO8859_2'; CharSetWidth: 1; CodePage: 28592),
318 <  (CharsetID: 23; CharSetName: 'ISO8859_3'; CharSetWidth: 1; CodePage: 28593),
319 <  (CharsetID: 24; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
320 <  (CharsetID: 25; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
220 <  (CharsetID: 26; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
221 <  (CharsetID: 27; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
222 <  (CharsetID: 28; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
223 <  (CharsetID: 29; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
224 <  (CharsetID: 30; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
225 <  (CharsetID: 31; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
226 <  (CharsetID: 32; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
227 <  (CharsetID: 33; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
228 <  (CharsetID: 34; CharSetName: 'ISO8859_4'; CharSetWidth: 1; CodePage: 28594),
229 <  (CharsetID: 35; CharSetName: 'ISO8859_5'; CharSetWidth: 1; CodePage: 28595),
230 <  (CharsetID: 36; CharSetName: 'ISO8859_6'; CharSetWidth: 1; CodePage: 28596),
231 <  (CharsetID: 37; CharSetName: 'ISO8859_7'; CharSetWidth: 1; CodePage: 28597),
232 <  (CharsetID: 38; CharSetName: 'ISO8859_8'; CharSetWidth: 1; CodePage: 28598),
233 <  (CharsetID: 39; CharSetName: 'ISO8859_9'; CharSetWidth: 1; CodePage: 28599),
234 <  (CharsetID: 40; CharSetName: 'ISO8859_13'; CharSetWidth: 1; CodePage: 28603),
235 <  (CharsetID: 41; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
236 <  (CharsetID: 42; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
237 <  (CharsetID: 43; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
238 <  (CharsetID: 44; CharSetName: 'KSC_5601'; CharSetWidth: 2; CodePage: 949),
239 <  (CharsetID: 45; CharSetName: 'DOS852'; CharSetWidth: 1; CodePage: 852),
240 <  (CharsetID: 46; CharSetName: 'DOS857'; CharSetWidth: 1; CodePage: 857),
241 <  (CharsetID: 47; CharSetName: 'DOS861'; CharSetWidth: 1; CodePage: 861),
242 <  (CharsetID: 48; CharSetName: 'DOS866'; CharSetWidth: 1; CodePage: 866),
243 <  (CharsetID: 49; CharSetName: 'DOS869'; CharSetWidth: 1; CodePage: 869),
244 <  (CharsetID: 50; CharSetName: 'CYRL'; CharSetWidth: 1; CodePage: 1251),
245 <  (CharsetID: 51; CharSetName: 'WIN1250'; CharSetWidth: 1; CodePage: 1250),
246 <  (CharsetID: 52; CharSetName: 'WIN1251'; CharSetWidth: 1; CodePage: 1251),
247 <  (CharsetID: 53; CharSetName: 'WIN1252'; CharSetWidth: 1; CodePage: 1252),
248 <  (CharsetID: 54; CharSetName: 'WIN1253'; CharSetWidth: 1; CodePage: 1253),
249 <  (CharsetID: 55; CharSetName: 'WIN1254'; CharSetWidth: 1; CodePage: 1254),
250 <  (CharsetID: 56; CharSetName: 'BIG_5'; CharSetWidth: 2; CodePage: 950),
251 <  (CharsetID: 57; CharSetName: 'GB_2312'; CharSetWidth: 2; CodePage: 936),
252 <  (CharsetID: 58; CharSetName: 'WIN1255'; CharSetWidth: 1; CodePage: 1255),
253 <  (CharsetID: 59; CharSetName: 'WIN1256'; CharSetWidth: 1; CodePage: 1256),
254 <  (CharsetID: 60; CharSetName: 'WIN1257'; CharSetWidth: 1; CodePage: 1257),
255 <  (CharsetID: 61; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
256 <  (CharsetID: 62; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
257 <  (CharsetID: 63; CharSetName: 'KOI8R'; CharSetWidth: 1; CodePage: 20866),
258 <  (CharsetID: 64; CharSetName: 'KOI8U'; CharSetWidth: 1; CodePage: 21866),
259 <  (CharsetID: 65; CharSetName: 'WIN1258'; CharSetWidth: 1; CodePage: 1258),
260 <  (CharsetID: 66; CharSetName: 'TIS620'; CharSetWidth: 1; CodePage: 874),
261 <  (CharsetID: 67; CharSetName: 'GBK'; CharSetWidth: 2; CodePage: 936),
262 <  (CharsetID: 68; CharSetName: 'CP943C'; CharSetWidth: 2; CodePage: 943),
263 <  (CharsetID: 69; CharSetName: 'GB18030'; CharSetWidth: 4; CodePage: 54936)
264 < );
265 <
266 <  {$IFDEF Unix}
267 <  {SetEnvironmentVariable doesn't exist so we have to use C Library}
268 <  function setenv(name:Pchar; value:Pchar; replace:integer):integer;cdecl;external clib name 'setenv';
269 <  function unsetenv(name:Pchar):integer;cdecl;external clib name 'unsetenv';
270 <  function SetEnvironmentVariable(name:PChar; value:PChar):boolean;
271 <  // Set environment variable; if empty string given, remove it.
293 > procedure TFBLibrary.FreeFBLibrary;
294 > begin
295 >  (FFirebirdAPI as TFBClientAPI).FBShutdown;
296 >  if FIBLibrary <> NilHandle then
297 >    FreeLibrary(FIBLibrary);
298 >  FIBLibrary := NilHandle;
299 >  FFBLibraryName := '';
300 > end;
301 >
302 > function TFBLibrary.GetLibraryName: string;
303 > begin
304 >  Result := ExtractFileName(FFBLibraryName);
305 > end;
306 >
307 > function TFBLibrary.GetFirebirdAPI: IFirebirdAPI;
308 > begin
309 >  Result := FFirebirdAPI;
310 > end;
311 >
312 > constructor TFBLibrary.Create(aLibPathName: string);
313 > begin
314 >  inherited Create;
315 >  SetupEnvironment;
316 >  FFBLibraryName := aLibPathName;
317 >  FIBLibrary := NilHandle;
318 >  FFirebirdAPI := GetFirebird3API;
319 >  FRequestedLibName := aLibPathName;
320 >  if aLibPathName <> '' then
321    begin
322 <    result:=false; //assume failure
323 <    if value = '' then
324 <    begin
325 <      // Assume user wants to remove variable.
326 <      if unsetenv(name)=0 then result:=true;
327 <    end
328 <    else
322 >    SetLength(FLibraryList,Length(FLibraryList)+1);
323 >    FLibraryList[Length(FLibraryList)-1] := self;
324 >  end;
325 >  if FFirebirdAPI <> nil then
326 >  begin
327 >    {First try Firebird 3}
328 >    if not LoadIBLibrary or not (FFirebirdAPI as TFBClientAPI).LoadInterface then
329 >      FFirebirdAPI := nil;
330 >  end;
331 >
332 >  if FFirebirdAPI = nil then
333 >  begin
334 >    {now try Firebird 2.5. Under Unix we need to reload the library in case we
335 >     are to use the embedded library}
336 >    FFirebirdAPI := GetLegacyFirebirdAPI;
337 >    if FFirebirdAPI <> nil then
338      begin
339 <      // Non empty so set the variable
340 <      if setenv(name, value, 1)=0 then result:=true;
339 >      {$IFDEF UNIX}
340 >      FreeFBLibrary;
341 >      {$ENDIF}
342 >      if not LoadIBLibrary or not (FFirebirdAPI as TFBClientAPI).LoadInterface then
343 >        FFirebirdAPI := nil;
344      end;
345    end;
346 <  {$ENDIF}
346 >  {Note: FFirebirdAPI will be set to nil if the Firebird API fails to load}
347 > end;
348  
349 < { TFBClientAPI }
349 > destructor TFBLibrary.Destroy;
350 > begin
351 >  FreeFBLibrary;
352 >  FFirebirdAPI := nil;
353 >  inherited Destroy;
354 > end;
355  
356 < constructor TFBClientAPI.Create;
356 > class function TFBLibrary.GetFBLibrary(aLibPathName: string): IFirebirdLibrary;
357 > var i: integer;
358   begin
359 <  inherited Create;
360 <  LoadIBLibrary;
293 <  if (IBLibrary <> NilHandle) then
359 >  Result := nil;
360 >  if aLibPathName <> '' then
361    begin
362 <    SetupEnvironment;
363 <    LoadInterface;
362 >    for i := 0 to Length(FLibraryList) - 1 do
363 >    begin
364 >      if (FLibraryList[i] as TFBLibrary).SameLibrary(aLibPathName) then
365 >      begin
366 >        Result := FLibraryList[i];
367 >        Exit;
368 >      end;
369 >    end;
370 >    Result := Create(aLibPathName);
371    end;
372 <  FirebirdClientAPI := self;
372 >
373   end;
374  
375 < destructor TFBClientAPI.Destroy;
375 > class procedure TFBLibrary.FreeLibraries;
376 > var i: integer;
377   begin
378 <  FirebirdClientAPI := nil;
379 <  if FOwnsIBLibrary and (IBLibrary <> NilHandle) then
380 <    UnloadLibrary(IBLibrary);
381 <  IBLibrary := NilHandle;
382 <  inherited Destroy;
378 >  for i := 0 to Length(FLibraryList) - 1 do
379 >    FLibraryList[i] := nil;
380 >  SetLength(FLibraryList,0);
381 > end;
382 >
383 > function TFBLibrary.SameLibrary(aLibName: string): boolean;
384 > begin
385 >  Result := FRequestedLibName = aLibName;
386 > end;
387 >
388 > function TFBLibrary.GetHandle: TLibHandle;
389 > begin
390 >  Result := FIBLibrary;
391 > end;
392 >
393 > { TFBClientAPI }
394 >
395 > constructor TFBClientAPI.Create(aFBLibrary: TFBLibrary);
396 > begin
397 >  inherited Create;
398 >  FFBLibrary := aFBLibrary;
399 >  GetTZDataSettings;
400   end;
401  
402   procedure TFBClientAPI.IBAlloc(var P; OldSize, NewSize: Integer);
# Line 312 | Line 404 | var
404    i: Integer;
405   begin
406    ReallocMem(Pointer(P), NewSize);
407 <  for i := OldSize to NewSize - 1 do PChar(P)[i] := #0;
407 >  for i := OldSize to NewSize - 1 do PAnsiChar(P)[i] := #0;
408   end;
409  
410   procedure TFBClientAPI.IBDataBaseError;
# Line 320 | Line 412 | begin
412    raise EIBInterBaseError.Create(GetStatus);
413   end;
414  
415 < {Under Unixes, if using an embedded server then set up local TMP and LOCK Directories}
324 <
325 < procedure TFBClientAPI.SetupEnvironment;
326 < var TmpDir: string;
327 < begin
328 <  {$IFDEF UNIX}
329 <    TmpDir := GetTempDir +
330 <        DirectorySeparator + 'firebird_' + sysutils.GetEnvironmentVariable('USER');
331 <    if sysutils.GetEnvironmentVariable('FIREBIRD_TMP') = '' then
332 <    begin
333 <      if not DirectoryExists(tmpDir) then
334 <        mkdir(tmpDir);
335 <      SetEnvironmentVariable('FIREBIRD_TMP',PChar(TmpDir));
336 <    end;
337 <    if sysutils.GetEnvironmentVariable('FIREBIRD_LOCK') = '' then
338 <    begin
339 <      if not DirectoryExists(tmpDir) then
340 <        mkdir(tmpDir);
341 <      SetEnvironmentVariable('FIREBIRD_LOCK',PChar(TmpDir));
342 <    end;
343 <  {$ENDIF}
344 < end;
345 <
346 < procedure TFBClientAPI.EncodeInteger(aValue: integer; len: integer; buffer: PChar);
415 > procedure TFBClientAPI.EncodeInteger(aValue: int64; len: integer; buffer: PByte);
416   begin
417    while len > 0 do
418    begin
419 <    buffer^ := char(aValue and $FF);
419 >    buffer^ := aValue and $FF;
420      Inc(buffer);
421      Dec(len);
422      aValue := aValue shr 8;
423    end;
424   end;
425  
426 + (*
427 +  DecodeInteger is Translated from
428 +
429 + SINT64 API_ROUTINE isc_portable_integer(const UCHAR* ptr, SSHORT length)
430 + if (!ptr || length <= 0 || length > 8)
431 +        return 0;
432 +
433 + SINT64 value = 0;
434 + int shift = 0;
435 +
436 + while (--length > 0)
437 + {
438 +        value += ((SINT64) *ptr++) << shift;
439 +        shift += 8;
440 + }
441 +
442 + value += ((SINT64)(SCHAR) *ptr) << shift;
443 +
444 + return value;
445 + *)
446 +
447 + function TFBClientAPI.DecodeInteger(bufptr: PByte; len: short): int64;
448 + var shift: integer;
449 + begin
450 +  Result := 0;
451 +  if (BufPtr = nil) or (len <= 0) or (len > 8) then
452 +    Exit;
453 +
454 +  shift := 0;
455 +  dec(len);
456 +  while len > 0 do
457 +  begin
458 +    Result := Result + (int64(bufptr^) shl shift);
459 +    Inc(bufptr);
460 +    shift := shift + 8;
461 +    dec(len);
462 +  end;
463 +  Result := Result + (int64(bufptr^) shl shift);
464 + end;
465 +
466 + function TFBClientAPI.Int128ToStr(bufptr: PByte; scale: integer): AnsiString;
467 + begin
468 +  if not HasInt128Support then
469 +    IBError(ibxeNotSupported,[]);
470 + end;
471 +
472 + procedure TFBClientAPI.StrToInt128(scale: integer; aValue: AnsiString; bufptr: PByte);
473 + begin
474 +  if not HasInt128Support then
475 +    IBError(ibxeNotSupported,[]);
476 + end;
477 +
478 + procedure TFBClientAPI.SQLDecFloatEncode(aValue: tBCD; SQLType: cardinal;
479 +  bufptr: PByte);
480 + begin
481 +  if not HasDecFloatSupport then
482 +    IBError(ibxeNotSupported,[]);
483 + end;
484 +
485 + function TFBClientAPI.SQLDecFloatDecode(SQLType: cardinal; bufptr: PByte): tBCD;
486 + begin
487 +  if not HasDecFloatSupport then
488 +    IBError(ibxeNotSupported,[]);
489 + end;
490 +
491   function TFBClientAPI.IsLibraryLoaded: boolean;
492   begin
493 <  Result := IBLibrary <> NilHandle;
493 >  Result := FFBLibrary.IBLibrary <> NilHandle;
494   end;
495  
496 < function TFBClientAPI.GetProcAddr(ProcName: PChar): Pointer;
496 > function TFBClientAPI.GetFBLibrary: IFirebirdLibrary;
497   begin
498 <  Result := GetProcAddress(IBLibrary, ProcName);
365 <  if not Assigned(Result) then
366 <    raise Exception.CreateFmt(SFirebirdAPIFuncNotFound,[ProcName]);
498 >  Result := FFBLibrary;
499   end;
500  
501 < function TFBClientAPI.GetOverrideLibName: string;
501 > function TFBClientAPI.FBTimeStampToDateTime(aDate, aTime: longint): TDateTime;
502   begin
503 <  Result := '';
504 <  if AllowUseOfFBLIB then
505 <    Result := GetEnvironmentVariable('FBLIB');
506 <  if Result = '' then
503 >  {aDate/aTime are in TTimestamp format but aTime is decimilliseconds}
504 >  aDate := aDate - DateDelta;
505 >  if aDate < 0 then
506 >    Result := trunc(aDate) - abs(frac(aTime / (MSecsPerDay*10)))
507 >  else
508 >    Result := trunc(aDate) + abs(frac(aTime / (MSecsPerDay*10)));
509 > end;
510 >
511 > {$IFDEF UNIX}
512 >
513 > procedure TFBClientAPI.GetTZDataSettings;
514 > var S: TStringList;
515 > begin
516 >  FLocalTimeOffset := GetLocalTimeOffset;
517 >  {$if declared(Gettzname)}
518 >  FLocalTimeZoneName := Gettzname(tzdaylight);
519 >  {$else}
520 >  FLocalTimeZoneName := tzname[tzdaylight];
521 >  {$ifend}
522 >  FIsDaylightSavingsTime := tzdaylight;
523 >  if FileExists(DefaultTimeZoneFile) then
524    begin
525 <    if assigned(OnGetLibraryName) then
526 <      OnGetLibraryName(Result)
525 >    S := TStringList.Create;
526 >    try
527 >      S.LoadFromFile(DefaultTimeZoneFile);
528 >      if S.Count > 0 then
529 >        FTZDataTimeZoneID := S[0];
530 >    finally
531 >      S.Free;
532 >    end;
533    end;
534   end;
535 + {$ENDIF}
536  
537 < procedure TFBClientAPI.LoadInterface;
537 > {$IFDEF WINDOWS}
538 > procedure TFBClientAPI.GetTZDataSettings;
539 > var TZInfo: TTimeZoneInformation;
540   begin
541 <  isc_sqlcode := GetProcAddr('isc_sqlcode'); {do not localize}
542 <  isc_sql_interprete := GetProcAddr('isc_sql_interprete'); {do not localize}
543 <  isc_interprete := GetProcAddr('isc_interprete'); {do not localize}
544 <  isc_event_counts := GetProcAddr('isc_event_counts'); {do not localize}
545 <  isc_event_block := GetProcAddr('isc_event_block'); {do not localize}
546 <  isc_free := GetProcAddr('isc_free'); {do not localize}
541 >  FIsDaylightSavingsTime := false;
542 >  {is there any way of working out the default TZData DB time zone ID under Windows?}
543 >  case GetTimeZoneInformation(TZInfo) of
544 >    TIME_ZONE_ID_UNKNOWN:
545 >      begin
546 >        FLocalTimeZoneName := '';
547 >        FLocalTimeOffset := 0;
548 >      end;
549 >    TIME_ZONE_ID_STANDARD:
550 >      begin
551 >        FLocalTimeZoneName := strpas(PWideChar(@TZInfo.StandardName));
552 >        FLocalTimeOffset := TZInfo.Bias;
553 >      end;
554 >    TIME_ZONE_ID_DAYLIGHT:
555 >      begin
556 >        FLocalTimeZoneName := strpas(PWideChar(@TZInfo.DaylightName));
557 >        FLocalTimeOffset := TZInfo.DayLightBias;
558 >        FIsDaylightSavingsTime := true;
559 >      end;
560 >  end;
561   end;
562 + {$ENDIF}
563  
564 < function TFBClientAPI.GetLibraryName: string;
564 > function TFBClientAPI.GetProcAddr(ProcName: PAnsiChar): Pointer;
565   begin
566 <  Result := FFBLibraryName;
566 >  Result := nil;
567 >  if assigned(FFBLibrary) and (FFBLibrary.IBLibrary <> NilHandle) then
568 >    Result := GetProcAddress(FFBLibrary.IBLibrary, ProcName);
569 >  if not Assigned(Result) then
570 >    raise Exception.CreateFmt(SFirebirdAPIFuncNotFound,[ProcName]);
571   end;
572  
573 < function TFBClientAPI.GetCharsetName(CharSetID: integer): string;
573 > function TFBClientAPI.HasDecFloatSupport: boolean;
574   begin
575 <  Result := '';
399 <  if (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap)) and
400 <                                  (CharSetMap[CharSetID].CharSetID = CharSetID) then
401 <    begin
402 <      Result := CharSetMap[CharSetID].CharSetName;
403 <      Exit;
404 <    end;
575 >  Result := GetClientMajor >= 4;
576   end;
577  
578 < function TFBClientAPI.CharSetID2CodePage(CharSetID: integer;
408 <  var CodePage: TSystemCodePage): boolean;
578 > function TFBClientAPI.HasInt128Support: boolean;
579   begin
580 <  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
411 <               and (CharSetMap[CharSetID].CharSetID = CharSetID);
412 <  if Result then
413 <    begin
414 <      CodePage := CharSetMap[CharSetID].CodePage;
415 <      Result := true;
416 <      Exit;
417 <    end;
580 >  Result := false;
581   end;
582  
583 < function TFBClientAPI.CodePage2CharSetID(CodePage: TSystemCodePage;
421 <  var CharSetID: integer): boolean;
422 < var i: integer;
583 > function TFBClientAPI.HasLocalTZDB: boolean;
584   begin
585    Result := false;
425  for i := Low(CharSetMap) to High(CharSetMap) do
426    if CharSetMap[i].CodePage = CodePage then
427    begin
428      CharSetID := CharSetMap[i].CharSetID;
429      Result := true;
430      Exit;
431    end;
586   end;
587  
588 < function TFBClientAPI.CharSetName2CharSetID(CharSetName: string;
435 <  var CharSetID: integer): boolean;
436 < var i: integer;
588 > function TFBClientAPI.HasExtendedTZSupport: boolean;
589   begin
590    Result := false;
439  for i := Low(CharSetMap) to High(CharSetMap) do
440    if CompareStr(CharSetMap[i].CharSetName, CharSetName) = 0 then
441    begin
442      CharSetID := CharSetMap[i].CharSetID;
443      Result := true;
444      Exit;
445    end;
591   end;
592  
593 < function TFBClientAPI.CharSetWidth(CharSetID: integer; var Width: integer
449 <  ): boolean;
593 > function TFBClientAPI.HasTimeZoneSupport: boolean;
594   begin
595 <  Result := (CharSetID >= Low(CharSetMap)) and (CharSetID <= High(CharSetMap))
452 <               and (CharSetMap[CharSetID].CharSetID = CharSetID);
453 <  if Result then
454 <    begin
455 <      Width := CharSetMap[CharSetID].CharSetWidth;
456 <      Result := true;
457 <      Exit;
458 <    end;
595 >  Result := false;
596   end;
597  
598 < const
599 <  IBLocalBufferLength = 512;
600 <  IBBigLocalBufferLength = IBLocalBufferLength * 2;
601 <  IBHugeLocalBufferLength = IBBigLocalBufferLength * 20;
598 > function TFBClientAPI.GetImplementationVersion: AnsiString;
599 > begin
600 >  Result := Format('%d.%d',[GetClientMajor,GetClientMinor]);
601 > end;
602 >
603 > function TFBClientAPI.LoadInterface: boolean;
604 > begin
605 >  isc_sqlcode := GetProcAddr('isc_sqlcode'); {do not localize}
606 >  isc_sql_interprete := GetProcAddr('isc_sql_interprete'); {do not localize}
607 >  fb_shutdown := GetProcAddr('fb_shutdown'); {do not localize}
608 >  Result := true; {don't case if these fail to load}
609 > end;
610 >
611 > procedure TFBClientAPI.FBShutdown;
612 > begin
613 >  if assigned(fb_shutdown) then
614 >    fb_shutdown(0,fb_shutrsn_exit_called);
615 > end;
616  
617   { TFBStatus }
618  
619 < constructor TFBStatus.Create(aOwner: TFBClientAPI);
619 > function TFBStatus.SQLCodeSupported: boolean;
620 > begin
621 >  Result:= (FOwner <> nil) and assigned(FOwner.isc_sqlcode) and  assigned(FOwner.isc_sql_interprete);
622 > end;
623 >
624 > function TFBStatus.GetSQLMessage: Ansistring;
625 > var local_buffer: array[0..IBHugeLocalBufferLength - 1] of AnsiChar;
626 > begin
627 >  Result := '';
628 >  if (FOwner <> nil) and assigned(FOwner.isc_sql_interprete) then
629 >  begin
630 >     FOwner.isc_sql_interprete(Getsqlcode, local_buffer, sizeof(local_buffer));
631 >     Result := strpas(local_buffer);
632 >  end;
633 > end;
634 >
635 > constructor TFBStatus.Create(aOwner: TFBClientAPI; prefix: AnsiString);
636   begin
637    inherited Create;
638    FOwner := aOwner;
639 <  FIBDataBaseErrorMessages := [ShowSQLMessage, ShowIBMessage];
639 >  FPrefix := prefix;
640 >  FIBDataBaseErrorMessages := [ShowIBMessage];
641 > end;
642 >
643 > procedure TFBStatus.Assign(src: TFBStatus);
644 > begin
645 >  FOwner := src.FOwner;
646 >  FPrefix := src.FPrefix;
647 >  SetIBDataBaseErrorMessages(src.GetIBDataBaseErrorMessages);
648   end;
649  
650 < function TFBStatus.GetIBErrorCode: Long;
650 > function TFBStatus.GetIBErrorCode: TStatusCode;
651   begin
652    Result := StatusVector^[1];
653   end;
654  
655 < function TFBStatus.Getsqlcode: Long;
655 > function TFBStatus.Getsqlcode: TStatusCode;
656   begin
657 <  with FOwner do
658 <    Result := isc_sqlcode(PISC_STATUS(StatusVector));
657 >  if (FOwner <> nil) and assigned(FOwner.isc_sqlcode) then
658 >    Result := FOwner.isc_sqlcode(PISC_STATUS(StatusVector))
659 >  else
660 >    Result := -999; {generic SQL Code}
661   end;
662  
663 < function TFBStatus.GetMessage: string;
664 < var local_buffer: array[0..IBHugeLocalBufferLength - 1] of char;
488 <    IBDataBaseErrorMessages: TIBDataBaseErrorMessages;
489 <    sqlcode: Long;
490 <    psb: PStatusVector;
663 > function TFBStatus.GetMessage: AnsiString;
664 > var IBDataBaseErrorMessages: TIBDataBaseErrorMessages;
665   begin
666 <  Result := '';
666 >  Result := FPrefix;
667    IBDataBaseErrorMessages := FIBDataBaseErrorMessages;
668 <  sqlcode := Getsqlcode;
669 <  if (ShowSQLCode in IBDataBaseErrorMessages) then
670 <    Result := Result + 'SQLCODE: ' + IntToStr(sqlcode); {do not localize}
671 <
672 <  Exclude(IBDataBaseErrorMessages, ShowSQLMessage);
673 <  if (ShowSQLMessage in IBDataBaseErrorMessages) then
674 <  begin
675 <    with FOwner do
676 <      isc_sql_interprete(sqlcode, local_buffer, IBLocalBufferLength);
677 <    if (ShowSQLCode in FIBDataBaseErrorMessages) then
678 <      Result := Result + CRLF;
505 <    Result := Result + strpas(local_buffer);
668 >  if SQLCodeSupported then
669 >  begin
670 >    if (ShowSQLCode in IBDataBaseErrorMessages) then
671 >      Result := Result + 'SQLCODE: ' + IntToStr(Getsqlcode); {do not localize}
672 >
673 >    if (ShowSQLMessage in IBDataBaseErrorMessages) then
674 >    begin
675 >      if ShowSQLCode in IBDataBaseErrorMessages then
676 >        Result := Result + LineEnding;
677 >      Result := Result + GetSQLMessage;
678 >    end;
679    end;
680  
681    if (ShowIBMessage in IBDataBaseErrorMessages) then
682    begin
683 <    if (ShowSQLCode in IBDataBaseErrorMessages) or
684 <       (ShowSQLMessage in IBDataBaseErrorMessages) then
685 <      Result := Result + CRLF;
513 <    psb := StatusVector;
514 <    with FOwner do
515 <    while (isc_interprete(@local_buffer, @psb) > 0) do
516 <    begin
517 <      if (Result <> '') and (Result[Length(Result)] <> LF) then
518 <        Result := Result + CRLF;
519 <      Result := Result + strpas(local_buffer);
520 <    end;
683 >    if Result <> FPrefix then
684 >      Result := Result + LineEnding;
685 >    Result := Result + 'Engine Code: ' + IntToStr(GetIBErrorCode) + LineEnding + GetIBMessage;
686    end;
687    if (Result <> '') and (Result[Length(Result)] = '.') then
688      Delete(Result, Length(Result), 1);
# Line 530 | Line 695 | var
695    i: Integer;
696    procedure NextP(i: Integer);
697    begin
698 <    p := PISC_STATUS(PChar(p) + (i * SizeOf(ISC_STATUS)));
698 >    p := PISC_STATUS(PAnsiChar(p) + (i * SizeOf(ISC_STATUS)));
699    end;
700   begin
701    p := PISC_STATUS(StatusVector);
# Line 556 | Line 721 | end;
721  
722   function TFBStatus.GetIBDataBaseErrorMessages: TIBDataBaseErrorMessages;
723   begin
724 <  EnterCriticalSection(FIBCS);
724 >  EnterCriticalSection(TFBClientAPI.FIBCS);
725    try
726      result := FIBDataBaseErrorMessages;
727    finally
728 <    LeaveCriticalSection(FIBCS);
728 >    LeaveCriticalSection(TFBClientAPI.FIBCS);
729    end;
730   end;
731  
732   procedure TFBStatus.SetIBDataBaseErrorMessages(Value: TIBDataBaseErrorMessages);
733   begin
734 <  EnterCriticalSection(FIBCS);
734 >  EnterCriticalSection(TFBClientAPI.FIBCS);
735    try
736      FIBDataBaseErrorMessages := Value;
737    finally
738 <    LeaveCriticalSection(FIBCS);
738 >    LeaveCriticalSection(TFBClientAPI.FIBCS);
739    end;
740   end;
741 +
742   initialization
743 <  TFBClientAPI.IBLibrary := NilHandle;
744 <  InitCriticalSection(TFBStatus.FIBCS);
743 >  TFBLibrary.FEnvSetupDone := false;
744 >  {$IFNDEF FPC}
745 >  InitializeCriticalSection(TFBClientAPI.FIBCS);
746 >  {$ELSE}
747 >  InitCriticalSection(TFBClientAPI.FIBCS);
748 >  {$ENDIF}
749  
750   finalization
751 <  DoneCriticalSection(TFBStatus.FIBCS);
752 <  if TFBClientAPI.IBLibrary <> NilHandle then
753 <  begin
754 <    FreeLibrary(TFBClientAPI.IBLibrary);
755 <    TFBClientAPI.IBLibrary := NilHandle;
756 <    TFBClientAPI.FFBLibraryName := '';
587 <  end;
588 <
751 >  TFBLibrary.FreeLibraries;
752 >  {$IFNDEF FPC}
753 >  DeleteCriticalSection(TFBClientAPI.FIBCS);
754 >  {$ELSE}
755 >  DoneCriticalSection(TFBClientAPI.FIBCS);
756 >  {$ENDIF}
757   end.
758  

Comparing:
ibx/trunk/fbintf/client/FBClientAPI.pas (property svn:eol-style), Revision 47 by tony, Mon Jan 9 15:31:51 2017 UTC vs.
ibx/branches/udr/client/FBClientAPI.pas (property svn:eol-style), Revision 387 by tony, Wed Jan 19 13:34:42 2022 UTC

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines