1 |
(* |
2 |
* Firebird Interface (fbintf). The fbintf components provide a set of |
3 |
* Pascal language bindings for the Firebird API. Although predominantly |
4 |
* a new development they include source code taken from IBX and may be |
5 |
* considered a derived product. This software thus also includes the copyright |
6 |
* notice and license conditions from IBX. |
7 |
* |
8 |
* Except for those parts dervied from IBX, contents of this file are subject |
9 |
* to the Initial Developer's Public License Version 1.0 (the "License"); you |
10 |
* may not use this file except in compliance with the License. You may obtain a |
11 |
* copy of the License here: |
12 |
* |
13 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl |
14 |
* |
15 |
* Software distributed under the License is distributed on an "AS |
16 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
17 |
* implied. See the License for the specific language governing rights |
18 |
* and limitations under the License. |
19 |
* |
20 |
* The Initial Developer of the Original Code is Tony Whyman. |
21 |
* |
22 |
* The Original Code is (C) 2016 Tony Whyman, MWA Software |
23 |
* (http://www.mwasoftware.co.uk). |
24 |
* |
25 |
* All Rights Reserved. |
26 |
* |
27 |
* Contributor(s): ______________________________________. |
28 |
* |
29 |
*) |
30 |
{************************************************************************} |
31 |
{ } |
32 |
{ Borland Delphi Visual Component Library } |
33 |
{ InterBase Express core components } |
34 |
{ } |
35 |
{ Copyright (c) 1998-2000 Inprise Corporation } |
36 |
{ } |
37 |
{ InterBase Express is based in part on the product } |
38 |
{ Free IB Components, written by Gregory H. Deatz for } |
39 |
{ Hoagland, Longo, Moran, Dunst & Doukas Company. } |
40 |
{ Free IB Components is used under license. } |
41 |
{ } |
42 |
{ The contents of this file are subject to the InterBase } |
43 |
{ Public License Version 1.0 (the "License"); you may not } |
44 |
{ use this file except in compliance with the License. You } |
45 |
{ may obtain a copy of the License at http://www.Inprise.com/IPL.html } |
46 |
{ Software distributed under the License is distributed on } |
47 |
{ an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either } |
48 |
{ express or implied. See the License for the specific language } |
49 |
{ governing rights and limitations under the License. } |
50 |
{ The Original Code was created by InterBase Software Corporation } |
51 |
{ and its successors. } |
52 |
{ Portions created by Inprise Corporation are Copyright (C) Inprise } |
53 |
{ Corporation. All Rights Reserved. } |
54 |
{ Contributor(s): Jeff Overcash } |
55 |
{ } |
56 |
{ IBX For Lazarus (Firebird Express) } |
57 |
{ Contributor: Tony Whyman, MWA Software http://www.mwasoftware.co.uk } |
58 |
{ Portions created by MWA Software are copyright McCallum Whyman } |
59 |
{ Associates Ltd 2011 - 2015 } |
60 |
{ } |
61 |
{************************************************************************} |
62 |
unit FBEvents; |
63 |
|
64 |
{$IFDEF FPC} |
65 |
{$mode objfpc}{$H+} |
66 |
{$interfaces COM} |
67 |
{$ENDIF} |
68 |
|
69 |
interface |
70 |
|
71 |
uses |
72 |
Classes, SysUtils, IB, FBClientAPI, syncobjs, FBActivityMonitor; |
73 |
|
74 |
type |
75 |
|
76 |
{ TFBEvents } |
77 |
|
78 |
TFBEvents = class(TActivityReporter) |
79 |
private |
80 |
FEvents: TStringList; |
81 |
FAttachment: IAttachment; |
82 |
FEventCounts: TEventCounts; |
83 |
protected |
84 |
FEventBuffer: PChar; |
85 |
FEventBufferLen: integer; |
86 |
FResultBuffer: PChar; |
87 |
FEventHandler: TEventHandler; |
88 |
FCriticalSection: TCriticalSection; |
89 |
FInWaitState: boolean; |
90 |
procedure CreateEventBlock; |
91 |
procedure CancelEvents(Force: boolean = false); virtual; |
92 |
procedure EventSignaled; |
93 |
function GetIEvents: IEvents; virtual; abstract; |
94 |
procedure ProcessEventCounts; |
95 |
public |
96 |
constructor Create(DBAttachment: IAttachment; aMonitor: IActivityMonitor; Events: TStrings); |
97 |
destructor Destroy; override; |
98 |
|
99 |
{IEvents} |
100 |
procedure GetEvents(EventNames: TStrings); |
101 |
procedure SetEvents(EventNames: TStrings); overload; |
102 |
procedure SetEvents(Event: string); overload; |
103 |
procedure Cancel; |
104 |
function ExtractEventCounts: TEventCounts; |
105 |
function GetAttachment: IAttachment; |
106 |
procedure AsyncWaitForEvent(EventHandler: TEventHandler); virtual; abstract; |
107 |
end; |
108 |
|
109 |
|
110 |
implementation |
111 |
|
112 |
uses FBMessages, IBExternals; |
113 |
|
114 |
const |
115 |
MaxEvents = 15; |
116 |
|
117 |
{ TFBEvents } |
118 |
|
119 |
procedure TFBEvents.CreateEventBlock; |
120 |
var |
121 |
i: integer; |
122 |
EventNames: array of PChar; |
123 |
begin |
124 |
with FirebirdClientAPI do |
125 |
begin |
126 |
if FEventBuffer <> nil then |
127 |
isc_free( FEventBuffer); |
128 |
FEventBuffer := nil; |
129 |
if FResultBuffer <> nil then |
130 |
isc_free( FResultBuffer); |
131 |
FResultBuffer := nil; |
132 |
|
133 |
setlength(EventNames,MaxEvents); |
134 |
try |
135 |
for i := 0 to FEvents.Count-1 do |
136 |
EventNames[i] := PChar(FEvents[i]); |
137 |
|
138 |
FEventBufferlen := isc_event_block(@FEventBuffer,@FResultBuffer, |
139 |
FEvents.Count, |
140 |
EventNames[0],EventNames[1],EventNames[2], |
141 |
EventNames[3],EventNames[4],EventNames[5], |
142 |
EventNames[6],EventNames[7],EventNames[8], |
143 |
EventNames[9],EventNames[10],EventNames[11], |
144 |
EventNames[12],EventNames[13],EventNames[14] |
145 |
); |
146 |
finally |
147 |
SetLength(EventNames,0) |
148 |
end; |
149 |
end; |
150 |
end; |
151 |
|
152 |
procedure TFBEvents.CancelEvents(Force: boolean); |
153 |
begin |
154 |
FEventHandler := nil; |
155 |
end; |
156 |
|
157 |
procedure TFBEvents.EventSignaled; |
158 |
var Handler: TEventHandler; |
159 |
begin |
160 |
Handler := nil; |
161 |
FCriticalSection.Enter; |
162 |
try |
163 |
if not FInWaitState then Exit; |
164 |
FInWaitState := false; |
165 |
ProcessEventCounts; |
166 |
if assigned(FEventHandler) then |
167 |
begin |
168 |
Handler := FEventHandler; |
169 |
FEventHandler := nil; |
170 |
end; |
171 |
finally |
172 |
FCriticalSection.Leave; |
173 |
end; |
174 |
if assigned(Handler) then |
175 |
Handler(GetIEvents); |
176 |
end; |
177 |
|
178 |
procedure TFBEvents.ProcessEventCounts; |
179 |
var P: PISC_LONG; |
180 |
EventCountList: array[0..19] of ISC_LONG; |
181 |
i: integer; |
182 |
j: integer; |
183 |
begin |
184 |
SetLength(FEventCounts,0); |
185 |
if FResultBuffer = nil then Exit; |
186 |
|
187 |
FillChar(EventCountList,sizeof(EventCountList),0); |
188 |
|
189 |
with FirebirdClientAPI do |
190 |
isc_event_counts( @EventCountList, FEventBufferLen, FEventBuffer, FResultBuffer); |
191 |
j := 0; |
192 |
P := EventCountList; |
193 |
for i := 0 to FEvents.Count - 1 do |
194 |
begin |
195 |
if EventCountList[i] <> 0 then |
196 |
begin |
197 |
Inc(j); |
198 |
SetLength(FEventCounts,j); |
199 |
FEventCounts[j-1].EventName := FEvents[i]; |
200 |
FEventCounts[j-1].Count := P^; |
201 |
Inc(P); |
202 |
// writeln('Event: ',FEventCounts[j-1].EventName,' Count = ',FEventCounts[j-1].Count); |
203 |
end; |
204 |
end; |
205 |
end; |
206 |
|
207 |
constructor TFBEvents.Create(DBAttachment: IAttachment; |
208 |
aMonitor: IActivityMonitor; Events: TStrings); |
209 |
begin |
210 |
inherited Create(aMonitor); |
211 |
FAttachment := DBAttachment; |
212 |
if Events.Count > MaxEvents then |
213 |
IBError(ibxeMaximumEvents, [nil]); |
214 |
|
215 |
FCriticalSection := TCriticalSection.Create; |
216 |
FEvents := TStringList.Create; |
217 |
FEvents.Assign(Events); |
218 |
CreateEventBlock; |
219 |
end; |
220 |
|
221 |
destructor TFBEvents.Destroy; |
222 |
begin |
223 |
if assigned(FCriticalSection) then FCriticalSection.Free; |
224 |
if assigned(FEvents) then FEvents.Free; |
225 |
with FirebirdClientAPI do |
226 |
begin |
227 |
if FEventBuffer <> nil then |
228 |
isc_free( FEventBuffer); |
229 |
if FResultBuffer <> nil then |
230 |
isc_free( FResultBuffer); |
231 |
end; |
232 |
inherited Destroy; |
233 |
end; |
234 |
|
235 |
procedure TFBEvents.GetEvents(EventNames: TStrings); |
236 |
begin |
237 |
EventNames.Assign(FEvents) |
238 |
end; |
239 |
|
240 |
procedure TFBEvents.SetEvents(EventNames: TStrings); |
241 |
begin |
242 |
if EventNames.Text <> FEvents.Text then |
243 |
begin |
244 |
Cancel; |
245 |
FEvents.Assign(EventNames); |
246 |
CreateEventBlock; |
247 |
end; |
248 |
end; |
249 |
|
250 |
procedure TFBEvents.SetEvents(Event: string); |
251 |
var S: TStringList; |
252 |
begin |
253 |
S := TStringList.Create; |
254 |
try |
255 |
S.Add(Event); |
256 |
SetEvents(S); |
257 |
finally |
258 |
S.Free; |
259 |
end; |
260 |
end; |
261 |
|
262 |
procedure TFBEvents.Cancel; |
263 |
begin |
264 |
if assigned(FEventHandler) then |
265 |
CancelEvents; |
266 |
end; |
267 |
|
268 |
function TFBEvents.ExtractEventCounts: TEventCounts; |
269 |
begin |
270 |
Result := FEventCounts; |
271 |
end; |
272 |
|
273 |
function TFBEvents.GetAttachment: IAttachment; |
274 |
begin |
275 |
Result := FAttachment; |
276 |
end; |
277 |
|
278 |
end. |
279 |
|