My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
unit dbxQuery;

interface
{ The purpose of this Component is to make it easy to convert from
TQuery to dbExpress, It really is very similar to TSimpleDataset
But it adds properties that match directly to TQuery.}
uses
SysUtils, Variants, Classes, DB, DBCommon, DBClient, Provider,
SqlExpr, SqlTimSt, SQLConst;

type
EDbxQuery = class(Exception);
EParamDisparity = class(Exception);

TdbxInternalSQLDataSet = class(TSQLQuery) //TCustomSQLDataSet)
protected
procedure DoAfterScroll; override;
procedure InternalClose; override;
published
property CommandText;
property CommandType;
property DataSource;
property GetMetadata default false;
property MaxBlobSize default 0;
property ParamCheck;
property Params;
property SortFieldNames;
end;

TDWSParam = class helper for TParam
private

protected
procedure SetAsOraDateTime(const Value: TDateTime);
function GetAsOraDateTime: TDateTime;
procedure SetAsOraFMTBCD(const Value: Extended);
function GetAsOraFMTBCD: Extended;
public
property AsOraDateTime: TDateTime read GetAsOraDateTime write SetAsOraDateTime;
property AsOraFMTBCD: Extended read GetAsOraFMTBCD write SetAsOraFMTBCD;
end;

TdbxCustomQuery = class(TCustomClientDataSet)
private
FRequestLive : Boolean;
FSQL : TStringList;
FAutoMaskCurrency:Boolean;
FConnection: TSQLConnection;
FDataSet: TdbxInternalSQLDataSet;
FProvider: TDataSetProvider;
FAutoApplyUpdates: Boolean;
FUpdateMode: TUpdateMode;
FKeyFields: string;
FAutoMaskExcludeFields : TStringList;
FOnRecError: TReconcileErrorEvent;
function InFmtExcludeList(FldName: String): boolean;
procedure GetTextFormatBCDField(Sender: TField; var Text: String;
DisplayText: Boolean);
procedure SetBCDFieldGetText(aDataSet: TDataSet;
aMethod: TFieldGetTextEvent);
function GetConnection: TSQLConnection;
procedure SetAutoApplyUpdates(const Value: Boolean);
//function GetParams: TParams;
//procedure SetParams(const Value: TParams);
function GetRequestLive: Boolean;
procedure SetRequestLive(const Value: Boolean);
function GetSQL: TStrings;
procedure SetSQL(const Value: TStrings);

procedure InternalReconcileError(DataSet: TCustomClientDataSet; E: EReconcileError;
UpdateKind: TUpdateKind; var Action: TReconcileAction);

protected
procedure CheckConnection;
//procedure SetActive(Value: Boolean); override;
procedure InternalInitFieldDefs; override;
procedure CreateFields; override;
procedure AllocDataSet; virtual;
procedure AllocProvider; virtual;
procedure Loaded; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure OpenCursor(InfoQuery: Boolean); override;
//procedure CloseCursor; override;
procedure SetConnection(Value: TSQLConnection); virtual;
procedure DoAfterPost; override;
procedure DoAfterDelete; override;
{ IProviderSupport }
function PSGetCommandText: string; override;
property DataSet: TdbxInternalSQLDataSet read FDataSet;
procedure DoBeforeGetParams(var OwnerData: OleVariant); override;
procedure OnProviderUpdate(Sender: TObject; DataSet: TCustomClientDataSet);
property BeforeOpen;
property AfterOpen;
property BeforeClose;
property AfterClose;
property BeforeInsert;
property AfterInsert;
property BeforeEdit;
property AfterEdit;
property BeforePost;
property AfterPost;
property BeforeCancel;
property AfterCancel;
property BeforeDelete;
property AfterDelete;
property BeforeScroll;
property AfterScroll;
property BeforeRefresh;
property AfterRefresh;
procedure WriteLog(value : String);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property AutoMaskCurrency:boolean read FAutoMaskCurrency write FAutoMaskCurrency;
property Connection: TSQLConnection read GetConnection write SetConnection;
property SQL : TStrings read GetSQL write SetSQL;
property RequestLive : Boolean read GetRequestLive write SetRequestLive default false;
//property Params : TParams read GetParams write SetParams;
property AutoApplyUpdates : Boolean read FAutoApplyUpdates write SetAutoApplyUpdates default true;
procedure Prepare;
function ExecSQL : Integer;
function ParamByName(Name : String) : TParam;
property UpdateMode: TUpdateMode read FUpdateMode write FUpdateMode default upWhereKeyOnly;
property KeyFields: string read FKeyFields write FKeyFields;
function FieldModified(FieldName : WideString):boolean;
procedure RevertFieldValue(FieldName : WideString);
procedure LogQueryDetails(E : Exception);
property AutoMaskExcludeFields : TStringList read FAutoMaskExcludeFields;
published
//published properties here
property OnReconcileError : TReconcileErrorEvent read FOnRecError write FOnRecError;
end;

TdbxQuery = class(TdbxCustomQuery)
private
public
property Dataset;
published
property Connection;
property SQL;
property RequestLive;
property Params;
property AutoApplyUpdates;
property Active;
property Aggregates;
property AggregatesActive;
property AutoCalcFields;
property Constraints;
property DisableStringTrim;
property FileName;
property Filter;
property Filtered;
property FilterOptions;
property FieldDefs;
property IndexDefs;
property IndexFieldNames;
property IndexName;
property FetchOnDemand;
property MasterFields;
property MasterSource;
property ObjectView;
property PacketRecords;
property ReadOnly;
property StoreDefs;
property BeforeOpen;
property AfterOpen;
property BeforeClose;
property AfterClose;
property BeforeInsert;
property AfterInsert;
property BeforeEdit;
property AfterEdit;
property BeforePost;
property AfterPost;
property BeforeCancel;
property AfterCancel;
property BeforeDelete;
property AfterDelete;
property BeforeScroll;
property AfterScroll;
property BeforeRefresh;
property AfterRefresh;
property OnCalcFields;
property OnDeleteError;
property OnEditError;
property OnFilterRecord;
property OnNewRecord;
property OnPostError;
property BeforeApplyUpdates;
property AfterApplyUpdates;
property BeforeGetRecords;
property AfterGetRecords;
property BeforeRowRequest;
property AfterRowRequest;
property BeforeExecute;
property AfterExecute;
property BeforeGetParams;
property AfterGetParams;
property AutoMaskCurrency;
property UpdateMode;
property KeyFields;
//property AutoMaskExcludeFields; //un-comment this to allow entry at design-time
end;


implementation

uses FmtBcd, TypInfo;

{ TdbxQuery }

function TdbxCustomQuery.InFmtExcludeList(FldName : String):boolean;
begin
Result := False;
if (AutoMaskExcludeFields.IndexOf(FldName) >= 0) then
Result := True;
end;

procedure TdbxCustomQuery.SetBCDFieldGetText(aDataSet:TDataSet;aMethod:TFieldGetTextEvent);
var x:integer;
TmpStr:string;
begin
for x := 0 to (aDataSet.Fields.Count - 1) do
begin
TmpStr := UpperCase(aDataSet.Fields[x].ClassName);
if (((TmpStr = 'TBCDFIELD') or (TmpStr = 'TFMTBCDFIELD'))
and ((aDataSet.Fields[x].Size = 2) or (aDataSet.Fields[x].Size = 8))
and (not InFmtExcludeList(aDataSet.Fields[x].FieldName))) then
aDataSet.Fields[x].OnGetText := aMethod;
end;
end;

procedure TdbxCustomQuery.GetTextFormatBCDField(Sender: TField; var Text: String; DisplayText: Boolean);
var TmpStr:string;
begin
TmpStr := UpperCase(Sender.ClassName);
if (((TmpStr = 'TBCDFIELD') or (TmpStr = 'TFMTBCDFIELD'))
and ((Sender.Size = 2) or (Sender.Size = 8))) then
begin
if (Sender.DataSet.RecordCount < 1) then
Text := ''
else if (Sender.IsNull) then
Text := '0.00'
else
Text := FormatFloat('#,0.00',Sender.AsFloat);
end
else
Text := Sender.AsString;
end;

procedure TdbxCustomQuery.InternalInitFieldDefs;
begin
inherited;
if AutoMaskCurrency then
SetBCDFieldGetText(Self,GetTextFormatBCDField);
end;

