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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines