Skip to content
Permalink
Browse files

Issue #12: detect foreign keys of SQLite tables

  • Loading branch information
ansgarbecker committed Jan 13, 2020
1 parent 245e033 commit 93a3c97d8cf119b7d3b13e2985a0ab67c93ea221
Showing with 28 additions and 0 deletions.
  1. +28 −0 source/dbconnection.pas
@@ -621,6 +621,7 @@ TSQLiteConnection = class(TDBConnection)
property LastRawResults: TSQLiteRawResults read FLastRawResults;
function GetTableColumns(Table: TDBObject): TTableColumnList; override;
function GetTableKeys(Table: TDBObject): TTableKeyList; override;
function GetTableForeignKeys(Table: TDBObject): TForeignKeyList; override;
end;


@@ -4922,6 +4923,33 @@ function TDBConnection.GetTableForeignKeys(Table: TDBObject): TForeignKeyList;
end;


function TSQLiteConnection.GetTableForeignKeys(Table: TDBObject): TForeignKeyList;
var
ForeignQuery: TDBQuery;
ForeignKey: TForeignKey;
begin
// SQLite: query PRAGMA foreign_key_list
Result := TForeignKeyList.Create(True);
ForeignQuery := GetResults('SELECT * from PRAGMA_foreign_key_list('+EscapeString(Table.Name)+')');
ForeignKey := nil;
while not ForeignQuery.Eof do begin
if (not Assigned(ForeignKey)) or (ForeignKey.KeyName <> ForeignQuery.Col('id')) then begin
ForeignKey := TForeignKey.Create(Self);
Result.Add(ForeignKey);
ForeignKey.KeyName := ForeignQuery.Col('id');
ForeignKey.OldKeyName := ForeignKey.KeyName;
ForeignKey.ReferenceTable := Table.Database + '.' + ForeignQuery.Col('table');
ForeignKey.OnUpdate := ForeignQuery.Col('on_update');
ForeignKey.OnDelete := ForeignQuery.Col('on_delete');
end;
ForeignKey.Columns.Add(ForeignQuery.Col('from'));
ForeignKey.ForeignColumns.Add(ForeignQuery.Col('to'));
ForeignQuery.Next;
end;
ForeignQuery.Free;
end;


function TMySQLConnection.GetRowCount(Obj: TDBObject): Int64;
var
Rows: String;

0 comments on commit 93a3c97

Please sign in to comment.
You can’t perform that action at this time.