procedure TdbxCustomQuery.CreateFields;
begin
inherited;
if AutoMaskCurrency then
SetBCDFieldGetText(Self,GetTextFormatBCDField);
end;

procedure TdbxCustomQuery.CheckConnection;
begin
if not Assigned(FConnection) then
raise EDbxQuery.Create('Required Connection Not Specified (' + self.Name + ')');
end;

constructor TdbxCustomQuery.Create(AOwner: TComponent);
begin
inherited;
FAutoMaskCurrency := True;
FAutoApplyUpdates := true;
FRequestLive := false;
ReadOnly := True;
FUpdateMode := upWhereKeyOnly;
FSQL := TStringList.Create;
FAutoMaskExcludeFields := TStringList.Create;
AllocProvider;
AllocDataSet;
inherited OnReconcileError := InternalReconcileError;
end;

destructor TdbxCustomQuery.Destroy;
begin
FSQL.Free;
FAutoMaskExcludeFields.Clear;
FAutoMaskExcludeFields.Free;
inherited; { Reserved }
end;

procedure TdbxCustomQuery.DoAfterDelete;
begin
if FAutoApplyUpdates then
Self.ApplyUpdates(0);
inherited;
end;

procedure TdbxCustomQuery.DoAfterPost;
begin
if FAutoApplyUpdates then
Self.ApplyUpdates(0);
inherited;
end;

