--- ibx/trunk/design/ibmodifysqleditor.pas 2011/02/18 16:26:16 6 +++ ibx/trunk/design/ibmodifysqleditor.pas 2012/08/05 18:28:19 7 @@ -1,3 +1,29 @@ +(* + * IBX For Lazarus (Firebird Express) + * + * The contents of this file are subject to the Initial Developer's + * Public License Version 1.0 (the "License"); you may not use this + * file except in compliance with the License. You may obtain a copy + * of the License here: + * + * http://www.firebirdsql.org/index.php?op=doc&id=idpl + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing rights + * and limitations under the License. + * + * The Initial Developer of the Original Code is Tony Whyman. + * + * The Original Code is (C) 2011 Tony Whyman, MWA Software + * (http://www.mwasoftware.co.uk). + * + * All Rights Reserved. + * + * Contributor(s): ______________________________________. + * +*) + unit ibmodifysqleditor; {$mode objfpc}{$H+} @@ -5,7 +31,7 @@ unit ibmodifysqleditor; interface uses - Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs, + Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, IBSystemTables, IBDatabase; type @@ -15,8 +41,10 @@ type TIBModifySQLEditorForm = class(TForm) Button1: TButton; Button2: TButton; - Button3: TButton; - Button4: TButton; + GenerateBtn: TButton; + TestBtn: TButton; + IBTransaction1: TIBTransaction; + IncludePrimaryKeys: TCheckBox; FieldList: TListBox; Label1: TLabel; Label2: TLabel; @@ -26,8 +54,8 @@ type QuoteFields: TCheckBox; SQLText: TMemo; TableNamesCombo: TComboBox; - procedure Button3Click(Sender: TObject); - procedure Button4Click(Sender: TObject); + procedure GenerateBtnClick(Sender: TObject); + procedure TestBtnClick(Sender: TObject); procedure FieldListDblClick(Sender: TObject); procedure FormShow(Sender: TObject); procedure PrimaryKeyListDblClick(Sender: TObject); @@ -39,37 +67,31 @@ type { public declarations } constructor Create(TheOwner: TComponent); override; destructor Destroy; override; - procedure SetDatabase(Database: TIBDatabase; Transaction: TIBTransaction); + procedure SetDatabase(Database: TIBDatabase); end; var IBModifySQLEditorForm: TIBModifySQLEditorForm; -function EditSQL(Database: TIBDatabase; Transaction: TIBTransaction; SelectSQL: TStrings): boolean; +function EditSQL(Database: TIBDatabase; SelectSQL: TStrings): boolean; implementation -function EditSQL(Database: TIBDatabase; Transaction: TIBTransaction; SelectSQL: TStrings): boolean; +{$R *.lfm} + +function EditSQL(Database: TIBDatabase; SelectSQL: TStrings): boolean; begin Result := false; if assigned(Database) then - begin - if not assigned(Transaction) then - begin - if not assigned(Database.DefaultTransaction)then - begin - ShowMessage('No Default Transaction!'); - Exit - end; - Transaction := Database.DefaultTransaction + try + Database.Connected := true; + except on E: Exception do + ShowMessage(E.Message) end; - Database.Connected := true; - end; - with TIBModifySQLEditorForm.Create(Application) do try - SetDatabase(Database,Transaction); + SetDatabase(Database); SQLText.Lines.Assign(SelectSQL); Result := ShowModal = mrOK; if Result then @@ -82,15 +104,25 @@ end; { TIBModifySQLEditorForm } procedure TIBModifySQLEditorForm.FormShow(Sender: TObject); +var TableName: string; begin + GenerateBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected; + TestBtn.Enabled := (IBTransaction1.DefaultDatabase <> nil) and IBTransaction1.DefaultDatabase.Connected; TableNamesCombo.Items.Clear; + try FIBSystemTables.GetTableNames(TableNamesCombo.Items); if TableNamesCombo.Items.Count > 0 then begin TableNamesCombo.ItemIndex := 0; - FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,false); + if Trim(SQLText.Text) <> '' then + begin + FIBSystemTables.GetTableAndColumns(SQLText.Text,TableName,nil); + TableNamesCombo.ItemIndex := TableNamesCombo.Items.IndexOf(TableName) + end; + FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,IncludePrimaryKeys.checked,false); FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items); end; + except {ignore} end; end; procedure TIBModifySQLEditorForm.PrimaryKeyListDblClick(Sender: TObject); @@ -105,36 +137,26 @@ begin SQLText.SetFocus end; -procedure TIBModifySQLEditorForm.Button3Click(Sender: TObject); -var FieldNames: TStringList; - I: integer; +procedure TIBModifySQLEditorForm.GenerateBtnClick(Sender: TObject); +var FieldNames: TStrings; begin - if FieldList.SelCount = 0 then + FieldNames := FIBSystemTables.GetFieldNames(FieldList); + try FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked, - FieldList.Items,SQLText.Lines) - else - begin - FieldNames := TStringList.Create; - try - for I := 0 to FieldList.Items.Count - 1 do - if FieldList.Selected[I] then - FieldNames.Add(FieldList.Items[I]); - FIBSystemTables.GenerateModifySQL(TableNamesCombo.Text,QuoteFields.Checked, - FieldNames,SQLText.Lines) - finally - FieldNames.Free - end; + FieldNames,SQLText.Lines) + finally + FieldNames.Free end; end; -procedure TIBModifySQLEditorForm.Button4Click(Sender: TObject); +procedure TIBModifySQLEditorForm.TestBtnClick(Sender: TObject); begin FIBSystemTables.TestSQL(SQLText.Lines.Text) end; procedure TIBModifySQLEditorForm.TableNamesComboCloseUp(Sender: TObject); begin - FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,false); + FIBSystemTables.GetFieldNames(TableNamesCombo.Text,FieldList.Items,IncludePrimaryKeys.checked,false); FIBSystemTables.GetPrimaryKeys(TableNamesCombo.Text,PrimaryKeyList.Items); end; @@ -150,13 +172,11 @@ begin inherited Destroy; end; -procedure TIBModifySQLEditorForm.SetDatabase(Database: TIBDatabase; Transaction: TIBTransaction); +procedure TIBModifySQLEditorForm.SetDatabase(Database: TIBDatabase); begin - FIBSystemTables.SelectDatabase(Database,Transaction) + IBTransaction1.DefaultDatabase := Database; + FIBSystemTables.SelectDatabase(Database,IBTransaction1) end; -initialization - {$I ibmodifysqleditor.lrs} - end.