DBIx::Tree::NestedSet

Implements a "Nested Set" parent/child tree
Download

DBIx::Tree::NestedSet Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Dan Collis Puro
  • Publisher web site:
  • http://search.cpan.org/~djcp/

DBIx::Tree::NestedSet Tags


DBIx::Tree::NestedSet Description

Implements a "Nested Set" parent/child tree DBIx::Tree::NestedSet is a Perl module that implements a "Nested Set" parent/child tree.SYNOPSISExample: #!/usr/bin/perl #This is in "scripts/tree_example.pl" of DBIx::Tree::NestedSet distribution use strict; use warnings; use DBIx::Tree::NestedSet; use DBI; #Create the connection. We'll use SQLite for now. #my $dbh=DBI->connect('DBI:mysql:test','user','pass') or die ($DBI::errstr); my $dbh=DBI->connect('DBI:SQLite2:test') or die ($DBI::errstr); my $db_type='SQLite'; #my $db_type='MySQL'; my $tree=DBIx::Tree::NestedSet->new( dbh=>$dbh, db_type=>$db_type ); #Let's see how the table will be created for this driver print "Default Create Table Statement for $db_type: "; print $tree->get_default_create_table_statement()." "; #Let's create it. $tree->create_default_table(); #Create the root node. my $root_id=$tree->add_child_to_right(name=>'Food'); #Second level my $vegetable_id=$tree->add_child_to_right(id=>$root_id,name=>'Vegetable'); my $animal_id=$tree->add_child_to_right(id=>$root_id,name=>'Animal'); my $mineral_id=$tree->add_child_to_right(id=>$root_id,name=>'Mineral'); #Third Level, under "Vegetable" foreach ('Froot','Beans','Legumes','Tubers') { $tree->add_child_to_right(id=>$vegetable_id,name=>$_); } #Third Level, under "Animal" foreach ('Beef','Chicken','Seafood') { $tree->add_child_to_right(id=>$animal_id,name=>$_); } #Hey! We forgot pork! Since it's the other white meat, #it should be first among the "Animal" crowd. $tree->add_child_to_left(id=>$animal_id,name=>'Pork'); #Oops. Misspelling. $tree->edit_node( id=>$tree->get_id_by_key(key_name=>'name',key_value=>'Froot'), name=>'Fruit' ); #Get the child nodes of the 2nd level "Animal" node my $children=$tree->get_self_and_children_flat(id=>$animal_id); #Grab the first node, which is "Animal" and the #parent of this subtree. my $parent=shift @$children; print 'Parent Node: '.$parent->{name}." "; #Loop through the children and do something. foreach my $child(@$children) { print ' Child ID: '.$child->{id}.' '.$child->{name}." "; } #Mineral? Get rid of it. $tree->delete_self_and_children(id=>$mineral_id); #Print the rudimentary report built into the module. print " The Complete Tree: "; print $tree->create_report();This module implements a "Nested Set" parent/child tree, and is focused (at least in my mind) towards offering methods that make developing web applications easier. It should be generally useful, though.See the "SEE ALSO" section for resources that explain the advantages and features of a nested set tree. This module gives you arbitrary levels of nodes, the ability to put in metadata associated with a node via simple method arguments and storage via DBI.There are currently drivers implemented for MySQL and SQLite version 2. For some reason this module segfaults on my debian boxes running SQLite 3. Use SQLite 2, unless you can figure out what's going on.It should be trivial to write one for your RDBMS, see DBIx::Tree::NestedSet::MySQL for an example driver.A nested set tree is "expensive" on updates because you have to edit quite a bit of the tree on inserts, deletes, or the movement of nodes. Conversely, it is "cheaper" on just queries of the tree because nearly every action (getting children, getting parents, getting siblings, etc) can be done with one SQL statement.If you're developing apps that require many reads and few updates to a tree (like pretty much every web app I've ever built) a nested set should offer significant performance advantages over the recursive queries required by the typical adjacency list model.Whew. Say that fast three times.Use the create_default_table() method to create your Nested Set table in your RDBMS. Requirements: · Perl


DBIx::Tree::NestedSet Related Software