📄 fieldeditor.pas
字号:
unit fieldeditor;
// -------------------------------------
// HeidiSQL
// Field-/Index-Editor
// -------------------------------------
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ImgList, ToolWin, ExtCtrls, SortListView, Buttons;
type
TFieldEditForm = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
ButtonCancel: TButton;
ButtonOK: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label5: TLabel;
EditDefault: TEdit;
EditLength: TEdit;
ComboBoxType: TComboBox;
EditFieldname: TEdit;
GroupBox1: TGroupBox;
CheckBoxBinary: TCheckBox;
CheckBoxUnsigned: TCheckBox;
CheckBoxZerofill: TCheckBox;
CheckBoxNotNull: TCheckBox;
CheckBoxAutoIncrement: TCheckBox;
ImageList1: TImageList;
TabSheet2: TTabSheet;
ComboBoxKeys: TComboBox;
Label4: TLabel;
CheckBoxUnique: TCheckBox;
ButtonAdd: TButton;
ButtonDelete: TButton;
Label6: TLabel;
ListBox1: TListBox;
ListBox2: TListBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Label7: TLabel;
ButtonAddPrimary: TButton;
ComboBoxPosition: TComboBox;
Label8: TLabel;
CheckBoxFulltext: TCheckBox;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
TabSheet3: TTabSheet;
Label9: TLabel;
procedure FormShow(Sender: TObject);
procedure ComboBoxTypeChange(Sender: TObject);
procedure AddUpdateField(Sender: TObject);
procedure ButtonCancelClick(Sender: TObject);
procedure PageControl1Change(Sender: TObject);
procedure OKClick(Sender: TObject);
procedure UpdateKeys(Sender: TObject);
procedure ComboBoxKeysChange(Sender: TObject);
procedure ButtonAddClick(Sender: TObject);
procedure InitFieldEditor(Sender: TObject);
procedure InitIndexEditor(Sender: TObject);
procedure RemoveField(Sender: TObject);
procedure ButtonDeleteClick(Sender: TObject);
procedure ButtonAddPrimaryClick(Sender: TObject);
procedure ShowKeys(index: Integer=0);
procedure CheckBoxUniqueClick(Sender: TObject);
procedure AddField(Sender: TObject);
procedure ComboBoxKeysOnDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
procedure CheckBoxFulltextClick(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure togglebuttons(Sender: TObject);
procedure ComboBoxKeysDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
private
{ Private declarations }
TempKeys : TStringList;
public
{ Public declarations }
UpdateField : Boolean; // Update or Add a field? (is set in formshow)
end;
var
FieldEditForm: TFieldEditForm;
const
tempfieldname = 'temp_fieldname';
crlf = #13#10;
type TMysqlIndex = record
Name : String[64];
Columns : TStringList;
Unique : Boolean;
Fulltext : Boolean;
Modified : Boolean;
Ready : Boolean;
end;
implementation
uses helpers, childwin, Main;
var klist : Array of TMysqlIndex;
{$R *.DFM}
procedure TFieldEditForm.InitFieldEditor(Sender: TObject);
var
strtype : String;
i : Integer;
begin
// ====================
// Field-Editor:
// ====================
ComboBoxPosition.Items.Clear;
ComboBoxPosition.Items.Add('At End of Table');
ComboBoxPosition.Items.Add('At Beginning of Table');
with TMDIChild(Application.Mainform.ActiveMDIChild).Feldliste do begin
for i:=0 to items.Count-1 do
ComboBoxPosition.Items.Add('AFTER ' + mainform.mask(items[i].Caption));
end;
if not UpdateField then begin // add a new field
EditFieldName.Text := 'FieldName';
ComboBoxType.ItemIndex := 0;
EditLength.Text := '';
EditDefault.Text := '';
CheckBoxUnsigned.Checked := true;
if TMDIChild(Application.Mainform.ActiveMDIChild).Feldliste.Selected <> nil then
ComboBoxPosition.ItemIndex := TMDIChild(Application.Mainform.ActiveMDIChild).Feldliste.Selected.Index+2
else
ComboBoxPosition.ItemIndex := 0;
end else
begin // edit exising field
with TMDIChild(Application.Mainform.ActiveMDIChild).Feldliste.Selected do
begin
EditFieldname.Text := Caption;
EditLength.Text := getklammervalues(Subitems[0]);
EditDefault.Text := Subitems[2];
// Type:
strtype := lowercase(Subitems[0]);
with ComboBoxType do
begin
if pos('int', strtype) <> 0 then ItemIndex := 3;
if pos('tinyint', strtype) <> 0 then ItemIndex := 0;
if pos('smallint', strtype) <> 0 then ItemIndex := 1;
if pos('mediumint', strtype) <> 0 then ItemIndex := 2;
if pos('bigint', strtype) <> 0 then ItemIndex := 4;
if pos('float', strtype) <> 0 then ItemIndex := 5;
if pos('double', strtype) <> 0 then ItemIndex := 6;
if pos('decimal', strtype) <> 0 then ItemIndex := 7;
if pos('date', strtype) <> 0 then ItemIndex := 8;
if pos('time', strtype) <> 0 then ItemIndex := 11;
if pos('datetime', strtype) <> 0 then ItemIndex := 9;
if pos('timestamp', strtype) <> 0 then ItemIndex := 10;
if pos('year', strtype) <> 0 then ItemIndex := 12;
if pos('char', strtype) <> 0 then ItemIndex := 13;
if pos('varchar', strtype) <> 0 then ItemIndex := 14;
if pos('text', strtype) <> 0 then ItemIndex := 17;
if pos('tinytext', strtype) <> 0 then ItemIndex := 16;
if pos('mediumtext', strtype) <> 0 then ItemIndex := 20;
if pos('longtext', strtype) <> 0 then ItemIndex := 22;
if pos('blob', strtype) <> 0 then ItemIndex := 18;
if pos('tinyblob', strtype) <> 0 then ItemIndex := 15;
if pos('mediumblob', strtype) <> 0 then ItemIndex := 19;
if pos('longblob', strtype) <> 0 then ItemIndex := 21;
if pos('enum', strtype) <> 0 then ItemIndex := 23;
if pos('set', strtype) <> 0 then ItemIndex := 24;
end;
// Attributes:
if pos('binary', strtype) <> 0 then
begin
CheckBoxBinary.Checked := true;
CheckBoxUnsigned.Checked := false;
CheckBoxZerofill.Checked := false;
end
else if pos('unsigned zerofill', strtype) <> 0 then
begin
CheckBoxBinary.Checked := false;
CheckBoxUnsigned.Checked := true;
CheckBoxZerofill.Checked := true;
end
else if pos('unsigned', strtype) <> 0 then
begin
CheckBoxBinary.Checked := false;
CheckBoxUnsigned.Checked := true;
CheckBoxZerofill.Checked := false;
end
else
begin
CheckBoxBinary.Checked := false;
CheckBoxUnsigned.Checked := false;
CheckBoxZerofill.Checked := false;
end;
if lowercase(Subitems[1]) = 'yes' then
CheckBoxNotNull.Checked := false
else
checkBoxNotNull.Checked := true;
if lowercase(Subitems[3]) = 'auto_increment' then
CheckBoxAutoIncrement.Checked := True
else
CheckBoxAutoIncrement.Checked := False;
end;
end;
end;
procedure TFieldEditForm.InitIndexEditor(Sender: TObject);
var
i : Integer;
begin
// ====================
// Index-Editor:
// ====================
ListBox1.Items.Clear;
ListBox2.Items.Clear;
setlength(klist, 0);
TempKeys := TStringList.Create;
with TMDIChild(Application.Mainform.ActiveMDIChild) do
begin
ZQuery3.SQL.Clear();
ZQuery3.SQL.Add( 'SHOW KEYS FROM ' + mainform.mask(ActualTable) );
ZQuery3.Open;
ZQuery3.First;
for i:=1 to ZQuery3.RecordCount do
begin
if TempKeys.IndexOf(ZQuery3.Fields[2].AsString) = -1 then
begin
TempKeys.Add(ZQuery3.Fields[2].AsString);
setlength(klist, length(klist)+1);
klist[length(klist)-1].Name := ZQuery3.Fields[2].AsString;
klist[length(klist)-1].Columns := TStringList.Create;
klist[length(klist)-1].Columns.Add(ZQuery3.Fields[4].AsString);
klist[length(klist)-1].Modified := false;
klist[length(klist)-1].Unique := (ZQuery3.Fields[1].AsString = '0');
if ZQuery3.FieldCount >= 10 then
klist[length(klist)-1].Fulltext := (ZQuery3.Fields[9].AsString = 'FULLTEXT')
else
klist[length(klist)-1].Fulltext := false;
end else
klist[TempKeys.IndexOf(ZQuery3.Fields[2].AsString)].Columns.Add(ZQuery3.Fields[4].AsString);
ZQuery3.Next;
end;
for i:=0 to Feldliste.Items.Count-1 do begin
if Feldliste.Items[i] <> nil then
self.ListBox2.Items.Add(Feldliste.Items[i].Caption);
end;
end;
showkeys();
end;
procedure TFieldEditForm.FormShow(Sender: TObject);
begin
Caption := TMDIChild(Mainform.ActiveMDIChild).ZConn.Hostname + ' - Field-Editor';
InitFieldEditor(self);
InitIndexEditor(self);
ComboBoxTypeChange(self);
PageControl1.ActivePage := TabSheet1;
EditFieldName.SetFocus;
PageControl1.OnChange(self);
end;
procedure TFieldEditForm.ComboBoxTypeChange(Sender: TObject);
begin
// Attributes
// Binary geht nur bei char und varchar
if ComboBoxType.ItemIndex in [13,14] then
CheckBoxBinary.Enabled := true
else begin
CheckBoxBinary.Checked := false;
CheckBoxBinary.Enabled := false;
end;
// Unsigned geht nur bei numerischen Feldern, (nicht bei float-feldern!)
if ComboBoxType.ItemIndex in [0,1,2,3,4] then
CheckBoxUnsigned.Enabled := true
else begin
CheckBoxUnsigned.Checked := false;
CheckBoxUnsigned.Enabled := false;
end;
// Zerofill geht bei numerischen und float-feldern
if ComboBoxType.ItemIndex in [0,1,2,3,4,5,6,7] then
CheckBoxZerofill.Enabled := true
else begin
CheckBoxZerofill.Checked := false;
CheckBoxZerofill.Enabled := false;
end;
// Length/Set geht nicht bei date/time/memo/blob-feldern
if ComboBoxType.ItemIndex in [8,9,11,15..22] then
EditLength.Enabled := false
else begin
EditLength.Enabled := true;
end;
// Default geht nicht bei memo/blob-feldern
if ComboBoxType.ItemIndex in [15..22] then
EditDefault.Enabled := false
else begin
EditDefault.Enabled := true;
end;
end;
procedure TFieldEditForm.AddUpdateField(Sender: TObject);
var
strNotNull,
strAttributes,
strAutoIncrement,
strLengthSet,
strDefault,
strPosition,
fielddef : String;
begin
// Apply Changes to field-definition
if (ComboBoxPosition.ItemIndex > -1) and UpdateField
then begin // Move field position
if MessageDLG('You are about to move a field''s position in the table-structure. While there is no handy one-query-method in MySQL to do that, this will be done in 4 steps:'+crlf+
' 1. Adding a temporary field at the specified position'+crlf+
' 2. Filling the temporary field with the same data as source field'+crlf+
' 3. Dropping the source-field'+crlf+
' 4. Renaming the temporary field to it''s original name.'+crlf+crlf+
'Be aware that this method can mess up existing indexes in your table or even can result in losing data! If you are not sure you should not use this function on indexed fields.'+crlf+crlf+
'Continue?',
mtConfirmation,
[mbYes, mbCancel],
0
) <> mrYes then
exit;
end;
Screen.Cursor := crSQLWait;
strAttributes := ''; // gar keines von den 3 Attributen Binary, Unsigned, Zerofill
strNotNull := '';
strDefault := '';
strAutoIncrement := '';
if CheckBoxBinary.Checked = true then
strAttributes := strAttributes + ' BINARY';
if CheckBoxUnsigned.Checked = true then
strAttributes := strAttributes + ' UNSIGNED';
if CheckBoxZerofill.Checked = true then
strAttributes := strAttributes + ' ZEROFILL';
if (length(EditDefault.Text) > 0) and EditDefault.Enabled then
strDefault := ' DEFAULT "' + escape_string(EditDefault.Text) + '"';
if CheckBoxNotNull.Checked = True then
strNotNull := ' NOT NULL';
if CheckBoxAutoIncrement.Checked = True then
strAutoIncrement := ' AUTO_INCREMENT';
if (EditLength.text <> '') and EditLength.Enabled then
strLengthSet := '(' + EditLength.text + ') '
else
strLengthSet := '';
strPosition := '';
case ComboBoxPosition.ItemIndex of
0 : ;
1 : strPosition := ' FIRST';
else
strPosition := ' ' + ComboBoxPosition.Text;
end;
fielddef := ComboBoxType.Text + // Type
strLengthSet + // Length/Set
strAttributes + // Attribute
strDefault + // Default
strNotNull + // Not Null
strAutoIncrement; // Auto_increment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -