Issue #156: merge code from follow-foreign-key branch

This commit is contained in:
Ansgar Becker
2022-04-28 19:23:46 +02:00
parent a6ad041a7c
commit 511e2bf19d
2 changed files with 81 additions and 0 deletions

View File

@@ -1247,6 +1247,7 @@ object MainForm: TMainForm
OnChange = AnyGridChange
OnColumnResize = DataGridColumnResize
OnCreateEditor = AnyGridCreateEditor
OnDblClick = AnyGridDblClick
OnEditCancelled = AnyGridEditCancelled
OnEdited = AnyGridEdited
OnEditing = AnyGridEditing
@@ -2374,6 +2375,13 @@ object MainForm: TMainForm
ImageName = 'icons8-close-button'
OnExecute = actExitApplicationExecute
end
object actFollowForeignKey: TAction
Category = 'Various'
Caption = 'Follow Foreign Key'
Hint = 'Follow foreign key to the linked table'
ImageIndex = 61
OnExecute = actFollowForeignKeyExecute
end
object actCopy: TAction
Category = 'Various'
Caption = '&Copy'
@@ -3785,6 +3793,10 @@ object MainForm: TMainForm
OnPopup = popupDataGridPopup
Left = 200
Top = 248
object FollowForeignKey: TMenuItem
Action = actFollowForeignKey
ShortCut = 32807
end
object Copy3: TMenuItem
Action = actCopy
end

View File

@@ -190,6 +190,7 @@ type
MainMenuFile: TMenuItem;
FileNewItem: TMenuItem;
MainMenuHelp: TMenuItem;
FollowForeignKey: TMenuItem;
N1: TMenuItem;
FileExitItem: TMenuItem;
menuAbout: TMenuItem;
@@ -198,6 +199,7 @@ type
PasteItem: TMenuItem;
StatusBar: TStatusBar;
ActionList1: TActionList;
actFollowForeignKey: TAction;
actCopy: TAction;
actPaste: TAction;
actNewWindow: TAction;
@@ -954,6 +956,7 @@ type
procedure ListTablesInitNode(Sender: TBaseVirtualTree; ParentNode,
Node: PVirtualNode; var InitialStates: TVirtualNodeInitStates);
procedure AnyGridAfterPaint(Sender: TBaseVirtualTree; TargetCanvas: TCanvas);
procedure actFollowForeignKeyExecute(Sender: TObject);
procedure actCopyOrCutExecute(Sender: TObject);
procedure actPasteExecute(Sender: TObject);
procedure actSelectAllExecute(Sender: TObject);
@@ -1167,6 +1170,7 @@ type
NewDPI: Integer);
procedure menuCloseTabOnDblClickClick(Sender: TObject);
procedure TimerRefreshTimer(Sender: TObject);
procedure AnyGridDblClick(Sender: TObject);
private
// Executable file details
FAppVerMajor: Integer;
@@ -7700,6 +7704,7 @@ begin
menuSQLHelpData.Enabled := InDataGrid;
Refresh3.Enabled := InDataGrid;
actGridEditFunction.Enabled := CellFocused;
actFollowForeignKey.Enabled := InDataGrid;
if not CellFocused then
Exit;
@@ -10322,6 +10327,12 @@ begin
end;
procedure TMainForm.AnyGridDblClick(Sender: TObject);
begin
//if KeyPressed(VK_MENU) then // VK_MENU is Alt key
actFollowForeignKey.Execute;
end;
procedure TMainForm.AnyGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
var
g: TVirtualStringTree;
@@ -11252,6 +11263,64 @@ begin
end;
procedure TMainForm.actFollowForeignKeyExecute(Sender: TObject);
var
CurrentControl: TWinControl;
Grid: TVirtualStringTree;
Results: TDBQuery;
RowNum: PInt64;
FocusedValue, FocusedColumnName, ForeignColumnName, ReferenceTable: String;
HasNulls: Boolean;
ForeignKey: TForeignKey;
i: Integer;
PDBObj: PDBObject;
DBObj: TDBObject;
Node: PVirtualNode;
begin
CurrentControl := Screen.ActiveControl;
Grid := CurrentControl as TVirtualStringTree;
Results := GridResult(Grid);
RowNum := Grid.GetNodeData(Grid.FocusedNode);
Results.RecNo := RowNum^;
FocusedValue := Results.Col(Grid.FocusedColumn);
//get column name from header
FocusedColumnName := Grid.Header.Columns[Grid.FocusedColumn].Text;
//find foreign key for current column
for ForeignKey in ActiveDBObj.TableForeignKeys do begin
i := ForeignKey.Columns.IndexOf(DataGrid.Header.Columns[Grid.FocusedColumn].Text);
if i > -1 then begin
ForeignColumnName := ForeignKey.ForeignColumns[i];
ReferenceTable := ForeignKey.ReferenceTable;
break;
end;
end;
if ForeignColumnName = '' then begin
LogSQL(f_('Foreign key not found for column "%s"', [FocusedColumnName]), lcInfo);
exit;
end;
// jump to ReferenceTable
Node := GetNextNode(ListTables, nil);
while Assigned(Node) do begin
PDBObj := ListTables.GetNodeData(Node);
DBObj := PDBObj^;
if DBObj.Database + '.' + DBObj.Name = ReferenceTable then begin
ActiveDBObj := DBObj;
break;
end;
Node := GetNextNode(ListTables, Node);
end;
// filter to show only rows linked by the foreign key
SynMemoFilter.UndoList.AddGroupBreak;
SynMemoFilter.Text := ForeignColumnName + ' = "' + FocusedValue + '"';
ToggleFilterPanel(True);
actApplyFilter.Execute;
// SynMemoFilter will be cleared and set value of asFilter (in HandleDataGridAttributes from DataGridBeforePaint)
AppSettings.SessionPath := GetRegKeyTable;
AppSettings.WriteString(asFilter, SynMemoFilter.Text);
end;
procedure TMainForm.actCopyOrCutExecute(Sender: TObject);
var
CurrentControl: TWinControl;