Skip to content

Commit

Permalink
Support naming result tabs per "-- name: xyz" comment. See https://ww…
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarbecker committed Apr 8, 2024
1 parent fcca03a commit 9364da6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
8 changes: 8 additions & 0 deletions source/dbconnection.pas
Expand Up @@ -877,6 +877,7 @@ TDBQuery = class(TComponent)
function DatabaseName: String; virtual; abstract;
function TableName: String; overload;
function TableName(Column: Integer): String; overload; virtual; abstract;
function ResultName: String;
function QuotedDbAndTableName: String;
procedure DiscardModifications;
procedure PrepareColumnAttributes;
Expand Down Expand Up @@ -9792,6 +9793,13 @@ function TDBQuery.QuotedDbAndTableName: String;
end;


function TDBQuery.ResultName: String;
begin
// Return name of query defined in a comment above the actual query
Result := RegExprGetMatch('--\s+name\:\s+(.+)[\r\n]', FSQL, 1, False, True);
Result := Trim(Result);
end;

function TDBQuery.GetKeyColumns: TStringList;
var
i: Integer;
Expand Down
14 changes: 6 additions & 8 deletions source/main.pas
Expand Up @@ -3330,15 +3330,13 @@ procedure TMainForm.AfterQueryExecution(Thread: TQueryThread);
Tab.ResultTabs.Add(NewTab);
NewTab.Results := Results;
try
TabCaption := NewTab.Results.TableName;
// Add postfix to tab name so tab captions are unique
i := 1;
while Tab.tabsetQuery.Tabs.IndexOf(TabCaption) > -1 do begin
Inc(i);
TabCaption := NewTab.Results.TableName + ' #' + IntToStr(i);
TabCaption := NewTab.Results.ResultName;
if TabCaption.IsEmpty then
TabCaption := NewTab.Results.TableName;
except
on E:EDbError do begin
TabCaption := _('Result')+' #'+IntToStr(Tab.ResultTabs.Count);
end;
except on E:EDbError do
TabCaption := _('Result')+' #'+IntToStr(Tab.ResultTabs.Count);
end;

TabCaption := TabCaption + ' (' + FormatNumber(Results.RecordCount) + 'r × ' + FormatNumber(Results.ColumnCount) + 'c)';
Expand Down

0 comments on commit 9364da6

Please sign in to comment.