My favorites | Sign in
Project Home 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
unit routine_editor;

interface

uses
Windows, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, SynEdit, SynMemo, StdCtrls,
ComCtrls, ToolWin, VirtualTrees,
dbconnection, helpers;

type
TFrame = TDBObjectEditor;
TfrmRoutineEditor = class(TFrame)
btnSave: TButton;
btnDiscard: TButton;
btnHelp: TButton;
lblSQLcode: TLabel;
SynMemoBody: TSynMemo;
PageControlMain: TPageControl;
tabOptions: TTabSheet;
tabParameters: TTabSheet;
tabCreateCode: TTabSheet;
chkDeterministic: TCheckBox;
editComment: TEdit;
comboSecurity: TComboBox;
comboDataAccess: TComboBox;
comboReturns: TComboBox;
comboType: TComboBox;
editName: TEdit;
lblName: TLabel;
lblType: TLabel;
lblReturns: TLabel;
lblSQL: TLabel;
lblSecurity: TLabel;
lblComment: TLabel;
listParameters: TVirtualStringTree;
tlbParameters: TToolBar;
btnAddParam: TToolButton;
btnRemoveParam: TToolButton;
btnClearParams: TToolButton;
SynMemoCREATEcode: TSynMemo;
btnRunProc: TButton;
lblDefiner: TLabel;
comboDefiner: TComboBox;
btnMoveUpParam: TToolButton;
btnMoveDownParam: TToolButton;
lblDisabledWhy: TLabel;
procedure comboTypeSelect(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure btnHelpClick(Sender: TObject);
procedure btnAddParamClick(Sender: TObject);
procedure listParametersGetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: String);
procedure listParametersGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: Integer);
procedure btnClearParamsClick(Sender: TObject);
procedure btnRemoveParamClick(Sender: TObject);
procedure listParametersBeforePaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas);
procedure listParametersFocusChanged(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex);
procedure listParametersCreateEditor(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; out EditLink: IVTEditLink);
procedure listParametersNewText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; NewText: String);
procedure listParametersEditing(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; var Allowed: Boolean);
procedure Modification(Sender: TObject);
procedure SynMemoBodyDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure SynMemoBodyDragDrop(Sender, Source: TObject; X, Y: Integer);
procedure listParametersPaintText(Sender: TBaseVirtualTree;
const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType);
procedure btnDiscardClick(Sender: TObject);
procedure comboDefinerDropDown(Sender: TObject);
procedure btnMoveParamClick(Sender: TObject);
private
{ Private declarations }
FAlterRoutineType: String;
function ComposeCreateStatement(NameOfObject: String): String;
public
{ Public declarations }
Parameters: TRoutineParamList;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Init(Obj: TDBObject); override;
function ApplyModifications: TModalResult; override;
end;


implementation

uses main, mysql_structures, grideditlinks;

{$R *.dfm}


constructor TfrmRoutineEditor.Create(AOwner: TComponent);
begin
inherited;
ScaleControls(Screen.PixelsPerInch, FORMS_DPI);
// Combo items in a .dfm are sporadically lost after an IDE restart,
// so we set them here to avoid developer annoyance
comboType.Items.Add('Procedure (doesn''t return a result)');
comboType.Items.Add('Function (returns a result)');
comboDataAccess.Items.Add('Contains SQL');
comboDataAccess.Items.Add('No SQL');
comboDataAccess.Items.Add('Reads SQL data');
comboDataAccess.Items.Add('Modifies SQL data');
comboSecurity.Items.Add('Definer');
comboSecurity.Items.Add('Invoker');
Mainform.SynCompletionProposal.AddEditor(SynMemoBody);
FixVT(listParameters);
Mainform.RestoreListSetup(listParameters);
Parameters := TRoutineParamList.Create;
editName.MaxLength := NAME_LEN;
end;


destructor TfrmRoutineEditor.Destroy;
begin
// Store GUI setup
OpenRegistry;
Mainform.SaveListSetup(listParameters);
inherited;
end;


procedure TfrmRoutineEditor.Init(Obj: TDBObject);
var
Definer, Returns, DataAccess, Security, Comment, Body: String;
Deterministic: Boolean;
i: Integer;
begin
inherited;
if Obj.NodeType = lntProcedure then FAlterRoutineType := 'PROCEDURE'
else FAlterRoutineType := 'FUNCTION';
editName.Text := DBObject.Name;
comboType.ItemIndex := 0;
comboReturns.Text := '';
comboReturns.Clear;
for i:=0 to High(Obj.Connection.Datatypes) do
comboReturns.Items.Add(Obj.Connection.Datatypes[i].Name);
chkDeterministic.Checked := False;
SelectNode(listParameters, nil);
listParameters.Clear;
Parameters.Clear;
comboDataAccess.ItemIndex := 0;
comboSecurity.ItemIndex := 0;
editComment.Clear;
comboDefiner.Text := '';
comboDefiner.TextHint := 'Current user ('+Obj.Connection.CurrentUserHostCombination+')';
comboDefiner.Hint := 'Leave empty for current user ('+Obj.Connection.CurrentUserHostCombination+')';
SynMemoBody.Text := 'BEGIN'+CRLF+CRLF+'END';
if DBObject.Name <> '' then begin
// Editing existing routine
comboType.ItemIndex := ListIndexByRegExpr(comboType.Items, '^'+FAlterRoutineType+'\b');
DBObject.Connection.ParseRoutineStructure(Obj.CreateCode, Parameters, Deterministic, Definer, Returns, DataAccess, Security, Comment, Body);
comboReturns.Text := Returns;
chkDeterministic.Checked := Deterministic;
if DataAccess <> '' then
comboDataAccess.ItemIndex := comboDataAccess.Items.IndexOf(DataAccess);
if Security <> '' then
comboSecurity.ItemIndex := comboSecurity.Items.IndexOf(Security);
editComment.Text := Comment;
comboDefiner.Text := Definer;
SynMemoBody.Text := Body;
// User may not be allowed to view code in SHOW CREATE FUNCTION, in which case we have an empty CreateCode.
// Disable editor in this case.
lblDisabledWhy.Visible := Body = '';
PageControlMain.Enabled := not lblDisabledWhy.Visible;
SynMemoBody.Enabled := PageControlMain.Enabled;
end else begin
editName.Text := '';
end;
comboTypeSelect(comboType);
Modified := False;
btnSave.Enabled := Modified;
btnDiscard.Enabled := Modified;
Mainform.actRunRoutines.Enabled := DBObject.Name <> '';
Mainform.ShowStatusMsg;
Screen.Cursor := crDefault;
end;


procedure TfrmRoutineEditor.Modification(Sender: TObject);
begin
Modified := True;
btnSave.Enabled := Modified and (editName.Text <> '');
btnDiscard.Enabled := Modified;
SynMemoCreateCode.Text := ComposeCreateStatement(editName.Text);
end;


procedure TfrmRoutineEditor.comboTypeSelect(Sender: TObject);
var
isfunc: Boolean;
begin
isfunc := (Sender as TComboBox).ItemIndex = 1;
lblReturns.Enabled := isfunc;
comboReturns.Enabled := isfunc;
Modification(Sender);
listParameters.Repaint;
end;


procedure TfrmRoutineEditor.comboDefinerDropDown(Sender: TObject);
begin
// Populate definers from mysql.user
(Sender as TComboBox).Items.Assign(GetDefiners);
end;


procedure TfrmRoutineEditor.btnAddParamClick(Sender: TObject);
var
Param: TRoutineParam;
begin
Param := TRoutineParam.Create;
Param.Name := 'Param'+IntToStr(Parameters.Count+1);
Param.Datatype := 'INT';
Param.Context := 'IN';
Parameters.Add(Param);
// See List.OnPaint:
listParameters.Repaint;
Modification(Sender);
end;


procedure TfrmRoutineEditor.btnRemoveParamClick(Sender: TObject);
begin
Parameters.Delete(ListParameters.FocusedNode.Index);
listParameters.Repaint;
Modification(Sender);
end;


procedure TfrmRoutineEditor.btnClearParamsClick(Sender: TObject);
begin
Parameters.Clear;
listParameters.Repaint;
Modification(Sender);
end;


procedure TfrmRoutineEditor.btnMoveParamClick(Sender: TObject);
var
Source, Target: Integer;
begin
// Move param up or down
Source := ListParameters.FocusedNode.Index;
if Sender = btnMoveUpParam then
Target := Source -1
else
Target := Source +1;
Parameters.Exchange(Source, Target);
SelectNode(listParameters, Target);
listParameters.Repaint;
Modification(Sender);
end;


procedure TfrmRoutineEditor.listParametersBeforePaint(Sender: TBaseVirtualTree;
TargetCanvas: TCanvas);
begin
(Sender as TVirtualStringTree).RootNodeCount := Parameters.Count;
end;


procedure TfrmRoutineEditor.listParametersGetImageIndex(Sender: TBaseVirtualTree;
Node: PVirtualNode; Kind: TVTImageKind; Column: TColumnIndex;
var Ghosted: Boolean; var ImageIndex: Integer);
var
List: TVirtualStringTree;
Context: String;
begin
// Draw arrow icon to indicate in/out context
if not (Kind in [ikNormal, ikSelected]) then Exit;
List := Sender as TVirtualStringTree;
if Column <> 3 then
ImageIndex := -1
else begin
Context := List.Text[Node, 3];
if Context = 'IN' then ImageIndex := 120
else if Context = 'OUT' then ImageIndex := 121
else if Context = 'INOUT' then ImageIndex := 122;
end;
end;


procedure TfrmRoutineEditor.listParametersGetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: String);
var
Param: TRoutineParam;
begin
Param := Parameters[Node.Index];
case Column of
0: CellText := IntToStr(Node.Index+1);
1: CellText := Param.Name;
2: CellText := Param.Datatype;
3: begin
if comboType.ItemIndex = 1 then
CellText := 'IN' // A function can only have IN parameters
else
CellText := Param.Context;
end;
end;
end;


procedure TfrmRoutineEditor.listParametersPaintText(Sender: TBaseVirtualTree;
const TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex;
TextType: TVSTTextType);
begin
if (Column = 3) and (comboType.ItemIndex = 1) then
TargetCanvas.Font.Color := clBtnShadow;
end;


procedure TfrmRoutineEditor.listParametersFocusChanged(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex);
var
HasNode: Boolean;
begin
// Enable/disable buttons
HasNode := Assigned(Node);
btnRemoveParam.Enabled := HasNode;
btnMoveUpParam.Enabled := HasNode and (Node <> Sender.GetFirst);
btnMoveDownParam.Enabled := HasNode and (Node <> Sender.GetLast);

if HasNode and (not ((comboType.ItemIndex = 1) and (Column=3))) then
Sender.EditNode(Node, Column);
end;


procedure TfrmRoutineEditor.listParametersNewText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; NewText: String);
var
Param: TRoutineParam;
begin
Param := Parameters[Node.Index];
case Column of
1: Param.Name := NewText;
2: Param.Datatype := NewText;
3: Param.Context := NewText;
end;
Modification(Sender);
end;


procedure TfrmRoutineEditor.listParametersCreateEditor(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; out EditLink: IVTEditLink);
var
VT: TVirtualStringTree;
EnumEditor: TEnumEditorLink;
Datatype: String;
DBDatatype: TDBDatatype;
begin
VT := Sender as TVirtualStringTree;
if Column = 1 then
EditLink := TStringEditLink.Create
else if Column = 2 then begin
EnumEditor := TEnumEditorLink.Create(VT);
EnumEditor.Connection := DBObject.Connection;
EnumEditor.AllowCustomText := True;
EnumEditor.ValueList := TStringList.Create;
for DBDatatype in DBObject.Connection.Datatypes do begin
Datatype := DBDatatype.Name;
if DBDatatype.RequiresLength then
Datatype := Datatype + '(' + DBDatatype.DefLengthSet + ')';
EnumEditor.ValueList.Add(Datatype);
end;
EditLink := EnumEditor;
end else if Column = 3 then begin
EnumEditor := TEnumEditorLink.Create(VT);
EnumEditor.Connection := DBObject.Connection;
EnumEditor.ValueList := TStringList.Create;
EnumEditor.ValueList.Add('IN');
EnumEditor.ValueList.Add('OUT');
EnumEditor.ValueList.Add('INOUT');
EditLink := EnumEditor;
end;
end;


