ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibupdatesqleditor.pas
Revision: 17
Committed: Sat Dec 28 19:22:24 2013 UTC (10 years, 10 months ago) by tony
Content type: text/x-pascal
File size: 8751 byte(s)
Log Message:
Committing updates for Release R1-0-5

File Contents

# User Rev Content
1 tony 17 (*
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     unit IBUpdateSQLEditor;
27    
28     {$mode objfpc}{$H+}
29    
30     interface
31    
32     uses
33     Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
34     ComCtrls, StdCtrls, ExtCtrls, IBSystemTables, IBUpdateSQL, IBDatabase;
35    
36     type
37    
38     { TIBUpdateSQLEditorForm }
39    
40     TIBUpdateSQLEditorForm = class(TForm)
41     TestBtn: TButton;
42     CancelButton: TButton;
43     FieldsPage: TTabSheet;
44     GenerateBtn: TButton;
45     GroupBox1: TGroupBox;
46     IBTransaction1: TIBTransaction;
47     IncludePrimaryKeys: TCheckBox;
48     PrimaryKeyList: TListBox;
49     Label1: TLabel;
50     Label2: TLabel;
51     Label3: TLabel;
52     Label4: TLabel;
53     OkButton: TButton;
54     PageControl: TPageControl;
55     QuoteFields: TCheckBox;
56     SQLMemo: TMemo;
57     SQLPage: TTabSheet;
58     StatementType: TRadioGroup;
59     FieldList: TListBox;
60     TableNamesCombo: TComboBox;
61     procedure TestBtnClick(Sender: TObject);
62     procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
63     procedure FormShow(Sender: TObject);
64     procedure GenerateBtnClick(Sender: TObject);
65     procedure SQLMemoChange(Sender: TObject);
66     procedure SQLPageShow(Sender: TObject);
67     procedure StatementTypeClick(Sender: TObject);
68     procedure TableNamesComboCloseUp(Sender: TObject);
69     private
70     { private declarations }
71     FUpdateObject: TIBUpdateSQL;
72     FIBSystemTables: TIBSystemTables;
73     FDirty: boolean;
74     FCurrentStatement: integer;
75     FModifySQL: TStringList;
76     FInsertSQL: TStringList;
77     FDeleteSQL: TStringList;
78     FRefreshSQL: TStringList;
79     procedure UpdateSQLMemo;
80     public
81     { public declarations }
82     constructor Create(TheOwner: TComponent); override;
83     destructor Destroy; override;
84     procedure SetUpdateObject(AObject: TIBUpdateSQL);
85     end;
86    
87     var
88     IBUpdateSQLEditorForm: TIBUpdateSQLEditorForm;
89    
90     function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
91    
92     implementation
93    
94     uses IBQuery;
95    
96     {$R *.lfm}
97    
98     function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
99     begin
100     Result := false;
101     if assigned(UpdateObject.DataSet) and assigned(UpdateObject.DataSet.Database) then
102     try
103     UpdateObject.DataSet.Database.Connected := true;
104     except on E: Exception do
105     ShowMessage(E.Message)
106     end;
107    
108     with TIBUpdateSQLEditorForm.Create(Application) do
109     try
110     SetUpdateObject(UpdateObject);
111     Result := ShowModal = mrOK
112     finally
113     Free
114     end;
115    
116     end;
117    
118     { TIBUpdateSQLEditorForm }
119    
120     procedure TIBUpdateSQLEditorForm.FormShow(Sender: TObject);
121     var TableName: string;
122     begin
123     GenerateBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected;
124     TestBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected;
125     PageControl.ActivePage := FieldsPage;
126     FModifySQL.Assign(FUpdateObject.ModifySQL);
127     FInsertSQL.Assign(FUpdateObject.InsertSQL);
128     FDeleteSQL.Assign(FUpdateObject.DeleteSQL);
129     FRefreshSQL.Assign(FUpdateObject.RefreshSQL);
130     FCurrentStatement := -1;
131     TableNamesCombo.Items.Clear;
132     FIBSystemTables.GetTableNames(TableNamesCombo.Items);
133     if TableNamesCombo.Items.Count > 0 then
134     begin
135     TableNamesCombo.ItemIndex := 0;
136     if assigned(FUpdateObject.DataSet) and (FUpdateObject.DataSet is TIBQuery) and
137     ((FUpdateObject.DataSet as TIBQuery).SQL.Text <> '') then
138     try
139     FIBSystemTables.GetTableAndColumns((FUpdateObject.DataSet as TIBQuery).SQL.Text,TableName,nil);
140     TableNamesCombo.ItemIndex := TableNamesCombo.Items.IndexOf(TableName);
141     except //ignore
142     end;
143     FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,IncludePrimaryKeys.checked,false);
144     FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items);
145     end;
146     end;
147    
148     procedure TIBUpdateSQLEditorForm.FormClose(Sender: TObject;
149     var CloseAction: TCloseAction);
150     begin
151     if ModalResult = mrOK then
152     begin
153     UpdateSQLMemo;
154     FUpdateObject.ModifySQL.Assign(FModifySQL);
155     FUpdateObject.InsertSQL.Assign(FInsertSQL);
156     FUpdateObject.DeleteSQL.Assign(FDeleteSQL);
157     FUpdateObject.RefreshSQL.Assign(FRefreshSQL);
158     end;
159     end;
160    
161     procedure TIBUpdateSQLEditorForm.TestBtnClick(Sender: TObject);
162     begin
163     if SQLMemo.Lines.Text <> '' then
164     FIBSystemTables.TestSQL(SQLMemo.Lines.Text);
165     end;
166    
167     procedure TIBUpdateSQLEditorForm.GenerateBtnClick(Sender: TObject);
168     var FieldNames: TStringList;
169     I: integer;
170     begin
171     FieldNames := TStringList.Create;
172     try
173     FUpdateObject.RefreshSQL.Clear;
174     FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldNames);
175     FIBSystemTables.GenerateRefreshSQL(TableNamesCombo.Text,QuoteFields.Checked,FieldNames,FRefreshSQL);
176     FIBSystemTables.GenerateDeleteSQL(TableNamesCombo.Text,QuoteFields.Checked,FDeleteSQL);
177     FieldNames.Clear;
178     FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldNames,true,false);
179     FIBSystemTables.GenerateInsertSQL(TableNamesCombo.Text,QuoteFields.Checked,
180     FieldNames,FInsertSQL);
181     if FieldList.SelCount = 0 then
182     begin
183     FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked,
184     FieldList.Items,FModifySQL);
185    
186     end
187     else
188     begin
189     FieldNames.Clear;
190     for I := 0 to FieldList.Items.Count - 1 do
191     if FieldList.Selected[I] then
192     FieldNames.Add(FieldList.Items[I]);
193     FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked,
194     FieldNames,FModifySQL);
195     end;
196     FDirty := false;
197     PageControl.ActivePage := SQLPage;
198     finally
199     FieldNames.Free
200     end;
201     end;
202    
203     procedure TIBUpdateSQLEditorForm.SQLMemoChange(Sender: TObject);
204     begin
205     FDirty := true
206     end;
207    
208     procedure TIBUpdateSQLEditorForm.SQLPageShow(Sender: TObject);
209     begin
210     UpdateSQLMemo
211     end;
212    
213     procedure TIBUpdateSQLEditorForm.StatementTypeClick(Sender: TObject);
214     begin
215     UpdateSQLMemo
216     end;
217    
218     procedure TIBUpdateSQLEditorForm.TableNamesComboCloseUp(Sender: TObject);
219     begin
220     FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,IncludePrimaryKeys.checked,false);
221     FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items);
222     end;
223    
224     procedure TIBUpdateSQLEditorForm.UpdateSQLMemo;
225     begin
226     if FDirty then
227     case FCurrentStatement of
228     0: //Modify
229     FModifySQL.Assign(SQLMemo.Lines);
230     1: //Insert
231     FInsertSQL.Assign(SQLMemo.Lines);
232     2: // Delete
233     FDeleteSQL.Assign(SQLMemo.Lines);
234     3: //Refresh
235     FRefreshSQL.Assign(SQLMemo.Lines);
236     end;
237     FDirty := false;
238     case StatementType.ItemIndex of
239     0: // Modify
240     SQLMemo.Lines.Assign(FModifySQL) ;
241     1: //Insert
242     SQLMemo.Lines.Assign(FInsertSQL) ;
243     2: // Delete
244     SQLMemo.Lines.Assign(FDeleteSQL) ;
245     3: //Refresh
246     SQLMemo.Lines.Assign(FRefreshSQL) ;
247     end;
248     FCurrentStatement := StatementType.ItemIndex;
249     end;
250    
251     constructor TIBUpdateSQLEditorForm.Create(TheOwner: TComponent);
252     begin
253     inherited Create(TheOwner);
254     FIBSystemTables := TIBSystemTables.Create;
255     FModifySQL := TStringList.Create;
256     FInsertSQL := TStringList.Create;
257     FDeleteSQL := TStringList.Create;
258     FRefreshSQL := TStringList.Create;
259     end;
260    
261     destructor TIBUpdateSQLEditorForm.Destroy;
262     begin
263     if assigned(FIBSystemTables) then FIBSystemTables.Free;
264     if assigned(FModifySQL) then FModifySQL.Free;
265     if assigned(FInsertSQL) then FInsertSQL.Free;
266     if assigned(FDeleteSQL) then FDeleteSQL.Free;
267     if assigned(FRefreshSQL) then FRefreshSQL.Free;
268     inherited Destroy;
269     end;
270    
271     procedure TIBUpdateSQLEditorForm.SetUpdateObject(AObject: TIBUpdateSQL);
272     begin
273     FUpdateObject := AObject;
274     if assigned(FUpdateObject.DataSet) then
275     begin
276     IBTransaction1.DefaultDatabase := FUpdateObject.DataSet.Database;
277     FIBSystemTables.SelectDatabase(FUpdateObject.DataSet.Database,IBTransaction1);
278     end;
279     end;
280    
281    
282     end.
283