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+} |
31 |
|
interface |
32 |
|
|
33 |
|
uses |
34 |
< |
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, |
35 |
< |
ComCtrls, StdCtrls, ExtCtrls, IBSystemTables, IBCustomDataSet; |
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 |
< |
Button1: TButton; |
43 |
> |
FieldNamesGrid: TIBDynamicGrid; |
44 |
> |
GenerateParams: TCheckBox; |
45 |
> |
IBSQLEditFrame1: TIBSQLEditFrame; |
46 |
> |
IncludeSysTables: TCheckBox; |
47 |
> |
PrimaryKeysGrid: TIBDynamicGrid; |
48 |
> |
SelectSelectAll: TCheckBox; |
49 |
> |
SelectTableNames: TIBLookupComboEditBox; |
50 |
> |
TestBtn: TButton; |
51 |
|
CancelButton: TButton; |
52 |
|
FieldsPage: TTabSheet; |
53 |
|
GenerateButton: TButton; |
54 |
|
GroupBox1: TGroupBox; |
55 |
< |
PrimaryKeyList: TListBox; |
55 |
> |
IncludePrimaryKeys: TCheckBox; |
56 |
|
Label1: TLabel; |
57 |
|
Label2: TLabel; |
58 |
|
Label3: TLabel; |
60 |
|
OkButton: TButton; |
61 |
|
PageControl: TPageControl; |
62 |
|
QuoteFields: TCheckBox; |
29 |
– |
SQLMemo: TMemo; |
63 |
|
SQLPage: TTabSheet; |
64 |
|
StatementType: TRadioGroup; |
65 |
< |
FieldList: TListBox; |
66 |
< |
TableNamesCombo: TComboBox; |
67 |
< |
procedure Button1Click(Sender: TObject); |
65 |
> |
procedure IncludeSysTablesChange(Sender: TObject); |
66 |
> |
procedure SelectSelectAllClick(Sender: TObject); |
67 |
> |
procedure TestBtnClick(Sender: TObject); |
68 |
|
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); |
69 |
|
procedure FormShow(Sender: TObject); |
70 |
|
procedure GenerateButtonClick(Sender: TObject); |
71 |
|
procedure SQLMemoChange(Sender: TObject); |
72 |
|
procedure SQLPageShow(Sender: TObject); |
73 |
|
procedure StatementTypeClick(Sender: TObject); |
41 |
– |
procedure TableNamesComboCloseUp(Sender: TObject); |
74 |
|
private |
75 |
|
{ private declarations } |
76 |
|
FDataSet: TIBDataSet; |
45 |
– |
FIBSystemTables: TIBSystemTables; |
77 |
|
FDirty: boolean; |
78 |
|
FCurrentStatement: integer; |
79 |
|
FSelectSQL: TStringList; |
82 |
|
FDeleteSQL: TStringList; |
83 |
|
FRefreshSQL: TStringList; |
84 |
|
procedure UpdateSQLMemo; |
85 |
+ |
procedure HandleUserTablesOpened(Sender: TObject); |
86 |
+ |
protected |
87 |
+ |
procedure Loaded; override; |
88 |
|
public |
89 |
|
{ public declarations } |
90 |
|
constructor Create(TheOwner: TComponent); override; |
91 |
|
destructor Destroy; override; |
58 |
– |
procedure SetDataSet(AObject: TIBDataSet); |
92 |
|
end; |
93 |
|
|
94 |
|
var |
98 |
|
|
99 |
|
implementation |
100 |
|
|
101 |
+ |
{$R *.lfm} |
102 |
+ |
|
103 |
|
function EditIBDataSet(DataSet: TIBDataSet): boolean; |
104 |
|
begin |
105 |
|
Result := false; |
106 |
|
if assigned(DataSet) and assigned(DataSet.Database) then |
107 |
< |
begin |
73 |
< |
if not assigned(DataSet.Transaction) then |
74 |
< |
begin |
75 |
< |
ShowMessage('No Default Transaction!'); |
76 |
< |
Exit |
77 |
< |
end; |
78 |
< |
|
107 |
> |
try |
108 |
|
DataSet.Database.Connected := true; |
109 |
+ |
except on E: Exception do |
110 |
+ |
ShowMessage(E.Message) |
111 |
|
end; |
112 |
|
|
113 |
|
with TIBDataSetEditorForm.Create(Application) do |
114 |
|
try |
115 |
< |
SetDataSet(DataSet); |
116 |
< |
Result := ShowModal = mrOK |
115 |
> |
if assigned(DataSet) then |
116 |
> |
begin |
117 |
> |
IBSQLEditFrame1.Database := DataSet.Database; |
118 |
> |
GenerateParams.Checked := DataSet.GenerateParamNames; |
119 |
> |
end; |
120 |
> |
FDataSet := DataSet; |
121 |
> |
Result := ShowModal = mrOK; |
122 |
> |
if Result and assigned(DataSet) then |
123 |
> |
DataSet.GenerateParamNames := GenerateParams.Checked |
124 |
|
finally |
125 |
|
Free |
126 |
|
end; |
138 |
|
FDeleteSQL.Assign(FDataSet.DeleteSQL); |
139 |
|
FRefreshSQL.Assign(FDataSet.RefreshSQL); |
140 |
|
FSelectSQL.Assign(FDataSet.SelectSQL); |
141 |
+ |
GenerateButton.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected; |
142 |
+ |
TestBtn.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected; |
143 |
|
FCurrentStatement := -1; |
144 |
< |
TableNamesCombo.Items.Clear; |
105 |
< |
FIBSystemTables.GetTableNames(TableNamesCombo.Items); |
106 |
< |
if TableNamesCombo.Items.Count > 0 then |
144 |
> |
if (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected then |
145 |
|
begin |
146 |
< |
TableNamesCombo.ItemIndex := 0; |
147 |
< |
if FSelectSQL.Text <> '' then |
110 |
< |
try |
111 |
< |
FIBSystemTables.GetTableAndColumns(FSelectSQL.Text,TableName,nil); |
112 |
< |
TableNamesCombo.ItemIndex := TableNamesCombo.Items.IndexOf(TableName); |
113 |
< |
except end;//ignore |
114 |
< |
FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,false); |
115 |
< |
FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items); |
146 |
> |
IBSQLEditFrame1.UserTables.Active := true; |
147 |
> |
IBSQLEditFrame1.SyncQueryBuilder(FSelectSQL); |
148 |
|
end; |
149 |
|
end; |
150 |
|
|
162 |
|
end; |
163 |
|
end; |
164 |
|
|
165 |
< |
procedure TIBDataSetEditorForm.Button1Click(Sender: TObject); |
165 |
> |
procedure TIBDataSetEditorForm.TestBtnClick(Sender: TObject); |
166 |
|
begin |
167 |
< |
if SQLMemo.Lines.Text <> '' then |
136 |
< |
FIBSystemTables.TestSQL(SQLMemo.Lines.Text); |
167 |
> |
IBSQLEditFrame1.TestSQL(GenerateParams.Checked); |
168 |
|
end; |
169 |
|
|
170 |
< |
procedure TIBDataSetEditorForm.GenerateButtonClick(Sender: TObject); |
140 |
< |
var FieldNames: TStringList; |
141 |
< |
I: integer; |
170 |
> |
procedure TIBDataSetEditorForm.IncludeSysTablesChange(Sender: TObject); |
171 |
|
begin |
172 |
< |
FDataSet.RefreshSQL.Clear; |
173 |
< |
FIBSystemTables.GenerateSelectSQL(TableNamesCombo.Text,QuoteFields.Checked,FSelectSQL); |
145 |
< |
FIBSystemTables.GenerateRefreshSQL(TableNamesCombo.Text,QuoteFields.Checked,FRefreshSQL); |
146 |
< |
FIBSystemTables.GenerateDeleteSQL(TableNamesCombo.Text,QuoteFields.Checked,FDeleteSQL); |
147 |
< |
if FieldList.SelCount = 0 then |
148 |
< |
begin |
149 |
< |
FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked, |
150 |
< |
FieldList.Items,FModifySQL); |
151 |
< |
FIBSystemTables.GenerateInsertSQL(TableNamesCombo.Text,QuoteFields.Checked, |
152 |
< |
FieldList.Items,FInsertSQL); |
172 |
> |
IBSQLEditFrame1.IncludeSystemTables := IncludeSysTables.Checked; |
173 |
> |
end; |
174 |
|
|
175 |
< |
end |
176 |
< |
else |
177 |
< |
begin |
178 |
< |
FieldNames := TStringList.Create; |
179 |
< |
try |
180 |
< |
for I := 0 to FieldList.Items.Count - 1 do |
181 |
< |
if FieldList.Selected[I] then |
182 |
< |
FieldNames.Add(FieldList.Items[I]); |
183 |
< |
FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked, |
184 |
< |
FieldNames,FModifySQL); |
185 |
< |
FIBSystemTables.GenerateInsertSQL(TableNamesCombo.Text,QuoteFields.Checked, |
186 |
< |
FieldNames,FInsertSQL); |
166 |
< |
finally |
167 |
< |
FieldNames.Free |
168 |
< |
end; |
169 |
< |
end; |
175 |
> |
procedure TIBDataSetEditorForm.SelectSelectAllClick(Sender: TObject); |
176 |
> |
begin |
177 |
> |
IBSQLEditFrame1.SelectAllFields(SelectSelectAll.Checked); |
178 |
> |
end; |
179 |
> |
|
180 |
> |
procedure TIBDataSetEditorForm.GenerateButtonClick(Sender: TObject); |
181 |
> |
begin |
182 |
> |
IBSQLEditFrame1.GenerateSelectSQL(QuoteFields.Checked,FSelectSQL); |
183 |
> |
IBSQLEditFrame1.GenerateRefreshSQL(QuoteFields.Checked,FRefreshSQL); |
184 |
> |
IBSQLEditFrame1.GenerateDeleteSQL(QuoteFields.Checked,FDeleteSQL); |
185 |
> |
IBSQLEditFrame1.GenerateInsertSQL(QuoteFields.Checked,FInsertSQL); |
186 |
> |
IBSQLEditFrame1.GenerateModifySQL(QuoteFields.Checked,FModifySQL, not IncludePrimaryKeys.Checked); |
187 |
|
FDirty := false; |
188 |
|
PageControl.ActivePage := SQLPage; |
189 |
|
end; |
203 |
|
UpdateSQLMemo |
204 |
|
end; |
205 |
|
|
189 |
– |
procedure TIBDataSetEditorForm.TableNamesComboCloseUp(Sender: TObject); |
190 |
– |
begin |
191 |
– |
FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,false); |
192 |
– |
FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items); |
193 |
– |
end; |
194 |
– |
|
206 |
|
procedure TIBDataSetEditorForm.UpdateSQLMemo; |
207 |
|
begin |
208 |
|
if FDirty then |
209 |
|
case FCurrentStatement of |
210 |
|
0: // Select |
211 |
< |
FSelectSQL.Assign(SQLMemo.Lines); |
211 |
> |
FSelectSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
212 |
|
1: //Modify |
213 |
< |
FModifySQL.Assign(SQLMemo.Lines); |
213 |
> |
FModifySQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
214 |
|
2: //Insert |
215 |
< |
FInsertSQL.Assign(SQLMemo.Lines); |
215 |
> |
FInsertSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
216 |
|
3: // Delete |
217 |
< |
FDeleteSQL.Assign(SQLMemo.Lines); |
217 |
> |
FDeleteSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
218 |
|
4: //Refresh |
219 |
< |
FRefreshSQL.Assign(SQLMemo.Lines); |
219 |
> |
FRefreshSQL.Assign(IBSQLEditFrame1.SQLText.Lines); |
220 |
|
end; |
221 |
|
FDirty := false; |
222 |
|
case StatementType.ItemIndex of |
223 |
|
0: //Select |
224 |
< |
SQLMemo.Lines.Assign(FSelectSQL); |
224 |
> |
IBSQLEditFrame1.SQLText.Lines.Assign(FSelectSQL); |
225 |
|
1: // Modify |
226 |
< |
SQLMemo.Lines.Assign(FModifySQL) ; |
226 |
> |
IBSQLEditFrame1.SQLText.Lines.Assign(FModifySQL) ; |
227 |
|
2: //Insert |
228 |
< |
SQLMemo.Lines.Assign(FInsertSQL) ; |
228 |
> |
IBSQLEditFrame1.SQLText.Lines.Assign(FInsertSQL) ; |
229 |
|
3: // Delete |
230 |
< |
SQLMemo.Lines.Assign(FDeleteSQL) ; |
230 |
> |
IBSQLEditFrame1.SQLText.Lines.Assign(FDeleteSQL) ; |
231 |
|
4: //Refresh |
232 |
< |
SQLMemo.Lines.Assign(FRefreshSQL) ; |
232 |
> |
IBSQLEditFrame1.SQLText.Lines.Assign(FRefreshSQL) ; |
233 |
|
end; |
234 |
|
FCurrentStatement := StatementType.ItemIndex; |
235 |
|
end; |
236 |
|
|
237 |
+ |
procedure TIBDataSetEditorForm.HandleUserTablesOpened(Sender: TObject); |
238 |
+ |
begin |
239 |
+ |
SelectSelectAll.Checked := true; |
240 |
+ |
end; |
241 |
+ |
|
242 |
+ |
procedure TIBDataSetEditorForm.Loaded; |
243 |
+ |
begin |
244 |
+ |
inherited Loaded; |
245 |
+ |
if IBSQLEditFrame1 <> nil then |
246 |
+ |
begin |
247 |
+ |
IBSQLEditFrame1.OnUserTablesOpened := @HandleUserTablesOpened; |
248 |
+ |
if SelectTableNames <> nil then |
249 |
+ |
SelectTableNames.ListSource := IBSQLEditFrame1.UserTableSource; |
250 |
+ |
if FieldNamesGrid <> nil then |
251 |
+ |
FieldNamesGrid.DataSource := IBSQLEditFrame1.FieldsSource; |
252 |
+ |
if PrimaryKeysGrid <> nil then |
253 |
+ |
PrimaryKeysGrid.DataSource := IBSQLEditFrame1.PrimaryKeySource; |
254 |
+ |
end; |
255 |
+ |
end; |
256 |
+ |
|
257 |
|
constructor TIBDataSetEditorForm.Create(TheOwner: TComponent); |
258 |
|
begin |
259 |
|
inherited Create(TheOwner); |
229 |
– |
FIBSystemTables := TIBSystemTables.Create; |
260 |
|
FModifySQL := TStringList.Create; |
261 |
|
FInsertSQL := TStringList.Create; |
262 |
|
FDeleteSQL := TStringList.Create; |
266 |
|
|
267 |
|
destructor TIBDataSetEditorForm.Destroy; |
268 |
|
begin |
239 |
– |
if assigned(FIBSystemTables) then FIBSystemTables.Free; |
269 |
|
if assigned(FModifySQL) then FModifySQL.Free; |
270 |
|
if assigned(FInsertSQL) then FInsertSQL.Free; |
271 |
|
if assigned(FDeleteSQL) then FDeleteSQL.Free; |
272 |
|
if assigned(FRefreshSQL) then FRefreshSQL.Free; |
273 |
< |
if assigned(FSelectSQL) then FSelectSQL.Free; |
273 |
> |
if assigned(FSelectSQL) then FSelectSQL.Free; |
274 |
|
inherited Destroy; |
275 |
|
end; |
276 |
|
|
248 |
– |
procedure TIBDataSetEditorForm.SetDataSet(AObject: TIBDataSet); |
249 |
– |
begin |
250 |
– |
FDataSet := AObject; |
251 |
– |
if assigned(FDataSet) then |
252 |
– |
FIBSystemTables.SelectDatabase(FDataSet.Database,FDataSet.Transaction); |
253 |
– |
end; |
254 |
– |
|
255 |
– |
|
256 |
– |
initialization |
257 |
– |
{$I ibdataseteditor.lrs} |
258 |
– |
|
277 |
|
end. |
260 |
– |
|