1 |
tony |
5 |
unit IBDataSetEditor; |
2 |
|
|
|
3 |
|
|
{$mode objfpc}{$H+} |
4 |
|
|
|
5 |
|
|
interface |
6 |
|
|
|
7 |
|
|
uses |
8 |
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, |
9 |
|
|
ComCtrls, StdCtrls, ExtCtrls, IBSystemTables, IBCustomDataSet; |
10 |
|
|
|
11 |
|
|
type |
12 |
|
|
|
13 |
|
|
{ TIBDataSetEditorForm } |
14 |
|
|
|
15 |
|
|
TIBDataSetEditorForm = class(TForm) |
16 |
|
|
Button1: TButton; |
17 |
|
|
CancelButton: TButton; |
18 |
|
|
FieldsPage: TTabSheet; |
19 |
|
|
GenerateButton: TButton; |
20 |
|
|
GroupBox1: TGroupBox; |
21 |
|
|
PrimaryKeyList: TListBox; |
22 |
|
|
Label1: TLabel; |
23 |
|
|
Label2: TLabel; |
24 |
|
|
Label3: TLabel; |
25 |
|
|
Label4: TLabel; |
26 |
|
|
OkButton: TButton; |
27 |
|
|
PageControl: TPageControl; |
28 |
|
|
QuoteFields: TCheckBox; |
29 |
|
|
SQLMemo: TMemo; |
30 |
|
|
SQLPage: TTabSheet; |
31 |
|
|
StatementType: TRadioGroup; |
32 |
|
|
FieldList: TListBox; |
33 |
|
|
TableNamesCombo: TComboBox; |
34 |
|
|
procedure Button1Click(Sender: TObject); |
35 |
|
|
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); |
36 |
|
|
procedure FormShow(Sender: TObject); |
37 |
|
|
procedure GenerateButtonClick(Sender: TObject); |
38 |
|
|
procedure SQLMemoChange(Sender: TObject); |
39 |
|
|
procedure SQLPageShow(Sender: TObject); |
40 |
|
|
procedure StatementTypeClick(Sender: TObject); |
41 |
|
|
procedure TableNamesComboCloseUp(Sender: TObject); |
42 |
|
|
private |
43 |
|
|
{ private declarations } |
44 |
|
|
FDataSet: TIBDataSet; |
45 |
|
|
FIBSystemTables: TIBSystemTables; |
46 |
|
|
FDirty: boolean; |
47 |
|
|
FCurrentStatement: integer; |
48 |
|
|
FSelectSQL: TStringList; |
49 |
|
|
FModifySQL: TStringList; |
50 |
|
|
FInsertSQL: TStringList; |
51 |
|
|
FDeleteSQL: TStringList; |
52 |
|
|
FRefreshSQL: TStringList; |
53 |
|
|
procedure UpdateSQLMemo; |
54 |
|
|
public |
55 |
|
|
{ public declarations } |
56 |
|
|
constructor Create(TheOwner: TComponent); override; |
57 |
|
|
destructor Destroy; override; |
58 |
|
|
procedure SetDataSet(AObject: TIBDataSet); |
59 |
|
|
end; |
60 |
|
|
|
61 |
|
|
var |
62 |
|
|
IBDataSetEditorForm: TIBDataSetEditorForm; |
63 |
|
|
|
64 |
|
|
function EditIBDataSet(DataSet: TIBDataSet): boolean; |
65 |
|
|
|
66 |
|
|
implementation |
67 |
|
|
|
68 |
|
|
function EditIBDataSet(DataSet: TIBDataSet): boolean; |
69 |
|
|
begin |
70 |
|
|
Result := false; |
71 |
|
|
if assigned(DataSet) and assigned(DataSet.Database) then |
72 |
|
|
begin |
73 |
|
|
if not assigned(DataSet.Transaction) then |
74 |
|
|
begin |
75 |
|
|
ShowMessage('No Default Transaction!'); |
76 |
|
|
Exit |
77 |
|
|
end; |
78 |
|
|
|
79 |
|
|
DataSet.Database.Connected := true; |
80 |
|
|
end; |
81 |
|
|
|
82 |
|
|
with TIBDataSetEditorForm.Create(Application) do |
83 |
|
|
try |
84 |
|
|
SetDataSet(DataSet); |
85 |
|
|
Result := ShowModal = mrOK |
86 |
|
|
finally |
87 |
|
|
Free |
88 |
|
|
end; |
89 |
|
|
|
90 |
|
|
end; |
91 |
|
|
|
92 |
|
|
{ TIBDataSetEditorForm } |
93 |
|
|
|
94 |
|
|
procedure TIBDataSetEditorForm.FormShow(Sender: TObject); |
95 |
|
|
var TableName: string; |
96 |
|
|
begin |
97 |
|
|
PageControl.ActivePage := FieldsPage; |
98 |
|
|
FModifySQL.Assign(FDataSet.ModifySQL); |
99 |
|
|
FInsertSQL.Assign(FDataSet.InsertSQL); |
100 |
|
|
FDeleteSQL.Assign(FDataSet.DeleteSQL); |
101 |
|
|
FRefreshSQL.Assign(FDataSet.RefreshSQL); |
102 |
|
|
FSelectSQL.Assign(FDataSet.SelectSQL); |
103 |
|
|
FCurrentStatement := -1; |
104 |
|
|
TableNamesCombo.Items.Clear; |
105 |
|
|
FIBSystemTables.GetTableNames(TableNamesCombo.Items); |
106 |
|
|
if TableNamesCombo.Items.Count > 0 then |
107 |
|
|
begin |
108 |
|
|
TableNamesCombo.ItemIndex := 0; |
109 |
|
|
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); |
116 |
|
|
end; |
117 |
|
|
end; |
118 |
|
|
|
119 |
|
|
procedure TIBDataSetEditorForm.FormClose(Sender: TObject; |
120 |
|
|
var CloseAction: TCloseAction); |
121 |
|
|
begin |
122 |
|
|
if ModalResult = mrOK then |
123 |
|
|
begin |
124 |
|
|
UpdateSQLMemo; |
125 |
|
|
FDataSet.ModifySQL.Assign(FModifySQL); |
126 |
|
|
FDataSet.InsertSQL.Assign(FInsertSQL); |
127 |
|
|
FDataSet.DeleteSQL.Assign(FDeleteSQL); |
128 |
|
|
FDataSet.RefreshSQL.Assign(FRefreshSQL); |
129 |
|
|
FDataSet.SelectSQL.Assign(FSelectSQL); |
130 |
|
|
end; |
131 |
|
|
end; |
132 |
|
|
|
133 |
|
|
procedure TIBDataSetEditorForm.Button1Click(Sender: TObject); |
134 |
|
|
begin |
135 |
|
|
if SQLMemo.Lines.Text <> '' then |
136 |
|
|
FIBSystemTables.TestSQL(SQLMemo.Lines.Text); |
137 |
|
|
end; |
138 |
|
|
|
139 |
|
|
procedure TIBDataSetEditorForm.GenerateButtonClick(Sender: TObject); |
140 |
|
|
var FieldNames: TStringList; |
141 |
|
|
I: integer; |
142 |
|
|
begin |
143 |
|
|
FDataSet.RefreshSQL.Clear; |
144 |
|
|
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); |
153 |
|
|
|
154 |
|
|
end |
155 |
|
|
else |
156 |
|
|
begin |
157 |
|
|
FieldNames := TStringList.Create; |
158 |
|
|
try |
159 |
|
|
for I := 0 to FieldList.Items.Count - 1 do |
160 |
|
|
if FieldList.Selected[I] then |
161 |
|
|
FieldNames.Add(FieldList.Items[I]); |
162 |
|
|
FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked, |
163 |
|
|
FieldNames,FModifySQL); |
164 |
|
|
FIBSystemTables.GenerateInsertSQL(TableNamesCombo.Text,QuoteFields.Checked, |
165 |
|
|
FieldNames,FInsertSQL); |
166 |
|
|
finally |
167 |
|
|
FieldNames.Free |
168 |
|
|
end; |
169 |
|
|
end; |
170 |
|
|
FDirty := false; |
171 |
|
|
PageControl.ActivePage := SQLPage; |
172 |
|
|
end; |
173 |
|
|
|
174 |
|
|
procedure TIBDataSetEditorForm.SQLMemoChange(Sender: TObject); |
175 |
|
|
begin |
176 |
|
|
FDirty := true |
177 |
|
|
end; |
178 |
|
|
|
179 |
|
|
procedure TIBDataSetEditorForm.SQLPageShow(Sender: TObject); |
180 |
|
|
begin |
181 |
|
|
UpdateSQLMemo |
182 |
|
|
end; |
183 |
|
|
|
184 |
|
|
procedure TIBDataSetEditorForm.StatementTypeClick(Sender: TObject); |
185 |
|
|
begin |
186 |
|
|
UpdateSQLMemo |
187 |
|
|
end; |
188 |
|
|
|
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 |
|
|
|
195 |
|
|
procedure TIBDataSetEditorForm.UpdateSQLMemo; |
196 |
|
|
begin |
197 |
|
|
if FDirty then |
198 |
|
|
case FCurrentStatement of |
199 |
|
|
0: // Select |
200 |
|
|
FSelectSQL.Assign(SQLMemo.Lines); |
201 |
|
|
1: //Modify |
202 |
|
|
FModifySQL.Assign(SQLMemo.Lines); |
203 |
|
|
2: //Insert |
204 |
|
|
FInsertSQL.Assign(SQLMemo.Lines); |
205 |
|
|
3: // Delete |
206 |
|
|
FDeleteSQL.Assign(SQLMemo.Lines); |
207 |
|
|
4: //Refresh |
208 |
|
|
FRefreshSQL.Assign(SQLMemo.Lines); |
209 |
|
|
end; |
210 |
|
|
FDirty := false; |
211 |
|
|
case StatementType.ItemIndex of |
212 |
|
|
0: //Select |
213 |
|
|
SQLMemo.Lines.Assign(FSelectSQL); |
214 |
|
|
1: // Modify |
215 |
|
|
SQLMemo.Lines.Assign(FModifySQL) ; |
216 |
|
|
2: //Insert |
217 |
|
|
SQLMemo.Lines.Assign(FInsertSQL) ; |
218 |
|
|
3: // Delete |
219 |
|
|
SQLMemo.Lines.Assign(FDeleteSQL) ; |
220 |
|
|
4: //Refresh |
221 |
|
|
SQLMemo.Lines.Assign(FRefreshSQL) ; |
222 |
|
|
end; |
223 |
|
|
FCurrentStatement := StatementType.ItemIndex; |
224 |
|
|
end; |
225 |
|
|
|
226 |
|
|
constructor TIBDataSetEditorForm.Create(TheOwner: TComponent); |
227 |
|
|
begin |
228 |
|
|
inherited Create(TheOwner); |
229 |
|
|
FIBSystemTables := TIBSystemTables.Create; |
230 |
|
|
FModifySQL := TStringList.Create; |
231 |
|
|
FInsertSQL := TStringList.Create; |
232 |
|
|
FDeleteSQL := TStringList.Create; |
233 |
|
|
FRefreshSQL := TStringList.Create; |
234 |
|
|
FSelectSQL := TStringList.Create; |
235 |
|
|
end; |
236 |
|
|
|
237 |
|
|
destructor TIBDataSetEditorForm.Destroy; |
238 |
|
|
begin |
239 |
|
|
if assigned(FIBSystemTables) then FIBSystemTables.Free; |
240 |
|
|
if assigned(FModifySQL) then FModifySQL.Free; |
241 |
|
|
if assigned(FInsertSQL) then FInsertSQL.Free; |
242 |
|
|
if assigned(FDeleteSQL) then FDeleteSQL.Free; |
243 |
|
|
if assigned(FRefreshSQL) then FRefreshSQL.Free; |
244 |
|
|
if assigned(FSelectSQL) then FSelectSQL.Free; |
245 |
|
|
inherited Destroy; |
246 |
|
|
end; |
247 |
|
|
|
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 |
|
|
|
259 |
|
|
end. |
260 |
|
|
|