mirror of
https://github.com/HeidiSQL/HeidiSQL.git
synced 2025-08-14 18:12:05 +08:00
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:
@ -560,6 +560,7 @@ object MDIChild: TMDIChild
|
|||||||
Width = 200
|
Width = 200
|
||||||
end>
|
end>
|
||||||
GridLines = True
|
GridLines = True
|
||||||
|
MultiSelect = True
|
||||||
RowSelect = True
|
RowSelect = True
|
||||||
PopupMenu = popupTableGrid
|
PopupMenu = popupTableGrid
|
||||||
SmallImages = MainForm.ImageList1
|
SmallImages = MainForm.ImageList1
|
||||||
@ -1698,8 +1699,8 @@ object MDIChild: TMDIChild
|
|||||||
OnClick = MenuAddFieldClick
|
OnClick = MenuAddFieldClick
|
||||||
end
|
end
|
||||||
object DropField1: TMenuItem
|
object DropField1: TMenuItem
|
||||||
Caption = 'Drop Field...'
|
Caption = 'Drop Field(s)...'
|
||||||
Hint = 'Delete Field from Table'
|
Hint = 'Delete Field(s) from Table'
|
||||||
ImageIndex = 33
|
ImageIndex = 33
|
||||||
ShortCut = 16430
|
ShortCut = 16430
|
||||||
OnClick = DropField
|
OnClick = DropField
|
||||||
|
@ -2967,12 +2967,24 @@ end;
|
|||||||
procedure TMDIChild.DropField(Sender: TObject);
|
procedure TMDIChild.DropField(Sender: TObject);
|
||||||
var
|
var
|
||||||
tn : TTreeNode;
|
tn : TTreeNode;
|
||||||
selectedField: Integer;
|
i, j: Integer;
|
||||||
|
dropCmd : String;
|
||||||
|
dropList : TStringList;
|
||||||
begin
|
begin
|
||||||
// Drop Column
|
// Drop Columns
|
||||||
if ListColumns.Items.Count = 1 then
|
|
||||||
|
// We allow the user to select and delete multiple listItems
|
||||||
|
dropList := TStringList.Create;
|
||||||
|
for i := 0 to ListColumns.Items.Count - 1 do
|
||||||
begin
|
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
|
begin
|
||||||
Screen.Cursor := crSQLWait;
|
Screen.Cursor := crSQLWait;
|
||||||
ExecUpdateQuery( 'DROP TABLE '+mask(ActualTable) );
|
ExecUpdateQuery( 'DROP TABLE '+mask(ActualTable) );
|
||||||
@ -2984,16 +2996,33 @@ begin
|
|||||||
Screen.Cursor := crDefault;
|
Screen.Cursor := crDefault;
|
||||||
end;
|
end;
|
||||||
end else
|
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
|
try
|
||||||
ExecUpdateQuery( 'ALTER TABLE '+mask(ActualTable)+' DROP '+mask(ListColumns.Selected.Caption) );
|
// Concat fields for ALTER query
|
||||||
// Rely on the server respective ExecUpdateQuery raising an exception on an error
|
for i := 0 to dropList.Count - 1 do
|
||||||
selectedField := ListColumns.Selected.Index;
|
dropCmd := dropCmd + 'DROP ' + mask(dropList[i]) + ', ';
|
||||||
ListColumns.Selected.Delete;
|
// Remove trailing comma
|
||||||
// Set focus on next available field
|
delete(dropCmd, Length(dropCmd)-1, 2);
|
||||||
if selectedField = ListColumns.Items.Count then
|
|
||||||
dec(selectedField);
|
// Execute field dropping
|
||||||
ListColumns.Selected := ListColumns.Items[selectedField];
|
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
|
except
|
||||||
On E : Exception do
|
On E : Exception do
|
||||||
begin
|
begin
|
||||||
|
Reference in New Issue
Block a user