forked from madorin/fibplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pFIBErrorHandler.pas
408 lines (371 loc) · 12.4 KB
/
pFIBErrorHandler.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
{***************************************************************}
{ FIBPlus - component library for direct access to Firebird and }
{ InterBase databases }
{ }
{ FIBPlus is based in part on the product }
{ Free IB Components, written by Gregory H. Deatz for }
{ Hoagland, Longo, Moran, Dunst & Doukas Company. }
{ mailto:gdeatz@hlmdd.com }
{ }
{ Copyright (c) 1998-2007 Devrace Ltd. }
{ Written by Serge Buzadzhy (buzz@devrace.com) }
{ }
{ ------------------------------------------------------------- }
{ FIBPlus home page: http://www.fibplus.com/ }
{ FIBPlus support : http://www.devrace.com/support/ }
{ ------------------------------------------------------------- }
{ }
{ Please see the file License.txt for full license information }
{***************************************************************}
unit pFIBErrorHandler;
interface
{$I FIBPlus.inc}
uses
SysUtils, Classes, fib, FIBDatabase, pFIBDatabase, IB_ErrorCodes,
ibase, IB_Intf, IB_Externals, DB, pFIBDataInfo, FIBQuery;
type
TOptionErrorHandler = (oeException, oeForeignKey, oeLostConnect, oeCheck,
oeUniqueViolation
);
TKindIBError = (keNoError, keException, keForeignKey, keLostConnect,
keSecurity, keCheck, keUniqueViolation, keOther
);
TOnFIBErrorEvent = procedure(Sender: TObject; ErrorValue: EFIBError;
KindIBError: TKindIBError;
var DoRaise: boolean
) of object;
TOptionsErrorHandler = set of TOptionErrorHandler;
TErrorLexems = class(TPersistent)
private
FConstraint:string;
FIndex :string;
FException :string;
FAt :string;
function StoredConstraintProp:boolean;
function StoredIndexProp:boolean;
function StoredExceptionProp: Boolean;
function StoredAtProp: Boolean;
public
constructor Create;
published
property Constraint:string read FConstraint write FConstraint stored StoredConstraintProp;
property Index :string read FIndex write FIndex stored StoredIndexProp;
property Exception :string read FException write FException stored StoredExceptionProp;
property At :string read FAt write FAt stored StoredAtProp;
end;
TpFibErrorHandler = class(TComponent)
private
FLastError: TKindIBError;
FOnFIBErrorEvent: TOnFIBErrorEvent;
FOptions: TOptionsErrorHandler;
FExceptionNumber: integer;
FConstraintName: string;
FExceptionName :string;
FErrorLexems:TErrorLexems;
procedure DefaultOnError(Sender: TObject; ErrorValue: EFIBError;
var DoRaise: boolean);
function GetConstraintName(const Msg: string): string;
function GetTr(Sender: TObject): TFIBTransaction;
procedure SetErrorLexems(const Value: TErrorLexems);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DoOnErrorEvent(Sender: TObject; ErrorValue: EFIBError;
var DoRaise: boolean); dynamic; // for internal use
procedure DoOnLostConnect(DataBase:TFIBDatabase;ErrorValue: EFIBError;var DoRaise:boolean);
property ExceptionNumber: integer read FExceptionNumber;
property LastError: TKindIBError read FLastError;
property ConstraintName: string read FConstraintName;
property ExceptionName :string read FExceptionName;
published
property OnFIBErrorEvent: TOnFIBErrorEvent read FOnFIBErrorEvent write
FOnFIBErrorEvent;
property Options: TOptionsErrorHandler read FOptions write FOptions
default [oeException, oeLostConnect]
;
property ErrorLexems:TErrorLexems read FErrorLexems write SetErrorLexems;
end;
function IsConnectionLost(const IBErrorCode: integer): boolean;
implementation
uses FIBConsts, StrUtil;
constructor TpFibErrorHandler.Create(AOwner: TComponent);
begin
if ErrorHandlerRegistered and not (csDesigning in ComponentState) then
raise Exception.Create(SFIBErrorHandlerExists);
inherited Create(AOwner);
RegisterErrorHandler(Self);
Options := [oeException, oeLostConnect];
FLastError := keNoError;
FErrorLexems:=TErrorLexems.Create;
end;
destructor TpFibErrorHandler.Destroy;
begin
FErrorLexems.Free;
UnRegisterErrorHandler;
inherited Destroy;
end;
function IsConnectionLost(const IBErrorCode: integer): boolean;
begin
case IBErrorCode of
isc_shutdown,isc_network_error, isc_lost_db_connection, //isc_net_connect_err,
isc_net_connect_listen_err, isc_net_event_connect_err,
isc_net_event_listen_err,isc_net_read_err, isc_net_write_err,isc_att_shutdown:
Result := True;
else
Result := False;
end;
end;
type
THackDatabase = class(TFIBDatabase);
function FindDatabaseForObject(Sender: TObject):THackDatabase;
begin
if Sender is TFIBDataBase then
Result:=THackDatabase(Sender)
else
if Sender is TFIBQuery then
Result:=THackDatabase(TFIBQuery(Sender).Database)
else
if Sender is TFIBTransaction then
Result:=THackDatabase(TFIBTransaction(Sender).MainDatabase)
else
Result := nil;
end;
procedure TpFibErrorHandler.DefaultOnError(Sender: TObject;
ErrorValue: EFIBError;
var DoRaise: boolean);
var
p: integer;
s: string;
CurTr:TFIBTransaction;
begin
FConstraintName := '';
FExceptionNumber := -1;
FLastError := keOther;
with ErrorValue do
case SQLCode of
sqlcode_unique_violation :
begin
case IBErrorCode of
isc_unique_key_violation,isc_no_dup:
begin
FLastError := keUniqueViolation;
if oeUniqueViolation in Options then
begin
FConstraintName := GetConstraintName(ErrorValue.IBMessage);
if (GetTr(Sender) <> nil) then
s := ListErrorMessages.ErrorMessage(GetTr(Sender), FConstraintName);
if s <> '' then
begin
ErrorValue.Message := s;
Exit;
end;
ErrorValue.Message := ErrorValue.IBMessage;
end;
end ;
end;
end;
sqlcode_exception:
begin
//Developer exception
FLastError := keException;
if oeException in Options then
begin
p := Pos(ErrorLexems.FException, AnsiLowerCase(Message));
if p > 0 then
Message := FastCopy(Message, p + 10, MaxInt);
p := Pos(ErrorLexems.FException, AnsiLowerCase(Message));
if p > 0 then
Message := FastCopy(Message, p + 10, MaxInt);
p := PosCh('.', Message);
if p > 0 then
try
FExceptionNumber := StrToInt(FastCopy(Message, 1, p - 1));
Message := TrimCLRF(FastCopy(Message, p + 1, MaxInt));
FExceptionName:='Unknown';
CurTr:=GetTr(Sender);
if CurTr<>nil then
begin
if CurTr.DefaultDatabase.IsFirebirdConnect and (
CurTr.DefaultDatabase.ServerMajorVersion>=2
) then
begin
p := PosCh('.', Message);
if p>0 then
begin
FExceptionName:=FastCopy(Message,1, p - 1);
Message:=FastCopy(Message, p + 1,MaxInt);
p:=Pos(ErrorLexems.FAt, AnsiLowerCase(Message));
if (p>0) and (p<Length(Message)-3) and (Message[p+2]= ' ') then
Message:=FastCopy(Message,1,p-1);
end;
end;
end;
except
end;
end;
end ;
sqlcode_foreign_or_create_schema:
begin
if (IBErrorCode = isc_foreign_key) then
begin
// Is Foreign Key
FLastError := keForeignKey;
FConstraintName := GetConstraintName(ErrorValue.IBMessage);
if (oeForeignKey in Options) and (GetTr(Sender) <> nil) then
begin
s := ListErrorMessages.ErrorMessage(GetTr(Sender), FConstraintName);
if s <> '' then
ErrorValue.Message := s;
end;
end
end;
sqlcode_notpermission:
FLastError := keSecurity;
sqlcode_checkconstraint:
begin
FLastError := keCheck;
if oeCheck in Options then
begin
FConstraintName := GetConstraintName(ErrorValue.IBMessage);
if (GetTr(Sender) <> nil) then
s := ListErrorMessages.ErrorMessage(GetTr(Sender), FConstraintName);
if s <> '' then
begin
ErrorValue.Message := s;
Exit;
end;
ErrorValue.Message := ErrorValue.IBMessage;
end
end;
else
if IsConnectionLost(IBErrorCode) or
((SQLCode = sqlcode_902) and (IBErrorCode = isc_network_error)) then
begin
// if (IBErrorCode=isc_shutdown) or (IBErrorCode=isc_att_shutdown) and (FindDatabaseForObject(Sender)<>nil) then
if (IBErrorCode=isc_shutdown) and (FindDatabaseForObject(Sender)<>nil) then
FindDatabaseForObject(Sender).InternalClose(True,True);
FLastError := keLostConnect;
if oeLostConnect in Options then
DoOnLostConnect(FindDatabaseForObject(Sender),ErrorValue,DoRaise);
end
end;
end;
function TpFibErrorHandler.GetConstraintName(const Msg: string): string;
var
i: integer;
lcLexem, Lexem: string;
InConstrName: boolean;
begin
Lexem := '';
InConstrName := False;
for i := 1 to Length(Msg) do
case Msg[i] of
' ', #13, #10:
begin
lcLexem := AnsiLowerCase(Lexem);
if (lcLexem = FErrorLexems.FConstraint) or (lcLexem = FErrorLexems.FIndex) then
begin
InConstrName := True;
Lexem := '';
end
else
if not InConstrName then
Lexem := ''
else
Break
end;
else
if Msg[i] <> '"' then
Lexem := Lexem + Msg[i]
end;
Result := Lexem;
if (Length(Result) > 0) and (Result[Length(Result)] = '.') then
SetLength(Result, Length(Result) - 1)
end;
function TpFibErrorHandler.GetTr(Sender: TObject): TFIBTransaction;
begin
if Sender is TFIBQuery then
Result := TFIBQuery(Sender).Transaction
else if Sender is TFIBDatabase then
Result := TFIBDatabase(Sender).DefaultTransaction
else
Result := nil;
end;
type
THackpFIBDatabase = class(TpFIBDatabase);
procedure TpFibErrorHandler.DoOnLostConnect(DataBase:TFIBDatabase;ErrorValue: EFIBError;var DoRaise:boolean);
var
i: integer;
Actions: TOnLostConnectActions;
begin
if DataBase=nil then
with DatabaseList.LockList do
try
for i := 0 to Pred(Count) do
if TFIBDatabase(Items[i]) is TpFIBDatabase then
with THackpFIBDatabase(Items[i]) do
begin
{ if not Connected then
Continue;}
Actions := laCloseConnect;
DoOnLostConnect(TFIBDatabase(Items[i]), ErrorValue, Actions,DoRaise);
end;
finally
DatabaseList.UnlockList;
end
else
// if Database.Connected then
begin
Actions := laCloseConnect;
THackpFIBDatabase(DataBase).DoOnLostConnect(DataBase, ErrorValue, Actions,DoRaise)
end;
end;
procedure TpFibErrorHandler.
DoOnErrorEvent(Sender: TObject; ErrorValue: EFIBError; var DoRaise: boolean);
var
vDB:TFIBDatabase;
begin
DefaultOnError(Sender, ErrorValue, DoRaise);
if Assigned(FOnFIBErrorEvent) then
begin
FOnFIBErrorEvent(Sender, ErrorValue, LastError, DoRaise);
if (LastError=keLostConnect) and (not DoRaise) then
begin
vDB:=FindDatabaseForObject(Sender);
if not (Assigned(vDB) and (vDB is TpFIBDatabase) and
TpFIBDatabase(vDB).InRestoreConnect)
then
Abort;
end;
end;
end;
{ TErrorLexems }
constructor TErrorLexems.Create;
begin
FConstraint:='constraint';
FIndex :='index';
FException :='exception';
FAt :='at'
end;
function TErrorLexems.StoredAtProp: Boolean;
begin
Result:= FAt<>'at';
end;
function TErrorLexems.StoredConstraintProp: boolean;
begin
Result:= FConstraint<>'constraint';
end;
function TErrorLexems.StoredExceptionProp: Boolean;
begin
Result:= FException<>'exception';
end;
function TErrorLexems.StoredIndexProp: boolean;
begin
Result:= FIndex<>'index';
end;
procedure TpFibErrorHandler.SetErrorLexems(const Value: TErrorLexems);
begin
FErrorLexems.FConstraint:=Value.FConstraint;
FErrorLexems.FIndex:=Value.FIndex;
end;
end.