ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/design/ibupdatesqleditor.pas
(Generate patch)

Comparing ibx/trunk/design/ibupdatesqleditor.pas (file contents):
Revision 5 by tony, Fri Feb 18 16:26:16 2011 UTC vs.
Revision 81 by tony, Mon Jan 1 11:31:10 2018 UTC

# Line 1 | Line 1
1 < unit IBUpdateSQLEditor;
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-17 Tony Whyman, MWA Software
19 > *  (http://www.mwasoftware.co.uk).
20 > *
21 > *  All Rights Reserved.
22 > *
23 > *  Contributor(s): ______________________________________.
24 > *
25 > *)
26 >
27 > unit ibupdatesqleditor;
28  
29   {$mode objfpc}{$H+}
30  
31   interface
32  
33   uses
34 <  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
35 <  ComCtrls, StdCtrls, ExtCtrls, IBSystemTables, IBUpdateSQL;
34 >  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
35 >  StdCtrls, ExtCtrls,  IBSQLEditFrame, IBCustomDataSet,
36 >  IBDatabase, IBLookupComboEditBox, IBDynamicGrid, Types, IBUpdateSQL;
37  
38   type
39  
40    { TIBUpdateSQLEditorForm }
41  
42    TIBUpdateSQLEditorForm = 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;
# Line 26 | Line 60 | type
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      FUpdateObject: TIBUpdateSQL;
45    FIBSystemTables: TIBSystemTables;
77      FDirty: boolean;
78      FCurrentStatement: integer;
79      FModifySQL: TStringList;
# Line 50 | Line 81 | type
81      FDeleteSQL: TStringList;
82      FRefreshSQL: TStringList;
83      procedure UpdateSQLMemo;
84 +    procedure HandleUserTablesOpened(Sender: TObject);
85 +  protected
86 +    procedure Loaded; override;
87    public
88      { public declarations }
89      constructor Create(TheOwner: TComponent); override;
90      destructor Destroy; override;
57    procedure SetUpdateObject(AObject: TIBUpdateSQL);
91    end;
92  
93   var
94    IBUpdateSQLEditorForm: TIBUpdateSQLEditorForm;
95  
96 < function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
96 >  function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
97  
98   implementation
99  
100 < uses IBQuery;
100 > {$R *.lfm}
101  
102   function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
103   begin
104    Result := false;
105 <  if assigned(UpdateObject.DataSet) and assigned(UpdateObject.DataSet.Database) then
106 <  begin
74 <    if not assigned(UpdateObject.DataSet.Transaction) then
75 <    begin
76 <      ShowMessage('No Default Transaction!');
77 <      Exit
78 <    end;
79 <
105 >  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  
112    with TIBUpdateSQLEditorForm.Create(Application) do
113    try
114 <    SetUpdateObject(UpdateObject);
115 <    Result := ShowModal = mrOK
114 >    if assigned(UpdateObject) and assigned(UpdateObject.DataSet) then
115 >    begin
116 >      IBSQLEditFrame1.Database := UpdateObject.DataSet.Database;
117 >      GenerateParams.Checked := UpdateObject.DataSet.GenerateParamNames;
118 >    end;
119 >    FUpdateObject := UpdateObject;
120 >    Result := ShowModal = mrOK;
121 >    if Result then
122 >      UpdateObject.DataSet.GenerateParamNames := GenerateParams.Checked
123    finally
124      Free
125    end;
# Line 100 | Line 136 | begin
136    FInsertSQL.Assign(FUpdateObject.InsertSQL);
137    FDeleteSQL.Assign(FUpdateObject.DeleteSQL);
138    FRefreshSQL.Assign(FUpdateObject.RefreshSQL);
139 +  GenerateButton.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
140 +  TestBtn.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
141    FCurrentStatement := -1;
142 <  TableNamesCombo.Items.Clear;
105 <  FIBSystemTables.GetTableNames(TableNamesCombo.Items);
106 <  if TableNamesCombo.Items.Count > 0 then
142 >  if (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected then
143    begin
144 <    TableNamesCombo.ItemIndex := 0;
145 <    if assigned(FUpdateObject.DataSet) and (FUpdateObject.DataSet is TIBQuery) and
110 <       ((FUpdateObject.DataSet as TIBQuery).SQL.Text <> '') then
111 <    try
112 <       FIBSystemTables.GetTableAndColumns((FUpdateObject.DataSet as TIBQuery).SQL.Text,TableName,nil);
113 <       TableNamesCombo.ItemIndex := TableNamesCombo.Items.IndexOf(TableName);
114 <    except  //ignore
115 <    end;
116 <    FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,false);
117 <    FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items);
144 >    IBSQLEditFrame1.UserTables.Active := true;
145 >    IBSQLEditFrame1.SyncQueryBuilder(FRefreshSQL);
146    end;
147   end;
148  
# Line 131 | Line 159 | begin
159    end;
160   end;
161  
162 < procedure TIBUpdateSQLEditorForm.Button1Click(Sender: TObject);
162 > procedure TIBUpdateSQLEditorForm.TestBtnClick(Sender: TObject);
163   begin
164 <  if SQLMemo.Lines.Text <> '' then
137 <    FIBSystemTables.TestSQL(SQLMemo.Lines.Text);
164 >  IBSQLEditFrame1.TestSQL(GenerateParams.Checked);
165   end;
166  
167 < procedure TIBUpdateSQLEditorForm.GenerateButtonClick(Sender: TObject);
141 < var FieldNames: TStringList;
142 <    I: integer;
167 > procedure TIBUpdateSQLEditorForm.IncludeSysTablesChange(Sender: TObject);
168   begin
169 <  FUpdateObject.RefreshSQL.Clear;
170 <  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);
169 >  IBSQLEditFrame1.IncludeSystemTables := IncludeSysTables.Checked;
170 > end;
171  
172 <  end
173 <  else
174 <  begin
175 <    FieldNames := TStringList.Create;
176 <    try
177 <      for I := 0 to FieldList.Items.Count - 1 do
178 <        if FieldList.Selected[I] then
179 <          FieldNames.Add(FieldList.Items[I]);
180 <      FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked,
181 <        FieldNames,FModifySQL);
182 <    FIBSystemTables.GenerateInsertSQL(TableNamesCombo.Text,QuoteFields.Checked,
165 <      FieldNames,FInsertSQL);
166 <    finally
167 <      FieldNames.Free
168 <    end;
169 <  end;
172 > procedure TIBUpdateSQLEditorForm.SelectSelectAllClick(Sender: TObject);
173 > begin
174 >  IBSQLEditFrame1.SelectAllFields(SelectSelectAll.Checked);
175 > end;
176 >
177 > 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;
# Line 186 | Line 199 | begin
199    UpdateSQLMemo
200   end;
201  
189 procedure TIBUpdateSQLEditorForm.TableNamesComboCloseUp(Sender: TObject);
190 begin
191  FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,false);
192  FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items);
193 end;
194
202   procedure TIBUpdateSQLEditorForm.UpdateSQLMemo;
203   begin
204    if FDirty then
205      case FCurrentStatement of
206      0: //Modify
207 <        FModifySQL.Assign(SQLMemo.Lines);
207 >        FModifySQL.Assign(IBSQLEditFrame1.SQLText.Lines);
208      1: //Insert
209 <        FInsertSQL.Assign(SQLMemo.Lines);
209 >        FInsertSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
210      2: // Delete
211 <        FDeleteSQL.Assign(SQLMemo.Lines);
211 >        FDeleteSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
212      3: //Refresh
213 <        FRefreshSQL.Assign(SQLMemo.Lines);
213 >        FRefreshSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
214      end;
215    FDirty := false;
216    case StatementType.ItemIndex of
217    0: // Modify
218 <    SQLMemo.Lines.Assign(FModifySQL)  ;
218 >    IBSQLEditFrame1.SQLText.Lines.Assign(FModifySQL)  ;
219    1: //Insert
220 <    SQLMemo.Lines.Assign(FInsertSQL)  ;
220 >    IBSQLEditFrame1.SQLText.Lines.Assign(FInsertSQL)  ;
221    2: // Delete
222 <    SQLMemo.Lines.Assign(FDeleteSQL)  ;
222 >    IBSQLEditFrame1.SQLText.Lines.Assign(FDeleteSQL)  ;
223    3: //Refresh
224 <    SQLMemo.Lines.Assign(FRefreshSQL)  ;
224 >    IBSQLEditFrame1.SQLText.Lines.Assign(FRefreshSQL)  ;
225    end;
226    FCurrentStatement := StatementType.ItemIndex;
227   end;
228  
229 + 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   constructor TIBUpdateSQLEditorForm.Create(TheOwner: TComponent);
250   begin
251    inherited Create(TheOwner);
225  FIBSystemTables := TIBSystemTables.Create;
252    FModifySQL := TStringList.Create;
253    FInsertSQL := TStringList.Create;
254    FDeleteSQL := TStringList.Create;
# Line 231 | Line 257 | end;
257  
258   destructor TIBUpdateSQLEditorForm.Destroy;
259   begin
234  if assigned(FIBSystemTables) then FIBSystemTables.Free;
260    if assigned(FModifySQL) then FModifySQL.Free;
261    if assigned(FInsertSQL) then FInsertSQL.Free;
262    if assigned(FDeleteSQL) then FDeleteSQL.Free;
# Line 239 | Line 264 | begin
264    inherited Destroy;
265   end;
266  
242 procedure TIBUpdateSQLEditorForm.SetUpdateObject(AObject: TIBUpdateSQL);
243 begin
244  FUpdateObject := AObject;
245  if assigned(FUpdateObject.DataSet) then
246    FIBSystemTables.SelectDatabase(FUpdateObject.DataSet.Database,FUpdateObject.DataSet.Transaction);
247 end;
248
249
250 initialization
251  {$I ibupdatesqleditor.lrs}
252
267   end.
254

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines