1 |
(* |
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 |
* The Original Code is (C) 2011 Tony Whyman, MWA Software |
19 |
* (http://www.mwasoftware.co.uk). |
20 |
* |
21 |
* All Rights Reserved. |
22 |
* |
23 |
* Contributor(s): ______________________________________. |
24 |
* |
25 |
*) |
26 |
|
27 |
unit IBDataSetEditor; |
28 |
|
29 |
{$mode objfpc}{$H+} |
30 |
|
31 |
interface |
32 |
|
33 |
uses |
34 |
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls, |
35 |
StdCtrls, ExtCtrls, IBSQLEditFrame, IBCustomDataSet, |
36 |
IBDatabase, IBLookupComboEditBox, IBDynamicGrid, Types; |
37 |
|
38 |
type |
39 |
|
40 |
{ TIBDataSetEditorForm } |
41 |
|
42 |
TIBDataSetEditorForm = class(TForm) |
43 |
FieldNamesGrid: TIBDynamicGrid; |
44 |
GenerateParams: TCheckBox; |
45 |
IBSQLEditFrame1: TIBSQLEditFrame; |
46 |
IncludeSysTables: TCheckBox; |
47 |
Label5: TLabel; |
48 |
Label6: TLabel; |
49 |
PrimaryKeysGrid: TIBDynamicGrid; |
50 |
IdentityGrid: TIBDynamicGrid; |
51 |
ReadOnlyGrid: TIBDynamicGrid; |
52 |
SelectSelectAll: TCheckBox; |
53 |
SelectTableNames: TIBLookupComboEditBox; |
54 |
TestBtn: TButton; |
55 |
CancelButton: TButton; |
56 |
FieldsPage: TTabSheet; |
57 |
GenerateButton: TButton; |
58 |
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 |
procedure IncludeSysTablesChange(Sender: TObject); |
70 |
procedure SelectSelectAllClick(Sender: TObject); |
71 |
procedure TestBtnClick(Sender: TObject); |
72 |
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); |
73 |
procedure FormShow(Sender: TObject); |
74 |
procedure GenerateButtonClick(Sender: TObject); |
75 |
procedure SQLMemoChange(Sender: TObject); |
76 |
procedure SQLPageShow(Sender: TObject); |
77 |
procedure StatementTypeClick(Sender: TObject); |
78 |
private |
79 |
{ private declarations } |
80 |
FDataSet: TIBDataSet; |
81 |
FDirty: boolean; |
82 |
FCurrentStatement: integer; |
83 |
FSelectSQL: TStringList; |
84 |
FModifySQL: TStringList; |
85 |
FInsertSQL: TStringList; |
86 |
FDeleteSQL: TStringList; |
87 |
FRefreshSQL: TStringList; |
88 |
procedure UpdateSQLMemo; |
89 |
procedure HandleUserTablesOpened(Sender: TObject); |
90 |
protected |
91 |
procedure Loaded; override; |
92 |
public |
93 |
{ public declarations } |
94 |
constructor Create(TheOwner: TComponent); override; |
95 |
destructor Destroy; override; |
96 |
end; |
97 |
|
98 |
var |
99 |
IBDataSetEditorForm: TIBDataSetEditorForm; |
100 |
|
101 |
function EditIBDataSet(DataSet: TIBDataSet): boolean; |
102 |
|
103 |
implementation |
104 |
|
105 |
{$R *.lfm} |
106 |
|
107 |
function EditIBDataSet(DataSet: TIBDataSet): boolean; |
108 |
begin |
109 |
Result := false; |
110 |
if assigned(DataSet) and assigned(DataSet.Database) then |
111 |
try |
112 |
DataSet.Database.Connected := true; |
113 |
except on E: Exception do |
114 |
ShowMessage(E.Message) |
115 |
end; |
116 |
|
117 |
with TIBDataSetEditorForm.Create(Application) do |
118 |
try |
119 |
if assigned(DataSet) then |
120 |
begin |
121 |
IBSQLEditFrame1.Database := DataSet.Database; |
122 |
GenerateParams.Checked := DataSet.GenerateParamNames; |
123 |
end; |
124 |
FDataSet := DataSet; |
125 |
with IBSQLEditFrame1 do |
126 |
IncludeReadOnlyFields := false; |
127 |
Result := ShowModal = mrOK; |
128 |
if Result and assigned(DataSet) then |
129 |
DataSet.GenerateParamNames := GenerateParams.Checked |
130 |
finally |
131 |
Free |
132 |
end; |
133 |
|
134 |
end; |
135 |
|
136 |
{ TIBDataSetEditorForm } |
137 |
|
138 |
procedure TIBDataSetEditorForm.FormShow(Sender: TObject); |
139 |
var TableName: string; |
140 |
begin |
141 |
PageControl.ActivePage := FieldsPage; |
142 |
FModifySQL.Assign(FDataSet.ModifySQL); |
143 |
FInsertSQL.Assign(FDataSet.InsertSQL); |
144 |
FDeleteSQL.Assign(FDataSet.DeleteSQL); |
145 |
FRefreshSQL.Assign(FDataSet.RefreshSQL); |
146 |
FSelectSQL.Assign(FDataSet.SelectSQL); |
147 |
GenerateButton.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected; |
148 |
TestBtn.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected; |
149 |
FCurrentStatement := -1; |
150 |
if (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected then |
151 |
begin |
152 |
IBSQLEditFrame1.UserTables.Active := true; |
153 |
IBSQLEditFrame1.SyncQueryBuilder(FSelectSQL); |
154 |
end; |
155 |
end; |
156 |
|
157 |
procedure TIBDataSetEditorForm.FormClose(Sender: TObject; |
158 |
var CloseAction: TCloseAction); |
159 |
begin |
160 |
if ModalResult = mrOK then |
161 |
begin |
162 |
UpdateSQLMemo; |
163 |
FDataSet.ModifySQL.Assign(FModifySQL); |
164 |
FDataSet.InsertSQL.Assign(FInsertSQL); |
165 |
FDataSet.DeleteSQL.Assign(FDeleteSQL); |
166 |
FDataSet.RefreshSQL.Assign(FRefreshSQL); |
167 |
FDataSet.SelectSQL.Assign(FSelectSQL); |
168 |
end; |
169 |
end; |
170 |
|
171 |
procedure TIBDataSetEditorForm.TestBtnClick(Sender: TObject); |
172 |
begin |
173 |
IBSQLEditFrame1.TestSQL(GenerateParams.Checked); |
174 |
end; |
175 |
|
176 |
procedure TIBDataSetEditorForm.IncludeSysTablesChange(Sender: TObject); |
177 |
begin |
178 |
IBSQLEditFrame1.IncludeSystemTables := IncludeSysTables.Checked; |
179 |
end; |
180 |
|
181 |
procedure TIBDataSetEditorForm.SelectSelectAllClick(Sender: TObject); |
182 |
begin |
183 |
IBSQLEditFrame1.SelectAllFields(SelectSelectAll.Checked); |
184 |
end; |
185 |
|
186 |
procedure TIBDataSetEditorForm.GenerateButtonClick(Sender: TObject); |
187 |
begin |
188 |
IBSQLEditFrame1.GenerateSelectSQL(QuoteFields.Checked,FSelectSQL,true); |
189 |
IBSQLEditFrame1.GenerateRefreshSQL(QuoteFields.Checked,FRefreshSQL,true); |
190 |
IBSQLEditFrame1.GenerateDeleteSQL(QuoteFields.Checked,FDeleteSQL); |
191 |
IBSQLEditFrame1.GenerateInsertSQL(QuoteFields.Checked,FInsertSQL); |
192 |
IBSQLEditFrame1.GenerateModifySQL(QuoteFields.Checked,FModifySQL, IncludePrimaryKeys.Checked); |
193 |
FDirty := false; |
194 |
PageControl.ActivePage := SQLPage; |
195 |
end; |
196 |
|
197 |
procedure TIBDataSetEditorForm.SQLMemoChange(Sender: TObject); |
198 |
begin |
199 |
FDirty := true |
200 |
end; |
201 |
|
202 |
procedure TIBDataSetEditorForm.SQLPageShow(Sender: TObject); |
203 |
begin |
204 |
UpdateSQLMemo |
205 |
end; |
206 |
|
207 |
procedure TIBDataSetEditorForm.StatementTypeClick(Sender: TObject); |
208 |
begin |
209 |
UpdateSQLMemo |
210 |
end; |
211 |
|
212 |
procedure TIBDataSetEditorForm.UpdateSQLMemo; |
213 |
begin |
214 |
if FDirty then |
215 |
case FCurrentStatement of |
216 |
0: // Select |
217 |
FSelectSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
218 |
1: //Modify |
219 |
FModifySQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
220 |
2: //Insert |
221 |
FInsertSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
222 |
3: // Delete |
223 |
FDeleteSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
224 |
4: //Refresh |
225 |
FRefreshSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
226 |
end; |
227 |
FDirty := false; |
228 |
case StatementType.ItemIndex of |
229 |
0: //Select |
230 |
IBSQLEditFrame1.SQLText.Lines.Assign(FSelectSQL); |
231 |
1: // Modify |
232 |
IBSQLEditFrame1.SQLText.Lines.Assign(FModifySQL) ; |
233 |
2: //Insert |
234 |
IBSQLEditFrame1.SQLText.Lines.Assign(FInsertSQL) ; |
235 |
3: // Delete |
236 |
IBSQLEditFrame1.SQLText.Lines.Assign(FDeleteSQL) ; |
237 |
4: //Refresh |
238 |
IBSQLEditFrame1.SQLText.Lines.Assign(FRefreshSQL) ; |
239 |
end; |
240 |
FCurrentStatement := StatementType.ItemIndex; |
241 |
end; |
242 |
|
243 |
procedure TIBDataSetEditorForm.HandleUserTablesOpened(Sender: TObject); |
244 |
begin |
245 |
SelectSelectAll.Checked := true; |
246 |
end; |
247 |
|
248 |
procedure TIBDataSetEditorForm.Loaded; |
249 |
begin |
250 |
inherited Loaded; |
251 |
if IBSQLEditFrame1 <> nil then |
252 |
begin |
253 |
IBSQLEditFrame1.OnUserTablesOpened := @HandleUserTablesOpened; |
254 |
if SelectTableNames <> nil then |
255 |
SelectTableNames.ListSource := IBSQLEditFrame1.UserTableSource; |
256 |
if FieldNamesGrid <> nil then |
257 |
FieldNamesGrid.DataSource := IBSQLEditFrame1.FieldsSource; |
258 |
if PrimaryKeysGrid <> nil then |
259 |
PrimaryKeysGrid.DataSource := IBSQLEditFrame1.PrimaryKeySource; |
260 |
if IdentityGrid <> nil then |
261 |
IdentityGrid.DataSource := IBSQLEditFrame1.IdentityColsSource; |
262 |
if ReadOnlyGrid <> nil then |
263 |
ReadOnlyGrid.DataSource := IBSQLEditFrame1.ReadOnlyFieldsSource; |
264 |
end; |
265 |
end; |
266 |
|
267 |
constructor TIBDataSetEditorForm.Create(TheOwner: TComponent); |
268 |
begin |
269 |
inherited Create(TheOwner); |
270 |
FModifySQL := TStringList.Create; |
271 |
FInsertSQL := TStringList.Create; |
272 |
FDeleteSQL := TStringList.Create; |
273 |
FRefreshSQL := TStringList.Create; |
274 |
FSelectSQL := TStringList.Create; |
275 |
end; |
276 |
|
277 |
destructor TIBDataSetEditorForm.Destroy; |
278 |
begin |
279 |
if assigned(FModifySQL) then FModifySQL.Free; |
280 |
if assigned(FInsertSQL) then FInsertSQL.Free; |
281 |
if assigned(FDeleteSQL) then FDeleteSQL.Free; |
282 |
if assigned(FRefreshSQL) then FRefreshSQL.Free; |
283 |
if assigned(FSelectSQL) then FSelectSQL.Free; |
284 |
inherited Destroy; |
285 |
end; |
286 |
|
287 |
end. |