1 {*********************************************************}
3 { Zeos Database Objects }
4 { SQL Monitor component }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
59 {$IFDEF MSWINDOWS} // 64-bit too
65 SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Graphtype,
66 StdCtrls, ExtCtrls, ComCtrls, Buttons, ZDataset, Menus, ZMessages;
67 //{$IFDEF USE_SYNEDIT}
68 //, SynEdit, SynEditHighlighter, SynHighlighterSQL
72 //, SynCompletionProposal
80 TZROSQLEditorForm = class(TForm)
85 PopupMenu1: TPopupMenu;
93 ListBoxRelations: TListBox;
94 ListBoxFields: TListBox;
101 chkReplace: TCheckBox;
104 //{$IFDEF USE_SYNEDIT}
105 //edtSelect: TSynEdit;
106 //SynSQLSyn1: TSynSQLSyn;
108 //SynCompletion:TSynCompletion;
110 //SynCompletion:TSynCompletionProposal;
115 FDS: TZReadOnlyQuery;
116 FSaveConnected: Boolean;
117 procedure FormClose(Sender: TObject; var Action: TCloseAction);
118 procedure ListBoxRelationsDblClick(Sender: TObject);
119 procedure ListBoxRelationsSelectionChange(Sender: TObject; User: boolean);
120 procedure PageControl1Change(Sender: TObject);
121 procedure ZeosROSQLEditorDestroy(Sender: TObject);
122 procedure btnGenerateClick(Sender: TObject);
123 procedure btnTestClick(Sender: TObject);
124 procedure mnuLoadClick(Sender: TObject);
125 procedure mnuSaveClick(Sender: TObject);
126 procedure LoadTableList;
127 procedure FillIdentifier;
128 function ActiveEditor:TMemo;
129 //function ActiveEditor:{$IFDEF USE_SYNEDIT} TSynEdit {$ELSE} TMemo {$ENDIF};
130 //{$IFDEF USE_SYNEDIT}
132 //procedure ccComplete(var Value: ansistring; Shift: TShiftState);
133 //procedure ccExecute(Sender: TObject);
135 //procedure ccComplete(Sender: TObject; var Value: string;
136 //Shift: TShiftState; Index: Integer; EndToken: Char);
137 //procedure ccExecute(Kind: SynCompletionType; Sender: TObject;
138 //var CurrentInput: string; var x, y: Integer; var CanExecute: Boolean);
142 PageControl1: TPageControl;
143 constructor CreateEditor(ADS:TZReadOnlyQuery);
147 ZeosROSQLEditorForm: TZROSQLEditorForm;
151 ZSqlTestForm, ZDbcIntfs, ZCompatibility;
154 SQLDefaultFilter = 'SQL files (*.sql)|*.sql';
158 constructor TZROSQLEditorForm.CreateEditor(ADS: TZReadOnlyQuery);
160 inherited CreateNew(Application,0);
167 Position := poDesktopCenter;
168 Caption := sFormEditor;
169 OnClose := FormClose;
170 OnDestroy := ZeosROSQLEditorDestroy;
171 btnOk := TBitBtn.Create(self);
175 Anchors := [akRight, akBottom];
185 Caption := SButtonOk;
187 btnCancel := TBitBtn.Create(self);
191 Anchors := [akRight, akBottom];
196 ModalResult := mrCancel;
201 Caption := SButtonCancel;
203 btnGenerate := TButton.Create(self);
207 Anchors := [akLeft,akBottom];
208 Caption := SButtonGenerate;
211 OnClick := btnGenerateClick;
217 btnCheck := TButton.Create(self);
221 Anchors := [akLeft,akBottom];
222 Caption := SButtonCheck;
232 btnTest := TButton.Create(self);
236 Anchors := [akLeft,akBottom];
237 Caption := SButtonTest;
240 OnClick := btnTestClick;
246 dlgOpen := TOpenDialog.Create(self);
250 Options := [ofEnableSizing,ofViewDetail];
251 Title := SDialogOpenTitle;
252 Filter := SQLDefaultFilter;
254 dlgSave := TSaveDialog.Create(self);
258 Options := [ofEnableSizing,ofViewDetail];
259 Title := SDialogSaveTitle;
260 Filter := SQLDefaultFilter;
262 mnuLoad := TMenuItem.Create(self);
265 Caption := SMenuLoad;
266 OnClick := mnuLoadClick;
268 mnuSave := TMenuItem.Create(self);
271 Caption := SMenuSave;
272 OnClick := mnuSaveClick;
274 PopupMenu1 := TPopupMenu.Create(self);
281 Panel2 := TPanel.Create(self);
286 Anchors := [akTop,akLeft,akRight,akBottom];
287 FullRepaint := False;
295 Splitter1 := TSplitter.Create(self);
300 Anchors := [akTop,akRight,akBottom];
306 PageControl1 := TPageControl.Create(self);
311 Anchors := [akTop,akLeft,akRight,akBottom];
314 OnChange := PageControl1Change;
317 TabPosition := tpTop;
322 tbsSelect := TTabSheet.Create(self);
325 Parent := PageControl1;
326 Caption := STabSheetSelect;
332 Panel1 := TPanel.Create(self);
337 Anchors := [akTop,akRight,akBottom];
338 FullRepaint := false;
346 Splitter2 := TSplitter.Create(self);
351 Anchors := [akLeft,akRight,akBottom];
358 ListBoxRelations := TListBox.Create(self);
359 with ListBoxRelations do
363 Anchors := [akTop,akLeft,akRight,akBottom];
367 OnDblClick := ListBoxRelationsDblClick;
368 OnSelectionChange := ListBoxRelationsSelectionChange;
373 ListBoxFields := TListBox.Create(self);
374 with ListBoxFields do
378 Anchors := [akLeft,akRight,akBottom];
387 Panel3 := TPanel.Create(self);
392 Anchors := [akTop,akLeft,akRight,akBottom];
393 BevelOuter := bvNone;
394 FullRepaint := False;
402 edtAlias := TEdit.Create(self);
406 Anchors := [akTop,akLeft];
413 lblAlias := TLabel.Create(self);
417 Anchors := [akTop, akLeft];
418 Caption := STableAlias;
419 FocusControl := edtAlias;
425 chkReplace := TCheckBox.Create(self);
429 Anchors := [akTop,akLeft];
430 Caption := SReplaceSQL;
439 edtSelect := TMemo.Create(self);
444 Anchors := [akTop,akLeft,akRight,akBottom];
447 Font.Name := 'Courier New';
449 Font.Pitch := fpFixed;
452 PopupMenu := PopupMenu1;
458 //{$IFDEF USE_SYNEDIT}
461 //TableNameAttri.Background:=clWindow;
462 //TableNameAttri.Foreground:=clGreen;
463 //TableNameAttri.Style:=[fsUnderline];
464 //with ADS.Connection do
465 //if (strpos(PChar(Protocol),'firebird') <> nil) or
466 //(strpos(PChar(Protocol),'interbase') <> nil) then
468 //SQLDialect:=sqlInterbase6;
470 //else if Protocol = 'mssql' then
472 //SQLDialect:=sqlMSSQL2K;
474 //else if (strpos(PChar(Protocol),'mysql') <> nil) then
476 //SQLDialect:=sqlMySQL;
478 //else if (strpos(PChar(Protocol),'postgresql') <> nil) then
480 //SQLDialect:=sqlStandard;
482 //else if Protocol = 'sybase' then
484 //SQLDialect:=sqlSybase;
486 //DefaultFilter:=SQLDefaultFilter;
489 //SynCompletion:=TSynCompletion.Create(Self);
491 //SynCompletion:=TSynCompletionProposal.Create(Self);
493 //with SynCompletion do begin
494 //AddEditor(edtSelect);
496 //OnCodeCompletion:=ccComplete;
497 //OnExecute:=ccExecute;
500 FSaveConnected := True;
502 if Assigned(FDS) then
504 if not FDS.Connection.Connected then
506 FSaveConnected := False;
507 FDS.Connection.Connected := True;
509 edtSelect.Lines.Text:=FDS.SQL.Text;
516 procedure TZROSQLEditorForm.FillIdentifier;
520 //{$IFDEF USE_SYNEDIT}
521 //SynSQLSyn1.TableNames.Clear;
522 //for i:=0 to ListBoxRelations.Items.Count-1 do
524 //SynSQLSyn1.TableNames.Add(ListBoxRelations.Items[i]);
529 procedure TZROSQLEditorForm.FormClose(Sender: TObject;
530 var Action: TCloseAction);
532 if (ModalResult=mrOk) and Assigned(FDS) then
534 FDS.SQL.Text:=edtSelect.Lines.Text;
538 procedure TZROSQLEditorForm.LoadTableList;
540 Metadata: IZDatabaseMetadata;
541 TableTypes: TStringDynArray;
543 if Assigned(FDS.Connection) and FDS.Connection.Connected then
545 Metadata := FDS.Connection.DbcConnection.GetMetadata;
546 SetLength(TableTypes, 2);
547 TableTypes[0] := 'TABLE';
548 TableTypes[1] := 'VIEW';
549 FDS.Connection.ShowSQLHourGlass;
550 ListBoxRelations.Items.Clear;
551 with Metadata.GetTables(FDS.Connection.Catalog, '', '', TableTypes) do
554 if ListBoxRelations.Items.IndexOf(trim(GetString(3))) = -1 then
555 ListBoxRelations.Items.Add(trim(GetString(3)));
559 FDS.Connection.HideSQLHourGlass;
563 function TZROSQLEditorForm.ActiveEditor:TMemo;
564 //function TZROSQLEditorForm.ActiveEditor:
565 //{$IFDEF USE_SYNEDIT}TSynEdit{$ELSE}TMemo{$ENDIF};
567 case PageControl1.ActivePageIndex of
572 procedure TZROSQLEditorForm.ListBoxRelationsDblClick(
576 Als, FieldsStr:string;
578 if ActiveEditor<>nil then
581 if chkReplace.Checked then Lines.Clear;
582 if ListBoxFields.SelCount=0 then
587 Lines.Add(' '+ListBoxRelations.Items[ListBoxRelations.ItemIndex]+' '+edtAlias.Text);
592 if edtAlias.Text<>'' then Als:=edtAlias.Text
593 else Als:=ListBoxRelations.Items[ListBoxRelations.ItemIndex];
595 for i:=0 to ListBoxFields.Items.Count-1 do
597 if ListBoxFields.Selected[i] then
599 if FieldsStr<>'' then
600 Lines.Add(FieldsStr+',');
601 FieldsStr:=' '+Als+'.'+ListBoxFields.Items[i];
604 if FieldsStr<>'' then Lines.Add(FieldsStr);
606 Lines.Add(' '+ListBoxRelations.Items[ListBoxRelations.ItemIndex]+' '+edtAlias.Text);
611 procedure TZROSQLEditorForm.ListBoxRelationsSelectionChange(Sender: TObject;
614 Metadata: IZDatabaseMetadata;
618 if Assigned(FDS.Connection) and FDS.Connection.Connected then
620 FDS.Connection.ShowSQLHourGlass;
621 Metadata := FDS.Connection.DbcConnection.GetMetadata;
622 with ListBoxFields do begin
628 if ListBoxRelations.ItemIndex >= 0 then
630 TPName := ListBoxRelations.Items[ListBoxRelations.ItemIndex];
631 with Metadata.GetColumns(FDS.Connection.Catalog,'', TPName,'') do
634 if ListBoxFields.Items.IndexOf(GetStringByName('COLUMN_NAME')) = -1 then
635 ListBoxFields.Items.Add(GetStringByName('COLUMN_NAME'));
640 FDS.Connection.HideSQLHourGlass;
644 procedure TZROSQLEditorForm.PageControl1Change(Sender: TObject);
646 case PageControl1.ActivePageIndex of
647 0:edtSelect.SetFocus;
651 procedure TZROSQLEditorForm.ZeosROSQLEditorDestroy(Sender: TObject);
653 if not FSaveConnected then
654 FDS.Connection.Connected := False;
657 procedure TZROSQLEditorForm.btnGenerateClick(Sender: TObject);
659 ListBoxRelationsDblClick(nil);
662 procedure TZROSQLEditorForm.btnTestClick(Sender: TObject);
664 if Assigned(FDS.Connection) and FDS.Connection.Connected then
666 ZeosSQLEditorTestForm:=TZeosSQLEditorTestForm.Create(Application);
667 with ZeosSQLEditorTestForm do
670 ZeosSQL.Connection:=FDS.Connection;
671 ZeosSQL.SQL.Text:=ActiveEditor.Lines.Text;
672 ZeosSQL.Active := true;
676 ShowMessage(E.Message);
683 procedure TZROSQLEditorForm.mnuLoadClick(Sender: TObject);
685 if (dlgOpen.Execute) then
686 ActiveEditor.Lines.LoadFromFile(dlgOpen.FileName);
689 procedure TZROSQLEditorForm.mnuSaveClick(Sender: TObject);
691 if dlgSave.Execute then
692 ActiveEditor.Lines.SaveToFile(dlgSave.FileName);
695 //{$IFDEF USE_SYNEDIT}
696 //procedure TZROSQLEditorForm.ccComplete
698 //(var Value: ansistring;
699 //Shift: TShiftState)
701 //(Sender: TObject; var Value: string;
702 //Shift: TShiftState; Index: Integer; EndToken: Char)
708 //procedure TZROSQLEditorForm.ccExecute
712 //(Kind: SynCompletionType; Sender: TObject;
713 //var CurrentInput: string; var x, y: Integer; var CanExecute: Boolean)
716 //function GetCurWord:string;
722 //with ActiveEditor do
724 //S:=Trim(Copy(LineText, 1, CaretX));
726 //while (i>0) and (S[i]<>'.') do Dec(I);
731 //while (j>0) and CharInSet(S[j], ['A'..'z','"']) do Dec(j);
732 //Result:=trim(Copy(S, j+1, i-j-1));
738 //Metadata: IZDatabaseMetadata;
741 //S:=AnsiUpperCase(GetCurWord);
744 //if Assigned(FDS.Connection) and FDS.Connection.Connected then
746 //FDS.Connection.ShowSQLHourGlass;
747 //Metadata := FDS.Connection.DbcConnection.GetMetadata;
748 //SynCompletion.ItemList.Clear;
750 //SynCompletion.OnPaintItem;
753 //with Metadata.GetColumns(FDS.Connection.Catalog,'',S,'') do
756 //if SynCompletion.ItemList.IndexOf(GetStringByName('COLUMN_NAME')) = -1 then
757 //SynCompletion.ItemList.Add(GetStringByName('COLUMN_NAME'));
761 //FDS.Connection.HideSQLHourGlass;