Skip to content

Commit

Permalink
Issue #75: get "Add constraint" and "Delete constraint" buttons worki…
Browse files Browse the repository at this point in the history
…ng, provide function list when editing check clause, and fix some more issues
  • Loading branch information
ansgarbecker committed Jan 16, 2021
1 parent 8afca5d commit 461c734
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 38 deletions.
8 changes: 7 additions & 1 deletion out/locale/en/LC_MESSAGES/default.po
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: HeidiSQL\n"
"POT-Creation-Date: 2012-11-05 21:40\n"
"PO-Revision-Date: 2021-01-08 16:47+0100\n"
"PO-Revision-Date: 2021-01-16 16:20+0100\n"
"Last-Translator: Ansgar Becker <anse@heidisql.com>\n"
"Language-Team: English (http://www.transifex.com/projects/p/heidisql/language/en/)\n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -6649,3 +6649,9 @@ msgstr "Auto detecting the encoding of a file is highly discouraged. You may exp

msgid "keyword"
msgstr "keyword"

msgid "Check constraints"
msgstr "Check constraints"

msgid "Check clause"
msgstr "Check clause"
2 changes: 2 additions & 0 deletions source/table_editor.dfm
Expand Up @@ -500,13 +500,15 @@ object frmTableEditor: TfrmTableEditor
Enabled = False
ImageIndex = 46
Wrap = True
OnClick = btnRemoveCheckConstraintClick
end
object btnClearCheckConstraints: TToolButton
Left = 0
Top = 44
Caption = 'Clear'
Enabled = False
ImageIndex = 26
OnClick = btnClearCheckConstraintsClick
end
end
object listCheckConstraints: TVirtualStringTree
Expand Down
135 changes: 98 additions & 37 deletions source/table_editor.pas
Expand Up @@ -194,6 +194,8 @@ TfrmTableEditor = class(TFrame)
Node: PVirtualNode; Column: TColumnIndex; out EditLink: IVTEditLink);
procedure listCheckConstraintsNewText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; NewText: string);
procedure btnClearCheckConstraintsClick(Sender: TObject);
procedure btnRemoveCheckConstraintClick(Sender: TObject);
private
{ Private declarations }
FLoaded: Boolean;
Expand All @@ -202,7 +204,9 @@ TfrmTableEditor = class(TFrame)
FKeys: TTableKeyList;
FForeignKeys: TForeignKeyList;
FCheckConstraints: TCheckConstraintList;
DeletedKeys, DeletedForeignKeys: TStringList;
FDeletedKeys,
FDeletedForeignKeys,
FDeletedCheckConstraints: TStringList;
procedure ValidateColumnControls;
procedure ValidateIndexControls;
procedure MoveFocusedIndexPart(NewIdx: Cardinal);
Expand Down Expand Up @@ -251,8 +255,10 @@ constructor TfrmTableEditor.Create(AOwner: TComponent);
FColumns := TTableColumnList.Create;
FKeys := TTableKeyList.Create;
FForeignKeys := TForeignKeyList.Create;
DeletedKeys := TStringList.Create;
DeletedForeignKeys := TStringList.Create;
FDeletedKeys := TStringList.Create;
FDeletedForeignKeys := TStringList.Create;
FDeletedCheckConstraints := TStringList.Create;
FDeletedCheckConstraints.Duplicates := dupIgnore;
editName.MaxLength := NAME_LEN;
end;

Expand Down Expand Up @@ -507,19 +513,25 @@ procedure TfrmTableEditor.ResetModificationFlags;
FColumns[i].Status := esUntouched;
end;
end;
DeletedKeys.Clear;
FDeletedKeys.Clear;
for i:=0 to FKeys.Count-1 do begin
FKeys[i].OldName := FKeys[i].Name;
FKeys[i].OldIndexType := FKeys[i].IndexType;
FKeys[i].Added := False;
FKeys[i].Modified := False;
end;
DeletedForeignKeys.Clear;
FDeletedForeignKeys.Clear;
for i:=0 to FForeignKeys.Count-1 do begin
FForeignKeys[i].OldKeyName := FForeignKeys[i].KeyName;
FForeignKeys[i].Added := False;
FForeignKeys[i].Modified := False;
end;
FDeletedCheckConstraints.Clear;
for i:=0 to FCheckConstraints.Count-1 do begin
FCheckConstraints[i].Added := False;
FCheckConstraints[i].Modified := False;
end;

Modified := False;
btnSave.Enabled := Modified;
btnDiscard.Enabled := Modified;
Expand Down Expand Up @@ -731,11 +743,11 @@ function TfrmTableEditor.ComposeAlterStatement: TSQLBatch;
end;

// Drop indexes, also changed indexes, which will be readded below
for i:=0 to DeletedKeys.Count-1 do begin
if DeletedKeys[i] = TTableKey.PRIMARY then
for i:=0 to FDeletedKeys.Count-1 do begin
if FDeletedKeys[i] = TTableKey.PRIMARY then
IndexSQL := 'PRIMARY KEY'
else
IndexSQL := 'INDEX ' + Conn.QuoteIdent(DeletedKeys[i]);
IndexSQL := 'INDEX ' + Conn.QuoteIdent(FDeletedKeys[i]);
Specs.Add('DROP '+IndexSQL);
end;
// Add changed or added indexes
Expand All @@ -751,17 +763,19 @@ function TfrmTableEditor.ComposeAlterStatement: TSQLBatch;
Specs.Add('ADD '+FKeys[i].SQLCode);
end;

for i:=0 to DeletedForeignKeys.Count-1 do
Specs.Add('DROP FOREIGN KEY '+Conn.QuoteIdent(DeletedForeignKeys[i]));
for i:=0 to FDeletedForeignKeys.Count-1 do
Specs.Add('DROP FOREIGN KEY '+Conn.QuoteIdent(FDeletedForeignKeys[i]));
for i:=0 to FForeignKeys.Count-1 do begin
if FForeignKeys[i].Added or FForeignKeys[i].Modified then
Specs.Add('ADD '+FForeignKeys[i].SQLCode(True));
end;

// Check constraints
// Todo: process constraint marked for deletion
for i:=0 to FDeletedCheckConstraints.Count-1 do begin
Specs.Add('DROP CONSTRAINT ' + Conn.QuoteIdent(FDeletedCheckConstraints[i]));
end;
for Constraint in FCheckConstraints do begin
if Constraint.Added or Constraint.Modified or (Specs.Count > 0) then
if Constraint.Added or Constraint.Modified then
Specs.Add('ADD ' + Constraint.SQLCode);
end;

Expand Down Expand Up @@ -876,23 +890,6 @@ procedure TfrmTableEditor.Modification(Sender: TObject);
end;


procedure TfrmTableEditor.btnAddCheckConstraintClick(Sender: TObject);
var
CheckConstraint: TCheckConstraint;
idx: Integer;
begin
// Add new check constraint
CheckConstraint := TCheckConstraint.Create(DBObject.Connection);
idx := FCheckConstraints.Add(CheckConstraint);
CheckConstraint.Name := 'CC'+IntToStr(idx+1);
CheckConstraint.CheckClause := '';
CheckConstraint.Added := True;
Modification(Sender);
listCheckConstraints.Repaint;
SelectNode(listCheckConstraints, idx);
listCheckConstraints.EditNode(listCheckConstraints.FocusedNode, listCheckConstraints.Header.MainColumn);
end;

procedure TfrmTableEditor.btnAddColumnClick(Sender: TObject);
var
NewCol: TTableColumn;
Expand Down Expand Up @@ -1671,7 +1668,7 @@ procedure TfrmTableEditor.btnRemoveIndexClick(Sender: TObject);
0: begin
idx := treeIndexes.FocusedNode.Index;
if not FKeys[idx].Added then
DeletedKeys.Add(FKeys[idx].OldName);
FDeletedKeys.Add(FKeys[idx].OldName);
FKeys.Delete(idx);
// Delete node although ReinitChildren would do the same, but the Repaint before
// creates AVs in certain cases. See issue #2557
Expand Down Expand Up @@ -1705,7 +1702,7 @@ procedure TfrmTableEditor.btnClearIndexesClick(Sender: TObject);
SelectNode(treeIndexes, nil);
for TblKey in FKeys do begin
if not TblKey.Added then
DeletedKeys.Add(TblKey.OldName);
FDeletedKeys.Add(TblKey.OldName);
end;
FKeys.Clear;
Modification(Sender);
Expand Down Expand Up @@ -1824,6 +1821,55 @@ procedure TfrmTableEditor.ValidateIndexControls;
end;


