New feature: Allow the user to select and delete multiple fields in listColumns. Just like in listTables where we allow to drop multiple tables.

This commit is contained in:
Ansgar Becker
2007-08-01 20:43:48 +00:00
parent a03d3cd2a7
commit fa11cf2fb6
2 changed files with 45 additions and 15 deletions

View File

@ -560,6 +560,7 @@ object MDIChild: TMDIChild
Width = 200
end>
GridLines = True
MultiSelect = True
RowSelect = True
PopupMenu = popupTableGrid
SmallImages = MainForm.ImageList1
@ -1698,8 +1699,8 @@ object MDIChild: TMDIChild
OnClick = MenuAddFieldClick
end
object DropField1: TMenuItem
Caption = 'Drop Field...'
Hint = 'Delete Field from Table'
Caption = 'Drop Field(s)...'
Hint = 'Delete Field(s) from Table'
ImageIndex = 33
ShortCut = 16430
OnClick = DropField

View File

@ -2967,12 +2967,24 @@ end;
procedure TMDIChild.DropField(Sender: TObject);
var
tn : TTreeNode;
selectedField: Integer;
i, j: Integer;
dropCmd : String;
dropList : TStringList;
begin
// Drop Column
if ListColumns.Items.Count = 1 then
// Drop Columns
// We allow the user to select and delete multiple listItems
dropList := TStringList.Create;
for i := 0 to ListColumns.Items.Count - 1 do
begin
if MessageDlg('Can''t drop the last Field - drop Table '+ActualTable+'?', mtConfirmation, [mbok,mbcancel], 0) = mrok then
if ListColumns.Items[i].Selected then
dropList.Add(ListColumns.Items[i].Caption);
end;
// In case all listItems are selected:
if dropList.Count = ListColumns.Items.Count then
begin
if MessageDlg('Can''t drop all or the last Field - drop Table '+ActualTable+'?', mtConfirmation, [mbok,mbcancel], 0) = mrok then
begin
Screen.Cursor := crSQLWait;
ExecUpdateQuery( 'DROP TABLE '+mask(ActualTable) );
@ -2984,16 +2996,33 @@ begin
Screen.Cursor := crDefault;
end;
end else
if MessageDlg('Drop field ' + ListColumns.Selected.Caption + ' ?', mtConfirmation, [mbok,mbcancel], 0) = mrok then
if MessageDlg('Drop ' + IntToStr(dropList.Count) + ' field(s): ' + ImplodeStr( ', ', dropList ) + ' ?', mtConfirmation, [mbok,mbcancel], 0) = mrok then
try
ExecUpdateQuery( 'ALTER TABLE '+mask(ActualTable)+' DROP '+mask(ListColumns.Selected.Caption) );
// Rely on the server respective ExecUpdateQuery raising an exception on an error
selectedField := ListColumns.Selected.Index;
ListColumns.Selected.Delete;
// Set focus on next available field
if selectedField = ListColumns.Items.Count then
dec(selectedField);
ListColumns.Selected := ListColumns.Items[selectedField];
// Concat fields for ALTER query
for i := 0 to dropList.Count - 1 do
dropCmd := dropCmd + 'DROP ' + mask(dropList[i]) + ', ';
// Remove trailing comma
delete(dropCmd, Length(dropCmd)-1, 2);
// Execute field dropping
ExecUpdateQuery( 'ALTER TABLE '+mask(ActualTable)+' ' + dropCmd );
// Rely on the server respective ExecUpdateQuery has raised an exception so the
// following code will be skipped on any error
for i := ListColumns.Items.Count - 1 downto 0 do
begin
for j := 0 to dropList.Count - 1 do
begin
if dropList[j] = ListColumns.Items[i].Caption then
begin
ListColumns.Items[i].Delete;
break;
end;
end;
end;
// Set focus on automatically focused item
ListColumns.Selected := ListColumns.ItemFocused;
except
On E : Exception do
begin