From ad024cb74b64a3a8f51e831020c9e14c9774bbb7 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Tue, 11 Feb 2020 16:56:41 +0100 Subject: [PATCH] Reference table of a foreign key is prefixed with its database, which causes the table editor to display no columns in the dropdown editor. Fix that by outsourcing some logic from main unit into TForeignKey.ReferenceTableObj. Closes #158 --- source/dbconnection.pas | 22 ++++++++++++++++++++++ source/main.pas | 10 +--------- source/table_editor.pas | 2 +- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 85ba30af..6398a4df 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -19,6 +19,7 @@ type TConnectionParameters = class; TDBQuery = class; TDBQueryList = TObjectList; + TDBObject = class; TColumnPart = (cpAll, cpName, cpType, cpAllowNull, cpDefault, cpVirtuality, cpComment, cpCollation); TColumnParts = Set of TColumnPart; @@ -94,6 +95,7 @@ type destructor Destroy; override; procedure Assign(Source: TPersistent); override; function SQLCode(IncludeSymbolName: Boolean): String; + function ReferenceTableObj: TDBObject; end; TForeignKeyList = class(TObjectList) public @@ -4251,6 +4253,9 @@ begin Break; end; end; + if not Assigned(Result) then begin + Log(lcDebug, Format('Could not find object "%s" in database "%s"', [Obj, DB])); + end; end; @@ -9017,6 +9022,23 @@ begin Result := Result + ' ON DELETE ' + OnDelete; end; + +function TForeignKey.ReferenceTableObj: TDBObject; +var + RefDb, RefTable: String; +begin + // Find database object of reference table + RefDb := ReferenceTable.Substring(0, Pos('.', ReferenceTable)-1); + if not RefDb.IsEmpty then begin + RefTable := ReferenceTable.Substring(Length(RefDb)+1); + end else begin + RefDb := FConnection.Database; + RefTable := ReferenceTable; + end; + Result := FConnection.FindObject(RefDb, RefTable); +end; + + procedure TForeignKeyList.Assign(Source: TForeignKeyList); var Item, ItemCopy: TForeignKey; diff --git a/source/main.pas b/source/main.pas index 734266af..e382e34f 100644 --- a/source/main.pas +++ b/source/main.pas @@ -9539,7 +9539,6 @@ var ForeignResults, Results: TDBQuery; Conn: TDBConnection; RowNum: PInt64; - RefDb, RefTable: String; RefObj: TDBObject; AllowEdit: Boolean; begin @@ -9557,14 +9556,7 @@ begin idx := ForeignKey.Columns.IndexOf(DataGrid.Header.Columns[Column].Text); if idx > -1 then try // Find the first text column if available and use that for displaying in the pulldown instead of using meaningless id numbers - RefDb := ForeignKey.ReferenceTable.Substring(0, Pos('.', ForeignKey.ReferenceTable)-1); - if not RefDb.IsEmpty then begin - RefTable := ForeignKey.ReferenceTable.Substring(Length(RefDb)+1); - end else begin - RefDb := Conn.Database; - RefTable := ForeignKey.ReferenceTable; - end; - RefObj := Conn.FindObject(RefDb, RefTable); + RefObj := ForeignKey.ReferenceTableObj; TextCol := ''; if Assigned(RefObj) then begin Columns := RefObj.TableColumns; diff --git a/source/table_editor.pas b/source/table_editor.pas index 475e7aa5..e11b9866 100644 --- a/source/table_editor.pas +++ b/source/table_editor.pas @@ -2319,7 +2319,7 @@ begin 3: begin Key := FForeignKeys[Node.Index]; SetEditor := TSetEditorLink.Create(VT, True); - Obj := DBObject.Connection.FindObject(DBObject.Database, Key.ReferenceTable); + Obj := Key.ReferenceTableObj; if Obj <> nil then begin Columns := Obj.TableColumns; for Col in Columns do begin