procedure TfrmTableEditor.btnAddCheckConstraintClick(Sender: TObject);
var
CheckConstraint: TCheckConstraint;
idx: Integer;
begin
// Add new check constraint
CheckConstraint := TCheckConstraint.Create(DBObject.Connection);
idx := FCheckConstraints.Add(CheckConstraint);
CheckConstraint.Name := 'CC'+IntToStr(idx+1);
CheckConstraint.CheckClause := '';
CheckConstraint.Added := True;
Modification(Sender);
listCheckConstraints.Repaint;
SelectNode(listCheckConstraints, idx);
listCheckConstraints.EditNode(listCheckConstraints.FocusedNode, listCheckConstraints.Header.MainColumn);
end;


procedure TfrmTableEditor.btnRemoveCheckConstraintClick(Sender: TObject);
var
Constraint: TCheckConstraint;
begin
// Remove a foreign key
listCheckConstraints.CancelEditNode;
Constraint := FCheckConstraints[listCheckConstraints.FocusedNode.Index];
if (not Constraint.Added) and (not Constraint.Modified) then
FDeletedCheckConstraints.Add(Constraint.Name);
FCheckConstraints.Delete(listCheckConstraints.FocusedNode.Index);
Modification(Sender);
listCheckConstraints.Repaint;
end;


procedure TfrmTableEditor.btnClearCheckConstraintsClick(Sender: TObject);
var
i: Integer;
begin
// Clear all check constraints
listCheckConstraints.CancelEditNode;
for i:=FCheckConstraints.Count-1 downto 0 do begin
if (not FCheckConstraints[i].Added) and (not FCheckConstraints[i].Modified) then
FDeletedCheckConstraints.Add(FCheckConstraints[i].Name);
FCheckConstraints.Delete(i);
end;
Modification(Sender);
listCheckConstraints.Repaint;
end;


procedure TfrmTableEditor.listCheckConstraintsBeforePaint(
Sender: TBaseVirtualTree; TargetCanvas: TCanvas);
begin
Expand All @@ -1839,13 +1885,26 @@ procedure TfrmTableEditor.listCheckConstraintsCreateEditor(
var
VT: TVirtualStringTree;
Edit: TInplaceEditorLink;
EnumEditor: TEnumEditorLink;
i: Integer;
begin
// Edit check constraint
VT := Sender as TVirtualStringTree;
Edit := TInplaceEditorLink.Create(VT, True);
Edit.TitleText := VT.Header.Columns[Column].Text;
Edit.ButtonVisible := True;
EditLink := Edit;
case Column of
0: begin
Edit := TInplaceEditorLink.Create(VT, True);
Edit.TitleText := VT.Header.Columns[Column].Text;
Edit.ButtonVisible := True;
EditLink := Edit;
end;
1: begin
EnumEditor := TEnumEditorLink.Create(VT, True);
for i:=Low(MySQLFunctions) to High(MySQLFunctions) do
EnumEditor.ValueList.Add(MySQLFunctions[i].Name + MySQLFunctions[i].Declaration);
EnumEditor.AllowCustomText := True;
EditLink := EnumEditor;
end;
end;
end;


Expand Down Expand Up @@ -1892,6 +1951,8 @@ procedure TfrmTableEditor.listCheckConstraintsNewText(Sender: TBaseVirtualTree;
begin
// Check constraint edited
Constraint := FCheckConstraints[Node.Index];
if (not Constraint.Added) and (not Constraint.Modified) then
FDeletedCheckConstraints.Add(Constraint.Name);
case Column of
0: Constraint.Name := NewText;
1: Constraint.CheckClause := NewText;
Expand Down Expand Up @@ -2361,7 +2422,7 @@ procedure TfrmTableEditor.btnRemoveForeignKeyClick(Sender: TObject);
listForeignKeys.CancelEditNode;
Key := FForeignKeys[listForeignKeys.FocusedNode.Index];
if not Key.Added then
DeletedForeignKeys.Add(Key.OldKeyName);
FDeletedForeignKeys.Add(Key.OldKeyName);
FForeignKeys.Delete(listForeignKeys.FocusedNode.Index);
Modification(Sender);
listForeignKeys.Repaint;
Expand All @@ -2377,7 +2438,7 @@ procedure TfrmTableEditor.btnClearForeignKeysClick(Sender: TObject);
listForeignKeys.CancelEditNode;
for i:=FForeignKeys.Count-1 downto 0 do begin
if not FForeignKeys[i].Added then
DeletedForeignKeys.Add(FForeignKeys[i].OldKeyName);
FDeletedForeignKeys.Add(FForeignKeys[i].OldKeyName);
FForeignKeys.Delete(i);
end;
Modification(Sender);
Expand Down

0 comments on commit 461c734

Please sign in to comment.