procedure TfrmRoutineEditor.listParametersEditing(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; var Allowed: Boolean);
begin
// Do not allow the number cells to be edited
Allowed := Column > 0;
if (Column = 3) and (comboType.ItemIndex = 1) then begin
Allowed := False;
MessageDialog('A stored function can only have IN parameters so context editing is blocked.', mtInformation, [mbOK]);
end;
end;


procedure TfrmRoutineEditor.SynMemoBodyDragOver(Sender, Source: TObject; X,
Y: Integer; State: TDragState; var Accept: Boolean);
begin
// Allow dragging parameters here
Accept := Source = listParameters;
// set cursor position
SynMemoBody.CaretX := (x - SynMemoBody.Gutter.Width) div SynMemoBody.CharWidth - 1 + SynMemoBody.LeftChar;
SynMemoBody.CaretY := y div SynMemoBody.LineHeight + SynMemoBody.TopLine;
if not SynMemoBody.Focused then
SynMemoBody.SetFocus;
end;


procedure TfrmRoutineEditor.SynMemoBodyDragDrop(Sender, Source: TObject; X,
Y: Integer);
var
list: TVirtualStringTree;
memo: TSynMemo;
begin
list := Source as TVirtualStringTree;
memo := Sender as TSynMemo;
memo.SelText := list.Text[list.FocusedNode, 1];
end;


procedure TfrmRoutineEditor.btnSaveClick(Sender: TObject);
begin
// Apply or OK button clicked
ApplyModifications;
end;


function TfrmRoutineEditor.ApplyModifications: TModalResult;
var
TempName: String;
i: Integer;
allRoutineNames: TStringList;
ProcOrFunc: String;
TargetExists: Boolean;
begin
// Save changes
Result := mrOk;
ProcOrFunc := UpperCase(GetFirstWord(comboType.Text));

// There is no way to ALTER parameters or the name of it.
// Create a temp routine, check for syntax errors, then drop the old routine and create it.
// See also: http://dev.mysql.com/doc/refman/5.0/en/alter-procedure.html
try
if DBObject.Name <> '' then begin
// Create temp name
i := 0;
allRoutineNames := DBObject.Connection.GetCol('SELECT ROUTINE_NAME FROM '+DBObject.Connection.QuoteIdent('information_schema')+'.'+DBObject.Connection.QuoteIdent('ROUTINES')+
' WHERE ROUTINE_SCHEMA = '+esc(Mainform.ActiveDatabase)+
' AND ROUTINE_TYPE = '+esc(ProcOrFunc)
);
TargetExists := ((editName.Text <> DBObject.Name) or (ProcOrFunc <> FAlterRoutineType)) and
(allRoutineNames.IndexOf(editName.Text) > -1);
if TargetExists then begin
Result := MessageDialog('Overwrite?', 'Routine "'+editName.Text+'" already exists.',
mtConfirmation, [mbYes, mbNo, mbCancel]);
if Result = mrNo then
Exit;
end;
while True do begin
inc(i);
TempName := APPNAME + '_temproutine_' + IntToStr(i);
if allRoutineNames.IndexOf(TempName) = -1 then
break;
end;
DBObject.Connection.Query(ComposeCreateStatement(tempName));
// Drop temporary routine, used for syntax checking
DBObject.Connection.Query('DROP '+ProcOrFunc+' IF EXISTS '+DBObject.Connection.QuoteIdent(TempName));
// Drop edited routine
DBObject.Connection.Query('DROP '+FAlterRoutineType+' IF EXISTS '+DBObject.Connection.QuoteIdent(DBObject.Name));
if TargetExists then begin
// Drop target routine - overwriting has been confirmed, see above
DBObject.Connection.Query('DROP '+ProcOrFunc+' IF EXISTS '+DBObject.Connection.QuoteIdent(editName.Text));
end;
end;
DBObject.Connection.Query(ComposeCreateStatement(editName.Text));
// Set editing name if create/alter query was successful
DBObject.Name := editName.Text;
DBObject.CreateCode := '';
FAlterRoutineType := UpperCase(GetFirstWord(comboType.Text));
if FAlterRoutineType = 'PROCEDURE' then DBObject.NodeType := lntProcedure
else DBObject.NodeType := lntFunction;
Mainform.UpdateEditorTab;
Mainform.RefreshTree(DBObject);
Modified := False;
btnSave.Enabled := Modified;
btnDiscard.Enabled := Modified;
Mainform.actRunRoutines.Enabled := True;
except
on E:EDatabaseError do begin
ErrorDialog(E.Message);
Result := mrAbort;
end;
end;
end;


