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 385 by tony, Mon Jan 17 15:56:35 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 94 | Line 117 | type
117  
118    TFBStatus = class(TFBInterfacedObject)
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  
131      {IStatus}
132 <    function GetIBErrorCode: Long;
133 <    function Getsqlcode: Long;
134 <    function GetMessage: string;
132 >    function GetIBErrorCode: TStatusCode;
133 >    function Getsqlcode: TStatusCode;
134 >    function GetMessage: AnsiString;
135      function CheckStatusVector(ErrorCodes: array of TFBStatusCode): Boolean;
136      function GetIBDataBaseErrorMessages: TIBDataBaseErrorMessages;
137      procedure SetIBDataBaseErrorMessages(Value: TIBDataBaseErrorMessages);
138    end;
139  
140 +  { TFBLibrary }
141 +
142 +  TFBLibrary = class(TFBInterfacedObject,IFirebirdLibrary)
143 +  private
144 +    class var FEnvSetupDone: boolean;
145 +    class var FLibraryList: array of IFirebirdLibrary;
146 +  private
147 +    FFirebirdAPI: IFirebirdAPI;
148 +    FRequestedLibName: string;
149 +    function LoadIBLibrary: boolean;
150 +  protected
151 +    FFBLibraryName: string;
152 +    FIBLibrary: TLibHandle;
153 +    procedure FreeFBLibrary;
154 +    function GetOverrideLibName: string;
155 +    class procedure SetupEnvironment;
156 +  protected
157 +    function GetFirebird3API: IFirebirdAPI; virtual; abstract;
158 +    function GetLegacyFirebirdAPI: IFirebirdAPI; virtual; abstract;
159 +  public
160 +    constructor Create(aLibPathName: string='');
161 +    destructor Destroy; override;
162 +    class function GetFBLibrary(aLibPathName: string): IFirebirdLibrary;
163 +    class procedure FreeLibraries;
164 +    function SameLibrary(aLibName: string): boolean;
165 +
166 +  public
167 +    {IFirebirdLibrary}
168 +    function GetHandle: TLibHandle;
169 +    function GetLibraryName: string;
170 +    function GetLibraryFilePath: string;
171 +    function GetFirebirdAPI: IFirebirdAPI;
172 +    property IBLibrary: TLibHandle read FIBLibrary;
173 +  end;
174 +
175    { TFBClientAPI }
176  
177    TFBClientAPI = class(TFBInterfacedObject)
178    private
179 <    FOwnsIBLibrary: boolean;
180 <    procedure LoadIBLibrary;
179 >    FLocalTimeZoneName: AnsiString; {Informal Time Zone Name from tzname e.g. GMT or BST}
180 >    FTZDataTimeZoneID: AnsiString; {TZData DB ID e.g. Europe/London}
181 >    FLocalTimeOffset: integer;
182 >    FIsDaylightSavingsTime: boolean;
183 >    class var FIBCS: TRTLCriticalSection;
184 >    function FBTimeStampToDateTime(aDate, aTime: longint): TDateTime;
185 >    procedure GetTZDataSettings;
186    protected
187 <    FFBLibraryName: string; static;
188 <    FFBLibraryPath: string; static;
189 <    IBLibrary: TLibHandle; static;
190 <    function GetProcAddr(ProcName: PChar): Pointer;
191 <    function GetOverrideLibName: string;
192 <    {$IFDEF UNIX}
193 <    function GetFirebirdLibList: string; virtual; abstract;
194 <    {$ENDIF}
195 <    procedure LoadInterface; virtual;
187 >    FFBLibrary: TFBLibrary;
188 >    function GetProcAddr(ProcName: PAnsiChar): Pointer;
189 >
190 >  protected type
191 >    Tfb_shutdown = function (timeout: uint;
192 >                                 const reason: int): int;
193 >                   {$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
194 >  protected
195 >    {FB Shutdown API}
196 >    fb_shutdown: Tfb_shutdown;
197 >
198    public
199      {Taken from legacy API}
132    isc_sqlcode: Tisc_sqlcode;
200      isc_sql_interprete: Tisc_sql_interprete;
201 <    isc_interprete: Tisc_interprete;
135 <    isc_event_counts: Tisc_event_counts;
136 <    isc_event_block: Tisc_event_block;
137 <    isc_free: Tisc_free;
201 >    isc_sqlcode: Tisc_sqlcode;
202  
203 <    constructor Create;
140 <    destructor Destroy; override;
203 >    constructor Create(aFBLibrary: TFBLibrary);
204      procedure IBAlloc(var P; OldSize, NewSize: Integer);
205      procedure IBDataBaseError;
206 <    procedure SetupEnvironment;
206 >    function LoadInterface: boolean; virtual;
207 >    procedure FBShutdown; virtual;
208 >    function GetAPI: IFirebirdAPI; virtual; abstract;
209 >    {$IFDEF UNIX}
210 >    function GetFirebirdLibList: string; virtual; abstract;
211 >    {$ENDIF}
212 >    function HasDecFloatSupport: boolean;
213 >    function HasInt128Support: boolean; virtual;
214 >    function HasLocalTZDB: boolean; virtual;
215 >    function HasExtendedTZSupport: boolean; virtual;
216 >    function HasTimeZoneSupport: boolean; virtual;
217  
218 +  public
219 +    property LocalTimeZoneName: AnsiString read FLocalTimeZoneName;
220 +    property TZDataTimeZoneID: AnsiString read FTZDataTimeZoneID;
221 +    property LocalTimeOffset: integer read FLocalTimeOffset;
222 +  public
223      {Encode/Decode}
224 <    procedure EncodeInteger(aValue: integer; len: integer; buffer: PChar);
225 <    function DecodeInteger(bufptr: PChar; len: short): integer; virtual; abstract;
226 <    procedure SQLEncodeDate(aDate: TDateTime; bufptr: PChar); virtual; abstract;
227 <    function SQLDecodeDate(byfptr: PChar): TDateTime; virtual; abstract;
228 <    procedure SQLEncodeTime(aTime: TDateTime; bufptr: PChar); virtual; abstract;
229 <    function SQLDecodeTime(bufptr: PChar): TDateTime;  virtual; abstract;
230 <    procedure SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PChar); virtual; abstract;
231 <    function SQLDecodeDateTime(bufptr: PChar): TDateTime; virtual; abstract;
232 <
224 >    procedure EncodeInteger(aValue: int64; len: integer; buffer: PByte);
225 >    function DecodeInteger(bufptr: PByte; len: short): int64;
226 >    procedure SQLEncodeDate(aDate: TDateTime; bufptr: PByte);  virtual; abstract;
227 >    function SQLDecodeDate(byfptr: PByte): TDateTime;  virtual; abstract;
228 >    procedure SQLEncodeTime(aTime: TDateTime; bufptr: PByte);  virtual; abstract;
229 >    function SQLDecodeTime(bufptr: PByte): TDateTime;  virtual; abstract;
230 >    procedure SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PByte); virtual; abstract;
231 >    function  SQLDecodeDateTime(bufptr: PByte): TDateTime; virtual; abstract;
232 >    function Int128ToStr(bufptr: PByte; scale: integer): AnsiString; virtual;
233 >    procedure StrToInt128(scale: integer; aValue: AnsiString; bufptr: PByte);
234 >      virtual;
235 >    procedure SQLDecFloatEncode(aValue: tBCD; SQLType: cardinal; bufptr: PByte); virtual;
236 >    function SQLDecFloatDecode(SQLType: cardinal;  bufptr: PByte): tBCD; virtual;
237  
238      {IFirebirdAPI}
239      function GetStatus: IStatus; virtual; abstract;
240      function IsLibraryLoaded: boolean;
241      function IsEmbeddedServer: boolean; virtual; abstract;
242 <    function GetLibraryName: string;
243 <    function GetCharsetName(CharSetID: integer): string;
244 <    function CharSetID2CodePage(CharSetID: integer; var CodePage: TSystemCodePage): boolean;
245 <    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;
242 >    function GetFBLibrary: IFirebirdLibrary;
243 >    function GetImplementationVersion: AnsiString;
244 >    function GetClientMajor: integer;  virtual; abstract;
245 >    function GetClientMinor: integer;  virtual; abstract;
246    end;
247  
248 < const FirebirdClientAPI: TFBClientAPI = nil;
248 >    IJournallingHook = interface
249 >      ['{7d3e45e0-3628-416a-9e22-c20474825031}']
250 >      procedure TransactionStart(Tr: ITransaction);
251 >      function TransactionEnd(TransactionID: integer; Action: TTransactionAction): boolean;
252 >      procedure TransactionRetained(Tr: ITransaction; OldTransactionID: integer; Action: TTransactionAction);
253 >      procedure ExecQuery(Stmt: IStatement);
254 >    end;
255  
256   implementation
257  
258 < uses IBUtils, {$IFDEF Unix} initc, {$ENDIF}
258 > uses IBUtils, Registry,
259 >  {$IFDEF Unix} unix, initc, dl, {$ENDIF}
260 > {$IFDEF FPC}
261   {$IFDEF WINDOWS }
262 < Windows,Registry, WinDirs,
262 > WinDirs,
263 > {$ENDIF}
264 > {$ELSE}
265 > ShlObj,
266   {$ENDIF}
267   SysUtils;
268  
269   {$IFDEF UNIX}
270 < {$I uloadlibrary.inc}
270 > {$I 'include/uloadlibrary.inc'}
271   {$ELSE}
272 < {$I wloadlibrary.inc}
272 > {$I 'include/wloadlibrary.inc'}
273   {$ENDIF}
274  
275 < type
276 <  TCharsetMap = record
277 <    CharsetID: integer;
278 <    CharSetName: string;
279 <    CharSetWidth: integer;
280 <    CodePage: TSystemCodePage;
275 >
276 > { TFBLibrary }
277 >
278 > function TFBLibrary.GetOverrideLibName: string;
279 > begin
280 >  Result := FFBLibraryName;
281 >  if (Result = '') and AllowUseOfFBLIB then
282 >    Result := GetEnvironmentVariable('FBLIB');
283 >  if Result = '' then
284 >  begin
285 >    if assigned(OnGetLibraryName) then
286 >      OnGetLibraryName(Result)
287    end;
288 + end;
289  
290 < const
291 <  CharSetMap: array [0..69] of TCharsetMap = (
292 <  (CharsetID: 0; CharSetName: 'NONE'; CharSetWidth: 1; CodePage: CP_ACP),
293 <  (CharsetID: 1; CharSetName: 'OCTETS'; CharSetWidth: 1; CodePage: CP_NONE),
294 <  (CharsetID: 2; CharSetName: 'ASCII'; CharSetWidth: 1; CodePage: CP_ASCII),
295 <  (CharsetID: 3; CharSetName: 'UNICODE_FSS'; CharSetWidth: 3; CodePage: CP_UTF8),
296 <  (CharsetID: 4; CharSetName: 'UTF8'; CharSetWidth: 4; CodePage: CP_UTF8),
297 <  (CharsetID: 5; CharSetName: 'SJIS_0208'; CharSetWidth: 2; CodePage: 20932),
298 <  (CharsetID: 6; CharSetName: 'EUCJ_0208'; CharSetWidth: 2; CodePage: 20932),
299 <  (CharsetID: 7; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
300 <  (CharsetID: 8; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
301 <  (CharsetID: 9; CharSetName: 'DOS737'; CharSetWidth: 1; CodePage: 737),
302 <  (CharsetID: 10; CharSetName: 'DOS437'; CharSetWidth: 1; CodePage: 437),
303 <  (CharsetID: 11; CharSetName: 'DOS850'; CharSetWidth: 1; CodePage: 850),
304 <  (CharsetID: 12; CharSetName: 'DOS865'; CharSetWidth: 1; CodePage: 865),
305 <  (CharsetID: 13; CharSetName: 'DOS860'; CharSetWidth: 1; CodePage: 860),
306 <  (CharsetID: 14; CharSetName: 'DOS863'; CharSetWidth: 1; CodePage: 863),
307 <  (CharsetID: 15; CharSetName: 'DOS775'; CharSetWidth: 1; CodePage: 775),
308 <  (CharsetID: 16; CharSetName: 'DOS858'; CharSetWidth: 1; CodePage: 858),
309 <  (CharsetID: 17; CharSetName: 'DOS862'; CharSetWidth: 1; CodePage: 862),
310 <  (CharsetID: 18; CharSetName: 'DOS864'; CharSetWidth: 1; CodePage: 864),
311 <  (CharsetID: 19; CharSetName: 'NEXT'; CharSetWidth: 1; CodePage: CP_NONE),
312 <  (CharsetID: 20; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
313 <  (CharsetID: 21; CharSetName: 'ISO8859_1'; CharSetWidth: 1; CodePage: 28591),
314 <  (CharsetID: 22; CharSetName: 'ISO8859_2'; CharSetWidth: 1; CodePage: 28592),
315 <  (CharsetID: 23; CharSetName: 'ISO8859_3'; CharSetWidth: 1; CodePage: 28593),
316 <  (CharsetID: 24; CharSetName: 'Unknown'; CharSetWidth: 0; CodePage: CP_NONE),
317 <  (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.
290 > procedure TFBLibrary.FreeFBLibrary;
291 > begin
292 >  (FFirebirdAPI as TFBClientAPI).FBShutdown;
293 >  if FIBLibrary <> NilHandle then
294 >    FreeLibrary(FIBLibrary);
295 >  FIBLibrary := NilHandle;
296 >  FFBLibraryName := '';
297 > end;
298 >
299 > function TFBLibrary.GetLibraryName: string;
300 > begin
301 >  Result := ExtractFileName(FFBLibraryName);
302 > end;
303 >
304 > function TFBLibrary.GetFirebirdAPI: IFirebirdAPI;
305 > begin
306 >  Result := FFirebirdAPI;
307 > end;
308 >
309 > constructor TFBLibrary.Create(aLibPathName: string);
310 > begin
311 >  inherited Create;
312 >  SetupEnvironment;
313 >  FFBLibraryName := aLibPathName;
314 >  FIBLibrary := NilHandle;
315 >  FFirebirdAPI := GetFirebird3API;
316 >  FRequestedLibName := aLibPathName;
317 >  if aLibPathName <> '' then
318    begin
319 <    result:=false; //assume failure
320 <    if value = '' then
321 <    begin
322 <      // Assume user wants to remove variable.
323 <      if unsetenv(name)=0 then result:=true;
324 <    end
325 <    else
319 >    SetLength(FLibraryList,Length(FLibraryList)+1);
320 >    FLibraryList[Length(FLibraryList)-1] := self;
321 >  end;
322 >  if FFirebirdAPI <> nil then
323 >  begin
324 >    {First try Firebird 3}
325 >    if not LoadIBLibrary or not (FFirebirdAPI as TFBClientAPI).LoadInterface then
326 >      FFirebirdAPI := nil;
327 >  end;
328 >
329 >  if FFirebirdAPI = nil then
330 >  begin
331 >    {now try Firebird 2.5. Under Unix we need to reload the library in case we
332 >     are to use the embedded library}
333 >    FFirebirdAPI := GetLegacyFirebirdAPI;
334 >    if FFirebirdAPI <> nil then
335      begin
336 <      // Non empty so set the variable
337 <      if setenv(name, value, 1)=0 then result:=true;
336 >      {$IFDEF UNIX}
337 >      FreeFBLibrary;
338 >      {$ENDIF}
339 >      if not LoadIBLibrary or not (FFirebirdAPI as TFBClientAPI).LoadInterface then
340 >        FFirebirdAPI := nil;
341      end;
342    end;
343 <  {$ENDIF}
343 >  {Note: FFirebirdAPI will be set to nil if the Firebird API fails to load}
344 > end;
345  
346 < { TFBClientAPI }
346 > destructor TFBLibrary.Destroy;
347 > begin
348 >  FreeFBLibrary;
349 >  FFirebirdAPI := nil;
350 >  inherited Destroy;
351 > end;
352  
353 < constructor TFBClientAPI.Create;
353 > class function TFBLibrary.GetFBLibrary(aLibPathName: string): IFirebirdLibrary;
354 > var i: integer;
355   begin
356 <  inherited Create;
357 <  LoadIBLibrary;
293 <  if (IBLibrary <> NilHandle) then
356 >  Result := nil;
357 >  if aLibPathName <> '' then
358    begin
359 <    SetupEnvironment;
360 <    LoadInterface;
359 >    for i := 0 to Length(FLibraryList) - 1 do
360 >    begin
361 >      if (FLibraryList[i] as TFBLibrary).SameLibrary(aLibPathName) then
362 >      begin
363 >        Result := FLibraryList[i];
364 >        Exit;
365 >      end;
366 >    end;
367 >    Result := Create(aLibPathName);
368    end;
369 <  FirebirdClientAPI := self;
369 >
370   end;
371  
372 < destructor TFBClientAPI.Destroy;
372 > class procedure TFBLibrary.FreeLibraries;
373 > var i: integer;
374   begin
375 <  FirebirdClientAPI := nil;
376 <  if FOwnsIBLibrary and (IBLibrary <> NilHandle) then
377 <    UnloadLibrary(IBLibrary);
378 <  IBLibrary := NilHandle;
379 <  inherited Destroy;
375 >  for i := 0 to Length(FLibraryList) - 1 do
376 >    FLibraryList[i] := nil;
377 >  SetLength(FLibraryList,0);
378 > end;
379 >
380 > function TFBLibrary.SameLibrary(aLibName: string): boolean;
381 > begin
382 >  Result := FRequestedLibName = aLibName;
383 > end;
384 >
385 > function TFBLibrary.GetHandle: TLibHandle;
386 > begin
387 >  Result := FIBLibrary;
388 > end;
389 >
390 > { TFBClientAPI }
391 >
392 > constructor TFBClientAPI.Create(aFBLibrary: TFBLibrary);
393 > begin
394 >  inherited Create;
395 >  FFBLibrary := aFBLibrary;
396 >  GetTZDataSettings;
397   end;
398  
399   procedure TFBClientAPI.IBAlloc(var P; OldSize, NewSize: Integer);
# Line 312 | Line 401 | var
401    i: Integer;
402   begin
403    ReallocMem(Pointer(P), NewSize);
404 <  for i := OldSize to NewSize - 1 do PChar(P)[i] := #0;
404 >  for i := OldSize to NewSize - 1 do PAnsiChar(P)[i] := #0;
405   end;
406  
407   procedure TFBClientAPI.IBDataBaseError;
# Line 320 | Line 409 | begin
409    raise EIBInterBaseError.Create(GetStatus);
410   end;
411  
412 < {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);
412 > procedure TFBClientAPI.EncodeInteger(aValue: int64; len: integer; buffer: PByte);
413   begin
414    while len > 0 do
415    begin
416 <    buffer^ := char(aValue and $FF);
416 >    buffer^ := aValue and $FF;
417      Inc(buffer);
418      Dec(len);
419      aValue := aValue shr 8;
420    end;
421   end;
422  
423 + (*
424 +  DecodeInteger is Translated from
425 +
426 + SINT64 API_ROUTINE isc_portable_integer(const UCHAR* ptr, SSHORT length)
427 + if (!ptr || length <= 0 || length > 8)
428 +        return 0;
429 +
430 + SINT64 value = 0;
431 + int shift = 0;
432 +
433 + while (--length > 0)
434 + {
435 +        value += ((SINT64) *ptr++) << shift;
436 +        shift += 8;
437 + }
438 +
439 + value += ((SINT64)(SCHAR) *ptr) << shift;
440 +
441 + return value;
442 + *)
443 +
444 + function TFBClientAPI.DecodeInteger(bufptr: PByte; len: short): int64;
445 + var shift: integer;
446 + begin
447 +  Result := 0;
448 +  if (BufPtr = nil) or (len <= 0) or (len > 8) then
449 +    Exit;
450 +
451 +  shift := 0;
452 +  dec(len);
453 +  while len > 0 do
454 +  begin
455 +    Result := Result + (int64(bufptr^) shl shift);
456 +    Inc(bufptr);
457 +    shift := shift + 8;
458 +    dec(len);
459 +  end;
460 +  Result := Result + (int64(bufptr^) shl shift);
461 + end;
462 +
463 + function TFBClientAPI.Int128ToStr(bufptr: PByte; scale: integer): AnsiString;
464 + begin
465 +  if not HasInt128Support then
466 +    IBError(ibxeNotSupported,[]);
467 + end;
468 +
469 + procedure TFBClientAPI.StrToInt128(scale: integer; aValue: AnsiString; bufptr: PByte);
470 + begin
471 +  if not HasInt128Support then
472 +    IBError(ibxeNotSupported,[]);
473 + end;
474 +
475 + procedure TFBClientAPI.SQLDecFloatEncode(aValue: tBCD; SQLType: cardinal;
476 +  bufptr: PByte);
477 + begin
478 +  if not HasDecFloatSupport then
479 +    IBError(ibxeNotSupported,[]);
480 + end;
481 +
482 + function TFBClientAPI.SQLDecFloatDecode(SQLType: cardinal; bufptr: PByte): tBCD;
483 + begin
484 +  if not HasDecFloatSupport then
485 +    IBError(ibxeNotSupported,[]);
486 + end;
487 +
488   function TFBClientAPI.IsLibraryLoaded: boolean;
489   begin
490 <  Result := IBLibrary <> NilHandle;
490 >  Result := FFBLibrary.IBLibrary <> NilHandle;
491   end;
492  
493 < function TFBClientAPI.GetProcAddr(ProcName: PChar): Pointer;
493 > function TFBClientAPI.GetFBLibrary: IFirebirdLibrary;
494   begin
495 <  Result := GetProcAddress(IBLibrary, ProcName);
365 <  if not Assigned(Result) then
366 <    raise Exception.CreateFmt(SFirebirdAPIFuncNotFound,[ProcName]);
495 >  Result := FFBLibrary;
496   end;
497  
498 < function TFBClientAPI.GetOverrideLibName: string;
498 > function TFBClientAPI.FBTimeStampToDateTime(aDate, aTime: longint): TDateTime;
499   begin
500 <  Result := '';
501 <  if AllowUseOfFBLIB then
502 <    Result := GetEnvironmentVariable('FBLIB');
503 <  if Result = '' then
500 >  {aDate/aTime are in TTimestamp format but aTime is decimilliseconds}
501 >  aDate := aDate - DateDelta;
502 >  if aDate < 0 then
503 >    Result := trunc(aDate) - abs(frac(aTime / (MSecsPerDay*10)))
504 >  else
505 >    Result := trunc(aDate) + abs(frac(aTime / (MSecsPerDay*10)));
506 > end;
507 >
508 > {$IFDEF UNIX}
509 >
510 > procedure TFBClientAPI.GetTZDataSettings;
511 > var S: TStringList;
512 > begin
513 >  FLocalTimeOffset := GetLocalTimeOffset;
514 >  {$if declared(Gettzname)}
515 >  FLocalTimeZoneName := Gettzname(tzdaylight);
516 >  {$else}
517 >  FLocalTimeZoneName := tzname[tzdaylight];
518 >  {$ifend}
519 >  FIsDaylightSavingsTime := tzdaylight;
520 >  if FileExists(DefaultTimeZoneFile) then
521    begin
522 <    if assigned(OnGetLibraryName) then
523 <      OnGetLibraryName(Result)
522 >    S := TStringList.Create;
523 >    try
524 >      S.LoadFromFile(DefaultTimeZoneFile);
525 >      if S.Count > 0 then
526 >        FTZDataTimeZoneID := S[0];
527 >    finally
528 >      S.Free;
529 >    end;
530    end;
531   end;
532 + {$ENDIF}
533  
534 < procedure TFBClientAPI.LoadInterface;
534 > {$IFDEF WINDOWS}
535 > procedure TFBClientAPI.GetTZDataSettings;
536 > var TZInfo: TTimeZoneInformation;
537   begin
538 <  isc_sqlcode := GetProcAddr('isc_sqlcode'); {do not localize}
539 <  isc_sql_interprete := GetProcAddr('isc_sql_interprete'); {do not localize}
540 <  isc_interprete := GetProcAddr('isc_interprete'); {do not localize}
541 <  isc_event_counts := GetProcAddr('isc_event_counts'); {do not localize}
542 <  isc_event_block := GetProcAddr('isc_event_block'); {do not localize}
543 <  isc_free := GetProcAddr('isc_free'); {do not localize}
538 >  FIsDaylightSavingsTime := false;
539 >  {is there any way of working out the default TZData DB time zone ID under Windows?}
540 >  case GetTimeZoneInformation(TZInfo) of
541 >    TIME_ZONE_ID_UNKNOWN:
542 >      begin
543 >        FLocalTimeZoneName := '';
544 >        FLocalTimeOffset := 0;
545 >      end;
546 >    TIME_ZONE_ID_STANDARD:
547 >      begin
548 >        FLocalTimeZoneName := strpas(PWideChar(@TZInfo.StandardName));
549 >        FLocalTimeOffset := TZInfo.Bias;
550 >      end;
551 >    TIME_ZONE_ID_DAYLIGHT:
552 >      begin
553 >        FLocalTimeZoneName := strpas(PWideChar(@TZInfo.DaylightName));
554 >        FLocalTimeOffset := TZInfo.DayLightBias;
555 >        FIsDaylightSavingsTime := true;
556 >      end;
557 >  end;
558   end;
559 + {$ENDIF}
560  
561 < function TFBClientAPI.GetLibraryName: string;
561 > function TFBClientAPI.GetProcAddr(ProcName: PAnsiChar): Pointer;
562   begin
563 <  Result := FFBLibraryName;
563 >  Result := nil;
564 >  if assigned(FFBLibrary) and (FFBLibrary.IBLibrary <> NilHandle) then
565 >    Result := GetProcAddress(FFBLibrary.IBLibrary, ProcName);
566 >  if not Assigned(Result) then
567 >    raise Exception.CreateFmt(SFirebirdAPIFuncNotFound,[ProcName]);
568   end;
569  
570 < function TFBClientAPI.GetCharsetName(CharSetID: integer): string;
570 > function TFBClientAPI.HasDecFloatSupport: boolean;
571   begin
572 <  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;
572 >  Result := GetClientMajor >= 4;
573   end;
574  
575 < function TFBClientAPI.CharSetID2CodePage(CharSetID: integer;
408 <  var CodePage: TSystemCodePage): boolean;
575 > function TFBClientAPI.HasInt128Support: boolean;
576   begin
577 <  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;
577 >  Result := false;
578   end;
579  
580 < function TFBClientAPI.CodePage2CharSetID(CodePage: TSystemCodePage;
421 <  var CharSetID: integer): boolean;
422 < var i: integer;
580 > function TFBClientAPI.HasLocalTZDB: boolean;
581   begin
582    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;
583   end;
584  
585 < function TFBClientAPI.CharSetName2CharSetID(CharSetName: string;
435 <  var CharSetID: integer): boolean;
436 < var i: integer;
585 > function TFBClientAPI.HasExtendedTZSupport: boolean;
586   begin
587    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;
588   end;
589  
590 < function TFBClientAPI.CharSetWidth(CharSetID: integer; var Width: integer
449 <  ): boolean;
590 > function TFBClientAPI.HasTimeZoneSupport: boolean;
591   begin
592 <  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;
592 >  Result := false;
593   end;
594  
595 < const
596 <  IBLocalBufferLength = 512;
597 <  IBBigLocalBufferLength = IBLocalBufferLength * 2;
598 <  IBHugeLocalBufferLength = IBBigLocalBufferLength * 20;
595 > function TFBClientAPI.GetImplementationVersion: AnsiString;
596 > begin
597 >  Result := Format('%d.%d',[GetClientMajor,GetClientMinor]);
598 > end;
599 >
600 > function TFBClientAPI.LoadInterface: boolean;
601 > begin
602 >  isc_sqlcode := GetProcAddr('isc_sqlcode'); {do not localize}
603 >  isc_sql_interprete := GetProcAddr('isc_sql_interprete'); {do not localize}
604 >  fb_shutdown := GetProcAddr('fb_shutdown'); {do not localize}
605 >  Result := true; {don't case if these fail to load}
606 > end;
607 >
608 > procedure TFBClientAPI.FBShutdown;
609 > begin
610 >  if assigned(fb_shutdown) then
611 >    fb_shutdown(0,fb_shutrsn_exit_called);
612 > end;
613  
614   { TFBStatus }
615  
616 < constructor TFBStatus.Create(aOwner: TFBClientAPI);
616 > function TFBStatus.SQLCodeSupported: boolean;
617 > begin
618 >  Result:= (FOwner <> nil) and assigned(FOwner.isc_sqlcode) and  assigned(FOwner.isc_sql_interprete);
619 > end;
620 >
621 > function TFBStatus.GetSQLMessage: Ansistring;
622 > var local_buffer: array[0..IBHugeLocalBufferLength - 1] of AnsiChar;
623 > begin
624 >  Result := '';
625 >  if (FOwner <> nil) and assigned(FOwner.isc_sql_interprete) then
626 >  begin
627 >     FOwner.isc_sql_interprete(Getsqlcode, local_buffer, sizeof(local_buffer));
628 >     Result := strpas(local_buffer);
629 >  end;
630 > end;
631 >
632 > constructor TFBStatus.Create(aOwner: TFBClientAPI; prefix: AnsiString);
633   begin
634    inherited Create;
635    FOwner := aOwner;
636 <  FIBDataBaseErrorMessages := [ShowSQLMessage, ShowIBMessage];
636 >  FPrefix := prefix;
637 >  FIBDataBaseErrorMessages := [ShowIBMessage];
638   end;
639  
640 < function TFBStatus.GetIBErrorCode: Long;
640 > function TFBStatus.GetIBErrorCode: TStatusCode;
641   begin
642    Result := StatusVector^[1];
643   end;
644  
645 < function TFBStatus.Getsqlcode: Long;
645 > function TFBStatus.Getsqlcode: TStatusCode;
646   begin
647 <  with FOwner do
648 <    Result := isc_sqlcode(PISC_STATUS(StatusVector));
647 >  if (FOwner <> nil) and assigned(FOwner.isc_sqlcode) then
648 >    Result := FOwner.isc_sqlcode(PISC_STATUS(StatusVector))
649 >  else
650 >    Result := -999; {generic SQL Code}
651   end;
652  
653 < function TFBStatus.GetMessage: string;
654 < var local_buffer: array[0..IBHugeLocalBufferLength - 1] of char;
488 <    IBDataBaseErrorMessages: TIBDataBaseErrorMessages;
489 <    sqlcode: Long;
490 <    psb: PStatusVector;
653 > function TFBStatus.GetMessage: AnsiString;
654 > var IBDataBaseErrorMessages: TIBDataBaseErrorMessages;
655   begin
656 <  Result := '';
656 >  Result := FPrefix;
657    IBDataBaseErrorMessages := FIBDataBaseErrorMessages;
658 <  sqlcode := Getsqlcode;
659 <  if (ShowSQLCode in IBDataBaseErrorMessages) then
660 <    Result := Result + 'SQLCODE: ' + IntToStr(sqlcode); {do not localize}
661 <
662 <  Exclude(IBDataBaseErrorMessages, ShowSQLMessage);
663 <  if (ShowSQLMessage in IBDataBaseErrorMessages) then
664 <  begin
665 <    with FOwner do
666 <      isc_sql_interprete(sqlcode, local_buffer, IBLocalBufferLength);
667 <    if (ShowSQLCode in FIBDataBaseErrorMessages) then
668 <      Result := Result + CRLF;
505 <    Result := Result + strpas(local_buffer);
658 >  if SQLCodeSupported then
659 >  begin
660 >    if (ShowSQLCode in IBDataBaseErrorMessages) then
661 >      Result := Result + 'SQLCODE: ' + IntToStr(Getsqlcode); {do not localize}
662 >
663 >    if (ShowSQLMessage in IBDataBaseErrorMessages) then
664 >    begin
665 >      if ShowSQLCode in IBDataBaseErrorMessages then
666 >        Result := Result + LineEnding;
667 >      Result := Result + GetSQLMessage;
668 >    end;
669    end;
670  
671    if (ShowIBMessage in IBDataBaseErrorMessages) then
672    begin
673 <    if (ShowSQLCode in IBDataBaseErrorMessages) or
674 <       (ShowSQLMessage in IBDataBaseErrorMessages) then
675 <      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;
673 >    if Result <> FPrefix then
674 >      Result := Result + LineEnding;
675 >    Result := Result + 'Engine Code: ' + IntToStr(GetIBErrorCode) + LineEnding + GetIBMessage;
676    end;
677    if (Result <> '') and (Result[Length(Result)] = '.') then
678      Delete(Result, Length(Result), 1);
# Line 530 | Line 685 | var
685    i: Integer;
686    procedure NextP(i: Integer);
687    begin
688 <    p := PISC_STATUS(PChar(p) + (i * SizeOf(ISC_STATUS)));
688 >    p := PISC_STATUS(PAnsiChar(p) + (i * SizeOf(ISC_STATUS)));
689    end;
690   begin
691    p := PISC_STATUS(StatusVector);
# Line 556 | Line 711 | end;
711  
712   function TFBStatus.GetIBDataBaseErrorMessages: TIBDataBaseErrorMessages;
713   begin
714 <  EnterCriticalSection(FIBCS);
714 >  EnterCriticalSection(TFBClientAPI.FIBCS);
715    try
716      result := FIBDataBaseErrorMessages;
717    finally
718 <    LeaveCriticalSection(FIBCS);
718 >    LeaveCriticalSection(TFBClientAPI.FIBCS);
719    end;
720   end;
721  
722   procedure TFBStatus.SetIBDataBaseErrorMessages(Value: TIBDataBaseErrorMessages);
723   begin
724 <  EnterCriticalSection(FIBCS);
724 >  EnterCriticalSection(TFBClientAPI.FIBCS);
725    try
726      FIBDataBaseErrorMessages := Value;
727    finally
728 <    LeaveCriticalSection(FIBCS);
728 >    LeaveCriticalSection(TFBClientAPI.FIBCS);
729    end;
730   end;
731 +
732   initialization
733 <  TFBClientAPI.IBLibrary := NilHandle;
734 <  InitCriticalSection(TFBStatus.FIBCS);
733 >  TFBLibrary.FEnvSetupDone := false;
734 >  {$IFNDEF FPC}
735 >  InitializeCriticalSection(TFBClientAPI.FIBCS);
736 >  {$ELSE}
737 >  InitCriticalSection(TFBClientAPI.FIBCS);
738 >  {$ENDIF}
739  
740   finalization
741 <  DoneCriticalSection(TFBStatus.FIBCS);
742 <  if TFBClientAPI.IBLibrary <> NilHandle then
743 <  begin
744 <    FreeLibrary(TFBClientAPI.IBLibrary);
745 <    TFBClientAPI.IBLibrary := NilHandle;
746 <    TFBClientAPI.FFBLibraryName := '';
587 <  end;
588 <
741 >  TFBLibrary.FreeLibraries;
742 >  {$IFNDEF FPC}
743 >  DeleteCriticalSection(TFBClientAPI.FIBCS);
744 >  {$ELSE}
745 >  DoneCriticalSection(TFBClientAPI.FIBCS);
746 >  {$ENDIF}
747   end.
748  

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 385 by tony, Mon Jan 17 15:56:35 2022 UTC

# Line 0 | Line 1
1 + native

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines