ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibupdatesqleditor.pas
Revision: 81
Committed: Mon Jan 1 11:31:10 2018 UTC (6 years, 3 months ago) by tony
Content type: text/x-pascal
File size: 7983 byte(s)
Log Message:
Protect missing database name error in property editor

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