| Finding Links Between Tables |
|
|
|
| Contributed by Manuele Grueff | |
| 04 June 2001 | |
|
Use this code tip to find dependencies between tables which use referential integrity.Finding Links Between Tables © 2001Manuele Grueff In many situations would be nice to know before tinkering with a table (copying records from another source, restructuring, and many other situations) if it depends on other tables, or, as may say, what's the "order" of the table. I mean that if for example you have an "Articles" table and an "Orders" table, if you're a good fella you should have a referential integrity bond between the two: Records in the Orders table should point to an article in the other table, by means of a nice Id field which has to be bound by Ref. int. Now suppose that you want to copy the contents of the two tables from an alias to another (one obvious situation is when you're upgrading an app to a newer version, and want to regenerate tables on the way): you can't do this in a casual manner, since no record can be appended to Orders before the corresponding Article has been inserted; you should then copy first the content of articles, and then the contents of Orders. I've built a simple recursive routine (surely you can write a better one! but it does indeed function) that tells you the "order" of a table: in the latter example, Articles would be of order=0 and Orders would be of order=1, and so on: if we had a table depending on Orders, it'll be of order=2, and ... you can easily figure the rest! Here's my code snippet (it needs the table name or full path and returns the "order"): method gettableorder(tblname string) longint
var
order, neworder longint
tmptc1, tmptc2 tcursor
endvar
;tracerwrite("Calculating order of table "+tblname+"...")
tmptc1.open(tblname)
tmptc1.enumrefintstruct(tmptc2)
order=0
scan tmptc2 for tmptc2.slave="Y":
neworder=gettableorder(tmptc2.othertable)+1
if neworder>order then
order=neworder
endif
endscan
return order
endMethod
Enjoy and comment freely at grueff@atipica.it! |
| < Prev | Next > |
|---|





