From 9364da6661223066d0b276db2e2116797ac7f466 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Mon, 8 Apr 2024 14:32:35 +0200 Subject: [PATCH] Support naming result tabs per "-- name: xyz" comment. See https://www.heidisql.com/forum.php?t=10493 --- source/dbconnection.pas | 8 ++++++++ source/main.pas | 14 ++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 0301eb2f..af280b87 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -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; @@ -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; diff --git a/source/main.pas b/source/main.pas index 6cb655cb..35d7ffd5 100644 --- a/source/main.pas +++ b/source/main.pas @@ -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)';