⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fieldeditor.pas

📁 MysqlFront的源码
💻 PAS
📖 第 1 页 / 共 2 页
字号:

  with TMDIChild(Mainform.ActiveMDIChild) do
  begin
    if not self.UpdateField then
      ExecQuery('ALTER TABLE ' + mainform.mask(ActualTable) +           // table
        ' ADD ' + mainform.mask(EditFieldname.Text) + ' ' +     // new name
        fielddef +
        strPosition                                             // Position
        )
    else begin
      ExecQuery('ALTER TABLE ' + mainform.mask(ActualTable) +                      // table
        ' CHANGE ' + mainform.mask(FeldListe.Selected.Caption) + ' ' +     // old name
        mainform.mask(EditFieldName.Text) + ' ' +                          // new name
        fielddef
        );
      if ComboBoxPosition.ItemIndex > -1 then begin // Move field position
        ExecQuery('ALTER TABLE ' + mainform.mask(ActualTable) +           // table
          ' ADD ' + mainform.mask(tempfieldname) + ' ' +          // new name
          fielddef +
          strPosition                                             // Position
          );
        ExecQuery('UPDATE ' + mainform.mask(ActualTable) + ' SET '+mainform.mask(tempfieldname)+'='+mainform.mask(EditFieldName.Text));
        ExecQuery('ALTER TABLE ' + mainform.mask(ActualTable) + ' DROP '+mainform.mask(EditFieldName.Text));
        ExecQuery('ALTER TABLE ' + mainform.mask(ActualTable) + ' CHANGE '+
          mainform.mask(tempfieldname)+' '+mainform.mask(EditFieldName.Text) + ' ' +
          fielddef
        );
      end;
    end;

    ShowTableProperties(self);
  end;

  Screen.Cursor := crDefault;
  close;
end;

procedure TFieldEditForm.ButtonCancelClick(Sender: TObject);
begin
  // cancel
  close;
end;

procedure TFieldEditForm.PageControl1Change(Sender: TObject);
begin
  case PageControl1.ActivePageIndex of
    0 : begin
      if not UpdateField then
        ButtonOK.Caption := 'Add Field' else
        ButtonOK.Caption := 'Update Field';
      end;
    1 : ButtonOK.Caption := 'Update Keys';
    2 : ButtonOK.Caption := 'Close';
  end;
end;


procedure TFieldEditForm.OKClick(Sender: TObject);
begin
  // add/update what?
  if PageControl1.ActivePage = TabSheet1 then
    AddUpdateField(self)
  else if PageControl1.ActivePage = TabSheet2 then
    UpdateKeys(self)
  else if PageControl1.ActivePage = TabSheet3 then
    close;
end;


procedure TFieldEditForm.ComboBoxKeysChange(Sender: TObject);
var i : Integer;
begin
  // Change actual index!
  if ComboBoxKeys.ItemIndex > -1 then begin
    ListBox2.Items.Clear;
    with TMDIChild(Application.Mainform.ActiveMDIChild) do begin
      for i:=0 to Feldliste.Items.Count-1 do
        if (Feldliste.Items[i] <> nil) and (klist[self.ComboBoxKeys.ItemIndex].columns.Indexof(Feldliste.Items[i].Caption)=-1) then
          self.ListBox2.Items.Add(Feldliste.Items[i].Caption);
    end;
    with klist[ComboBoxKeys.ItemIndex] do begin
      ListBox1.Items := Columns;
      ListBox1.Items := Columns;
      CheckBoxUnique.OnClick := nil;
      CheckBoxUnique.Checked := Unique;
      CheckBoxUnique.OnClick := CheckBoxUniqueClick;
      CheckBoxFulltext.OnClick := nil;
      CheckBoxFulltext.Checked := Fulltext;
      CheckBoxFulltext.OnClick := CheckBoxFulltextClick;
      CheckBoxUnique.Enabled := not (ComboBoxKeys.Text = 'PRIMARY');
      CheckBoxFulltext.Enabled := not (ComboBoxKeys.Text = 'PRIMARY');
      ButtonDelete.Enabled := true;
      ListBox1.Enabled := true;
      ListBox2.Enabled := true;
    end;
  end;
  togglebuttons(self);
end;