function TfrmRoutineEditor.ComposeCreateStatement(NameOfObject: String): String;
var
ProcOrFunc: String;
i: Integer;
begin
ProcOrFunc := UpperCase(GetFirstWord(comboType.Text));
Result := 'CREATE ';
if comboDefiner.Text <> '' then
Result := Result + 'DEFINER='+DBObject.Connection.QuoteIdent(comboDefiner.Text, True, '@')+' ';
Result := Result + ProcOrFunc+' '+DBObject.Connection.QuoteIdent(NameOfObject)+'(';
for i:=0 to Parameters.Count-1 do begin
if ProcOrFunc = 'PROCEDURE' then
Result := Result + Parameters[i].Context + ' ';
Result := Result + DBObject.Connection.QuoteIdent(Parameters[i].Name) + ' ' + Parameters[i].Datatype;
if i < Parameters.Count-1 then
Result := Result + ', ';
end;
Result := Result + ')'+CRLF;
if comboReturns.Enabled then
Result := Result + #9 + 'RETURNS '+comboReturns.Text+CRLF;
Result := Result + #9 + 'LANGUAGE SQL'+CRLF + #9;
if not chkDeterministic.Checked then
Result := Result + 'NOT ';
Result := Result + 'DETERMINISTIC'+CRLF
+ #9 + UpperCase(comboDataAccess.Text)+CRLF
+ #9 + 'SQL SECURITY ' + UpperCase(comboSecurity.Text)+CRLF
+ #9 + 'COMMENT ' + esc(editComment.Text)+CRLF
+ SynMemoBody.Text;
end;


procedure TfrmRoutineEditor.btnDiscardClick(Sender: TObject);
begin
Modified := False;
Init(DBObject);
end;


procedure TfrmRoutineEditor.btnHelpClick(Sender: TObject);
begin
// Help button
Mainform.CallSQLHelpWithKeyword('CREATE PROCEDURE');
end;


end.

Change log

r4137 by ansgar.becker on May 1, 2012   Diff
Remove unused unit inclusions, detected by
CNPack/Uses cleaner
Go to: 

Older revisions

r4093 by ansgar.becker on Mar 13, 2012   Diff
SelectNode triggers a potentially set
OnFocusChanged event, benefit from
that logic in some places where I used
to assign Focusnode := xyz.
r4082 by ansgar.becker on Mar 7, 2012   Diff
Disable editing routines if user has
no privilege to select from mysql.proc
or if he's not the owner of that
routine. See http://www.heidisql.com/f
orum.php?t=10231
r3852 by ansgar.becker on May 28, 2011   Diff
Accidentally forgotten files for r3849
All revisions of this file

File info

Size: 18393 bytes, 546 lines
Powered by Google Project Hosting