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

This commit is contained in:
Ansgar Becker
2020-02-11 16:56:41 +01:00
parent ef980de42f
commit ad024cb74b
3 changed files with 24 additions and 10 deletions

View File

@ -19,6 +19,7 @@ type
TConnectionParameters = class;
TDBQuery = class;
TDBQueryList = TObjectList<TDBQuery>;
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<TForeignKey>)
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;

View File

@ -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;

View File

@ -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