File: MyxRoutineEditorFrame.pas

package info (click to toggle)
mysql-gui-tools 5.0r14%2BopenSUSE-2.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 116,956 kB
  • ctags: 48,715
  • sloc: sql: 341,918; pascal: 276,698; ansic: 91,020; cpp: 90,451; objc: 33,236; sh: 29,481; yacc: 10,756; xml: 10,589; java: 10,079; php: 2,806; python: 2,092; makefile: 1,783; perl: 4
file content (504 lines) | stat: -rw-r--r-- 13,589 bytes parent folder | download | duplicates (4)
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
unit MyxRoutineEditorFrame;

interface

uses
  gnugettext, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UniCodeEditor, StdCtrls, TntStdCtrls, ExtCtrls, TntExtCtrls,
  Grt, UCEHighlighter, UCESQLHighlighter, ActnList, TntActnList, Menus,
  TntMenus, PngImage, PngTools, AuxFuncs;

type
  THeaderDragEvent = procedure(Sender: TObject; YOffset: Integer) of object;

  TRoutineFrame = class(TFrame)
    RoutinePanel: TTntPanel;
    RoutineHeaderPanel: TTntPanel;
    HeaderShape: TTntShape;
    RoutineLbl: TTntLabel;
    RoutineUce: TUniCodeEdit;
    UCESQLHighlighter: TUCESQLHighlighter;
    RoutinePopupMenu: TTntPopupMenu;
    TntActionList1: TTntActionList;
    DeleteRoutineAction: TTntAction;
    DeleteProcedureMI: TTntMenuItem;
    StateImg: TTntImage;
    SetBodyProcedureAction: TTntAction;
    ProcedureBodyMI: TTntMenuItem;
    N1: TTntMenuItem;
    SetBodyFunctionAction: TTntAction;
    FunctionBodyMI: TTntMenuItem;
    procedure DeleteRoutineActionExecute(Sender: TObject);
    procedure DeleteRoutineActionUpdate(Sender: TObject);
    procedure HeaderShapeMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure HeaderShapeMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure HeaderShapeMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure StateImgClick(Sender: TObject);
    procedure SetBodyFunctionActionExecute(Sender: TObject);
    procedure SetBodyProcedureActionExecute(Sender: TObject);
  private
    { Private declarations }
    FSchema: Pointer;
    FRoutine: Pointer;
    FOnChange: TNotifyEvent;
    FOnDelete: TNotifyEvent;
    FOnHeaderDragStart: TNotifyEvent;
    FOnHeaderDragStop: TNotifyEvent;
    FOnHeaderDrag: THeaderDragEvent;

    FDragStarted: Boolean;
    FDragYStart: Integer;

    FAutoCalcSize,
    FExpanded: Boolean;
    FExpandedHeight: Integer;

    FCollapsedPngImg,
    FExpandedPngImg: TPngObject;
  protected
    function GetGrt: TGrt;
    procedure SetRoutine(Routine: Pointer); virtual;
    procedure DoSqlUceChange(Sender: TObject; Line: TUCELine);  virtual;

    procedure SetAutoCalcSize(AutoCalcSize: Boolean);
    procedure SetExpanded(Expanded: Boolean);

    procedure Changed;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;

    property Schema: Pointer read FSchema write FSchema;
    property Routine: Pointer read FRoutine write SetRoutine;
    property Grt: TGrt read GetGrt;

    property OnChange: TNotifyEvent write FOnChange;
    property OnDelete: TNotifyEvent write FOnDelete;

    property OnHeaderDragStart: TNotifyEvent write FOnHeaderDragStart;
    property OnHeaderDragStop: TNotifyEvent write FOnHeaderDragStop;
    property OnHeaderDrag: THeaderDragEvent write FOnHeaderDrag;

    property AutoCalcSize: Boolean read FAutoCalcSize write SetAutoCalcSize;
    property Expanded: Boolean read FExpanded write SetExpanded;
    property ExpandedHeight: Integer read FExpandedHeight write FExpandedHeight;

    procedure ApplyChanges;
  end;

  TTriggerFrame = class(TRoutineFrame)
    procedure SetRoutine(Routine: Pointer); override;
    procedure ApplyChanges;
    procedure DoSqlUceChange(Sender: TObject; Line: TUCELine); override;
  end;

implementation

{$R *.dfm}


// -----------------------------------------------------------------------------

constructor TRoutineFrame.Create(AOwner: TComponent);

begin
  inherited Create(AOwner);

  FRoutine := nil;
  FSchema := nil;
  FOnChange := nil;
  FOnDelete := nil;

  FDragStarted := False;

  FAutoCalcSize := False;
  FExpanded := True;

  RoutineUce.Content.OnChangeLine := DoSqlUceChange;

  FCollapsedPngImg := LoadPNGImageFromResource('source_collapsed');
  FExpandedPngImg := LoadPNGImageFromResource('source_expanded');

  StateImg.Picture.Assign(FExpandedPngImg);
end;

// -----------------------------------------------------------------------------

function TRoutineFrame.GetGrt: TGrt;

begin
  Result := RuntimeEnvironment;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.SetRoutine(Routine: Pointer);

begin
  FRoutine := Routine;

  if (FRoutine <> nil) then
  begin
    RoutineLbl.Caption := Grt.DictString[FRoutine, 'name'];

    RoutineUce.Content.Text :=
      Grt.DictString[FRoutine, 'routineCode'];
  end;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.ApplyChanges;

var
  Val: Pointer;
  Name: WideString;
  Sql: WideString;
  Task: IGrtTask;

begin
  if (FRoutine = nil) then
    Exit;

  Sql := Trim(RoutineUce.Content.Text);

  Task := Grt.CreateStandardTask(_('Getting a routine''s name'), 'DbUtils', 'getRoutineName', [Sql], nil, nil, True);
  Grt.AddTaskAndWait(Task, True);
  Val := Task.Result;

  if (Val <> nil) then
  begin
    Name := Grt.ValueString[Val];

    if (Name <> '') then
    begin
      Grt.DictString[FRoutine, 'name'] := Name;
      RoutineLbl.Caption := Name;
    end;

    Grt.ValueRelease(Val);
  end;

  Task := Grt.CreateStandardTask(_('Getting a routine''s type'), 'DbUtils', 'getRoutineType', [Sql], nil, nil, True);
  Grt.AddTaskAndWait(Task, True);
  Val := Task.Result;

  if (Val <> nil) then
  begin
    Name := Grt.ValueString[Val];
    Grt.DictString[FRoutine, 'routineType'] := WideUpperCase(Name);

    Grt.ValueRelease(Val);
  end;

  Grt.DictString[FRoutine, 'routineCode'] := Sql;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.DoSqlUceChange(Sender: TObject; Line: TUCELine);

begin
  if FRoutine = nil then
    FRoutine := Grt.ExecuteStandardTask(_('Adding new routine'), 'Workbench', 'addRoutine', [Schema, 'New_Routine'], True);

  Changed;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.Changed;

begin
  if (Assigned(FOnChange)) then
    FOnChange(self);
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.DeleteRoutineActionExecute(Sender: TObject);

begin
  if (Assigned(FOnDelete)) then
    FOnDelete(self);
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.DeleteRoutineActionUpdate(Sender: TObject);

begin
  if (Sender is TTntAction) then
    TTntAction(Sender).Enabled := (FRoutine <> nil);
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.HeaderShapeMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin
  if (FExpanded) then
  begin
    FDragStarted := True;
    FDragYStart := Mouse.CursorPos.Y;

    if (Assigned(FOnHeaderDragStart)) then
      FOnHeaderDragStart(self);
  end;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.HeaderShapeMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);

begin
  if FDragStarted and Assigned(FOnHeaderDrag) then
    FOnHeaderDrag(self, Mouse.CursorPos.Y - FDragYStart);
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.HeaderShapeMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin
  if (FDragStarted) then
  begin
    FDragStarted := False;

    if (Assigned(FOnHeaderDragStop)) then
      FOnHeaderDragStop(self);

    if (Mouse.CursorPos.Y - FDragYStart <> 0) then
     Changed;
  end;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.SetAutoCalcSize(AutoCalcSize: Boolean);

begin

end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.SetExpanded(Expanded: Boolean);

begin
  if (FExpanded <> Expanded) then
  begin
    FExpanded := Expanded;

    if (FExpanded) then
      StateImg.Picture.Assign(FExpandedPngImg)
    else
      StateImg.Picture.Assign(FCollapsedPngImg);

    if (Not(Expanded)) then
    begin
      StateImg.Picture.Assign(FCollapsedPngImg);
      FExpandedHeight := Height;
      Height := 16;
    end
    else
    begin
      StateImg.Picture.Assign(FExpandedPngImg);
      Height := FExpandedHeight;
    end;
  end;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.StateImgClick(Sender: TObject);

begin
  Expanded := Not(Expanded);

  Changed;
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.SetBodyFunctionActionExecute(Sender: TObject);

begin
  if (Trim(RoutineUce.Content.Text) <> '') then
    if (ShowModalDialog(_('Replace Text'),
      _('Are you sure you want to replace the current routine text?'),
      myx_mtConfirmation, _('Replace') + #13#10 + _('Cancel')) <> 1) then
      Exit;

  RoutineUce.Content.Text :=
    '-- Comment' + #13#10 +
    '-- ' + #13#10#13#10 +
    'CREATE FUNCTION test() RETURNS INT' + #13#10 +
    'BEGIN' + #13#10 +
    '  RETURN 0;' + #13#10 +
    'END';
end;

// -----------------------------------------------------------------------------

procedure TRoutineFrame.SetBodyProcedureActionExecute(Sender: TObject);

begin
  if (Trim(RoutineUce.Content.Text) <> '') then
    if (ShowModalDialog(_('Replace Text'),
      _('Are you sure you want to replace the current routine text?'),
      myx_mtConfirmation, _('Replace') + #13#10 + _('Cancel')) <> 1) then
      Exit;

  RoutineUce.Content.Text :=
    '-- Comment' + #13#10 +
    '-- ' + #13#10#13#10 +
    'CREATE PROCEDURE test()' + #13#10 +
    'BEGIN' + #13#10#13#10 +
    'END';
end;

// -----------------------------------------------------------------------------

procedure TTriggerFrame.SetRoutine(Routine: Pointer);

var
  Table: Pointer;
  Stmt: WideString;

begin
  FRoutine := Routine;

  if (FRoutine <> nil) then
  begin
    RoutineLbl.Caption := Grt.DictString[FRoutine, 'name'];
    Table := Grt.DictRef[FRoutine, 'owner'];
    Stmt := Grt.DictString[FRoutine, 'statement'];

    RoutineUce.Content.Text :=
      'CREATE TRIGGER ' +
      Grt.DictString[FRoutine, 'name'] + ' ' +
      Grt.DictString[FRoutine, 'timing'] + ' ' +
      Grt.DictString[FRoutine, 'event'] + ' ON ' +
      Grt.DictString[Table, 'name'] + ' FOR EACH ROW' + #13 + #10 +
      Stmt;
  end
  else
    RoutineLbl.Caption := 'New_Trigger';
end;

// -----------------------------------------------------------------------------

function RemoveChar(Str : WideString; c: WideChar) : WideString;

var
  R: WideString;
  I: Integer;

begin
  for I := 1 to Length(Str) do
    if Str[i] <> c then
      R := R + Str[i];
  Result := R;    
end;

// -----------------------------------------------------------------------------

procedure TTriggerFrame.ApplyChanges;

var
  Val: Pointer;
  Name: WideString;
  Sql: WideString;
  Task: IGrtTask;

begin
  if (FRoutine = nil) then
    Exit;

  Sql := Trim(RoutineUce.Content.Text);

  Task := Grt.CreateStandardTask(_('Getting a trigger''s name'), 'DbUtils', 'getRoutineName', [Sql], nil, nil, True);
  Grt.AddTaskAndWait(Task, True);
  Val := Task.Result;

  if (Val <> nil) then
  begin
    Name := Grt.ValueString[Val];

    if (Name <> '') then
    begin
      Grt.DictString[FRoutine, 'name'] := Name;
      RoutineLbl.Caption := Name;
    end;

    Grt.ValueRelease(Val);
  end;

  Task := Grt.CreateStandardTask(_('Getting a trigger''s statement'), 'DbUtils', 'getTriggerStatement', [Sql], nil, nil, True);
  Grt.AddTaskAndWait(Task, True);
  Val := Task.Result;

  if (Val <> nil) then
  begin
    Name := Grt.ValueString[Val];
    Name := RemoveChar(Name, #13);
    Grt.DictString[FRoutine, 'statement'] := Name;

    Grt.ValueRelease(Val);
  end;

  Task := Grt.CreateStandardTask(_('Getting a trigger''s event'), 'DbUtils', 'getTriggerEvent', [Sql], nil, nil, True);
  Grt.AddTaskAndWait(Task, True);
  Val := Task.Result;

  if (Val <> nil) then
  begin
    Name := Grt.ValueString[Val];
    Grt.DictString[FRoutine, 'event'] := Name;

    Grt.ValueRelease(Val);
  end;

  Task := Grt.CreateStandardTask(_('Getting a trigger''s timing'), 'DbUtils', 'getTriggerTiming', [Sql], nil, nil, True);
  Grt.AddTaskAndWait(Task, True);
  Val := Task.Result;

  if (Val <> nil) then
  begin
    Name := Grt.ValueString[Val];
    Grt.DictString[FRoutine, 'timing'] := Name;

    Grt.ValueRelease(Val);
  end;

end;

// -----------------------------------------------------------------------------

procedure TTriggerFrame.DoSqlUceChange(Sender: TObject; Line: TUCELine);

var
  s: widestring;

begin
  if FRoutine = nil then
  begin
    s := Grt.DictStructName[Schema];
    s := Grt.DictString[Schema, 'name'];
    FRoutine := Grt.ExecuteStandardTask(_('Adding new trigger'), 'Workbench', 'createTrigger', [Schema, 'New_Trigger'],
      True);
  end;

  Changed;
end;

// -----------------------------------------------------------------------------

end.