1 |
(*
|
2 |
* Firebird Interface (fbintf). The fbintf components provide a set of
|
3 |
* Pascal language bindings for the Firebird API.
|
4 |
*
|
5 |
* The contents of this file are subject to the Initial Developer's
|
6 |
* Public License Version 1.0 (the "License"); you may not use this
|
7 |
* file except in compliance with the License. You may obtain a copy
|
8 |
* of the License here:
|
9 |
*
|
10 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
|
11 |
*
|
12 |
* Software distributed under the License is distributed on an "AS
|
13 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
14 |
* implied. See the License for the specific language governing rights
|
15 |
* and limitations under the License.
|
16 |
*
|
17 |
* The Initial Developer of the Original Code is Tony Whyman.
|
18 |
*
|
19 |
* The Original Code is (C) 2016 Tony Whyman, MWA Software
|
20 |
* (http://www.mwasoftware.co.uk).
|
21 |
*
|
22 |
* All Rights Reserved.
|
23 |
*
|
24 |
* Contributor(s): ______________________________________.
|
25 |
*
|
26 |
*)
|
27 |
unit FB30ClientAPI;
|
28 |
|
29 |
{$IFDEF FPC}
|
30 |
{$mode delphi}
|
31 |
{$interfaces COM}
|
32 |
{$ENDIF}
|
33 |
|
34 |
interface
|
35 |
|
36 |
uses
|
37 |
Classes, SysUtils, FBClientAPI, Firebird, IB, IBExternals;
|
38 |
|
39 |
type
|
40 |
|
41 |
{ TFB30Status }
|
42 |
|
43 |
TFB30Status = class(TFBStatus,IStatus)
|
44 |
private
|
45 |
FStatus: Firebird.IStatus;
|
46 |
public
|
47 |
procedure Init;
|
48 |
function InErrorState: boolean;
|
49 |
function GetStatus: Firebird.IStatus;
|
50 |
function StatusVector: PStatusVector; override;
|
51 |
end;
|
52 |
|
53 |
Tfb_get_master_interface = function: IMaster;
|
54 |
{$IFDEF WINDOWS} stdcall; {$ELSE} cdecl; {$ENDIF}
|
55 |
|
56 |
{ TFB30ClientAPI }
|
57 |
|
58 |
TFB30ClientAPI = class(TFBClientAPI,IFirebirdAPI)
|
59 |
private
|
60 |
FMaster: Firebird.IMaster;
|
61 |
FUtil: Firebird.IUtil;
|
62 |
FProvider: Firebird.IProvider;
|
63 |
FConfigManager: Firebird.IConfigManager;
|
64 |
FStatus: TFB30Status;
|
65 |
FIsEmbeddedServer: boolean;
|
66 |
FStatusIntf: IStatus; {Keep a reference to the interface - automatic destroy
|
67 |
when this class is freed and last reference to IStatus
|
68 |
goes out of scope.}
|
69 |
procedure CheckPlugins;
|
70 |
protected
|
71 |
{$IFDEF UNIX}
|
72 |
function GetFirebirdLibList: string; override;
|
73 |
{$ENDIF}
|
74 |
procedure LoadInterface; override;
|
75 |
public
|
76 |
constructor Create;
|
77 |
destructor Destroy; override;
|
78 |
|
79 |
function StatusIntf: Firebird.IStatus;
|
80 |
procedure Check4DataBaseError;
|
81 |
function InErrorState: boolean;
|
82 |
|
83 |
public
|
84 |
{IFirebirdAPI}
|
85 |
function GetStatus: IStatus; override;
|
86 |
function AllocateDPB: IDPB;
|
87 |
function AllocateTPB: ITPB;
|
88 |
|
89 |
{Database connections}
|
90 |
function OpenDatabase(DatabaseName: string; DPB: IDPB; RaiseExceptionOnConnectError: boolean=true): IAttachment;
|
91 |
function CreateDatabase(DatabaseName: string; DPB: IDPB; RaiseExceptionOnError: boolean=true): IAttachment;
|
92 |
|
93 |
{Start Transaction against multiple databases}
|
94 |
function StartTransaction(Attachments: array of IAttachment;
|
95 |
TPB: array of byte; DefaultCompletion: TTransactionCompletion): ITransaction; overload;
|
96 |
function StartTransaction(Attachments: array of IAttachment;
|
97 |
TPB: ITPB; DefaultCompletion: TTransactionCompletion): ITransaction; overload;
|
98 |
|
99 |
{Service Manager}
|
100 |
function AllocateSPB: ISPB;
|
101 |
function GetServiceManager(ServerName: string; Protocol: TProtocol; SPB: ISPB): IServiceManager;
|
102 |
|
103 |
{Information}
|
104 |
function HasServiceAPI: boolean;
|
105 |
function HasRollbackRetaining: boolean;
|
106 |
function IsEmbeddedServer: boolean; override;
|
107 |
function GetImplementationVersion: string;
|
108 |
|
109 |
{Firebird 3 API}
|
110 |
function HasMasterIntf: boolean;
|
111 |
function GetIMaster: TObject;
|
112 |
|
113 |
{Encode/Decode}
|
114 |
function DecodeInteger(bufptr: PChar; len: short): integer; override;
|
115 |
procedure SQLEncodeDate(aDate: TDateTime; bufptr: PChar); override;
|
116 |
function SQLDecodeDate(bufptr: PChar): TDateTime; override;
|
117 |
procedure SQLEncodeTime(aTime: TDateTime; bufptr: PChar); override;
|
118 |
function SQLDecodeTime(bufptr: PChar): TDateTime; override;
|
119 |
procedure SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PChar); override;
|
120 |
function SQLDecodeDateTime(bufptr: PChar): TDateTime; override;
|
121 |
|
122 |
{Firebird Interfaces}
|
123 |
property MasterIntf: Firebird.IMaster read FMaster;
|
124 |
property UtilIntf: Firebird.IUtil read FUtil;
|
125 |
property ProviderIntf: Firebird.IProvider read FProvider;
|
126 |
end;
|
127 |
|
128 |
var Firebird30ClientAPI: TFB30ClientAPI;
|
129 |
|
130 |
implementation
|
131 |
|
132 |
uses FBParamBlock, FB30Attachment, dynlibs, FBMessages, FB30Services,
|
133 |
FB30Transaction;
|
134 |
|
135 |
type
|
136 |
PISC_DATE = ^ISC_DATE;
|
137 |
PISC_TIME = ^ISC_TIME;
|
138 |
|
139 |
{ TFB30Status }
|
140 |
|
141 |
procedure TFB30Status.Init;
|
142 |
begin
|
143 |
if assigned(FStatus) then
|
144 |
FStatus.Init;
|
145 |
end;
|
146 |
|
147 |
function TFB30Status.InErrorState: boolean;
|
148 |
begin
|
149 |
with GetStatus do
|
150 |
Result := ((getState and STATE_ERRORS) <> 0);
|
151 |
end;
|
152 |
|
153 |
function TFB30Status.GetStatus: Firebird.IStatus;
|
154 |
begin
|
155 |
if FStatus = nil then
|
156 |
with FOwner do
|
157 |
FStatus := (FOwner as TFB30ClientAPI).MasterIntf.GetStatus;
|
158 |
Result := FStatus;
|
159 |
end;
|
160 |
|
161 |
function TFB30Status.StatusVector: PStatusVector;
|
162 |
begin
|
163 |
Result := PStatusVector(GetStatus.getErrors);
|
164 |
end;
|
165 |
|
166 |
{ TFB30ClientAPI }
|
167 |
|
168 |
procedure TFB30ClientAPI.CheckPlugins;
|
169 |
var FBConf: Firebird.IFirebirdConf;
|
170 |
Plugins: string;
|
171 |
PluginsList: TStringList;
|
172 |
begin
|
173 |
FIsEmbeddedServer := false;
|
174 |
FBConf := FConfigManager.getFirebirdConf;
|
175 |
try
|
176 |
Plugins := FBConf.asString(FBConf.getKey('Providers'));
|
177 |
finally
|
178 |
FBConf.release;
|
179 |
end;
|
180 |
if Plugins = '' then Exit;
|
181 |
|
182 |
PluginsList := TStringList.Create;
|
183 |
try
|
184 |
PluginsList.CommaText := Plugins;
|
185 |
FIsEmbeddedServer := PluginsList.IndexOf('Engine12') <> -1;
|
186 |
finally
|
187 |
PluginsList.Free;
|
188 |
end;
|
189 |
end;
|
190 |
|
191 |
{$IFDEF UNIX}
|
192 |
function TFB30ClientAPI.GetFirebirdLibList: string;
|
193 |
begin
|
194 |
Result := 'libfbclient.so:libfbclient.so.2';
|
195 |
end;
|
196 |
{$ENDIF}
|
197 |
|
198 |
procedure TFB30ClientAPI.LoadInterface;
|
199 |
var
|
200 |
fb_get_master_interface: Tfb_get_master_interface;
|
201 |
begin
|
202 |
inherited LoadInterface;
|
203 |
fb_get_master_interface := GetProcAddress(IBLibrary, 'fb_get_master_interface'); {do not localize}
|
204 |
if assigned(fb_get_master_interface) then
|
205 |
begin
|
206 |
FMaster := fb_get_master_interface;
|
207 |
FUtil := FMaster.getUtilInterface;
|
208 |
FProvider := FMaster.getDispatcher;
|
209 |
FConfigManager := FMaster.getConfigManager;
|
210 |
CheckPlugins;
|
211 |
end;
|
212 |
end;
|
213 |
|
214 |
constructor TFB30ClientAPI.Create;
|
215 |
begin
|
216 |
inherited;
|
217 |
FStatus := TFB30Status.Create(self);
|
218 |
FStatusIntf := FStatus;
|
219 |
Firebird30ClientAPI := self;
|
220 |
end;
|
221 |
|
222 |
destructor TFB30ClientAPI.Destroy;
|
223 |
begin
|
224 |
if assigned(FProvider) then
|
225 |
FProvider.release;
|
226 |
inherited Destroy;
|
227 |
end;
|
228 |
|
229 |
function TFB30ClientAPI.StatusIntf: Firebird.IStatus;
|
230 |
begin
|
231 |
Result := FStatus.GetStatus;
|
232 |
Result.Init;
|
233 |
end;
|
234 |
|
235 |
procedure TFB30ClientAPI.Check4DataBaseError;
|
236 |
begin
|
237 |
if FStatus.InErrorState then
|
238 |
IBDataBaseError;
|
239 |
end;
|
240 |
|
241 |
function TFB30ClientAPI.InErrorState: boolean;
|
242 |
begin
|
243 |
Result := FStatus.InErrorState;
|
244 |
end;
|
245 |
|
246 |
function TFB30ClientAPI.GetStatus: IStatus;
|
247 |
begin
|
248 |
Result := FStatusIntf;
|
249 |
end;
|
250 |
|
251 |
function TFB30ClientAPI.AllocateDPB: IDPB;
|
252 |
begin
|
253 |
Result := TDPB.Create;
|
254 |
end;
|
255 |
|
256 |
function TFB30ClientAPI.AllocateTPB: ITPB;
|
257 |
begin
|
258 |
Result := TTPB.Create;
|
259 |
end;
|
260 |
|
261 |
function TFB30ClientAPI.OpenDatabase(DatabaseName: string; DPB: IDPB;
|
262 |
RaiseExceptionOnConnectError: boolean): IAttachment;
|
263 |
begin
|
264 |
Result := TFB30Attachment.Create(DatabaseName, DPB, RaiseExceptionOnConnectError);
|
265 |
if not Result.IsConnected then
|
266 |
Result := nil;
|
267 |
end;
|
268 |
|
269 |
function TFB30ClientAPI.CreateDatabase(DatabaseName: string; DPB: IDPB;
|
270 |
RaiseExceptionOnError: boolean): IAttachment;
|
271 |
begin
|
272 |
Result := TFB30Attachment.CreateDatabase(DatabaseName,DPB, RaiseExceptionOnError);
|
273 |
if not Result.IsConnected then
|
274 |
Result := nil;
|
275 |
end;
|
276 |
|
277 |
function TFB30ClientAPI.StartTransaction(Attachments: array of IAttachment;
|
278 |
TPB: array of byte; DefaultCompletion: TTransactionCompletion): ITransaction;
|
279 |
begin
|
280 |
Result := TFB30Transaction.Create(Attachments,TPB,DefaultCompletion);
|
281 |
end;
|
282 |
|
283 |
function TFB30ClientAPI.StartTransaction(Attachments: array of IAttachment;
|
284 |
TPB: ITPB; DefaultCompletion: TTransactionCompletion): ITransaction;
|
285 |
begin
|
286 |
Result := TFB30Transaction.Create(Attachments,TPB,DefaultCompletion);
|
287 |
end;
|
288 |
|
289 |
function TFB30ClientAPI.AllocateSPB: ISPB;
|
290 |
begin
|
291 |
Result := TSPB.Create;
|
292 |
end;
|
293 |
|
294 |
function TFB30ClientAPI.GetServiceManager(ServerName: string;
|
295 |
Protocol: TProtocol; SPB: ISPB): IServiceManager;
|
296 |
begin
|
297 |
Result := TFB30ServiceManager.Create(ServerName,Protocol,SPB);
|
298 |
end;
|
299 |
|
300 |
function TFB30ClientAPI.HasServiceAPI: boolean;
|
301 |
begin
|
302 |
Result := true;
|
303 |
end;
|
304 |
|
305 |
function TFB30ClientAPI.HasMasterIntf: boolean;
|
306 |
begin
|
307 |
Result := MasterIntf <> nil;
|
308 |
end;
|
309 |
|
310 |
function TFB30ClientAPI.GetIMaster: TObject;
|
311 |
begin
|
312 |
Result := FMaster;
|
313 |
end;
|
314 |
|
315 |
function TFB30ClientAPI.HasRollbackRetaining: boolean;
|
316 |
begin
|
317 |
Result := true;
|
318 |
end;
|
319 |
|
320 |
function TFB30ClientAPI.IsEmbeddedServer: boolean;
|
321 |
begin
|
322 |
Result := FIsEmbeddedServer;
|
323 |
end;
|
324 |
|
325 |
function TFB30ClientAPI.GetImplementationVersion: string;
|
326 |
begin
|
327 |
Result := Format('3.%d',[UtilIntf.GetClientVersion]);
|
328 |
end;
|
329 |
|
330 |
function TFB30ClientAPI.DecodeInteger(bufptr: PChar; len: short): integer;
|
331 |
var P: PChar;
|
332 |
begin
|
333 |
Result := 0;
|
334 |
P := Bufptr + len - 1;
|
335 |
while P >= bufptr do
|
336 |
begin
|
337 |
Result := (Result shl 8 ) or byte(P^);
|
338 |
Dec(P);
|
339 |
end;
|
340 |
end;
|
341 |
|
342 |
procedure TFB30ClientAPI.SQLEncodeDate(aDate: TDateTime; bufptr: PChar);
|
343 |
var
|
344 |
Yr, Mn, Dy: Word;
|
345 |
begin
|
346 |
DecodeDate(aDate, Yr, Mn, Dy);
|
347 |
PISC_Date(Bufptr)^ := UtilIntf.encodeDate(Yr, Mn, Dy);
|
348 |
end;
|
349 |
|
350 |
function TFB30ClientAPI.SQLDecodeDate(bufptr: PChar): TDateTime;
|
351 |
var
|
352 |
Yr, Mn, Dy: Word;
|
353 |
begin
|
354 |
UtilIntf.decodeDate(PISC_DATE(bufptr)^,@Yr, @Mn, @Dy);
|
355 |
try
|
356 |
result := EncodeDate(Yr, Mn,Dy);
|
357 |
except
|
358 |
on E: EConvertError do begin
|
359 |
IBError(ibxeInvalidDataConversion, [nil]);
|
360 |
end;
|
361 |
end;
|
362 |
end;
|
363 |
|
364 |
procedure TFB30ClientAPI.SQLEncodeTime(aTime: TDateTime; bufptr: PChar);
|
365 |
var
|
366 |
Hr, Mt, S, Ms: Word;
|
367 |
begin
|
368 |
DecodeTime(aTime, Hr, Mt, S, Ms);
|
369 |
PISC_TIME(bufptr)^ := UtilIntf.encodeTime(Hr, Mt, S, Ms*10);
|
370 |
end;
|
371 |
|
372 |
function TFB30ClientAPI.SQLDecodeTime(bufptr: PChar): TDateTime;
|
373 |
var
|
374 |
Hr, Mt, S, Ms: Word;
|
375 |
begin
|
376 |
UtilIntf.decodeTime(PISC_TIME(bufptr)^,@Hr, @Mt, @S, @Ms);
|
377 |
try
|
378 |
Result := EncodeTime(Hr, Mt, S, Ms div 10);
|
379 |
except
|
380 |
on E: EConvertError do begin
|
381 |
IBError(ibxeInvalidDataConversion, [nil]);
|
382 |
end;
|
383 |
end;
|
384 |
end;
|
385 |
|
386 |
procedure TFB30ClientAPI.SQLEncodeDateTime(aDateTime: TDateTime; bufptr: PChar);
|
387 |
begin
|
388 |
SQLEncodeDate(aDateTime,bufPtr);
|
389 |
Inc(bufptr,sizeof(ISC_DATE));
|
390 |
SQLEncodeTime(aDateTime,bufPtr);
|
391 |
end;
|
392 |
|
393 |
function TFB30ClientAPI.SQLDecodeDateTime(bufptr: PChar): TDateTime;
|
394 |
begin
|
395 |
Result := SQLDecodeDate(bufPtr);
|
396 |
Inc(bufptr,sizeof(ISC_DATE));
|
397 |
Result += SQLDecodeTime(bufPtr);
|
398 |
end;
|
399 |
|
400 |
end.
|
401 |
|