in reply to Re^2: Advise on sql table mapping.
in thread Advise on sql table mapping.
Exactly - $prefix contains the path taken so far, that's what the recursion does.
Again, why are you trying to print out the stuff from within find_joins? Please, don't do that. Just don't. Take the results of find_joins and then, convert them to the output format. It's much easier that way instead of trying to make a recursive function output the stuff you want at the end.
Your way of iterating over arrays is cumbersome, as you never need the array index anywhere:
for my $i ( 0 .. $#results ) {
is easier written as
for my $element ( @results ) {
But other than that, your code is close once you just take the data structure returned by find_joins and work on that:
# Build a lookup table for "left_table;right_table" => [ join columns +] my %link_columns = map { $_->[0] . ";" . $_->[1] => [ $_->[2], $_->[3] ], $_->[1] . ";" . $_->[0] => [ $_->[3], $_->[2] ] } @links; my @results = find_joins( 'apple' => 'owl' ); die "No path from 'apple' to 'owl'" unless @results; my $shortest_path = $results[0]; my $start = shift @$shortest_path; print "SELECT * FROM "; print "$start\n"; my $left = $start; for my $table (@$shortest_path) { my $key = "$left;$table"; print "-- $key\n"; my $join_cols = $link_columns{$key}; print " inner join $table on $join_cols->[0] = $join_cols->[1]\n"; $left = $table; };
|
|---|