procedure TFieldEditForm.ButtonAddClick(Sender: TObject);
var kname : String;
begin
  // Add Index!
  kname := 'NewIndex';
  if not InputQuery('New Index...', 'Index-Name:', kname) then
    exit;
  if ComboBoxKeys.Items.IndexOf(kname) > -1 then begin
    MessageDlg('Index-Name '''+kname+''' already used!', mtError, [mbOk], 0);
    exit;
  end;
  setlength(klist, length(klist)+1);
  klist[length(klist)-1].Name := kname;
  klist[length(klist)-1].Columns := TStringList.Create;
  klist[length(klist)-1].Unique := false;
  klist[length(klist)-1].Fulltext := false;
  klist[length(klist)-1].Modified := true;
  showkeys(length(klist)-1);
end;


procedure TFieldEditForm.ButtonDeleteClick(Sender: TObject);
var i,j : Integer;
begin
  // Delete Index!
  i := ComboBoxKeys.ItemIndex;
  if i > -1 then
  if MessageDlg('Delete Index ''' + ComboBoxKeys.Text + ''' ?',
    mtConfirmation, [mbYes,mbCancel], 0) = mrYes then begin
    inc(i); // jump to next entry after the one to delete!
    for j:=i to length(klist)-1 do
      klist[j-1] := klist[j];
    setlength(klist, length(klist)-1);
    ShowKeys(i-2);
  end;
end;

procedure TFieldEditForm.ButtonAddPrimaryClick(Sender: TObject);
begin
  // Add primary key!
  setlength(klist, length(klist)+1);
  klist[length(klist)-1].Name := 'PRIMARY';
  klist[length(klist)-1].Columns := TStringList.Create;
  klist[length(klist)-1].Unique := false;
  klist[length(klist)-1].Fulltext := false;
  klist[length(klist)-1].Modified := true;
  ShowKeys(length(klist)-1);
  ButtonAddPrimary.Enabled := false;
end;


procedure TFieldEditForm.ShowKeys(index: Integer=0);
var
  i : Integer;
begin
  // Show indexes in combobox!
  ComboBoxKeys.Items.Clear;
  ButtonAddPrimary.Enabled := true;
  ButtonDelete.Enabled := false;
  ListBox1.Enabled := false;
  ListBox2.Enabled := false;
  BitBtn1.Enabled := false;
  BitBtn2.Enabled := false;
  for i:=0 to length(klist)-1 do
    ComboBoxKeys.Items.Add(klist[i].Name);

  if ComboBoxKeys.Items.IndexOf('PRIMARY') > -1 then
    ButtonAddPrimary.Enabled := false;

  if (index = -1) and (length(klist) > 0) then
    index := 0;
  if index < length(klist) then // careful: only if given index is < length(klist)
    ComboBoxKeys.ItemIndex := index;

  with ComboBoxKeys do
  if Items.Count = 0 then begin
    Enabled := false;
    Color := clBtnFace;
  end else begin
    Enabled := true;
    Color := clWindow;
  end;

  ComboBoxKeys.OnChange(self);
end;


procedure TFieldEditForm.CheckBoxUniqueClick(Sender: TObject);
begin
  // make index unique!
  klist[ComboBoxKeys.ItemIndex].Unique := CheckBoxUnique.Checked;
  if CheckBoxUnique.Checked then begin
    klist[ComboBoxKeys.ItemIndex].Fulltext := false;
    CheckBoxFulltext.Checked := false;
  end;
  klist[ComboBoxKeys.ItemIndex].Modified := true;
end;

procedure TFieldEditForm.CheckBoxFulltextClick(Sender: TObject);
begin
  // make index fulltext!
  klist[ComboBoxKeys.ItemIndex].Fulltext := CheckBoxFulltext.Checked;
  if CheckBoxFulltext.Checked then begin
    klist[ComboBoxKeys.ItemIndex].Unique := false;
    CheckBoxUnique.Checked := false;
  end;
  klist[ComboBoxKeys.ItemIndex].Modified := true;
end;


procedure TFieldEditForm.UpdateKeys(Sender: TObject);
var
  i,j, index : Integer;
  query1, query : String;
begin
  // update keys!
  // fuer jeden TempKey (siehe OnFormShow) gucken, ob dieser ge鋘dert oder sogar
  // gel鰏cht wurde in klist
  query1 := 'ALTER TABLE ' + mainform.mask(TMDIChild(Application.Mainform.ActiveMDIChild).ActualTable);
  for i:=0 to TempKeys.Count-1 do begin

    index := -1;

    for j:=0 to length(klist)-1 do begin
      if TempKeys[i] = klist[j].Name then begin
        index := j;
        break;
      end;
    end;

    if (index > -1) and (klist[index].Modified) then begin
      // modify existing key
      // PK:
      if klist[index].Name = 'PRIMARY' then
        query := query1 + ' DROP PRIMARY KEY' +
          ', ADD PRIMARY KEY (' + implodestr(',', klist[index].Columns) + ')'
      // UNIQUE:
      else if klist[index].Unique then
        query := query1 + ' DROP INDEX ' + klist[index].Name +
          ', ADD UNIQUE ' + klist[index].Name + ' (' + implodestr(',', klist[index].Columns) + ')'
      // FULLTEXT:
      else if klist[index].Fulltext then
        query := query1 + ' DROP INDEX ' + klist[index].Name +
          ', ADD FULLTEXT ' + klist[index].Name + ' (' + implodestr(',', klist[index].Columns) + ')'
      // INDEX:
      else
        query := query1 + ' DROP INDEX '+klist[index].Name +
          ', ADD INDEX '+klist[index].Name+' (' + implodestr(',', klist[index].Columns) + ')';
      TMDIChild(Application.Mainform.ActiveMDIChild).ExecQuery(query);
      klist[index].Modified := false;
    end

    else if index = -1 then begin
      // delete existing key
      if TempKeys[i] = 'PRIMARY' then
        query := query1 + ' DROP PRIMARY KEY'
      else
        query := query1 + ' DROP INDEX ' + TempKeys[i];
      TMDIChild(Application.Mainform.ActiveMDIChild).ExecQuery(query);
    end;

    if index > -1 then
      klist[index].Ready := true;
  end;

  for j:=0 to length(klist)-1 do begin
    if (not klist[j].Ready) then begin
      // Add a new key
      // PK:
      if klist[j].Name = 'PRIMARY' then
        query := query1 + ' ADD PRIMARY KEY (' + implodestr(',', klist[j].Columns) + ')'
      // UNIQUE:
      else if klist[j].Unique then
        query := query1 + ' ADD UNIQUE ' + klist[j].Name + ' (' + implodestr(',', klist[j].Columns) + ')'
      // UNIQUE:
      else if klist[j].Fulltext then
        query := query1 + ' ADD FULLTEXT ' + klist[j].Name + ' (' + implodestr(',', klist[j].Columns) + ')'
      // INDEX:
      else
        query := query1 + ' ADD INDEX '+ klist[j].Name+' (' + implodestr(',', klist[j].Columns) + ')';
      TMDIChild(Application.Mainform.ActiveMDIChild).ExecQuery(query);
    end;
  end;

  TMDIChild(Application.Mainform.ActiveMDIChild).ShowTableProperties(self);
  close;
end;


procedure TFieldEditForm.AddField(Sender: TObject);
var sel : Integer;
begin
  // add column to index
  if ListBox2.ItemIndex > -1 then begin
    ListBox1.Items.Add(ListBox2.Items[ListBox2.ItemIndex]);
    klist[ComboBoxKeys.ItemIndex].Columns.Add(ListBox2.Items[ListBox2.ItemIndex]);
    if ListBox2.ItemIndex > 0 then
      sel := ListBox2.ItemIndex-1
    else if ListBox2.Items.Count > 1 then
      sel := 0;
    ListBox2.Items.Delete(ListBox2.ItemIndex);
    klist[ComboBoxKeys.ItemIndex].Modified := true;
    ListBox2.ItemIndex := sel;
  end;
  togglebuttons(self);
end;

procedure TFieldEditForm.BitBtn3Click(Sender: TObject);
begin
  // add all columns to index
  ListBox1.Items.AddStrings(ListBox2.Items);
  klist[ComboBoxKeys.ItemIndex].Columns.AddStrings(ListBox2.Items);
  ListBox2.Items.Clear;
  klist[ComboBoxKeys.ItemIndex].Modified := true;
  togglebuttons(self);
end;


procedure TFieldEditForm.RemoveField(Sender: TObject);
var sel : Integer;
begin
  // delete one column from index
  if ListBox1.ItemIndex > -1 then begin
    klist[ComboBoxKeys.ItemIndex].Columns.Delete(ListBox1.ItemIndex);
    klist[ComboBoxKeys.ItemIndex].Modified := true;
    if ListBox1.ItemIndex > 0 then
      sel := ListBox1.ItemIndex-1
    else if ListBox1.Items.Count > 1 then
      sel := 0;
    ListBox2.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
    ListBox1.Items.Delete(ListBox1.ItemIndex);
    ListBox1.ItemIndex := sel;
  end;
  togglebuttons(self);
end;


procedure TFieldEditForm.BitBtn4Click(Sender: TObject);
begin
  // delete all columns from index
  klist[ComboBoxKeys.ItemIndex].Columns.Clear;
  klist[ComboBoxKeys.ItemIndex].Modified := true;
  ListBox2.Items.AddStrings(ListBox1.Items);
  ListBox1.Items.Clear;
  togglebuttons(self);
end;


procedure TFieldEditForm.togglebuttons(Sender: TObject);
begin
  BitBtn1.Enabled := (ListBox2.ItemIndex > -1);
  BitBtn2.Enabled := (ListBox1.ItemIndex > -1);
  BitBtn3.Enabled := (ListBox2.Items.Count > 0);
  BitBtn4.Enabled := (ListBox1.Items.Count > 0);
end;


procedure TFieldEditForm.ComboBoxKeysOnDrawItem(Control: TWinControl; Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
begin
  // colors!!
end;




procedure TFieldEditForm.ComboBoxKeysDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  icon : Integer;
  c : tComboBox;
begin
  c := (Control as TComboBox);
  with c.Canvas do
    begin
      Brush.Color := clWindow;
      FillRect(rect);
      if (klist[index].Unique) and (klist[index].Name <> 'PRIMARY') then
        icon := 7
      else if klist[index].Fulltext then
        icon := 8
      else if klist[index].Name = 'PRIMARY' then
        icon := 5
      else
        icon := 6;
      ImageList1.Draw(c.canvas, Rect.Left, Rect.Top, Icon);
      Font.Color := clWindowText;
      TextOut(Rect.Left + 18, Rect.Top, c.Items[Index]);
    end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -