|
NAMEDBIx::Tree - Generate a tree from a self-referential database table Synopsis use DBIx::Tree;
# have DBIx::Tree build the necessary SQL from table & column names:
my $tree = new DBIx::Tree(connection => $dbh,
table => $table,
method => sub { disp_tree(@_) },
columns => [$id_col, $label_col, $parent_col],
start_id => $start_id);
$tree->traverse;
# alternatively, use your own custom SQL statement
my $sql = <<EOSQL;
SELECT nodes.id, labels.label, nodes.parent_id
FROM nodes
INNER JOIN labels
ON nodes.id = labels.node_id
WHERE labels.type = 'preferred label'
ORDER BY label ASC
EOSQL my $tree = new DBIx::Tree(connection => $dbh,
sql => $sql,
method => sub { disp_tree(@_) },
columns => ['id', 'label', 'parent_id'],
start_id => $start_id);
$tree->traverse;
# or use an already prepared DBI statement handle:
my $sth = $dbh->prepare($sql);
my $tree = new DBIx::Tree(connection => $dbh,
sth => $sth,
method => sub { disp_tree(@_) },
columns => ['id', 'label', 'parent_id'],
start_id => $start_id);
$tree->traverse;
DescriptionWhen you've got one of those nasty self-referential tables that you want to bust out into a tree, this is the module to check out. Assuming there are no horribly broken nodes in your tree and (heaven forbid) any circular references, this module will turn something like: food food_id parent_id
================== ======= =========
Food 001 NULL
Beans and Nuts 002 001
Beans 003 002
Nuts 004 002
Black Beans 005 003
Pecans 006 004
Kidney Beans 007 003
Red Kidney Beans 008 007
Black Kidney Beans 009 007
Dairy 010 001
Beverages 011 010
Whole Milk 012 011
Skim Milk 013 011
Cheeses 014 010
Cheddar 015 014
Stilton 016 014
Swiss 017 014
Gouda 018 014
Muenster 019 014
Coffee Milk 020 011
into: Food (001)
Dairy (010)
Beverages (011)
Coffee Milk (020)
Whole Milk (012)
Skim Milk (013)
Cheeses (014)
Cheddar (015)
Stilton (016)
Swiss (017)
Gouda (018)
Muenster (019)
Beans and Nuts (002)
Beans (003)
Black Beans (005)
Kidney Beans (007)
Red Kidney Beans (008)
Black Kidney Beans (009)
Nuts (004)
Pecans (006)
See the examples/ directory for two Tk examples. InstallationInstall DBIx::Tree as you would for any "Perl" module: Run: cpanm DBIx::Tree
Note: cpanm ships in App::cpanminus. See also App::perlbrew.
or run: sudo cpan DBIx::Tree or unpack the distro, and then either: perl Build.PL
./Build
./Build test
sudo ./Build install
or: perl Makefile.PL
make (or dmake or nmake)
make test
make install
Constructor and InitializationCalling new()new() is called as "my($obj) = DBIx::Tree -> new(k1 => v1, k2 => v2, ...)". It returns a new object of type "DBIx::Tree". Key-value pairs accepted in the parameter list:
Methodsnew(%args) my $tree = new DBIx::Tree(connection => $dbh,
table => $table,
sql => $sql,
sth => $sth,
method => sub { disp_tree(@_) },
columns => [$id_col, $label_col, $parent_col],
start_id => $start_id,
threshold => $threshold,
match_data => $match_data,
limit => $limit
recursive => 1 || 0);
traverse(%args)Begins a depth-first traversal of the hierarchical tree. The optional %args hash provides locally overriding values for the identical parameters set in the new() constructor. TODOGraceful handling of circular references. Better docs. Rewrite the algorithm. Separate data acquisition from data formatting. See AlsoDBIx::Tree::Persist. Parse::Taxonomy. Tree. Tree::Binary. Tree::DAG_Node. My favourite. Tree::DAG_Node::Persist. Tree::Persist. Tree::Simple. Tree::Simple::Visitor::Factory. Machine-Readable Change LogThe file Changes was converted into Changelog.ini by Module::Metadata::Changes. Repository<https://github.com/ronsavage/DBIx-Tree> SupportBugs should be reported via the CPAN bug tracker at <https://github.com/ronsavage/DBIx-Tree/issues> AuthorsBrian Jepson, bjepson@ids.net This module was inspired by the Expanding Hierarchies example that I stumbled across in the Microsoft SQL Server Database Developer's Companion section of the Microsoft SQL Server Programmer's Toolkit. Jan Mach <machj@ders.cz> contributed substantial performance improvements, ordering handling for tree output, and other bug fixes. Aaron Mackey <amackey@virginia.edu> has continued active development on the module based on Brian Jepson's version 0.91 release. Co-maintenance since V 1.91 is by Ron Savage <rsavage@cpan.org>. Uses of 'I' in previous versions is not me, but will be hereafter.
|