Skip to content

Commit 520a90c

Browse files
committed
fix: TDBQuery.TableName always returned an empty string on MS SQL
1 parent a01acd1 commit 520a90c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

source/dbconnection.pas

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9988,8 +9988,8 @@ function TSQLiteQuery.DatabaseName: String;
99889988
function TDBQuery.TableName: String;
99899989
var
99909990
i: Integer;
9991-
NextTable: String;
9992-
//rx: TRegExpr;
9991+
NextTable, Quotes, Ident: String;
9992+
rx: TRegExpr;
99939993
begin
99949994
// Get table name from a result set
99959995
Result := '';
@@ -10000,17 +10000,21 @@ function TDBQuery.TableName: String;
1000010000
if not NextTable.IsEmpty then
1000110001
Result := NextTable;
1000210002
end;
10003-
{if Result.IsEmpty then begin
10003+
if Result.IsEmpty then begin
10004+
// Poor detection of table name in the SQL query, only for cases where the driver is not able to do it better.
10005+
// Tested with MS SQL: SELECT * FROM master."dbo".containers
1000410006
// Untested with joins, compute columns and views
10005-
Result := GetTableNameFromSQLEx(SQL, idMixCase);
1000610007
rx := TRegExpr.Create;
10007-
rx.Expression := '\.([^\.]+)$';
10008-
if rx.Exec(Result) then
10009-
Result := rx.Match[1];
10008+
rx.ModifierI := True;
10009+
Quotes := QuoteRegExprMetaChars(Connection.QuoteChars);
10010+
Ident := '['+Quotes+']?[^\s\.'+Quotes+']+['+Quotes+']?';
10011+
rx.Expression := '\bFROM\s+('+Ident+'\.)?('+Ident+'\.)?('+Ident+')\b';
10012+
if rx.Exec(FSQL) then
10013+
Result := Connection.DeQuoteIdent(rx.Match[3]);
1001010014
rx.Free;
1001110015
if Result.IsEmpty then
1001210016
raise EDbError.Create('Could not determine name of table.');
10013-
end;}
10017+
end;
1001410018
end;
1001510019

1001610020

@@ -10053,7 +10057,8 @@ function TMySQLQuery.TableName(Column: Integer): String;
1005310057
{$IFDEF HASMSSQL}
1005410058
function TSqlSrvQuery.TableName(Column: Integer): String;
1005510059
begin
10056-
Result := '';
10060+
// Mostly empty on MSSQL, so the poor regex in TDBQuery.TableName has to be sufficient
10061+
Result := FCurrentResults.Fields[Column].Origin;
1005710062
end;
1005810063
{$ENDIF}
1005910064

0 commit comments

Comments
 (0)