-
-
Notifications
You must be signed in to change notification settings - Fork 563
Expand file tree
/
Copy pathsqlhelp.pas
More file actions
422 lines (369 loc) · 13.7 KB
/
sqlhelp.pas
File metadata and controls
422 lines (369 loc) · 13.7 KB
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
unit sqlhelp;
interface
uses
Winapi.Windows, System.SysUtils, System.Classes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls,
Vcl.Buttons, SynMemo, SynEditHighlighter, SynHighlighterURI, extra_controls,
SynURIOpener, SynEdit, VirtualTrees, Vcl.Graphics,
dbconnection, gnugettext, VirtualTrees.BaseTree, VirtualTrees.Types,
VirtualTrees.BaseAncestorVCL, VirtualTrees.AncestorVCL, dbstructures;
type
TfrmSQLhelp = class(TExtForm)
URIOpenerDescription: TSynURIOpener;
URIHighlighter: TSynURISyn;
URIOpenerExample: TSynURIOpener;
btnSearchOnline: TButton;
ButtonClose: TButton;
pnlMain: TPanel;
pnlLeft: TPanel;
treeTopics: TVirtualStringTree;
editFilter: TButtonedEdit;
pnlRight: TPanel;
Splitter2: TSplitter;
Splitter1: TSplitter;
lblDescription: TLabel;
lblKeyword: TLabel;
memoDescription: TSynMemo;
lblExample: TLabel;
MemoExample: TSynMemo;
timerSearch: TTimer;
procedure FormCreate(Sender: TObject);
procedure memosKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ButtonOnlinehelpClick(Sender: TObject);
procedure ButtonCloseClick(Sender: TObject);
procedure DoSearch(Sender: TObject);
procedure treeTopicsGetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
procedure treeTopicsInitNode(Sender: TBaseVirtualTree; ParentNode, Node: PVirtualNode;
var InitialStates: TVirtualNodeInitStates);
procedure treeTopicsInitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode;
var ChildCount: Cardinal);
procedure treeTopicsGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: TImageIndex);
procedure treeTopicsGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
procedure treeTopicsFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
procedure treeTopicsFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex);
procedure editFilterChange(Sender: TObject);
procedure editFilterRightButtonClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
FConnection: TDBConnection;
FKeyword: String;
FRootTopics: TDBQuery;
function GetHelpResult(Node: PVirtualNode): TDBQuery;
procedure SetKeyword(Value: String);
public
{ Public declarations }
property Keyword: String read FKeyword write SetKeyword;
end;
var
SqlHelpDialog: TfrmSQLhelp; // global var, so we can apply settings via SetupSynEditors
const
DEFAULT_WINDOW_CAPTION : String = 'Integrated SQL-help' ;
ICONINDEX_CATEGORY_CLOSED : Integer = 66;
ICONINDEX_CATEGORY_OPENED : Integer = 67;
ICONINDEX_HELPITEM : Integer = 68;
implementation
uses apphelpers, main;
{$I const.inc}
{$R *.dfm}
procedure TfrmSQLhelp.FormCreate(Sender: TObject);
begin
// Set window-layout
lblKeyword.Font.Style := [fsBold];
Caption := DEFAULT_WINDOW_CAPTION;
FixVT(treeTopics);
HasSizeGrip := True;
treeTopics.Clear;
FreeAndNil(FRootTopics);
FConnection := MainForm.ActiveConnection;
FRootTopics := FConnection.GetResults(FConnection.SqlProvider.GetSql(qHelpKeyword, [FConnection.EscapeString('CONTENTS')]));
treeTopics.RootNodeCount := FRootTopics.RecordCount;
end;
procedure TfrmSQLhelp.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AppSettings.WriteInt(asSQLHelpWindowLeft, Left );
AppSettings.WriteInt(asSQLHelpWindowTop, Top );
AppSettings.WriteIntDpiAware(asSQLHelpWindowWidth, Self, Width);
AppSettings.WriteIntDpiAware(asSQLHelpWindowHeight, Self, Height);
AppSettings.WriteIntDpiAware(asSQLHelpPnlLeftWidth, Self, pnlLeft.Width);
AppSettings.WriteIntDpiAware(asSQLHelpPnlRightTopHeight, Self, memoDescription.Height);
Action := caFree;
SqlHelpDialog := nil;
end;
procedure TfrmSQLhelp.treeTopicsFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex);
var
Results: TDBQuery;
VT: TVirtualStringTree;
begin
// Topic selected
VT := Sender as TVirtualStringTree;
if not Assigned(VT.FocusedNode) then
Exit;
if VT.HasChildren[VT.FocusedNode] then
Exit;
FKeyword := VT.Text[VT.FocusedNode, VT.FocusedColumn];
lblKeyword.Caption := Copy(FKeyword, 1, 100);
MemoDescription.Lines.Clear;
MemoExample.Lines.Clear;
Caption := DEFAULT_WINDOW_CAPTION;
if FKeyword <> '' then try
Screen.Cursor := crHourglass;
Results := FConnection.GetResults(FConnection.SqlProvider.GetSql(qHelpKeyword, [FConnection.EscapeString(FKeyword)]));
Caption := Caption + ' - ' + FKeyword;
MemoDescription.Text := fixNewlines(Results.Col('description', True));
MemoExample.Text := fixNewlines(Results.Col('example', True));
finally
FreeAndNil(Results);
Screen.Cursor := crDefault;
end;
// Show the user if topic is (not) available
if memoDescription.GetTextLen = 0 then
memoDescription.Text := _('No help available for this keyword or no keyword was selected.');
if memoExample.GetTextLen = 0 then
memoExample.Text := _('No example available or no keyword was selected.');
end;
procedure TfrmSQLhelp.treeTopicsFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
Results: PDBQuery;
begin
// Node gets destroyed - free memory used for bound SQL result
Results := Sender.GetNodeData(Node);
if Assigned(Results^) then
Results^.Free;
end;
procedure TfrmSQLhelp.treeTopicsGetImageIndex(Sender: TBaseVirtualTree; Node: PVirtualNode;
Kind: TVTImageKind; Column: TColumnIndex; var Ghosted: Boolean; var ImageIndex: TImageIndex);
begin
// Return open or closed book icon for folders, or document icon for topics
if not (Kind in [ikNormal, ikSelected]) then
Exit;
if Sender.HasChildren[Node] then begin
if Sender.Expanded[Node] then
ImageIndex := ICONINDEX_CATEGORY_OPENED
else
ImageIndex := ICONINDEX_CATEGORY_CLOSED;
end else
ImageIndex := ICONINDEX_HELPITEM;
end;
procedure TfrmSQLhelp.treeTopicsGetNodeDataSize(Sender: TBaseVirtualTree;
var NodeDataSize: Integer);
begin
// We bind one TDBQuery to a node
NodeDataSize := SizeOf(TDBQuery);
end;
function TfrmSQLhelp.GetHelpResult(Node: PVirtualNode): TDBQuery;
var
P: PDBQuery;
begin
// Find right result set for given node
if treeTopics.GetNodeLevel(Node) = 0 then
Result := FRootTopics
else begin
P := treeTopics.GetNodeData(Node.Parent);
Result := P^;
end;
end;
procedure TfrmSQLhelp.treeTopicsGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: string);
var
Results: TDBQuery;
begin
// Ask result set for node text
Results := GetHelpResult(Node);
Results.RecNo := Node.Index;
CellText := Results.Col('name');
end;
procedure TfrmSQLhelp.treeTopicsInitChildren(Sender: TBaseVirtualTree; Node: PVirtualNode;
var ChildCount: Cardinal);
var
Results: PDBQuery;
VT: TVirtualStringTree;
begin
// Return number of children for folder
VT := Sender as TVirtualStringTree;
Results := VT.GetNodeData(Node);
Results^ := FConnection.GetResults(FConnection.SqlProvider.GetSql(qHelpKeyword, [FConnection.EscapeString(VT.Text[Node, VT.Header.MainColumn])]));
ChildCount := Results.RecordCount;
end;
procedure TfrmSQLhelp.treeTopicsInitNode(Sender: TBaseVirtualTree; ParentNode,
Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
var
Results: TDBQuery;
ThisFolder, PrevFolder: String;
N: PVirtualNode;
VT: TVirtualStringTree;
RecursionAlarm: Boolean;
begin
// Display plus button for nodes which are folders
VT := Sender as TVirtualStringTree;
Results := GetHelpResult(Node);
Results.RecNo := Node.Index;
if Results.Col('is_it_category', True) = 'Y' then begin
// Some random server versions have duplicated category names in help tables, which would cause
// infinite tree recursion, e.g. for "Polygon properties" > "Contents". Do not display these
// duplicates as folder
RecursionAlarm := False;
ThisFolder := Results.Col('name');
N := VT.GetPreviousInitialized(Node);
while Assigned(N) do begin
PrevFolder := VT.Text[N, VT.Header.MainColumn];
if VT.HasChildren[N] and ((ThisFolder=PrevFolder) or (ThisFolder='Contents')) then begin
RecursionAlarm := True;
break;
end;
N := VT.GetPreviousInitialized(N);
end;
if not RecursionAlarm then
Include(InitialStates, ivsHasChildren);
end;
end;
procedure TfrmSQLhelp.ButtonCloseClick(Sender: TObject);
begin
Close;
end;
procedure TfrmSQLhelp.FormShow(Sender: TObject);
begin
Top := AppSettings.ReadInt(asSQLHelpWindowTop);
Left := AppSettings.ReadInt(asSQLHelpWindowLeft);
Width := AppSettings.ReadIntDpiAware(asSQLHelpWindowWidth, Self);
Height := AppSettings.ReadIntDpiAware(asSQLHelpWindowHeight, Self);
MakeFullyVisible;
pnlLeft.Width := AppSettings.ReadIntDpiAware(asSQLHelpPnlLeftWidth, Self);
memoDescription.Height := AppSettings.ReadIntDpiAware(asSQLHelpPnlRightTopHeight, Self);
// Apply themed colors in OnShow, not OnCreate, as a check with <> nil returns false otherwise
MainForm.SetupSynEditors(Self);
// These SynMemo's don't have any (SQL) highligher, so we have to assign correct colors for basic text
memoDescription.Font.Color := GetThemeColor(clWindowText);
MemoExample.Font.Color := GetThemeColor(clWindowText);
editFilter.SetFocus;
end;
procedure TfrmSQLhelp.ButtonOnlinehelpClick(Sender: TObject);
begin
// Link/redirect to mysql.com for further help
ShellExec(APPDOMAIN + 'sqlhelp.php?mysqlversion='+inttostr(FConnection.ServerVersionInt)+
'&keyword='+EncodeURLParam(keyword));
end;
procedure TfrmSQLhelp.memosKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
// Esc pressed - close form.
// Seems that if we're in a memo, the ButtonClose.Cancel=True doesn't have an effect
if Key = VK_ESCAPE then
Close;
end;
procedure TfrmSQLhelp.SetKeyword(Value: string);
var
VT: TVirtualStringTree;
Node: PVirtualNode;
Results: TDBQuery;
SearchNoInit: Boolean;
begin
// Find keyword in tree
FKeyword := Value;
if FKeyword = '' then
Exit;
Results := FConnection.GetResults(FConnection.SqlProvider.GetSql(qHelpKeyword, [FConnection.EscapeString(FKeyword)]));
while not Results.Eof do begin
if Results.Col('is_it_category', true) = 'N' then begin
FKeyword := Results.Col('name');
break;
end;
Results.Next;
end;
FreeAndNil(Results);
VT := treeTopics;
if (not Assigned(VT.FocusedNode)) // No node selected
or VT.HasChildren[VT.FocusedNode] // Selected node is a folder, not a document
or (VT.Text[VT.FocusedNode, VT.Header.MainColumn] <> FKeyword) // Node is not the right one
then begin
// Start searching in initialized nodes, to minimize number of "HELP xyz" queries in certain cases
Node := VT.GetFirst;
SearchNoInit := False;
while Assigned(Node) do begin
if (not VT.HasChildren[Node]) and (UpperCase(VT.Text[Node, VT.Header.MainColumn]) = UpperCase(FKeyword)) then begin
SelectNode(VT, Node);
break;
end;
if not SearchNoInit then begin
Node := VT.GetNextInitialized(Node);
if not Assigned(Node) then begin
SearchNoInit := True;
Node := VT.GetFirst;
end;
end;
if SearchNoInit then
Node := VT.GetNext(Node);
end;
end;
end;
procedure TfrmSQLhelp.editFilterChange(Sender: TObject);
begin
timerSearch.Enabled := False;
timerSearch.Enabled := True;
editFilter.RightButton.Visible := Trim(editFilter.Text) <> '';
end;
procedure TfrmSQLhelp.editFilterRightButtonClick(Sender: TObject);
begin
editFilter.Clear;
end;
procedure TfrmSQLhelp.DoSearch(Sender: TObject);
var
Node: PVirtualNode;
Search: String;
Vis: Boolean;
function HasVisibleChildItems(Node: PVirtualNode): Boolean;
var
N: PVirtualNode;
begin
N := treeTopics.GetFirstChild(Node);
Result := False;
while Assigned(N) do begin
if treeTopics.HasChildren[N] then
Result := HasVisibleChildItems(N)
else
Result := treeTopics.IsVisible[N];
if Result then
Exit;
N := treeTopics.GetNextSibling(N);
end;
end;
begin
// Apply filter text
Screen.Cursor := crHourglass;
treeTopics.BeginUpdate;
timerSearch.Enabled := False;
Search := Trim(editFilter.Text);
if Search = '' then begin
// Show all items
Node := treeTopics.GetFirstInitialized;
while Assigned(Node) do begin
treeTopics.IsVisible[Node] := True;
Node := treeTopics.GetNextInitialized(Node);
end;
end else begin
// Hide non matching child items
Node := treeTopics.GetFirst;
while Assigned(Node) do begin
if not treeTopics.HasChildren[Node] then
treeTopics.IsVisible[Node] := Pos(UpperCase(Search), UpperCase(treeTopics.Text[Node, treeTopics.Header.MainColumn])) > 0;
Node := treeTopics.GetNext(Node);
end;
// Hide empty folders
Node := treeTopics.GetFirst;
while Assigned(Node) do begin
if treeTopics.HasChildren[Node] then begin
Vis := HasVisibleChildItems(Node);
treeTopics.Expanded[Node] := Vis;
treeTopics.IsVisible[Node] := Vis;
end;
Node := treeTopics.GetNext(Node);
end;
end;
treeTopics.EndUpdate;
Screen.Cursor := crDefault;
end;
end.