ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/branches/journaling/design/ibsqleditor.pas
(Generate patch)

Comparing ibx/trunk/design/ibsqleditor.pas (file contents):
Revision 45 by tony, Tue Dec 6 10:33:46 2016 UTC vs.
Revision 106 by tony, Thu Jan 18 14:37:35 2018 UTC

# Line 23 | Line 23
23   *  Contributor(s): ______________________________________.
24   *
25   *)
26 < unit IBSQLEditor;
26 >
27 > unit ibsqleditor;
28  
29   {$mode objfpc}{$H+}
30  
31   interface
32  
33   uses
34 <  Classes, SysUtils, FileUtil,  Forms, Controls, Graphics, Dialogs,
35 <  StdCtrls, ExtCtrls, ComCtrls, IBSystemTables, IBSQL, IBDatabase, IB;
34 >  Classes, SysUtils, FileUtil, SynEdit, LResources, Forms, Controls, Graphics,
35 >  Dialogs, StdCtrls, ComCtrls, ibinsertsqleditor, IBSQLEditFrame,
36 >  IBLookupComboEditBox, IBDynamicGrid, IBDatabase, IBSQL, IBQuery, IB;
37  
38   type
39  
40    { TIBSQLEditorForm }
41  
42 <  TIBSQLEditorForm = class(TForm)
41 <    Button1: TButton;
42 <    Button2: TButton;
43 <    GenerateParams: TCheckBox;
44 <    GenerateBtn: TButton;
45 <    IBTransaction1: TIBTransaction;
46 <    Label5: TLabel;
47 <    SelectProcedure: TLabel;
48 <    TestBtn: TButton;
42 >  TIBSQLEditorForm = class(TIBInsertSQLEditorForm)
43      IncludePrimaryKeys: TCheckBox;
44 <    Label1: TLabel;
51 <    Label10: TLabel;
52 <    Label11: TLabel;
53 <    Label12: TLabel;
54 <    Label14: TLabel;
55 <    Label15: TLabel;
56 <    Label16: TLabel;
57 <    Label17: TLabel;
58 <    Label18: TLabel;
59 <    Label2: TLabel;
60 <    Label3: TLabel;
61 <    Label4: TLabel;
62 <    Label8: TLabel;
63 <    Label9: TLabel;
64 <    SelectFieldsList: TListBox;
65 <    ProcOutputList: TListBox;
66 <    SelectPrimaryKeys: TListBox;
67 <    InsertFieldsList: TListBox;
68 <    ModifyFieldsList: TListBox;
69 <    ModifyPrimaryKeys: TListBox;
70 <    DeletePrimaryKeys: TListBox;
71 <    ProcInputList: TListBox;
72 <    PageControl: TPageControl;
73 <    QuoteFields: TCheckBox;
74 <    SQLText: TMemo;
75 <    SelectPage: TTabSheet;
76 <    InsertPage: TTabSheet;
77 <    ModifyPage: TTabSheet;
78 <    DeletePage: TTabSheet;
79 <    ExecutePage: TTabSheet;
80 <    SelectTableNames: TComboBox;
81 <    InsertTableNames: TComboBox;
82 <    ModifyTableNames: TComboBox;
83 <    DeleteTableNames: TComboBox;
84 <    ProcedureNames: TComboBox;
85 <    procedure GenerateBtnClick(Sender: TObject);
86 <    procedure TestBtnClick(Sender: TObject);
87 <    procedure DeletePageShow(Sender: TObject);
88 <    procedure DeleteTableNamesCloseUp(Sender: TObject);
89 <    procedure ExecutePageShow(Sender: TObject);
44 >    TabControl1: TTabControl;
45      procedure FormShow(Sender: TObject);
46 <    procedure IncludePrimaryKeysClick(Sender: TObject);
47 <    procedure InsertPageShow(Sender: TObject);
93 <    procedure Label13Click(Sender: TObject);
94 <    procedure ModifyPageShow(Sender: TObject);
95 <    procedure ModifyTableNamesCloseUp(Sender: TObject);
96 <    procedure ProcedureNamesCloseUp(Sender: TObject);
97 <    procedure SelectFieldsListDblClick(Sender: TObject);
98 <    procedure SelectPageShow(Sender: TObject);
99 <    procedure SelectTableNamesCloseUp(Sender: TObject);
100 <    procedure InsertTableNamesCloseUp(Sender: TObject);
46 >    procedure GenerateBtnClick(Sender: TObject);
47 >    procedure TabControl1Change(Sender: TObject);
48    private
49 <    { private declarations }
103 <    FTableName: string;
104 <    FIBSystemTables: TIBSystemTables;
105 <    FExecuteOnly: boolean;
49 >    procedure SetupFlags;
50    protected
51 <    procedure Loaded; override;
51 >    procedure SetSQLStatementType(aType: TIBSQLStatementTypes); override;
52    public
53 <    { public declarations }
110 <    constructor Create(TheOwner: TComponent); override;
111 <    destructor Destroy; override;
112 <    procedure SetDatabase(Database: TIBDatabase);
53 >
54    end;
55  
56 + function EditSQL(aIBSQL: TIBSQL): boolean;
57 +
58   var
59    IBSQLEditorForm: TIBSQLEditorForm;
60  
118 function EditIBSQL(DataSet: TIBSQL): boolean;
119
61   implementation
62  
63   {$R *.lfm}
64  
65 < uses InterfaceBase;
125 <
126 < function EditIBSQL(DataSet: TIBSQL): boolean;
65 > function EditSQL(aIBSQL: TIBSQL): boolean;
66   begin
67    Result := false;
68 <  if assigned(DataSet.Database)  then
68 >  if assigned(aIBSQL) and assigned(aIBSQL.Database) then
69      try
70 <      DataSet.Database.Connected := true;
70 >      aIBSQL.Database.Connected := true;
71      except on E: Exception do
72        ShowMessage(E.Message)
73      end;
74  
75    with TIBSQLEditorForm.Create(Application) do
76    try
77 <    SetDatabase(DataSet.Database);
78 <    SQLText.Lines.Assign(DataSet.SQL);
79 <    GenerateParams.Checked := DataSet.GenerateParamNames;
77 >    if assigned(aIBSQL) then
78 >    begin
79 >        IBSQLEditFrame1.Database := aIBSQL.Database;
80 >        GenerateParams.Checked := aIBSQL.GenerateParamNames;
81 >    end;
82 >    with IBSQLEditFrame1 do
83 >    begin
84 >      IncludeReadOnlyFields := true;
85 >      ExecuteOnlyProcs := false;
86 >      SQLText.Lines.Assign(aIBSQL.SQL);
87 >    end;
88 >    IncludePrimaryKeys.Checked := false;
89      Result := ShowModal = mrOK;
90      if Result then
91 <      begin
92 <            DataSet.SQL.Assign(SQLText.Lines);
93 <            DataSet.GenerateParamNames := GenerateParams.Checked
94 <      end;
91 >    begin
92 >     aIBSQL.SQL.Assign(IBSQLEditFrame1.SQLText.Lines);
93 >     if assigned(aIBSQL) then
94 >          aIBSQL.GenerateParamNames := GenerateParams.Checked
95 >    end;
96    finally
97      Free
98    end;
150
99   end;
100  
101   { TIBSQLEditorForm }
102  
103 < procedure TIBSQLEditorForm.FormShow(Sender: TObject);
156 < var IsProcedureName: boolean;
103 > procedure TIBSQLEditorForm.TabControl1Change(Sender: TObject);
104   begin
105 <  if WidgetSet.LCLPlatform = lpGtk2 then
106 <    PageControl.TabPosition := tpLeft
105 >  case TabControl1.TabIndex of
106 >  4:
107 >    PageControl.ActivePage := ExecutePage;
108    else
109 <  PageControl.TabPosition := tpTop;
162 <  GenerateBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected;
163 <  TestBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected;
164 <  if Trim(SQLText.Text) <> '' then
165 <  begin
166 <    case FIBSystemTables.GetStatementType(SQLText.Text,IsProcedureName) of
167 <    SQLSelect:
168 <      if IsProcedureName then
169 <        PageControl.ActivePage := ExecutePage
170 <      else
171 <        PageControl.ActivePage := SelectPage;
172 <    SQLInsert:  PageControl.ActivePage := InsertPage;
173 <    SQLUpdate:  PageControl.ActivePage := ModifyPage;
174 <    SQLDelete:  PageControl.ActivePage := DeletePage;
175 <    SQLExecProcedure: PageControl.ActivePage := ExecutePage;
176 <    else
177 <      PageControl.ActivePage := SelectPage;
178 <    end;
179 <    FIBSystemTables.GetTableAndColumns(SQLText.Text,FTableName,nil)
109 >    PageControl.ActivePage := SelectPage;
110    end;
111 +  SetupFlags;
112   end;
113  
114 < procedure TIBSQLEditorForm.IncludePrimaryKeysClick(Sender: TObject);
114 > procedure TIBSQLEditorForm.FormShow(Sender: TObject);
115   begin
116 <  FIBSystemTables.GetFieldNames(ModifyTableNames.Text,ModifyFieldsList.Items,IncludePrimaryKeys.checked);
117 <  FIBSystemTables.GetPrimaryKeys(ModifyTableNames.Text,ModifyPrimaryKeys.Items);
187 < end;
188 <
189 < procedure TIBSQLEditorForm.DeletePageShow(Sender: TObject);
190 < var TableName: string;
191 < begin
192 <  FIBSystemTables.GetTableNames(DeleteTableNames.Items);
193 <  if Trim(SQLText.Text) = '' then
194 <  begin
195 <    if FTableName <> '' then
196 <      DeleteTableNames.ItemIndex := DeleteTableNames.Items.IndexOf(FTableName)
197 <    else
198 <    if DeleteTableNames.Items.Count > 0 then
199 <      DeleteTableNames.ItemIndex := 0
200 <  end
201 <  else
202 <  begin
203 <    FIBSystemTables.GetTableAndColumns(SQLText.Text,TableName,nil);
204 <    DeleteTableNames.ItemIndex := DeleteTableNames.Items.IndexOf(TableName);
205 <  end;
206 <  FIBSystemTables.GetPrimaryKeys(DeleteTableNames.Text,DeletePrimaryKeys.Items);
207 <
116 >  inherited;
117 >  SetupFlags;
118   end;
119  
120   procedure TIBSQLEditorForm.GenerateBtnClick(Sender: TObject);
211 var FieldNames: TStrings;
212 begin
213  FieldNames := nil;
214  if PageControl.ActivePage = SelectPage then
215  begin
216    FieldNames := FIBSystemTables.GetFieldNames(SelectFieldsList);
217    FIBSystemTables.GenerateSelectSQL(SelectTableNames.Text,QuoteFields.Checked,FieldNames,SQLText.Lines);
218  end
219  else
220  if PageControl.ActivePage = InsertPage then
221  begin
222    FieldNames := FIBSystemTables.GetFieldNames(InsertFieldsList);
223    FIBSystemTables.GenerateInsertSQL(InsertTableNames.Text,QuoteFields.Checked,FieldNames,SQLText.Lines);
224  end
225  else
226  if PageControl.ActivePage = ModifyPage then
227  begin
228    FieldNames := FIBSystemTables.GetFieldNames(ModifyFieldsList);
229    FIBSystemTables.GenerateModifySQL(ModifyTableNames.Text,QuoteFields.Checked,FieldNames,SQLText.Lines);
230  end
231  else
232  if PageControl.ActivePage = DeletePage then
233     FIBSystemTables.GenerateDeleteSQL(DeleteTableNames.Text,QuoteFields.Checked,SQLText.Lines)
234  else
235  if PageControl.ActivePage = ExecutePage then
236     FIBSystemTables.GenerateExecuteSQL(ProcedureNames.Text,QuoteFields.Checked, FExecuteOnly,
237             ProcInputList.Items,ProcOutputList.Items,SQLText.Lines);
238
239  if FieldNames <> nil then
240    FieldNames.Free
241 end;
242
243 procedure TIBSQLEditorForm.TestBtnClick(Sender: TObject);
244 begin
245  FIBSystemTables.TestSQL(SQLText.Text,GenerateParams.Checked);
246 end;
247
248 procedure TIBSQLEditorForm.DeleteTableNamesCloseUp(Sender: TObject);
249 begin
250  FTableName := DeleteTableNames.Text;
251  FIBSystemTables.GetPrimaryKeys(DeleteTableNames.Text,DeletePrimaryKeys.Items);
252 end;
253
254 procedure TIBSQLEditorForm.ExecutePageShow(Sender: TObject);
255 var ProcName: string;
256    IsProcedureName: boolean;
257 begin
258  FIBSystemTables.GetProcedureNames(ProcedureNames.Items);
259  if ProcedureNames.Items.Count > 0 then
260  begin
261    if (FIBSystemTables.GetStatementType(SQLText.Text,IsProcedureName) = SQLExecProcedure) or IsProcedureName then
262    begin
263      FIBSystemTables.GetTableAndColumns(SQLText.Text,ProcName,nil);
264      ProcedureNames.ItemIndex := ProcedureNames.Items.IndexOf(ProcName)
265    end
266    else
267      ProcedureNames.ItemIndex := 0;
268  end;
269  FIBSystemTables.GetProcParams(ProcedureNames.Text,FExecuteOnly,ProcInputList.Items,ProcOutputList.Items);
270  SelectProcedure.Visible :=  not FExecuteOnly;
271 end;
272
273 procedure TIBSQLEditorForm.InsertPageShow(Sender: TObject);
274 var TableName: string;
121   begin
122 <  FIBSystemTables.GetTableNames(InsertTableNames.Items);
123 <  if Trim(SQLText.Text) = '' then
124 <  begin
125 <    if FTableName <> '' then
126 <      InsertTableNames.ItemIndex := InsertTableNames.Items.IndexOf(FTableName)
127 <    else
128 <    if InsertTableNames.Items.Count > 0 then
129 <      InsertTableNames.ItemIndex := 0
130 <  end
131 <  else
132 <  begin
287 <    FIBSystemTables.GetTableAndColumns(SQLText.Text,TableName,nil);
288 <    InsertTableNames.ItemIndex := InsertTableNames.Items.IndexOf(TableName);
289 <  end;
290 <  FIBSystemTables.GetFieldNames(InsertTableNames.Text,InsertFieldsList.Items);
291 <
292 < end;
293 <
294 < procedure TIBSQLEditorForm.Label13Click(Sender: TObject);
295 < begin
296 <  FIBSystemTables.GetFieldNames(ModifyTableNames.Text,ModifyFieldsList.Items,IncludePrimaryKeys.checked);
297 <  FIBSystemTables.GetPrimaryKeys(ModifyTableNames.Text,ModifyPrimaryKeys.Items);
298 < end;
299 <
300 < procedure TIBSQLEditorForm.ModifyPageShow(Sender: TObject);
301 < var TableName: string;
302 < begin
303 <   FIBSystemTables.GetTableNames(ModifyTableNames.Items);
304 <  if Trim(SQLText.Text) = '' then
305 <  begin
306 <    if FTableName <> '' then
307 <      ModifyTableNames.ItemIndex := ModifyTableNames.Items.IndexOf(FTableName)
308 <    else
309 <    if ModifyTableNames.Items.Count > 0 then
310 <      ModifyTableNames.ItemIndex := 0;
311 <  end
312 <  else
313 <  begin
314 <    FIBSystemTables.GetTableAndColumns(SQLText.Text,TableName,nil);
315 <    ModifyTableNames.ItemIndex := ModifyTableNames.Items.IndexOf(TableName);
122 >  case TabControl1.TabIndex of
123 >  0:
124 >    IBSQLEditFrame1.GenerateSelectSQL(QuoteFields.Checked);
125 >  1:
126 >    IBSQLEditFrame1.GenerateInsertSQL(QuoteFields.Checked);
127 >  2:
128 >    IBSQLEditFrame1.GenerateModifySQL(QuoteFields.Checked,IncludePrimaryKeys.Checked);
129 >  3:
130 >    IBSQLEditFrame1.GenerateDeleteSQL(QuoteFields.Checked);
131 >  4:
132 >    IBSQLEditFrame1.GenerateExecuteSQL(QuoteFields.Checked);
133    end;
317  FIBSystemTables.GetFieldNames(ModifyTableNames.Text,ModifyFieldsList.Items,IncludePrimaryKeys.checked,false);
318  FIBSystemTables.GetPrimaryKeys(ModifyTableNames.Text,ModifyPrimaryKeys.Items);
134   end;
135  
136 < procedure TIBSQLEditorForm.ModifyTableNamesCloseUp(Sender: TObject);
137 < begin
138 <  FTableName := ModifyTableNames.Text;
139 <  FIBSystemTables.GetFieldNames(ModifyTableNames.Text,ModifyFieldsList.Items,IncludePrimaryKeys.checked,false);
140 <  FIBSystemTables.GetPrimaryKeys(ModifyTableNames.Text,ModifyPrimaryKeys.Items);
141 < end;
142 <
143 < procedure TIBSQLEditorForm.ProcedureNamesCloseUp(Sender: TObject);
144 < begin
145 <    FIBSystemTables.GetProcParams(ProcedureNames.Text,FExecuteOnly,ProcInputList.Items,ProcOutputList.Items);
146 <    SelectProcedure.Visible := not FExecuteOnly
147 < end;
148 <
149 < procedure TIBSQLEditorForm.SelectFieldsListDblClick(Sender: TObject);
150 < begin
151 <  SQLText.SelText:= (Sender as TListBox).Items[(Sender as TListBox).ItemIndex];
152 < end;
153 <
154 < procedure TIBSQLEditorForm.SelectPageShow(Sender: TObject);
155 < var TableName: string;
156 < begin
157 <  FIBSystemTables.GetTableNames(SelectTableNames.Items);
158 <  if Trim(SQLText.Text) = '' then
159 <  begin
160 <    if FTableName <> '' then
346 <      SelectTableNames.ItemIndex := SelectTableNames.Items.IndexOf(FTableName)
347 <    else
348 <    if SelectTableNames.Items.Count > 0 then
349 <      SelectTableNames.ItemIndex := 0;
350 <  end
136 > procedure TIBSQLEditorForm.SetupFlags;
137 > begin
138 >  IBSQLEditFrame1.IncludeReadOnlyFields := (TabControl1.TabIndex = 0);
139 >  IncludePrimaryKeys.Visible := TabControl1.TabIndex = 2;
140 >  FieldNamesGrid.Visible := TabControl1.TabIndex <> 3;
141 >  Label2.Visible := TabControl1.TabIndex <> 3;
142 >  IdentityGrid.Visible := TabControl1.TabIndex <> 3;
143 >  Label6.Visible := TabControl1.TabIndex <> 3;
144 >  ReadOnlyGrid.Visible := TabControl1.TabIndex <> 3;
145 >  Label5.Visible := TabControl1.TabIndex <> 3;
146 >  SelectSelectAll.Visible := TabControl1.TabIndex <> 3;
147 > end;
148 >
149 > procedure TIBSQLEditorForm.SetSQLStatementType(aType: TIBSQLStatementTypes);
150 > begin
151 >  inherited SetSQLStatementType(aType);
152 >  case aType of
153 >  SQLSelect:
154 >    TabControl1.TabIndex := 0;
155 >  SQLInsert:
156 >    TabControl1.TabIndex := 1;
157 >  SQLUpdate:
158 >    TabControl1.TabIndex := 2;
159 >  SQLDelete:
160 >    TabControl1.TabIndex := 3;
161    else
162 <  begin
353 <    FIBSystemTables.GetTableAndColumns(SQLText.Text,TableName,nil);
354 <    SelectTableNames.ItemIndex := SelectTableNames.Items.IndexOf(TableName);
162 >    TabControl1.TabIndex := 4;
163    end;
356  FIBSystemTables.GetFieldNames(SelectTableNames.Text,SelectFieldsList.Items);
357  FIBSystemTables.GetPrimaryKeys(SelectTableNames.Text,SelectPrimaryKeys.Items);
358 end;
359
360 procedure TIBSQLEditorForm.SelectTableNamesCloseUp(Sender: TObject);
361 begin
362  FTableName := SelectTableNames.Text;
363  try
364  FIBSystemTables.GetFieldNames(SelectTableNames.Text,SelectFieldsList.Items);
365  FIBSystemTables.GetPrimaryKeys(SelectTableNames.Text,SelectPrimaryKeys.Items);
366  except {ignore}  end;
367 end;
368
369 procedure TIBSQLEditorForm.InsertTableNamesCloseUp(Sender: TObject);
370 begin
371  FTableName := InsertTableNames.Text;
372  FIBSystemTables.GetFieldNames(InsertTableNames.Text,InsertFieldsList.Items);
373 end;
374
375 procedure TIBSQLEditorForm.Loaded;
376 begin
377  inherited Loaded;
378  {$IFDEF WINDOWS}
379  if assigned(PageControl) then
380    PageControl.TabPosition := tpTop;
381  {$ENDIF}
382 end;
383
384 constructor TIBSQLEditorForm.Create(TheOwner: TComponent);
385 begin
386  inherited Create(TheOwner);
387  FIBSystemTables := TIBSystemTables.Create;
388 end;
389
390 destructor TIBSQLEditorForm.Destroy;
391 begin
392  if assigned(FIBSystemTables) then FIBSystemTables.Free;
393  inherited Destroy;
394 end;
395
396 procedure TIBSQLEditorForm.SetDatabase(Database: TIBDatabase);
397 begin
398  IBTransaction1.DefaultDatabase := Database;
399  FIBSystemTables.SelectDatabase(Database,IBTransaction1)
164   end;
165  
166   end.
167 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines