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 |
tony |
105 |
IdentityGrid: TIBDynamicGrid;
|
47 |
tony |
80 |
IncludeSysTables: TCheckBox;
|
48 |
tony |
105 |
Label5: TLabel;
|
49 |
|
|
Label6: TLabel;
|
50 |
tony |
80 |
PrimaryKeysGrid: TIBDynamicGrid;
|
51 |
tony |
105 |
ReadOnlyGrid: TIBDynamicGrid;
|
52 |
tony |
80 |
SelectSelectAll: TCheckBox;
|
53 |
|
|
SelectTableNames: TIBLookupComboEditBox;
|
54 |
tony |
33 |
TestBtn: TButton;
|
55 |
|
|
CancelButton: TButton;
|
56 |
|
|
FieldsPage: TTabSheet;
|
57 |
tony |
80 |
GenerateButton: TButton;
|
58 |
tony |
33 |
GroupBox1: TGroupBox;
|
59 |
|
|
IncludePrimaryKeys: TCheckBox;
|
60 |
|
|
Label1: TLabel;
|
61 |
|
|
Label2: TLabel;
|
62 |
|
|
Label3: TLabel;
|
63 |
|
|
Label4: TLabel;
|
64 |
|
|
OkButton: TButton;
|
65 |
|
|
PageControl: TPageControl;
|
66 |
|
|
QuoteFields: TCheckBox;
|
67 |
|
|
SQLPage: TTabSheet;
|
68 |
|
|
StatementType: TRadioGroup;
|
69 |
tony |
80 |
procedure IncludeSysTablesChange(Sender: TObject);
|
70 |
|
|
procedure SelectSelectAllClick(Sender: TObject);
|
71 |
tony |
33 |
procedure TestBtnClick(Sender: TObject);
|
72 |
|
|
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
|
73 |
|
|
procedure FormShow(Sender: TObject);
|
74 |
tony |
80 |
procedure GenerateButtonClick(Sender: TObject);
|
75 |
tony |
33 |
procedure SQLMemoChange(Sender: TObject);
|
76 |
|
|
procedure SQLPageShow(Sender: TObject);
|
77 |
|
|
procedure StatementTypeClick(Sender: TObject);
|
78 |
|
|
private
|
79 |
|
|
{ private declarations }
|
80 |
|
|
FUpdateObject: TIBUpdateSQL;
|
81 |
|
|
FDirty: boolean;
|
82 |
|
|
FCurrentStatement: integer;
|
83 |
|
|
FModifySQL: TStringList;
|
84 |
|
|
FInsertSQL: TStringList;
|
85 |
|
|
FDeleteSQL: TStringList;
|
86 |
|
|
FRefreshSQL: TStringList;
|
87 |
|
|
procedure UpdateSQLMemo;
|
88 |
tony |
80 |
procedure HandleUserTablesOpened(Sender: TObject);
|
89 |
|
|
protected
|
90 |
|
|
procedure Loaded; override;
|
91 |
tony |
33 |
public
|
92 |
|
|
{ public declarations }
|
93 |
|
|
constructor Create(TheOwner: TComponent); override;
|
94 |
|
|
destructor Destroy; override;
|
95 |
|
|
end;
|
96 |
|
|
|
97 |
|
|
var
|
98 |
|
|
IBUpdateSQLEditorForm: TIBUpdateSQLEditorForm;
|
99 |
|
|
|
100 |
tony |
80 |
function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
|
101 |
tony |
33 |
|
102 |
|
|
implementation
|
103 |
|
|
|
104 |
|
|
{$R *.lfm}
|
105 |
|
|
|
106 |
|
|
function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
|
107 |
|
|
begin
|
108 |
|
|
Result := false;
|
109 |
tony |
80 |
if assigned(UpdateObject) and assigned(UpdateObject.DataSet) and assigned(UpdateObject.DataSet.Database) then
|
110 |
|
|
try
|
111 |
|
|
UpdateObject.DataSet.Database.Connected := true;
|
112 |
|
|
except on E: Exception do
|
113 |
|
|
ShowMessage(E.Message)
|
114 |
|
|
end;
|
115 |
tony |
33 |
|
116 |
|
|
with TIBUpdateSQLEditorForm.Create(Application) do
|
117 |
|
|
try
|
118 |
tony |
80 |
if assigned(UpdateObject) and assigned(UpdateObject.DataSet) then
|
119 |
|
|
begin
|
120 |
|
|
IBSQLEditFrame1.Database := UpdateObject.DataSet.Database;
|
121 |
tony |
33 |
GenerateParams.Checked := UpdateObject.DataSet.GenerateParamNames;
|
122 |
tony |
80 |
end;
|
123 |
tony |
106 |
with IBSQLEditFrame1 do
|
124 |
|
|
IncludeReadOnlyFields := false;
|
125 |
tony |
80 |
FUpdateObject := UpdateObject;
|
126 |
tony |
33 |
Result := ShowModal = mrOK;
|
127 |
tony |
80 |
if Result then
|
128 |
tony |
33 |
UpdateObject.DataSet.GenerateParamNames := GenerateParams.Checked
|
129 |
|
|
finally
|
130 |
|
|
Free
|
131 |
|
|
end;
|
132 |
|
|
|
133 |
|
|
end;
|
134 |
|
|
|
135 |
|
|
{ TIBUpdateSQLEditorForm }
|
136 |
|
|
|
137 |
|
|
procedure TIBUpdateSQLEditorForm.FormShow(Sender: TObject);
|
138 |
|
|
var TableName: string;
|
139 |
|
|
begin
|
140 |
|
|
PageControl.ActivePage := FieldsPage;
|
141 |
|
|
FModifySQL.Assign(FUpdateObject.ModifySQL);
|
142 |
|
|
FInsertSQL.Assign(FUpdateObject.InsertSQL);
|
143 |
|
|
FDeleteSQL.Assign(FUpdateObject.DeleteSQL);
|
144 |
|
|
FRefreshSQL.Assign(FUpdateObject.RefreshSQL);
|
145 |
tony |
80 |
GenerateButton.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
|
146 |
|
|
TestBtn.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
|
147 |
tony |
33 |
FCurrentStatement := -1;
|
148 |
tony |
81 |
if (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected then
|
149 |
|
|
begin
|
150 |
|
|
IBSQLEditFrame1.UserTables.Active := true;
|
151 |
|
|
IBSQLEditFrame1.SyncQueryBuilder(FRefreshSQL);
|
152 |
|
|
end;
|
153 |
tony |
33 |
end;
|
154 |
|
|
|
155 |
|
|
procedure TIBUpdateSQLEditorForm.FormClose(Sender: TObject;
|
156 |
|
|
var CloseAction: TCloseAction);
|
157 |
|
|
begin
|
158 |
|
|
if ModalResult = mrOK then
|
159 |
|
|
begin
|
160 |
|
|
UpdateSQLMemo;
|
161 |
|
|
FUpdateObject.ModifySQL.Assign(FModifySQL);
|
162 |
|
|
FUpdateObject.InsertSQL.Assign(FInsertSQL);
|
163 |
|
|
FUpdateObject.DeleteSQL.Assign(FDeleteSQL);
|
164 |
|
|
FUpdateObject.RefreshSQL.Assign(FRefreshSQL);
|
165 |
|
|
end;
|
166 |
|
|
end;
|
167 |
|
|
|
168 |
|
|
procedure TIBUpdateSQLEditorForm.TestBtnClick(Sender: TObject);
|
169 |
|
|
begin
|
170 |
tony |
80 |
IBSQLEditFrame1.TestSQL(GenerateParams.Checked);
|
171 |
tony |
33 |
end;
|
172 |
|
|
|
173 |
tony |
80 |
procedure TIBUpdateSQLEditorForm.IncludeSysTablesChange(Sender: TObject);
|
174 |
tony |
33 |
begin
|
175 |
tony |
80 |
IBSQLEditFrame1.IncludeSystemTables := IncludeSysTables.Checked;
|
176 |
|
|
end;
|
177 |
tony |
33 |
|
178 |
tony |
80 |
procedure TIBUpdateSQLEditorForm.SelectSelectAllClick(Sender: TObject);
|
179 |
|
|
begin
|
180 |
|
|
IBSQLEditFrame1.SelectAllFields(SelectSelectAll.Checked);
|
181 |
tony |
33 |
end;
|
182 |
|
|
|
183 |
tony |
80 |
procedure TIBUpdateSQLEditorForm.GenerateButtonClick(Sender: TObject);
|
184 |
|
|
begin
|
185 |
tony |
106 |
IBSQLEditFrame1.GenerateRefreshSQL(QuoteFields.Checked,FRefreshSQL,true);
|
186 |
tony |
80 |
IBSQLEditFrame1.GenerateDeleteSQL(QuoteFields.Checked,FDeleteSQL);
|
187 |
|
|
IBSQLEditFrame1.GenerateInsertSQL(QuoteFields.Checked,FInsertSQL);
|
188 |
tony |
106 |
IBSQLEditFrame1.GenerateModifySQL(QuoteFields.Checked,FModifySQL, IncludePrimaryKeys.Checked);
|
189 |
tony |
80 |
FDirty := false;
|
190 |
|
|
PageControl.ActivePage := SQLPage;
|
191 |
|
|
end;
|
192 |
|
|
|
193 |
tony |
33 |
procedure TIBUpdateSQLEditorForm.SQLMemoChange(Sender: TObject);
|
194 |
|
|
begin
|
195 |
|
|
FDirty := true
|
196 |
|
|
end;
|
197 |
|
|
|
198 |
|
|
procedure TIBUpdateSQLEditorForm.SQLPageShow(Sender: TObject);
|
199 |
|
|
begin
|
200 |
|
|
UpdateSQLMemo
|
201 |
|
|
end;
|
202 |
|
|
|
203 |
|
|
procedure TIBUpdateSQLEditorForm.StatementTypeClick(Sender: TObject);
|
204 |
|
|
begin
|
205 |
|
|
UpdateSQLMemo
|
206 |
|
|
end;
|
207 |
|
|
|
208 |
|
|
procedure TIBUpdateSQLEditorForm.UpdateSQLMemo;
|
209 |
|
|
begin
|
210 |
|
|
if FDirty then
|
211 |
|
|
case FCurrentStatement of
|
212 |
|
|
0: //Modify
|
213 |
tony |
80 |
FModifySQL.Assign(IBSQLEditFrame1.SQLText.Lines);
|
214 |
tony |
33 |
1: //Insert
|
215 |
tony |
80 |
FInsertSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
|
216 |
tony |
33 |
2: // Delete
|
217 |
tony |
80 |
FDeleteSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
|
218 |
tony |
33 |
3: //Refresh
|
219 |
tony |
80 |
FRefreshSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
|
220 |
tony |
33 |
end;
|
221 |
|
|
FDirty := false;
|
222 |
|
|
case StatementType.ItemIndex of
|
223 |
|
|
0: // Modify
|
224 |
tony |
80 |
IBSQLEditFrame1.SQLText.Lines.Assign(FModifySQL) ;
|
225 |
tony |
33 |
1: //Insert
|
226 |
tony |
80 |
IBSQLEditFrame1.SQLText.Lines.Assign(FInsertSQL) ;
|
227 |
tony |
33 |
2: // Delete
|
228 |
tony |
80 |
IBSQLEditFrame1.SQLText.Lines.Assign(FDeleteSQL) ;
|
229 |
tony |
33 |
3: //Refresh
|
230 |
tony |
80 |
IBSQLEditFrame1.SQLText.Lines.Assign(FRefreshSQL) ;
|
231 |
tony |
33 |
end;
|
232 |
|
|
FCurrentStatement := StatementType.ItemIndex;
|
233 |
|
|
end;
|
234 |
|
|
|
235 |
tony |
80 |
procedure TIBUpdateSQLEditorForm.HandleUserTablesOpened(Sender: TObject);
|
236 |
|
|
begin
|
237 |
|
|
SelectSelectAll.Checked := true;
|
238 |
|
|
end;
|
239 |
|
|
|
240 |
|
|
procedure TIBUpdateSQLEditorForm.Loaded;
|
241 |
|
|
begin
|
242 |
|
|
inherited Loaded;
|
243 |
|
|
if IBSQLEditFrame1 <> nil then
|
244 |
|
|
begin
|
245 |
|
|
IBSQLEditFrame1.OnUserTablesOpened := @HandleUserTablesOpened;
|
246 |
|
|
if SelectTableNames <> nil then
|
247 |
|
|
SelectTableNames.ListSource := IBSQLEditFrame1.UserTableSource;
|
248 |
|
|
if FieldNamesGrid <> nil then
|
249 |
|
|
FieldNamesGrid.DataSource := IBSQLEditFrame1.FieldsSource;
|
250 |
|
|
if PrimaryKeysGrid <> nil then
|
251 |
|
|
PrimaryKeysGrid.DataSource := IBSQLEditFrame1.PrimaryKeySource;
|
252 |
tony |
105 |
if IdentityGrid <> nil then
|
253 |
|
|
IdentityGrid.DataSource := IBSQLEditFrame1.IdentityColsSource;
|
254 |
|
|
if ReadOnlyGrid <> nil then
|
255 |
|
|
ReadOnlyGrid.DataSource := IBSQLEditFrame1.ReadOnlyFieldsSource;
|
256 |
tony |
80 |
end;
|
257 |
|
|
end;
|
258 |
|
|
|
259 |
tony |
33 |
constructor TIBUpdateSQLEditorForm.Create(TheOwner: TComponent);
|
260 |
|
|
begin
|
261 |
|
|
inherited Create(TheOwner);
|
262 |
|
|
FModifySQL := TStringList.Create;
|
263 |
|
|
FInsertSQL := TStringList.Create;
|
264 |
|
|
FDeleteSQL := TStringList.Create;
|
265 |
|
|
FRefreshSQL := TStringList.Create;
|
266 |
|
|
end;
|
267 |
|
|
|
268 |
|
|
destructor TIBUpdateSQLEditorForm.Destroy;
|
269 |
|
|
begin
|
270 |
|
|
if assigned(FModifySQL) then FModifySQL.Free;
|
271 |
|
|
if assigned(FInsertSQL) then FInsertSQL.Free;
|
272 |
|
|
if assigned(FDeleteSQL) then FDeleteSQL.Free;
|
273 |
|
|
if assigned(FRefreshSQL) then FRefreshSQL.Free;
|
274 |
|
|
inherited Destroy;
|
275 |
|
|
end;
|
276 |
|
|
|
277 |
|
|
end.
|