File: SqlCmdPanel.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 (384 lines) | stat: -rw-r--r-- 10,740 bytes parent folder | download
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
unit SqlCmdPanel;

interface

uses
  gnugettext, Windows, Messages, SysUtils, Classes, Controls,
  Graphics,
  TntExtCtrls, TntComCtrls, TntClasses, TntStdCtrls, Forms,
  UniCodeEditor, UniCodeConsole, VirtualTrees, TableDrag,
  MySQLConnection, ApplicationDataModule,
  myx_public_interface;

type
  TSQLCmdTabSheet = class(TTntPanel)
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure CommandLineEdKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure CommandLineEdDragDrop(Sender, Source: TObject;
      X, Y: Integer);
    procedure CommandLineEdDragOver(Sender, Source: TObject;
      X, Y: Integer; State: TDragState; var Accept: Boolean);
    procedure SQLMemoEnter(Sender: TObject);
    procedure CommandLineEdExecuteCommand(Cmd: WideString;
      Key: Word; Shift: TShiftState);
    function CommandLineEdIsMultilineCommand(Cmd: WideString;
      Key: Word; Shift: TShiftState): Boolean;
    procedure InitializeShell;
  public
    CommandLineEd: TUniCodeConsole;
    MySQLConn: TMySQLConn;
    TableDragForm: TTableDragForm;
    PTxShell: PMYX_TEXT_SHELL;
  end;

function TxShellOutput(text: PChar; user_data: Pointer): integer cdecl;

implementation

uses QueryBrowser;

var
  ShellOutputBuffer: WideString;
  GlobalConsoleBreak: Boolean;

function TxShellOutput(text: PChar; user_data: Pointer): integer cdecl;

var
  PSender: ^TUniCodeEdit;
  Output: WideString;