procedure TdbxCustomQuery.DoBeforeGetParams(var OwnerData: OleVariant);
begin
if ((CommandText + #13#10) <> FSQL.Text) then
CommandText := FSQL.Text;
inherited;
end;

function TdbxCustomQuery.ExecSQL : Integer;
var x:integer;
P: TParam;
begin
CheckConnection;
FDataSet.CommandText := FSQL.Text;
try
for x := 0 to (FDataSet.Params.Count - 1) do
begin
P := FDataSet.Params.FindParam(Params[X].Name);
if P <> nil then
P.Assign(Params[x]);
FDataSet.Params[x].ParamType := Params[x].ParamType;
FDataSet.Params[x].Size := Params[x].Size;
end;
except
on E:exception do
Raise EParamDisparity.Create('Parameter disparity found, parameters declared but not all parameters have a value.' + #13#10 + E.Message);
end;
try
result := FDataSet.ExecSQL; //(false);
except
on e : Exception do
begin
LogQueryDetails(e);
raise;
end;
end;
if (FDataSet.Params.Count > 0) then
Params.AssignValues(FDataSet.Params);
end;

function TdbxCustomQuery.FieldModified(FieldName: WideString): boolean;
begin
Result := False;
if (FieldByName(FieldName).OldValue <> FieldByName(FieldName).NewValue) then
Result := True;
end;

function TdbxCustomQuery.GetConnection: TSQLConnection;
begin
result := FConnection;
end;

function TdbxCustomQuery.GetRequestLive: Boolean;
begin
result := FRequestLive;
end;

function TdbxCustomQuery.GetSQL: TStrings;
begin
result := FSQL;
end;

procedure TdbxCustomQuery.Loaded;
begin
inherited;
end;


procedure TdbxCustomQuery.LogQueryDetails(E : Exception);
var
I : Integer;
lParamStr : String;
begin
try
WriteLog(E.ClassName + ':' + E.Message);
WriteLog(Name + '.SQL.Text :=');
WriteLog(FSQL.Text);
WriteLog('Params: ( ' + IntToStr(FDataSet.Params.Count) + ' ) ');
for I := 0 to (FDataSet.Params.Count - 1) do
begin
lParamStr := IntToStr(I) + ' ' + FDataSet.Params[I].Name + ' : ' +
GetEnumName(Typeinfo(TFieldType),Integer(FDataSet.Params[I].DataType)) + ' ' +
GetEnumName(Typeinfo(TParamType),Integer(FDataSet.Params[I].ParamType)) + ' = ' +
FDataset.Params[I].AsString;
WriteLog(lParamStr);
end;
except
// avoid this from ever producing errors as the error we are logging is more valuable than info on log failure
end;
end;

procedure TdbxCustomQuery.AllocDataSet;
begin
FDataSet := TdbxInternalSQLDataSet.Create(Self);
FDataSet.Name := 'InternalDataSet'; { Do not localize }
FDataSet.SQLConnection := FConnection;
//FDataSet.GetMetadata := True; //don't do this, it makes the query take 20 times longer to run
FDataSet.SetSubComponent(True);
FProvider.DataSet := FDataSet;
end;

procedure TdbxCustomQuery.AllocProvider;
begin
FProvider := TDataSetProvider.Create(Self);
FProvider.Options := [poAllowCommandText];
FProvider.DataSet := FDataSet;
FProvider.Name := 'InternalProvider'; { Do not localize }
FProvider.SetSubComponent(True);
FProvider.OnUpdateData := OnProviderUpdate;
SetProvider(FProvider);
end;

procedure TdbxCustomQuery.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
if ((aComponent = FConnection) and (Operation = opRemove)) then
begin
FConnection := nil;
FDataSet.SQLConnection := nil;
end;
end;

procedure TdbxCustomQuery.OnProviderUpdate(Sender: TObject;
DataSet: TCustomClientDataSet);
var FldList : TStringList;
x : integer;
begin
if (FUpdateMode = upWhereKeyOnly) then
begin
FldList := TStringList.Create;
try
FldList.Delimiter := ';';
FldList.DelimitedText := FKeyFields;
for x := 0 to (FldList.Count - 1) do
begin
DataSet.FieldByName(FldList[x]).ProviderFlags := [pfInUpdate, pfInWhere, pfInKey];
end;
finally
FldList.Free;
end;
end;
end;

procedure TdbxCustomQuery.OpenCursor(InfoQuery: Boolean);
var
lInternalTrans : Boolean;
lTransDesc : TTransactionDesc;
begin
CheckConnection;
if Assigned(FProvider) then
begin
if (FKeyFields = '') then
FProvider.UpdateMode := upWhereKeyOnly
else
FProvider.UpdateMode := FUpdateMode;
SetProvider(FProvider);
end;
if FProvider.DataSet = Self then
raise Exception.Create(SCircularProvider);
if ((CommandText + #13#10) <> FSQL.Text) then
CommandText := FSQL.Text;
// This transaction management is due to a bug in Oracle
// when crossing db links. If you don't declare a transaction
// it will start one on the remote link then, you get Ora-@#$@#$ Errors
// Ensuring that everyting is in a tranaction solves this problem.

// Are we in a Transaction.
lInternalTrans := FConnection.InTransaction;
// We are not in a Transaction Start One
if not lInternalTrans then
begin
lTransDesc.TransactionID := 1;
lTransDesc.GlobalID := 0;
lTransDesc.IsolationLevel := xilREADCOMMITTED;
lTransDesc.CustomIsolation := 0;
FConnection.StartTransaction(lTransDesc);
end;
try
try
inherited;
except
on e: exception do
begin
LogQueryDetails(e);
raise;
end;
end;
finally
// We started a transaction we better commit it
if not lInternalTrans then
begin
FConnection.Commit(lTransDesc);
end;
end;
end;

procedure TdbxCustomQuery.SetAutoApplyUpdates(const Value: Boolean);
begin
FAutoApplyUpdates := Value;
end;

procedure TdbxCustomQuery.SetConnection(Value: TSQLConnection);
begin
FConnection := Value;
FDataSet.SQLConnection := FConnection;
end;

//procedure TdbxCustomQuery.SetParams(const Value: TParams);
//begin
// if ((CommandText + #13#10) <> FSQL.Text) then
// CommandText := FSQL.Text;
// Params.AssignValues(Value);
// //if ((FDataSet.CommandText + #13#10) <> FSQL.Text) then
// // FDataSet.CommandText := FSQL.Text;
// //FDataSet.Params.AssignValues(Value);
//end;

procedure TdbxCustomQuery.SetRequestLive(const Value: Boolean);
begin
FRequestLive := Value;
ReadOnly := not Value;
end;

procedure TdbxCustomQuery.SetSQL(const Value: TStrings);
begin
FSQL.Clear;
FSQL.Text := Value.Text;
CommandText := FSQL.Text;
end;

procedure TdbxCustomQuery.WriteLog(value: String);
var
lLogFile : TextFile;
AppPath : String;
LogPath : String;
TodayLogName : String;
AppName : String;
Error : Boolean;
ErrorCnt : Integer;
begin
// Check to see if log sub directory exists
// \AppPath\Log
AppPath := ExtractFilePath(ParamStr(0));
LogPath := AppPath + 'LOG\';
AppName := ExtractFileName(ParamStr(0));

If Not DirectoryExists(LogPath) then
begin
ForceDirectories(LogPath);
end;
// LogFile Naming format 'YYYYMMDD.LOG'
TodayLogName := LogPath + FormatDateTime('YYYYMMDD',DATE) + '.LOG';

// This loops through trying to write multiple times, until sucessful.
ErrorCnt := 0;
repeat
Error := False;
try
AssignFile(lLogFile,TodayLogName);
try
if Not FileExists(TodayLogName) then
begin
// Write the file
Rewrite(lLogFile);
end
else
begin
System.Append(lLogFile);
end;
writeln(lLogFile,Value);
finally
CloseFile(lLogFile);
end;
except
Error := True;
inc(ErrorCnt);
end;
until (Error = false) or (ErrorCnt > 5);
end;



function TdbxCustomQuery.ParamByName(Name: String): TParam;
begin
if ((CommandText + #13#10) <> FSQL.Text) then
CommandText := FSQL.Text;
Result := Params.ParamByName(Name);
end;

procedure TdbxCustomQuery.Prepare;
begin
end;

function TdbxCustomQuery.PSGetCommandText: string;
var
IP: IProviderSupport;
begin
if Supports(FDataSet, IProviderSupport, IP) then
Result := IP.PSGetCommandText
else
Result := CommandText;
end;


procedure TdbxCustomQuery.RevertFieldValue(FieldName: WideString);
begin
FieldByName(FieldName).AsVariant := FieldByName(FieldName).OldValue;
end;

{ TDWSParam }

function TDWSParam.GetAsOraDateTime: TDateTime;
begin
if IsNull then
Result := 0 else
Result := VarToDateTime(Self.Value); //FData);
end;

function TDWSParam.GetAsOraFMTBCD: Extended;
begin
if IsNull then
Result := 0.0 else
Result := Self.AsFloat;
//Result := VarToBCD(Self.Value); //FData);
end;

procedure TDWSParam.SetAsOraDateTime(const Value: TDateTime);
var TmpVal : TDateTime;
begin
TmpVal := Value;
Self.DataType := ftTimeStamp; //ftDateTime;
Self.Value := TmpVal;
end;

procedure TDWSParam.SetAsOraFMTBCD(const Value: Extended);
var
S : String;
B : TBcd;
begin
// Fix for: Exception Message: 6.24517650265965E-289 is not a valid BCD value
S:= FormatFloat('###############################.###############################',Value);
B := StrToBcd(S);
Self.DataType := ftFMTBCD; //ftDateTime;
Self.AsFMTBCD := B;
end;

{ TdbxInternalSQLDataSet }

procedure TdbxInternalSQLDataSet.DoAfterScroll;
begin
inherited;
if ((Self.Eof) and (TdbxCustomQuery(Self.Owner).RequestLive = False)) then
begin
TdbxCustomQuery(Self.Owner).SetProvider(nil); //ProviderName := '';
Self.Active := False;
end;
end;

procedure TdbxInternalSQLDataSet.InternalClose;
begin
inherited;
// This fixes a cursor leak in TsqlQuery
FreeReader;
FreeCommand;
end;

{ TdbxQuery }

procedure TdbxCustomQuery.InternalReconcileError(DataSet: TCustomClientDataSet;
E: EReconcileError; UpdateKind: TUpdateKind; var Action: TReconcileAction);
begin
//Done in such a hack manner as the default behavior is Action = raSkip
// Which was ignoring errors.
Action := raCancel;
if Assigned(FOnRecError) then
FOnRecError(Dataset,e,UpdateKind,Action);
if Action = raCancel then
begin
// raise exception.create(e.message);
WriteLog(Name+ ' - ' +E.ClassName + ':' + E.Message);
if IsConsole then
raise Exception.Create(e.message)
else
ApplicationShowException(E);
end;
end;

end.

Change log

r34 by rl...@peakbiz.com on May 12, 2009   Diff
DelphiLive Presentation code, if someone
is looking out there.
I build a lot of code by hand in the
presentation that is not here.
Go to: 
Sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 19835 bytes, 694 lines
Powered by Google Project Hosting