From f4a82f4ec08eb2d0bf09d3753df6d7c24fecd00a Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Fri, 5 Jul 2013 04:37:06 +0000 Subject: [PATCH] Use TDBQuery.DBObject.Database in TDBQuery.DatabaseName(), so for VIEWs we do not get the db from the table which is selected. Fixes issue #3266. --- source/dbconnection.pas | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 91baddd9..03417ed7 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -4793,16 +4793,22 @@ var Field: PMYSQL_FIELD; i: Integer; begin - // Return first available Field.db property, or just the current database as fallback - for i:=0 to ColumnCount-1 do begin - Field := mysql_fetch_field_direct(FCurrentResults, i); - if Field.db <> '' then begin - Result := Connection.DecodeAPIString(Field.db); - break; + // Find and return name of database of current query + if FDBObject <> nil then begin + Result := FDBObject.Database; + end else begin + // Return first available Field.db property, or just the current database as fallback. + // For a view in db1 selecting from db2, this returns db2, which triggers errors in GetCreateViewCode! + for i:=0 to ColumnCount-1 do begin + Field := mysql_fetch_field_direct(FCurrentResults, i); + if Field.db <> '' then begin + Result := Connection.DecodeAPIString(Field.db); + break; + end; end; + if Result = '' then + Result := Connection.Database; end; - if Result = '' then - Result := Connection.Database; end;