begin
  PSender := user_data;

  Output := UTF8Decode(text);
  if (Copy(Output, Length(Output), 1) <> #10) then
    ShellOutputBuffer := ShellOutputBuffer + Output
  else
  begin
    if (ShellOutputBuffer <> '') then
    begin
      PSender.Content.Text := ShellOutputBuffer + Output;
      ShellOutputBuffer := '';
    end
    else
      PSender.Content.Text := Output;
  end;

  Result := Ord(GlobalConsoleBreak);
  GlobalConsoleBreak := False;

  Application.ProcessMessages;
end;

constructor TSQLCmdTabSheet.Create(AOwner: TComponent);
begin
  CommandLineEd := TUniCodeConsole.Create(self);
  CommandLineEd.Parent := self;
  CommandLineEd.Align := alClient;
  CommandLineEd.Name := 'CommandLineEd';
  CommandLineEd.OnDragDrop := CommandLineEdDragDrop;
  CommandLineEd.OnDragOver := CommandLineEdDragOver;
  CommandLineEd.OnEnter := SQLMemoEnter;
  CommandLineEd.OnExecuteCommand := CommandLineEdExecuteCommand;
  CommandLineEd.OnIsMultilineCommand := CommandLineEdIsMultilineCommand;
  CommandLineEd.OnKeyDown := CommandLineEdKeyDown;

  CommandLineEd.Color := clBlack;
  CommandLineEd.CharWidth := 6;
  CommandLineEd.ConsoleDelimiter := ';';
  CommandLineEd.ConsolePrompt := '>';
  CommandLineEd.Cursor := crIBeam;
  CommandLineEd.Font.Name := 'Bitstream Vera Sans Mono';
  CommandLineEd.Font.Color := clSilver;
  CommandLineEd.GutterWidth := 0;
  CommandLineEd.InsertCaret := ctHalfBlock;
  CommandLineEd.MaxUndo := 32000;
  CommandLineEd.Options := CommandLineEd.Options - [eoAutoIndent,
    eoAutoUnindent, eoHideSelection, eoScrollPastEOL];
  CommandLineEd.Options := CommandLineEd.Options + [eoKeepTrailingBlanks];

  InitializeShell;

  ShellOutputBuffer := '';

  GlobalConsoleBreak := False;

  TableDragForm := nil;
  PTxShell := nil;
end;

destructor TSQLCmdTabSheet.Destroy;
begin
  if (PTxShell <> nil) then
    myx_finalize_text_shell(PTxShell);
end;

procedure TSQLCmdTabSheet.CommandLineEdKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  Cmd: WideString;
begin
  if (Key = VK_TAB) then
  begin
    cmd := Copy(CommandLineEd.Content[CommandLineEd.CaretY].Text, 8,
      Length(CommandLineEd.Content[CommandLineEd.CaretY].Text));

    if (CompareText(cmd, Copy('select', 1, Length(cmd))) = 0) then
      CommandLineEd.Content[CommandLineEd.CaretY].Text := CommandLineEd.ConsolePrompt + 'select ';

    CommandLineEd.CaretX := Length(CommandLineEd.Content[CommandLineEd.CaretY].Text);

    Key := 0;
  end
  else
    if (Key = Ord('C')) and (ssCtrl in Shift) then
    begin
      GlobalConsoleBreak := True;
    end;
end;

procedure TSQLCmdTabSheet.CommandLineEdDragDrop(Sender, Source: TObject;
  X, Y: Integer);
var
  Node: PVirtualNode;
  BookmarkNodeData: PBookmarkNodeData;
  HistoryNodeData: PHistoryNodeData;
  NewSelStart: Integer;
begin
  if (Source is TUniCodeEdit) then
  begin
    CommandLineEd.ConsoleCommand := TUniCodeEdit(Source).Text;
  end
  else
    if (Source is TVirtualStringTree) then
    begin
      if (TVirtualStringTree(Source).Name = 'BookmarkVT') then
      begin
        Node := TVirtualStringTree(Source).FocusedNode;
        if (Node <> nil) then
        begin
          BookmarkNodeData := TVirtualStringTree(Source).GetNodeData(Node);
          if (BookmarkNodeData.bookmark <> nil) then
            CommandLineEd.ConsoleCommand := BookmarkNodeData.bookmark.sql;
        end;
      end
      else
        if (TVirtualStringTree(Source).Name = 'HistoryVT') then
        begin
          Node := TVirtualStringTree(Source).FocusedNode;
          if (Node <> nil) then
          begin
            HistoryNodeData := TVirtualStringTree(Source).GetNodeData(Node);
            if (HistoryNodeData.NodeType = HISTORY_ENTRY_TYPE) then
              CommandLineEd.ConsoleCommand := Trim(PPMYX_HISTORY_ENTRY(HistoryNodeData.Data)^.sql);
          end;
        end
        else
          if (TVirtualStringTree(Source).Name = 'CatalogVST') then
          begin
            CommandLineEd.ConsoleCommandExcludeDelim :=
              BuildDragSQLCommand(MySQLConn,
              TVirtualStringTree(Source),
              CommandLineEd.ConsoleCommandExcludeDelim, NewSelStart);
            CommandLineEd.ConsoleCommandSelStart := NewSelStart;

            if (CommandLineEd.CanFocus) then
              CommandLineEd.SetFocus;
          end;
    end;

  CommandLineEd.Invalidate;

  CommandLineEd.SetFocus;
end;

procedure TSQLCmdTabSheet.CommandLineEdDragOver(Sender, Source: TObject;
  X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  if (Source is TUniCodeEdit) or
    ((Source is TVirtualStringTree) and
    ((TVirtualStringTree(Source).Name = 'HistoryVT') or
    (TVirtualStringTree(Source).Name = 'BookmarkVT') or
    (TVirtualStringTree(Source).Name = 'CatalogVST'))) then
    Accept := True;

  if (GetKeyState(VK_MENU) < 0) or
    (not (ApplicationDM.QBOptions.ShowDragTargetWindowOnAltPressedOnly)) then
  begin
    //ConsoleDropTargetActionBarPaintBox.Visible:=True;

    if (TableDragForm = nil) then
    begin
      TableDragForm := TTableDragForm.Create(nil);
      TableDragForm.PlaceFormBelow(CommandLineEd);
      TableDragForm.Show;
    end;
  end;
end;

procedure TSQLCmdTabSheet.SQLMemoEnter(Sender: TObject);
begin
  {if(LastFocusedControl<>Sender)then
    if(Sender.InheritsFrom(TWinControl))then
      LastFocusedControl:=TWinControl(Sender);}
end;

procedure TSQLCmdTabSheet.CommandLineEdExecuteCommand(Cmd: WideString;
  Key: Word; Shift: TShiftState);
var
  Param: WideString;
  pRetValue: PChar;
begin
  //Command: USE
  Param := myx_parse_sqlmemo_command_use(Cmd);
  if (Param <> '') then
  begin
    CommandLineEd.PrepareNextConsoleCommand;

    MySQLConn.DefaultSchema := Param;
    Exit;
  end;

  //Command: DELIMITER
  Param := myx_parse_sqlmemo_command_delimiter(Cmd);
  if (Param <> '') then
  begin
    CommandLineEd.PrepareNextConsoleCommand;

    CommandLineEd.ConsoleDelimiter := Param;

    Exit;
  end;

  //Command: Exit
  if (myx_parse_sqlmemo_command_exit(Cmd) = 1) then
  begin
    Application.Terminate;
    Exit;
  end;

  //Command: HELP
  pRetValue := _myx_parse_sqlmemo_command_help(PChar(UTF8Encode(Cmd)));
  if (pRetValue <> nil) then
  begin
    myx_ts_display_help(PTxShell, UTF8Decode(Param));

    g_free(pRetValue);

    CommandLineEd.PrepareNextConsoleCommand;

    Exit;
  end
  else
    g_free(pRetValue);

  //Ctrl is held
  if (Shift = [ssCtrl]) then
  begin
    {SQLMemo.Text:=Cmd;

    QueryExecuteClick(Self);

    CommandLineEd.PrepareNextConsoleCommand;}
  end
  //Ctrl+Shift is held
  else
    if (Shift = [ssCtrl, ssShift]) then
    begin
    {SQLMemo.Text:=Cmd;

    QueryExecuteInNewTabClick(Self);

    CommandLineEd.PrepareNextConsoleCommand;}
    end
  //Ctrl+Alt is held
    else
      if (Shift = [ssCtrl, ssShift]) then
      begin
    {SQLMemo.Text:=Cmd;

    QuerySplitAndExecuteClick(Self);

    CommandLineEd.PrepareNextConsoleCommand;}
      end
  //Command: SQL
      else
      begin
    //Call shell command
        MySQLConn.Lock.Acquire;
        try
          Cmd := Trim(Cmd);
          Cmd := Copy(Cmd, 1, Length(Cmd) -
            Length(CommandLineEd.ConsoleDelimiter));

          myx_ts_execute_command(PTxShell, Cmd);
        finally
          MySQLConn.Lock.Release;
        end;

        CommandLineEd.PrepareNextConsoleCommand;
      end;
end;

function TSQLCmdTabSheet.CommandLineEdIsMultilineCommand(Cmd: WideString;
  Key: Word; Shift: TShiftState): Boolean;
var
  Param: WideString;
  pRetValue: PChar;
begin
  Result := True;

  //Command: DELIMITER
  Param := myx_parse_sqlmemo_command_delimiter(Cmd);
  if (Param <> '') then
    Result := False;

  //Command: HELP
  pRetValue := _myx_parse_sqlmemo_command_help(PChar(UTF8Encode(Cmd)));
  if (pRetValue <> nil) then
    Result := False;
  g_free(pRetValue);

  //Command: Exit
  if (myx_parse_sqlmemo_command_exit(Cmd) = 1) then
    Result := False;
end;

procedure TSQLCmdTabSheet.InitializeShell;
begin
  CommandLineEd.Text := 'MySQL GUI Command Line' + #13#10;

  if (MySQLConn.Connected) then
  begin
    CommandLineEd.Content.AddLine(
      Format(_('Your MySQL connection id is %d to server version : %s'),
      [myx_get_thread_id(MySQLConn.MySQL),
      myx_get_mysql_full_version(MySQLConn.MySQL)]));
    CommandLineEd.Content.AddLine('');
    CommandLineEd.Content.AddLine(
      Format(_('Type ''%s'' or ''%s'' for help. Type ''%s'' to clear the screen.'),
      ['help;', '\h', 'cls']));

    PTxShell := myx_init_text_shell(MySQLConn.MySQL);
    if (PTxShell <> nil) then
      myx_ts_set_output_callback(PTxShell, Addr(CommandLineEd), @TxShellOutput);

    CommandLineEd.Content.AddLine('');

    CommandLineEd.ConsolePrompt :=
      myx_get_default_schema(MySQLConn.MySQL) + '> ';

    CommandLineEd.PrepareNextConsoleCommand;
  end
  else
  begin
    CommandLineEd.Content.AddLine('');
    CommandLineEd.Content.AddLine('Not connected.');
  end;
end;

end.