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 209 by tony, Wed Mar 14 12:48:51 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, 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 >    IdentityGrid: TIBDynamicGrid;
47 >    IncludeSysTables: TCheckBox;
48 >    Label5: TLabel;
49 >    Label6: TLabel;
50 >    PrimaryKeysGrid: TIBDynamicGrid;
51 >    ReadOnlyGrid: TIBDynamicGrid;
52 >    SelectSelectAll: TCheckBox;
53 >    SelectTableNames: TIBLookupComboEditBox;
54 >    TestBtn: TButton;
55      CancelButton: TButton;
56      FieldsPage: TTabSheet;
57      GenerateButton: TButton;
58      GroupBox1: TGroupBox;
59 <    PrimaryKeyList: TListBox;
59 >    IncludePrimaryKeys: TCheckBox;
60      Label1: TLabel;
61      Label2: TLabel;
62      Label3: TLabel;
# Line 26 | Line 64 | type
64      OkButton: TButton;
65      PageControl: TPageControl;
66      QuoteFields: TCheckBox;
29    SQLMemo: TMemo;
67      SQLPage: TTabSheet;
68      StatementType: TRadioGroup;
69 <    FieldList: TListBox;
70 <    TableNamesCombo: TComboBox;
71 <    procedure Button1Click(Sender: TObject);
69 >    procedure IncludeSysTablesChange(Sender: TObject);
70 >    procedure SelectSelectAllClick(Sender: TObject);
71 >    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);
41    procedure TableNamesComboCloseUp(Sender: TObject);
78    private
79      { private declarations }
80      FUpdateObject: TIBUpdateSQL;
45    FIBSystemTables: TIBSystemTables;
81      FDirty: boolean;
82      FCurrentStatement: integer;
83      FModifySQL: TStringList;
# Line 50 | Line 85 | type
85      FDeleteSQL: TStringList;
86      FRefreshSQL: TStringList;
87      procedure UpdateSQLMemo;
88 +    procedure HandleUserTablesOpened(Sender: TObject);
89 +  protected
90 +    procedure Loaded; override;
91    public
92      { public declarations }
93      constructor Create(TheOwner: TComponent); override;
94      destructor Destroy; override;
57    procedure SetUpdateObject(AObject: TIBUpdateSQL);
95    end;
96  
97   var
98    IBUpdateSQLEditorForm: TIBUpdateSQLEditorForm;
99  
100 < function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
100 >  function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
101  
102   implementation
103  
104 < uses IBQuery;
104 > {$R *.lfm}
105  
106   function EditIBUpdateSQL(UpdateObject: TIBUpdateSQL): boolean;
107   begin
108    Result := false;
109 <  if assigned(UpdateObject.DataSet) and assigned(UpdateObject.DataSet.Database) then
110 <  begin
74 <    if not assigned(UpdateObject.DataSet.Transaction) then
75 <    begin
76 <      ShowMessage('No Default Transaction!');
77 <      Exit
78 <    end;
79 <
109 >  if assigned(UpdateObject) and assigned(UpdateObject.DataSet) and assigned(UpdateObject.DataSet.Database) then
110 >  try
111      UpdateObject.DataSet.Database.Connected := true;
112 +  except on E: Exception do
113 +    ShowMessage(E.Message)
114    end;
115  
116    with TIBUpdateSQLEditorForm.Create(Application) do
117    try
118 <    SetUpdateObject(UpdateObject);
119 <    Result := ShowModal = mrOK
118 >    if assigned(UpdateObject) and assigned(UpdateObject.DataSet) then
119 >    begin
120 >      IBSQLEditFrame1.Database := UpdateObject.DataSet.Database;
121 >      GenerateParams.Checked := UpdateObject.DataSet.GenerateParamNames;
122 >    end;
123 >    with IBSQLEditFrame1 do
124 >      IncludeReadOnlyFields := false;
125 >    FUpdateObject := UpdateObject;
126 >    Result := ShowModal = mrOK;
127 >    if Result then
128 >      UpdateObject.DataSet.GenerateParamNames := GenerateParams.Checked
129    finally
130      Free
131    end;
# Line 93 | Line 135 | end;
135   { TIBUpdateSQLEditorForm }
136  
137   procedure TIBUpdateSQLEditorForm.FormShow(Sender: TObject);
96 var TableName: string;
138   begin
139    PageControl.ActivePage := FieldsPage;
140    FModifySQL.Assign(FUpdateObject.ModifySQL);
141    FInsertSQL.Assign(FUpdateObject.InsertSQL);
142    FDeleteSQL.Assign(FUpdateObject.DeleteSQL);
143    FRefreshSQL.Assign(FUpdateObject.RefreshSQL);
144 +  GenerateButton.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
145 +  TestBtn.Enabled := (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected;
146    FCurrentStatement := -1;
147 <  TableNamesCombo.Items.Clear;
105 <  FIBSystemTables.GetTableNames(TableNamesCombo.Items);
106 <  if TableNamesCombo.Items.Count > 0 then
147 >  if (IBSQLEditFrame1.Database <> nil) and IBSQLEditFrame1.Database.Connected then
148    begin
149 <    TableNamesCombo.ItemIndex := 0;
150 <    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);
149 >    IBSQLEditFrame1.UserTables.Active := true;
150 >    IBSQLEditFrame1.SyncQueryBuilder(FRefreshSQL);
151    end;
152   end;
153  
# Line 131 | Line 164 | begin
164    end;
165   end;
166  
167 < procedure TIBUpdateSQLEditorForm.Button1Click(Sender: TObject);
167 > procedure TIBUpdateSQLEditorForm.TestBtnClick(Sender: TObject);
168   begin
169 <  if SQLMemo.Lines.Text <> '' then
137 <    FIBSystemTables.TestSQL(SQLMemo.Lines.Text);
169 >  IBSQLEditFrame1.TestSQL(GenerateParams.Checked);
170   end;
171  
172 < procedure TIBUpdateSQLEditorForm.GenerateButtonClick(Sender: TObject);
141 < var FieldNames: TStringList;
142 <    I: integer;
172 > procedure TIBUpdateSQLEditorForm.IncludeSysTablesChange(Sender: TObject);
173   begin
174 <  FUpdateObject.RefreshSQL.Clear;
175 <  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);
174 >  IBSQLEditFrame1.IncludeSystemTables := IncludeSysTables.Checked;
175 > end;
176  
177 <  end
178 <  else
179 <  begin
180 <    FieldNames := TStringList.Create;
181 <    try
182 <      for I := 0 to FieldList.Items.Count - 1 do
183 <        if FieldList.Selected[I] then
184 <          FieldNames.Add(FieldList.Items[I]);
185 <      FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked,
186 <        FieldNames,FModifySQL);
187 <    FIBSystemTables.GenerateInsertSQL(TableNamesCombo.Text,QuoteFields.Checked,
165 <      FieldNames,FInsertSQL);
166 <    finally
167 <      FieldNames.Free
168 <    end;
169 <  end;
177 > procedure TIBUpdateSQLEditorForm.SelectSelectAllClick(Sender: TObject);
178 > begin
179 >  IBSQLEditFrame1.SelectAllFields(SelectSelectAll.Checked);
180 > end;
181 >
182 > procedure TIBUpdateSQLEditorForm.GenerateButtonClick(Sender: TObject);
183 > begin
184 >  IBSQLEditFrame1.GenerateRefreshSQL(QuoteFields.Checked,FRefreshSQL,true);
185 >  IBSQLEditFrame1.GenerateDeleteSQL(QuoteFields.Checked,FDeleteSQL);
186 >  IBSQLEditFrame1.GenerateInsertSQL(QuoteFields.Checked,FInsertSQL);
187 >  IBSQLEditFrame1.GenerateModifySQL(QuoteFields.Checked,FModifySQL, IncludePrimaryKeys.Checked);
188    FDirty := false;
189    PageControl.ActivePage := SQLPage;
190   end;
# Line 186 | Line 204 | begin
204    UpdateSQLMemo
205   end;
206  
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
207   procedure TIBUpdateSQLEditorForm.UpdateSQLMemo;
208   begin
209    if FDirty then
210      case FCurrentStatement of
211      0: //Modify
212 <        FModifySQL.Assign(SQLMemo.Lines);
212 >        FModifySQL.Assign(IBSQLEditFrame1.SQLText.Lines);
213      1: //Insert
214 <        FInsertSQL.Assign(SQLMemo.Lines);
214 >        FInsertSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
215      2: // Delete
216 <        FDeleteSQL.Assign(SQLMemo.Lines);
216 >        FDeleteSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
217      3: //Refresh
218 <        FRefreshSQL.Assign(SQLMemo.Lines);
218 >        FRefreshSQL.Assign(IBSQLEditFrame1.SQLText.Lines);
219      end;
220    FDirty := false;
221    case StatementType.ItemIndex of
222    0: // Modify
223 <    SQLMemo.Lines.Assign(FModifySQL)  ;
223 >    IBSQLEditFrame1.SQLText.Lines.Assign(FModifySQL)  ;
224    1: //Insert
225 <    SQLMemo.Lines.Assign(FInsertSQL)  ;
225 >    IBSQLEditFrame1.SQLText.Lines.Assign(FInsertSQL)  ;
226    2: // Delete
227 <    SQLMemo.Lines.Assign(FDeleteSQL)  ;
227 >    IBSQLEditFrame1.SQLText.Lines.Assign(FDeleteSQL)  ;
228    3: //Refresh
229 <    SQLMemo.Lines.Assign(FRefreshSQL)  ;
229 >    IBSQLEditFrame1.SQLText.Lines.Assign(FRefreshSQL)  ;
230    end;
231    FCurrentStatement := StatementType.ItemIndex;
232   end;
233  
234 + procedure TIBUpdateSQLEditorForm.HandleUserTablesOpened(Sender: TObject);
235 + begin
236 +  SelectSelectAll.Checked := true;
237 + end;
238 +
239 + procedure TIBUpdateSQLEditorForm.Loaded;
240 + begin
241 +  inherited Loaded;
242 +  if IBSQLEditFrame1 <> nil then
243 +  begin
244 +    IBSQLEditFrame1.OnUserTablesOpened := @HandleUserTablesOpened;
245 +    if SelectTableNames <> nil then
246 +      SelectTableNames.ListSource :=  IBSQLEditFrame1.UserTableSource;
247 +    if FieldNamesGrid <> nil then
248 +      FieldNamesGrid.DataSource := IBSQLEditFrame1.FieldsSource;
249 +    if PrimaryKeysGrid <> nil then
250 +      PrimaryKeysGrid.DataSource := IBSQLEditFrame1.PrimaryKeySource;
251 +    if IdentityGrid <> nil then
252 +      IdentityGrid.DataSource := IBSQLEditFrame1.IdentityColsSource;
253 +    if ReadOnlyGrid <> nil then
254 +      ReadOnlyGrid.DataSource := IBSQLEditFrame1.ReadOnlyFieldsSource;
255 +  end;
256 + end;
257 +
258   constructor TIBUpdateSQLEditorForm.Create(TheOwner: TComponent);
259   begin
260    inherited Create(TheOwner);
225  FIBSystemTables := TIBSystemTables.Create;
261    FModifySQL := TStringList.Create;
262    FInsertSQL := TStringList.Create;
263    FDeleteSQL := TStringList.Create;
# Line 231 | Line 266 | end;
266  
267   destructor TIBUpdateSQLEditorForm.Destroy;
268   begin
234  if assigned(FIBSystemTables) then FIBSystemTables.Free;
269    if assigned(FModifySQL) then FModifySQL.Free;
270    if assigned(FInsertSQL) then FInsertSQL.Free;
271    if assigned(FDeleteSQL) then FDeleteSQL.Free;
# Line 239 | Line 273 | begin
273    inherited Destroy;
274   end;
275  
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
276   end.
254

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines