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