-
Notifications
You must be signed in to change notification settings - Fork 14
/
rpdbbrowser.pas
443 lines (419 loc) · 11.4 KB
/
rpdbbrowser.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
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
{*******************************************************}
{ }
{ Report Manager Designer }
{ }
{ rpdbbrowser }
{ Database broser frame }
{ }
{ }
{ Copyright (c) 1994-2019 Toni Martir }
{ }
{ This file is under the MPL license }
{ If you enhace this file you must provide }
{ source code }
{ }
{ }
{*******************************************************}
unit rpdbbrowser;
interface
{$I rpconf.inc}
uses
SysUtils, Classes, QGraphics, QControls, QForms,
rpdatainfo,rpmdconsts,rpreport,rptypeval,rpparser,rptypes,
{$IFDEF USEEVALHASH}
rphashtable,
rpstringhash,
{$ENDIF}
QDialogs, QComCtrls, QImgList, QMenus, QTypes;
type
TFRpBrowser = class(TFrame)
ATree: TTreeView;
ImageList1: TImageList;
PopupMenu1: TPopupMenu;
MRefresh: TMenuItem;
procedure ATreeExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
procedure ATreeMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure MRefreshClick(Sender: TObject);
private
{ Private declarations }
FShowDataTypes:Boolean;
FReport:TRpReport;
FShowDatasets:Boolean;
FShowDatabases:Boolean;
FShowEval:Boolean;
procedure SetReport(Value:TRpReport);
procedure InitTree;
procedure SetShowDataTypes(value:Boolean);
procedure FreeFieldsInfo;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property Report:TRpReport read FReport write SetReport;
property ShowEval:Boolean read FShowEval write FShowEval;
property ShowDatasets:Boolean read FShowDatasets write FShowDatasets;
property ShowDatabases:Boolean read FShowDatabases write FShowDatabases;
property ShowDataTypes:Boolean read FShowDataTypes write SetShowDataTypes
default true;
end;
TRpDBFieldInfo=class(TObject)
FieldSize:Integer;
dbinfo:TRpDatabaseInfoItem;
dinfo:TRpDataInfoItem;
end;
implementation
{$R *.xfm}
function NewTRpDBFieldInfo(fieldsize:integer;dbinfo:TRpDatabaseInfoItem;dinfo:TRpDataInfoItem):TRpDBFieldInfo;
begin
Result:=TRpDBFieldInfo.Create;
Result.FieldSize:=fieldsize;
Result.dbinfo:=dbinfo;
Result.dinfo:=dinfo;
end;
procedure TFRpBrowser.FreeFieldsInfo;
var
i:integer;
begin
for i:=0 to ATree.Items.Count-1 do
begin
if TObject(ATree.Items[i].Data) is TRpDBFieldInfo then
begin
TObject(ATree.Items[i].Data).free;
ATree.Items[i].Data:=nil;
end;
end;
ATree.Items.Clear;
end;
destructor TFRpBrowser.Destroy;
begin
FreeFieldsInfo;
inherited Destroy;
end;
procedure TFRpBrowser.SetReport(Value:TRpReport);
begin
FReport:=Value;
if Assigned(FReport) then
begin
InitTree;
end
else
begin
FreeFieldsInfo;
end;
end;
procedure TFRpBrowser.InitTree;
var
i:integer;
dbinfo:TRpDatabaseInfoList;
dinfo:TRpDataInfoList;
anode,nnode:TTreeNode;
aiden:TRpIdentifier;
alist:TStringList;
astringiden:string;
{$IFDEF USEEVALHASH}
ait:TstrHashIterator;
{$ENDIF}
begin
FreeFieldsInfo;
if FShowDatabases then
begin
dbinfo:=FReport.DatabaseInfo;
for i:=0 to dbinfo.Count-1 do
begin
anode:=ATree.Items.AddChild(nil,dbinfo.Items[i].Alias);
anode.Data:=NewTRpDBFieldInfo(0,dbinfo.Items[i],nil);
anode.ImageIndex:=0;
anode.SelectedIndex:=0;
// Place an empty child
ATree.Items.AddChild(anode,'');
end;
end;
if FShowDatasets then
begin
dinfo:=FReport.DataInfo;
for i:=0 to dinfo.Count-1 do
begin
anode:=ATree.Items.AddChild(nil,dinfo.Items[i].Alias);
anode.Data:=NewTRpDBFieldInfo(0,nil,dinfo.Items[i]);
anode.ImageIndex:=1;
anode.SelectedIndex:=1;
// Place an empty child
ATree.Items.AddChild(anode,'');
end;
end;
if FShowEval then
begin
anode:=ATree.Items.AddChild(nil,SRpVariables);
anode.ImageIndex:=1;
anode.SelectedIndex:=1;
FReport.InitEvaluator;
FReport.AddReportItemsToEvaluator(FReport.Evaluator);
alist:=TStringList.Create;
try
alist.Sorted:=true;
{$IFDEF USEEVALHASH}
ait:=FReport.Evaluator.Identifiers.getIterator;
while ait.hasnext do
begin
ait.next;
aiden:=TRpIdentifier(ait.getValue);
astringiden:=ait.getKey;
{$ENDIF}
{$IFNDEF USEEVALHASH}
for i:=0 to FReport.Evaluator.Identifiers.Count-1 do
begin
aiden:=TRpIdentifier(FReport.Evaluator.Identifiers.Objects[i]);
astringiden:=FReport.Evaluator.Identifiers.Strings[i];
{$ENDIF}
if Length(aiden.Idenname)>0 then
if ((astringiden<>'CIERTO') AND
(astringiden<>'M.PAGINA') AND (astringiden<>'M.NUMPAGINA')) then
begin
if alist.Indexof(aiden.IdenName)<0 then
begin
if aiden is TIdenConstant then
begin
alist.Add(astringiden);
nnode:=ATree.Items.AddChild(anode,astringiden);
nnode.ImageIndex:=2;
nnode.SelectedIndex:=2;
end
else
begin
if aiden is TIdenVariable then
begin
alist.Add(astringiden);
nnode:=ATree.Items.AddChild(anode,astringiden);
nnode.ImageIndex:=2;
nnode.SelectedIndex:=2;
end
else
begin
if aiden is TIdenFunction then
begin
if TIdenFunction(aiden).ParamCount=0 then
begin
alist.Add(astringiden);
nnode:=ATree.Items.AddChild(anode,astringiden);
nnode.ImageIndex:=2;
nnode.SelectedIndex:=2;
end;
end;
end;
end;
end;
end;
end;
nnode:=ATree.Items.AddChild(anode,'PAGECOUNT');
nnode.ImageIndex:=2;
nnode.SelectedIndex:=2;
nnode:=ATree.Items.AddChild(anode,'GROUPPAGECOUNT');
nnode.ImageIndex:=2;
nnode.SelectedIndex:=2;
finally
alist.free;
end;
end;
end;
procedure TFRpBrowser.ATreeExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
var
dbitem:TRpDatabaseInfoItem;
ditem:TRpDataInfoItem;
achild:TTreeNode;
alist,fieldtypes,fieldsizes:TStringList;
usebrackets:Boolean;
i,j:integer;
aname:string;
ainfo:TRpDBFieldInfo;
begin
if not assigned(Node.Data) then
exit;
if Node.Count<1 then
exit;
achild:=Node.Item[0];
// It's already readed
if achild.Text<>'' then
exit;
try
if Assigned(Node.Data) then
begin
ainfo:=TRpDBFieldInfo(Node.Data);
if Assigned(ainfo.dbinfo) then
begin
dbitem:=ainfo.dbinfo;
alist:=TStringList.Create;
fieldtypes:=TStringList.Create;
fieldsizes:=TStringList.Create;
try
// Tables
Atree.Items.Delete(achild);
if Node.Parent=nil then
begin
dbitem.GetTableNames(alist,FReport.Params);
if alist.count<1 then
AllowExpansion:=false
else
begin
for i:=0 to alist.Count-1 do
begin
aname:=alist.Strings[i];
if ((i<fieldtypes.Count) and (FShowDataTypes)) then
begin
aname:=aname+'-'+fieldtypes.Strings[i];
if (i<fieldsizes.Count) then
if Length(fieldsizes.Strings[i])>0 then
aname:=aname+'('+fieldsizes.Strings[i]+')';
end;
achild:=ATree.Items.AddChild(Node,aname);
achild.Data:=NewTRpDBFieldInfo(0,dbitem,nil);
ATree.Items.AddChild(achild,'');
end;
end;
end
// Fields
else
begin
dbitem.GetFieldNames(Node.Text,alist,fieldtypes,fieldsizes,FReport.Params);
if alist.count<1 then
AllowExpansion:=false
else
begin
for i:=0 to alist.Count-1 do
begin
aname:=alist.Strings[i];
if ((i<fieldtypes.Count) and (FShowDataTypes)) then
begin
aname:=aname+' '+fieldtypes.Strings[i];
if i<fieldsizes.Count then
if Length(fieldsizes.Strings[i])>0 then
aname:=aname+'('+fieldsizes.Strings[i]+')';
end;
achild:=ATree.Items.AddChild(Node,aname);
achild.ImageIndex:=2;
achild.SelectedIndex:=2;
achild.Data:=NewTRpDBFieldInfo(10,nil,nil);
if (i<fieldsizes.Count) then
if Length(fieldsizes.Strings[i])>0 then
begin
TObject(achild.Data).free;
achild.Data:=NewTRpDBFieldInfo(StrToInt(fieldsizes.strings[i]),nil,nil);
end;
achild.ImageIndex:=1;
achild.SelectedIndex:=1;
end;
end;
end;
finally
alist.free;
fieldtypes.free;
fieldsizes.free;
end;
end
else
begin
if Assigned(ainfo.dinfo) then
begin
ditem:=ainfo.dinfo;
// FReport.PrepareParamsBeforeOpen;
// ditem.Connect(Freport.DatabaseInfo,FReport.Params);
alist:=TStringList.Create;
fieldtypes:=TStringList.Create;
fieldsizes:=TStringList.Create;
try
Atree.Items.Delete(achild);
ditem.GetFieldNames(alist,fieldtypes,fieldsizes);
// FillFieldsInfo(ditem.Dataset,alist,fieldtypes,fieldsizes);
if alist.count<1 then
AllowExpansion:=false
else
begin
for i:=0 to alist.Count-1 do
begin
aname:=alist.Strings[i];
usebrackets:=false;
for j:=1 to Length(aname) do
begin
if Not (aname[j] in ParserSetChars) then
begin
usebrackets:=true;
break;
end;
end;
if usebrackets then
aname:='['+ditem.Alias+'.'+aname+']'
else
aname:=ditem.Alias+'.'+aname;
if ((i<fieldtypes.Count) and (FShowDataTypes)) then
begin
aname:=aname+' '+fieldtypes.Strings[i];
if Length(fieldsizes.Strings[i])>0 then
aname:=aname+'('+fieldsizes.Strings[i]+')';
end;
achild:=ATree.Items.AddChild(Node,aname);
achild.Data:=NewTRpDBFieldInfo(10,nil,nil);
if i<fieldsizes.Count then
if Length(fieldsizes.Strings[i])>0 then
begin
TObject(achild.Data).free;
achild.Data:=NewTRpDBFieldInfo(StrToInt(fieldsizes.strings[i]),nil,nil);
end;
achild.ImageIndex:=2;
achild.SelectedIndex:=2;
end;
end;
finally
alist.free;
fieldtypes.free;
fieldsizes.free;
end;
end;
end;
end;
except
on E:Exception do
begin
ShowMessage(E.Message);
AllowExpansion:=false;
end;
end;
end;
constructor TFRpBrowser.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FShowDatasets:=true;
FShowEval:=true;
FShowDatabases:=true;
MRefresh.Caption:=SRpRefresh;
end;
procedure TFRpBrowser.ATreeMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
anode:TTreeNode;
begin
if Not Assigned(ATree.Selected) then
exit;
anode:=ATree.Selected;
if Not Assigned(anode.Parent) then
exit;
if anode.ImageIndex=2 then
begin
BeginDrag(False);
end;
end;
procedure TFRpBrowser.MRefreshClick(Sender: TObject);
begin
SetReport(FReport);
end;
procedure TFRpBrowser.SetShowDataTypes(value:Boolean);
begin
if value<>FShowDataTypes then
begin
FShowDataTypes:=value;
SetReport(FReport);
end;
end;
end.