Registriert seit: 18. Nov 2015
Ort: Kehl
678 Beiträge
Delphi 11 Alexandria
|
AW: Firebird Reihenfolge der Tabellen bei Insert mit Foreignkey
10. Jun 2016, 18:48
Auf Basis von Firedac:
hatte ich mal für ein ähnliches InHouse Problem bei uns so gelöst:
Delphi-Quellcode:
procedure TdbMoveOld.Execute_copy_Work;
var
lTables: Tstringlist;
lTablesnoReference: Tstringlist;
lTablesWithReference: Tstringlist;
lTablesDummy: Tstringlist;
lTablename: string;
lfkey: string;
lFinishedTables: Tstringlist;
aindex: integer;
function getIndexforinsert: integer;
var
dummy: Tstringlist;
i, temp: integer;
s: string;
found: boolean;
begin
result := -1;
for i := 0 to lTablesWithReference.count - 1 do
begin
dummy := pointer(lTablesWithReference.Objects[i]);
if dummy <> nil then
begin
found := true;
for s in dummy do
if not lFinishedTables.find(s, temp) then
begin
found := false;
end;
if found then
exit(i);
end;
end;
end;
begin
lTables := Tstringlist.create;
lTablesnoReference := Tstringlist.create;
lTablesWithReference := Tstringlist.create;
lFinishedTables := Tstringlist.create;
lFinishedTables.Sorted := true;
try
// Get All Tables
FileDestConnection.GetTableNames('', '', '', lTables, [osMy], [tkTable]);
for lTablename in lTables do
begin
// CwLogSave.send(lTablename);
metaQuery.MetaInfoKind := mkForeignKeys;
metaQuery.ObjectName := lTablename;
metaQuery.Open;
if metaQuery.RecordCount > 0 then
begin
lTablesDummy := Tstringlist.create;
lTablesDummy.Sorted := true;
lTables.Duplicates := dupIgnore;
while not metaQuery.eof do
begin
lfkey := metaQuery.FieldValues['PKEY_TABLE_NAME'];
if lfkey <> lTablename then
lTablesDummy.add(lfkey);
metaQuery.Next;
end;
lTablesWithReference.AddObject(lTablename, lTablesDummy);
end
else
begin
// There is no reference so put in in the list
lTablesnoReference.add(lTablename);
end;
metaQuery.Close;
end;
// Now try the loop
// First all Tables without any references
for lTablename in lTablesnoReference do
begin
lFinishedTables.add(lTablename);
CopyData(lTablename); // Die must Du Dir selber bauen
end;
while lTablesWithReference.count > 0 do
begin
aindex := getIndexforinsert;
if aindex > -1 then
begin
// CwLogSave.send('Insert now ' + lTablesWithReference[aindex]);
lFinishedTables.add(lTablesWithReference[aindex]);
if (lTablesWithReference.Objects[aindex] <> nil) then
Tstringlist(lTablesWithReference.Objects[aindex]).free;
lTablename := lTablesWithReference[aindex];
lTablesWithReference.delete(aindex);
CopyData(lTablename); // Die must Du Dir selber bauen
end
else
break; // Ther is a error
end;
if lTablesWithReference.count > 0 then
begin
CwLogSave.senderror('Not all found');
CwLogSave.entermethod('Tables with references');
for lTablename in lTablesWithReference do
CwLogSave.send(lTablename);
CwLogSave.ExitMethod('Tables with references');
end;
finally
lTables.free;
lTablesnoReference.free;
lTablesWithReference.free;
lFinishedTables.free;
end;
end;
Fritz
Fritz Westermann
|
|
Zitat
|