ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibdataseteditor.pas
Revision: 107
Committed: Thu Jan 18 14:37:40 2018 UTC (6 years, 9 months ago) by tony
Content type: text/x-pascal
File size: 8536 byte(s)
Log Message:
Fixes merged

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     * 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 tony 80 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
35     StdCtrls, ExtCtrls, IBSQLEditFrame, IBCustomDataSet,
36     IBDatabase, IBLookupComboEditBox, IBDynamicGrid, Types;
37 tony 33
38     type
39    
40     { TIBDataSetEditorForm }
41    
42     TIBDataSetEditorForm = class(TForm)
43 tony 80 FieldNamesGrid: TIBDynamicGrid;
44 tony 33 GenerateParams: TCheckBox;
45 tony 80 IBSQLEditFrame1: TIBSQLEditFrame;
46     IncludeSysTables: TCheckBox;
47 tony 105 Label5: TLabel;
48     Label6: TLabel;
49 tony 80 PrimaryKeysGrid: TIBDynamicGrid;
50 tony 105 IdentityGrid: TIBDynamicGrid;
51     ReadOnlyGrid: TIBDynamicGrid;
52 tony 80 SelectSelectAll: TCheckBox;
53     SelectTableNames: TIBLookupComboEditBox;
54 tony 33 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 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     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 tony 80 procedure HandleUserTablesOpened(Sender: TObject);
90     protected
91     procedure Loaded; override;
92 tony 33 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 tony 80 begin
121     IBSQLEditFrame1.Database := DataSet.Database;
122     GenerateParams.Checked := DataSet.GenerateParamNames;
123     end;
124     FDataSet := DataSet;
125 tony 105 with IBSQLEditFrame1 do
126 tony 107 begin
127 tony 105 IncludeReadOnlyFields := false;
128 tony 107 end;
129 tony 33 Result := ShowModal = mrOK;
130     if Result and assigned(DataSet) then
131     DataSet.GenerateParamNames := GenerateParams.Checked
132     finally
133     Free
134     end;
135    
136     end;
137    
138     { TIBDataSetEditorForm }
139    
140     procedure TIBDataSetEditorForm.FormShow(Sender: TObject);
141     var TableName: string;
142     begin
143     PageControl.ActivePage := FieldsPage;
144     FModifySQL.Assign(FDataSet.ModifySQL);
145     FInsertSQL.Assign(FDataSet.InsertSQL);
146     FDeleteSQL.Assign(FDataSet.DeleteSQL);
147     FRefreshSQL.Assign(FDataSet.RefreshSQL);
148     FSelectSQL.Assign(FDataSet.SelectSQL);
149 tony 80 GenerateButton.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
150     TestBtn.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
151 tony 33 FCurrentStatement := -1;
152 tony 81 if (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected then
153     begin
154     IBSQLEditFrame1.UserTables.Active := true;
155     IBSQLEditFrame1.SyncQueryBuilder(FSelectSQL);
156     end;
157 tony 33 end;
158    
159     procedure TIBDataSetEditorForm.FormClose(Sender: TObject;
160     var CloseAction: TCloseAction);
161     begin
162     if ModalResult = mrOK then
163     begin
164     UpdateSQLMemo;
165     FDataSet.ModifySQL.Assign(FModifySQL);
166     FDataSet.InsertSQL.Assign(FInsertSQL);
167     FDataSet.DeleteSQL.Assign(FDeleteSQL);
168     FDataSet.RefreshSQL.Assign(FRefreshSQL);
169     FDataSet.SelectSQL.Assign(FSelectSQL);
170     end;
171     end;
172    
173     procedure TIBDataSetEditorForm.TestBtnClick(Sender: TObject);
174     begin
175 tony 80 IBSQLEditFrame1.TestSQL(GenerateParams.Checked);
176 tony 33 end;
177    
178 tony 80 procedure TIBDataSetEditorForm.IncludeSysTablesChange(Sender: TObject);
179 tony 33 begin
180 tony 80 IBSQLEditFrame1.IncludeSystemTables := IncludeSysTables.Checked;
181     end;
182 tony 33
183 tony 80 procedure TIBDataSetEditorForm.SelectSelectAllClick(Sender: TObject);
184     begin
185     IBSQLEditFrame1.SelectAllFields(SelectSelectAll.Checked);
186 tony 33 end;
187    
188 tony 80 procedure TIBDataSetEditorForm.GenerateButtonClick(Sender: TObject);
189     begin
190 tony 106 IBSQLEditFrame1.GenerateSelectSQL(QuoteFields.Checked,FSelectSQL,true);
191     IBSQLEditFrame1.GenerateRefreshSQL(QuoteFields.Checked,FRefreshSQL,true);
192 tony 80 IBSQLEditFrame1.GenerateDeleteSQL(QuoteFields.Checked,FDeleteSQL);
193     IBSQLEditFrame1.GenerateInsertSQL(QuoteFields.Checked,FInsertSQL);
194 tony 106 IBSQLEditFrame1.GenerateModifySQL(QuoteFields.Checked,FModifySQL, IncludePrimaryKeys.Checked);
195 tony 80 FDirty := false;
196     PageControl.ActivePage := SQLPage;
197     end;
198    
199 tony 33 procedure TIBDataSetEditorForm.SQLMemoChange(Sender: TObject);
200     begin
201     FDirty := true
202     end;
203    
204     procedure TIBDataSetEditorForm.SQLPageShow(Sender: TObject);
205     begin
206     UpdateSQLMemo
207     end;
208    
209     procedure TIBDataSetEditorForm.StatementTypeClick(Sender: TObject);
210     begin
211     UpdateSQLMemo
212     end;
213    
214     procedure TIBDataSetEditorForm.UpdateSQLMemo;
215     begin
216     if FDirty then
217     case FCurrentStatement of
218     0: // Select
219 tony 80 FSelectSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
220 tony 33 1: //Modify
221 tony 80 FModifySQL.Assign(IBSQLEditFrame1.SQLText.Lines);
222 tony 33 2: //Insert
223 tony 80 FInsertSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
224 tony 33 3: // Delete
225 tony 80 FDeleteSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
226 tony 33 4: //Refresh
227 tony 80 FRefreshSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
228 tony 33 end;
229     FDirty := false;
230     case StatementType.ItemIndex of
231     0: //Select
232 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FSelectSQL);
233 tony 33 1: // Modify
234 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FModifySQL) ;
235 tony 33 2: //Insert
236 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FInsertSQL) ;
237 tony 33 3: // Delete
238 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FDeleteSQL) ;
239 tony 33 4: //Refresh
240 tony 80 IBSQLEditFrame1.SQLText.Lines.Assign(FRefreshSQL) ;
241 tony 33 end;
242     FCurrentStatement := StatementType.ItemIndex;
243     end;
244    
245 tony 80 procedure TIBDataSetEditorForm.HandleUserTablesOpened(Sender: TObject);
246     begin
247     SelectSelectAll.Checked := true;
248     end;
249    
250     procedure TIBDataSetEditorForm.Loaded;
251     begin
252     inherited Loaded;
253     if IBSQLEditFrame1 <> nil then
254     begin
255     IBSQLEditFrame1.OnUserTablesOpened := @HandleUserTablesOpened;
256     if SelectTableNames <> nil then
257     SelectTableNames.ListSource := IBSQLEditFrame1.UserTableSource;
258     if FieldNamesGrid <> nil then
259     FieldNamesGrid.DataSource := IBSQLEditFrame1.FieldsSource;
260     if PrimaryKeysGrid <> nil then
261     PrimaryKeysGrid.DataSource := IBSQLEditFrame1.PrimaryKeySource;
262 tony 105 if IdentityGrid <> nil then
263     IdentityGrid.DataSource := IBSQLEditFrame1.IdentityColsSource;
264     if ReadOnlyGrid <> nil then
265     ReadOnlyGrid.DataSource := IBSQLEditFrame1.ReadOnlyFieldsSource;
266 tony 80 end;
267     end;
268    
269 tony 33 constructor TIBDataSetEditorForm.Create(TheOwner: TComponent);
270     begin
271     inherited Create(TheOwner);
272     FModifySQL := TStringList.Create;
273     FInsertSQL := TStringList.Create;
274     FDeleteSQL := TStringList.Create;
275     FRefreshSQL := TStringList.Create;
276     FSelectSQL := TStringList.Create;
277     end;
278    
279     destructor TIBDataSetEditorForm.Destroy;
280     begin
281     if assigned(FModifySQL) then FModifySQL.Free;
282     if assigned(FInsertSQL) then FInsertSQL.Free;
283     if assigned(FDeleteSQL) then FDeleteSQL.Free;
284     if assigned(FRefreshSQL) then FRefreshSQL.Free;
285 tony 80 if assigned(FSelectSQL) then FSelectSQL.Free;
286 tony 33 inherited Destroy;
287     end;
288    
289     end.