ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibupdatesqleditor.pas
Revision: 80
Committed: Mon Jan 1 11:31:07 2018 UTC (6 years, 3 months ago) by tony
Content type: text/x-pascal
File size: 7881 byte(s)
Log Message:
Fixes merged into public release

File Contents

# User Rev Content
1 tony 33 (*
2     * IBX For Lazarus (Firebird Express)
3     *
4     * The contents of this file are subject to the Initial Developer's
5     * Public License Version 1.0 (the "License"); you may not use this
6     * file except in compliance with the License. You may obtain a copy
7     * of the License here:
8     *
9     * http://www.firebirdsql.org/index.php?op=doc&id=idpl
10     *
11     * Software distributed under the License is distributed on an "AS
12     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
13     * implied. See the License for the specific language governing rights
14     * and limitations under the License.
15     *
16     * The Initial Developer of the Original Code is Tony Whyman.
17     *
18 tony 80 * The Original Code is (C) 2011-17 Tony Whyman, MWA Software
19 tony 33 * (http://www.mwasoftware.co.uk).
20     *
21     * All Rights Reserved.
22     *
23     * Contributor(s): ______________________________________.
24     *
25     *)
26    
27 tony 80 unit ibupdatesqleditor;
28    
29 tony 33 {$mode objfpc}{$H+}
30    
31     interface
32    
33     uses
34 tony 80 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
35     StdCtrls, ExtCtrls, IBSQLEditFrame, IBCustomDataSet,
36     IBDatabase, IBLookupComboEditBox, IBDynamicGrid, Types, IBUpdateSQL;
37 tony 33
38     type
39    
40     { TIBUpdateSQLEditorForm }
41    
42     TIBUpdateSQLEditorForm = class(TForm)
43 tony 80 FieldNamesGrid: TIBDynamicGrid;
44 tony 33 GenerateParams: TCheckBox;
45 tony 80 IBSQLEditFrame1: TIBSQLEditFrame;
46     IncludeSysTables: TCheckBox;
47     PrimaryKeysGrid: TIBDynamicGrid;
48     SelectSelectAll: TCheckBox;
49     SelectTableNames: TIBLookupComboEditBox;
50 tony 33 TestBtn: TButton;
51     CancelButton: TButton;
52     FieldsPage: TTabSheet;
53 tony 80 GenerateButton: TButton;
54 tony 33 GroupBox1: TGroupBox;
55     IncludePrimaryKeys: TCheckBox;
56     Label1: TLabel;
57     Label2: TLabel;
58     Label3: TLabel;
59     Label4: TLabel;
60     OkButton: TButton;
61     PageControl: TPageControl;
62     QuoteFields: TCheckBox;
63     SQLPage: TTabSheet;
64     StatementType: TRadioGroup;
65 tony 80 procedure IncludeSysTablesChange(Sender: TObject);
66     procedure SelectSelectAllClick(Sender: TObject);
67 tony 33 procedure TestBtnClick(Sender: TObject);
68     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
69     procedure FormShow(Sender: TObject);
70 tony 80 procedure GenerateButtonClick(Sender: TObject);
71 tony 33 procedure SQLMemoChange(Sender: TObject);
72     procedure SQLPageShow(Sender: TObject);
73     procedure StatementTypeClick(Sender: TObject);
74     private
75     { private declarations }
76     FUpdateObject: TIBUpdateSQL;
77     FDirty: boolean;
78     FCurrentStatement: integer;
79     FModifySQL: TStringList;
80     FInsertSQL: TStringList;
81     FDeleteSQL: TStringList;
82     FRefreshSQL: TStringList;
83     procedure UpdateSQLMemo;
84 tony 80 procedure HandleUserTablesOpened(Sender: TObject);
85     protected
86     procedure Loaded; override;
87 tony 33 public
88     { public declarations }
89     constructor Create(TheOwner: TComponent); override;
90     destructor Destroy; override;
91     end;
92    
93     var
94     IBUpdateSQLEditorForm: TIBUpdateSQLEditorForm;
95    
96 tony 80 function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
97 tony 33
98     implementation
99    
100     {$R *.lfm}
101    
102     function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
103     begin
104     Result := false;
105 tony 80 if assigned(UpdateObject) and assigned(UpdateObject.DataSet) and assigned(UpdateObject.DataSet.Database) then
106     try
107     UpdateObject.DataSet.Database.Connected := true;
108     except on E: Exception do
109     ShowMessage(E.Message)
110     end;
111 tony 33
112     with TIBUpdateSQLEditorForm.Create(Application) do
113     try
114 tony 80 if assigned(UpdateObject) and assigned(UpdateObject.DataSet) then
115     begin
116     IBSQLEditFrame1.Database := UpdateObject.DataSet.Database;
117 tony 33 GenerateParams.Checked := UpdateObject.DataSet.GenerateParamNames;
118 tony 80 end;
119     FUpdateObject := UpdateObject;
120 tony 33 Result := ShowModal = mrOK;
121 tony 80 if Result then
122 tony 33 UpdateObject.DataSet.GenerateParamNames := GenerateParams.Checked
123     finally
124     Free
125     end;
126    
127     end;
128    
129     { TIBUpdateSQLEditorForm }
130    
131     procedure TIBUpdateSQLEditorForm.FormShow(Sender: TObject);
132     var TableName: string;
133     begin
134     PageControl.ActivePage := FieldsPage;
135     FModifySQL.Assign(FUpdateObject.ModifySQL);
136     FInsertSQL.Assign(FUpdateObject.InsertSQL);
137     FDeleteSQL.Assign(FUpdateObject.DeleteSQL);
138     FRefreshSQL.Assign(FUpdateObject.RefreshSQL);
139 tony 80 GenerateButton.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
140     TestBtn.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
141 tony 33 FCurrentStatement := -1;
142 tony 80 IBSQLEditFrame1.UserTables.Active := true;
143     IBSQLEditFrame1.SyncQueryBuilder(FRefreshSQL);
144 tony 33 end;
145    
146     procedure TIBUpdateSQLEditorForm.FormClose(Sender: TObject;
147     var CloseAction: TCloseAction);
148     begin
149     if ModalResult = mrOK then
150     begin
151     UpdateSQLMemo;
152     FUpdateObject.ModifySQL.Assign(FModifySQL);
153     FUpdateObject.InsertSQL.Assign(FInsertSQL);
154     FUpdateObject.DeleteSQL.Assign(FDeleteSQL);
155     FUpdateObject.RefreshSQL.Assign(FRefreshSQL);
156     end;
157     end;
158    
159     procedure TIBUpdateSQLEditorForm.TestBtnClick(Sender: TObject);
160     begin
161 tony 80 IBSQLEditFrame1.TestSQL(GenerateParams.Checked);
162 tony 33 end;
163    
164 tony 80 procedure TIBUpdateSQLEditorForm.IncludeSysTablesChange(Sender: TObject);
165 tony 33 begin
166 tony 80 IBSQLEditFrame1.IncludeSystemTables := IncludeSysTables.Checked;
167     end;
168 tony 33
169 tony 80 procedure TIBUpdateSQLEditorForm.SelectSelectAllClick(Sender: TObject);
170     begin
171     IBSQLEditFrame1.SelectAllFields(SelectSelectAll.Checked);
172 tony 33 end;
173    
174 tony 80 procedure TIBUpdateSQLEditorForm.GenerateButtonClick(Sender: TObject);
175     begin
176     IBSQLEditFrame1.GenerateRefreshSQL(QuoteFields.Checked,FRefreshSQL);
177     IBSQLEditFrame1.GenerateDeleteSQL(QuoteFields.Checked,FDeleteSQL);
178     IBSQLEditFrame1.GenerateInsertSQL(QuoteFields.Checked,FInsertSQL);
179     IBSQLEditFrame1.GenerateModifySQL(QuoteFields.Checked,FModifySQL, not IncludePrimaryKeys.Checked);
180     FDirty := false;
181     PageControl.ActivePage := SQLPage;
182     end;
183    
184 tony 33 procedure TIBUpdateSQLEditorForm.SQLMemoChange(Sender: TObject);
185     begin
186     FDirty := true
187     end;
188    
189     procedure TIBUpdateSQLEditorForm.SQLPageShow(Sender: TObject);
190     begin
191     UpdateSQLMemo
192     end;
193    
194     procedure TIBUpdateSQLEditorForm.StatementTypeClick(Sender: TObject);
195     begin
196     UpdateSQLMemo
197     end;
198    
199     procedure TIBUpdateSQLEditorForm.UpdateSQLMemo;
200     begin
201     if FDirty then
202     case FCurrentStatement of
203     0: //Modify
204 tony 80 FModifySQL.Assign(IBSQLEditFrame1.SQLText.Lines);
205 tony 33 1: //Insert
206 tony 80 FInsertSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
207 tony 33 2: // Delete
208 tony 80 FDeleteSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
209 tony 33 3: //Refresh
210 tony 80 FRefreshSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
211 tony 33 end;
212     FDirty := false;
213     case StatementType.ItemIndex of
214     0: // Modify
215 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FModifySQL) ;
216 tony 33 1: //Insert
217 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FInsertSQL) ;
218 tony 33 2: // Delete
219 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FDeleteSQL) ;
220 tony 33 3: //Refresh
221 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FRefreshSQL) ;
222 tony 33 end;
223     FCurrentStatement := StatementType.ItemIndex;
224     end;
225    
226 tony 80 procedure TIBUpdateSQLEditorForm.HandleUserTablesOpened(Sender: TObject);
227     begin
228     SelectSelectAll.Checked := true;
229     end;
230    
231     procedure TIBUpdateSQLEditorForm.Loaded;
232     begin
233     inherited Loaded;
234     if IBSQLEditFrame1 <> nil then
235     begin
236     IBSQLEditFrame1.OnUserTablesOpened := @HandleUserTablesOpened;
237     if SelectTableNames <> nil then
238     SelectTableNames.ListSource := IBSQLEditFrame1.UserTableSource;
239     if FieldNamesGrid <> nil then
240     FieldNamesGrid.DataSource := IBSQLEditFrame1.FieldsSource;
241     if PrimaryKeysGrid <> nil then
242     PrimaryKeysGrid.DataSource := IBSQLEditFrame1.PrimaryKeySource;
243     end;
244     end;
245    
246 tony 33 constructor TIBUpdateSQLEditorForm.Create(TheOwner: TComponent);
247     begin
248     inherited Create(TheOwner);
249     FModifySQL := TStringList.Create;
250     FInsertSQL := TStringList.Create;
251     FDeleteSQL := TStringList.Create;
252     FRefreshSQL := TStringList.Create;
253     end;
254    
255     destructor TIBUpdateSQLEditorForm.Destroy;
256     begin
257     if assigned(FModifySQL) then FModifySQL.Free;
258     if assigned(FInsertSQL) then FInsertSQL.Free;
259     if assigned(FDeleteSQL) then FDeleteSQL.Free;
260     if assigned(FRefreshSQL) then FRefreshSQL.Free;
261     inherited Destroy;
262     end;
263    
264     end.