DBIx-Class-Tree-NestedSet-0.10/0000755446137100000240000000000011636651764015560 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/t/0000755446137100000240000000000011636651764016023 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/t/08-exception.t0000644446137100000240000000042111636645473020430 0ustar dochertigamesuse strict; use warnings; use Test::More tests => 1; use Test::Exception; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; use FailSchema; throws_ok(sub { FailSchema->load_classes(qw/MissingCols/); }, qr/required param/, 'missing params'); DBIx-Class-Tree-NestedSet-0.10/t/16-siblings.t0000755446137100000240000001625211636645473020257 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use Test::Exception; use DBICx::TestDatabase; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; use TestTree; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $test_tree = TestTree->new({schema => $schema}); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); # Create the tree my $tree1 = $trees->create({ content => '1 tree root', root_id => 10}); my $child1_1 = $tree1->add_to_children({ content => '1 child 1' }); my $child1_2 = $tree1->add_to_children({ content => '1 child 2' }); my $child1_3 = $tree1->add_to_children({ content => '1 child 3' }); my $child1_4 = $tree1->add_to_children({ content => '1 child 4' }); my $gchild1_1 = $child1_2->add_to_children({ content => '1 g-child 1' }); my $gchild1_2 = $child1_2->add_to_children({ content => '1 g-child 2' }); my $gchild1_3 = $child1_4->add_to_children({ content => '1 g-child 3' }); my $gchild1_4 = $child1_4->add_to_children({ content => '1 g-child 4' }); my $ggchild1 = $gchild1_2->add_to_children({ content => '1 gg-child 1' }); $tree1->discard_changes; $child1_1->discard_changes; $child1_2->discard_changes; $child1_3->discard_changes; $child1_4->discard_changes; my (@children, $sibling, @siblings, $siblings_rs, @ids); goto NEXT; NEXT: # Check that the test tree is constructed correctly is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_1, $child1_2, $gchild1_1, $gchild1_2, $ggchild1, $child1_3, $child1_4, $gchild1_3, $gchild1_4], 'Test Tree is organised correctly.', ); $test_tree->structure($tree1,"Initial Tree"); # siblings $siblings_rs = $tree1->siblings; is($siblings_rs, undef, "Root has no siblings"); @siblings = $child1_3->siblings; is(scalar @siblings, 3, "Scalar - siblings - correct number"); is_deeply( [map {$_->id} @siblings], [map {$_->id} $child1_1, $child1_2, $child1_4], 'List - siblings - correct ids', ); $siblings_rs = $child1_3->siblings; while (my $sibling = $siblings_rs->next) { push @ids, $sibling->id; } is_deeply( [@ids], [map {$_->id} $child1_1, $child1_2, $child1_4], 'Scalar - siblings - correct ids', ); # left siblings @siblings = $child1_3->left_siblings; is(scalar @siblings, 2, "Scalar - left_siblings - correct number"); is_deeply( [map {$_->id} @siblings], [map {$_->id} $child1_1, $child1_2], 'List - left_siblings - correct ids', ); $siblings_rs = $child1_3->left_siblings; undef @ids; while (my $sibling = $siblings_rs->next) { push @ids, $sibling->id; } is_deeply( [@ids], [map {$_->id} $child1_1, $child1_2], 'Scalar - left_siblings - correct ids', ); $siblings_rs = $child1_1->left_siblings; is($siblings_rs->next, undef, "Scalar - left_siblings - no left siblings"); # right siblings @siblings = $child1_3->right_siblings; is(scalar @siblings, 1, "Scalar - right_siblings - correct number"); is_deeply( [map {$_->id} @siblings], [map {$_->id} $child1_4], 'List - right_siblings - correct ids', ); $siblings_rs = $child1_3->right_siblings; undef @ids; while (my $sibling = $siblings_rs->next) { push @ids, $sibling->id; } is_deeply( [@ids], [map {$_->id} $child1_4], 'Scalar - right_siblings - correct ids', ); $siblings_rs = $child1_4->right_siblings; is($siblings_rs->next, undef, "Scalar - right_siblings - none expected"); # previous sibling $sibling = $child1_4->left_sibling; is($sibling->id, $child1_3->id, "Left Sibling - correct ID"); $sibling = $child1_1->left_sibling; is($sibling, undef, "Left Sibling - leftmost child"); # next sibling $sibling = $child1_2->right_sibling; is($sibling->id, $child1_3->id, "Right Sibling - correct ID"); $sibling = $child1_4->right_sibling; is($sibling, undef, "Right Sibling - rightmost child"); # first sibling $sibling = $child1_4->first_sibling; is($sibling->id, $child1_1->id, "First Sibling - correct ID"); $sibling = $child1_1->first_sibling; is($sibling, undef, "First Sibling - leftmost child"); # last sibling $sibling = $child1_2->last_sibling; is($sibling->id, $child1_4->id, "Last Sibling - correct ID"); $sibling = $child1_4->last_sibling; is($sibling, undef, "Last Sibling - rightmost child"); $test_tree->structure($tree1, "Initial Tree 2"); #### move operations #### # move left $sibling = $child1_1->move_left; is($sibling, undef, "Should not be able to move leftmost child left"); $test_tree->structure($tree1, "after attempt to move leftmost child left"); $tree1->discard_changes; $sibling = $child1_4->move_left; is($sibling->id, $child1_3->id, "Can move rightmost child left"); $tree1->discard_changes; $test_tree->structure($tree1, "after move rightmost child left"); @children = $tree1->children; is_deeply( [map {$_->id} @children], [map {$_->id} $child1_1, $child1_2, $child1_4, $child1_3], 'children after move rightmost child left', ); $tree1->discard_changes; $child1_4->discard_changes; $sibling = $child1_4->move_right; is($sibling->id, $child1_3->id, "Can move child back again"); $tree1->discard_changes; $test_tree->structure($tree1, "after move child back again"); @children = $tree1->children; is_deeply( [map {$_->id} @children], [map {$_->id} $child1_1, $child1_2, $child1_3, $child1_4], 'children after move child back again', ); # move right $tree1->discard_changes; $child1_4->discard_changes; $sibling = $child1_4->move_right; is($sibling, undef, "Should not be able to move rightmost child right"); $tree1->discard_changes; $test_tree->structure($tree1, "after attempt to move rightmost child right"); @children = $tree1->children; is_deeply( [map {$_->id} @children], [map {$_->id} $child1_1, $child1_2, $child1_3, $child1_4], 'children after move child back again', ); # move leftmost $tree1->discard_changes; $sibling = $child1_1->move_leftmost; is($sibling, undef, "Should not be able to move leftmost child leftmost"); $test_tree->structure($tree1, "after attempt to move leftmost child leftmost"); $tree1->discard_changes; $child1_3->discard_changes; $sibling = $child1_3->move_leftmost; is($sibling->id, $child1_1->id, "Can move child leftmost"); $tree1->discard_changes; $test_tree->structure($tree1, "after move child leftmost"); @children = $tree1->children; is_deeply( [map {$_->id} @children], [map {$_->id} $child1_3, $child1_1, $child1_2, $child1_4], 'children after move rightmost child left', ); # move rightmost $tree1->discard_changes; $child1_4->discard_changes; $sibling = $child1_4->move_rightmost; is($sibling, undef, "Should not be able to move rightmost child rightmost"); $tree1->discard_changes; $test_tree->structure($tree1, "after attempt to move rightmost child rightmost"); $tree1->discard_changes; $child1_3->discard_changes; $sibling = $child1_3->move_rightmost; is($sibling->id, $child1_4->id, "Can move node rightmost"); $tree1->discard_changes; $test_tree->structure($tree1, "after move node rightmost"); @children = $tree1->children; is_deeply( [map {$_->id} @children], [map {$_->id} $child1_1, $child1_2, $child1_4, $child1_3], 'children after move rightmost child left', ); done_testing(); exit; DBIx-Class-Tree-NestedSet-0.10/t/11-multi-pk.t0000755446137100000240000000164511636645473020202 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use DBICx::TestDatabase; use Data::Dumper; use Test::Exception; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $trees = $schema->resultset('MultiPK'); isa_ok($trees, 'DBIx::Class::ResultSet'); my $index = 1; my $tree1 = $trees->create({ id => $index++, id2 => 'one', content => 'tree1 root', root_id => 10}); my $tree2 = $trees->create({ id => $index++, id2 => 'two', content => 'tree2 root', root_id => 20}); throws_ok(sub { my $tree3 = $trees->create({ id => $index++, id2 => 'three', content => 'tree3 root'}); }, qr/Only single column primary keys are supported/, 'Cannot create a tree in a multi-pk schema'); done_testing(); exit; DBIx-Class-Tree-NestedSet-0.10/t/tlib/0000755446137100000240000000000011636651764016755 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/t/tlib/TestSchema/0000755446137100000240000000000011636651764021015 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/t/tlib/TestSchema/MultiTree.pm0000755446137100000240000000135011636647630023264 0ustar dochertigamesuse strict; use warnings; package TestSchema::MultiTree; use base 'DBIx::Class'; __PACKAGE__->load_components(qw/Tree::NestedSet Core/); __PACKAGE__->table('tree'); __PACKAGE__->add_columns( id => { data_type => 'integer', is_auto_increment => 1, }, root_id => { data_type => 'text', is_nullable => 1, }, lft => { data_type => 'integer' }, rgt => { data_type => 'integer' }, level => { data_type => 'integer' }, content => { data_type => 'text' }, ); __PACKAGE__->set_primary_key(qw/id/); __PACKAGE__->tree_columns({ root_column => 'root_id', left_column => 'lft', right_column => 'rgt', level_column => 'level', }); 1; DBIx-Class-Tree-NestedSet-0.10/t/tlib/TestSchema/MultiPK.pm0000755446137100000240000000141311636647553022703 0ustar dochertigamesuse strict; use warnings; package TestSchema::MultiPK; use base 'DBIx::Class'; __PACKAGE__->load_components(qw/Tree::NestedSet Core/); __PACKAGE__->table('multi_tree'); __PACKAGE__->add_columns( id => { data_type => 'integer', }, id2 => { data_type => 'text', }, root_id => { data_type => 'integer', is_nullable => 1, }, lft => { data_type => 'integer' }, rgt => { data_type => 'integer' }, level => { data_type => 'integer' }, content => { data_type => 'text' }, ); __PACKAGE__->set_primary_key(qw/id id2/); __PACKAGE__->tree_columns({ root_column => 'root_id', left_column => 'lft', right_column => 'rgt', level_column => 'level', }); 1; DBIx-Class-Tree-NestedSet-0.10/t/tlib/FailSchema.pm0000644446137100000240000000012411636645473021304 0ustar dochertigamesuse strict; use warnings; package FailSchema; use base 'DBIx::Class::Schema'; 1; DBIx-Class-Tree-NestedSet-0.10/t/tlib/TestSchema.pm0000644446137100000240000000016011636645473021350 0ustar dochertigamesuse strict; use warnings; package TestSchema; use base 'DBIx::Class::Schema'; __PACKAGE__->load_classes; 1; DBIx-Class-Tree-NestedSet-0.10/t/tlib/TestTree.pm0000755446137100000240000000272611636645473021064 0ustar dochertigamespackage TestTree; use strict; use warnings; use Test::More; use namespace::clean; sub new { my ($class, $args) = @_; use Data::Dumper; bless { schema => $args->{schema} }, $class; } sub schema { shift->{schema} }; sub structure { my ($self, $root, $test_str) = @_; is($root->lft, 1, "$test_str - [".$root->id."] Root left is correct"); my $nodes_count = $root->nodes->count; is($root->rgt, $nodes_count * 2, "$test_str - [".$root->id."] Correct number of nodes"); my $level = 0; is($root->level, $level, "$test_str - [".$root->id."] Correct level"); my $index = 1; my $current_node = $root; while ($index < $root->rgt) { my $next_node = $self->schema->resultset('MultiTree')->search({ -or => [ rgt => $index, lft => $index, ], }); is($next_node->count, 1, "$test_str - [$index] has a node"); my $node = $next_node->next; ok($node->lft < $node->rgt, "$test_str - [$index] left < right"); ok($node->rgt < $root->rgt || $node->lft == 1, "$test_str - [$index] right < root->right"); is(($node->rgt - $node->lft) % 2, 1, "$test_str - [$index] left/right diff is odd"); # we expect (right - 1 - left)/2 descendants my @descendants = $node->descendants; is(($node->rgt - 1 - $node->lft)/2, scalar @descendants, "$test_str - [$index] correct number of descendants"); $index++; } } 1; DBIx-Class-Tree-NestedSet-0.10/t/tlib/FailSchema/0000755446137100000240000000000011636651764020751 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/t/tlib/FailSchema/MissingCols.pm0000644446137100000240000000047111636645473023543 0ustar dochertigamesuse strict; use warnings; package FailSchema::MissingCols; use base 'DBIx::Class'; __PACKAGE__->load_components(qw/Tree::NestedSet Core/); __PACKAGE__->table('zomtec'); __PACKAGE__->add_columns(qw/affe/); __PACKAGE__->set_primary_key(qw/affe/); __PACKAGE__->tree_columns({ left_column => 'affe', }); 1; DBIx-Class-Tree-NestedSet-0.10/t/14-attach.t0000755446137100000240000000473211636645473017707 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use Test::Exception; use DBICx::TestDatabase; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; use TestTree; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $test_tree = TestTree->new({schema => $schema}); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); # Create the tree my $tree1 = $trees->create({ content => '1 tree root', root_id => 10}); my $child1_1 = $tree1->add_to_children({ content => '1 child 1' }); my $child1_2 = $tree1->add_to_children({ content => '1 child 2' }); my $child1_3 = $tree1->add_to_children({ content => '1 child 3' }); my $child1_4 = $tree1->add_to_children({ content => '1 child 4' }); my $gchild1_1 = $child1_2->add_to_children({ content => '1 g-child 1' }); my $gchild1_2 = $child1_2->add_to_children({ content => '1 g-child 2' }); my $gchild1_3 = $child1_4->add_to_children({ content => '1 g-child 3' }); my $gchild1_4 = $child1_4->add_to_children({ content => '1 g-child 4' }); my $ggchild1 = $gchild1_2->add_to_children({ content => '1 gg-child 1' }); # Check that the test tree is constructed correctly is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_1, $child1_2, $gchild1_1, $gchild1_2, $ggchild1, $child1_3, $child1_4, $gchild1_3, $gchild1_4], 'Test Tree is organised correctly.', ); $tree1->discard_changes; $test_tree->structure($tree1,"Initial Tree"); # Promote ggchild1 to become a sibling of descendant $child1_2->discard_changes; $ggchild1->discard_changes; $child1_2->attach_right_sibling($ggchild1); $tree1->discard_changes; $test_tree->structure($tree1,"After ggchild becomes child of root"); my @children = $tree1->children; is_deeply( [map { $_->id} @children], [map { $_->id} $child1_1, $child1_2, $ggchild1, $child1_3, $child1_4 ], 'Test if ggchild has become rightmost sibling of child1_2', ); $tree1->discard_changes; $gchild1_2->discard_changes; $ggchild1->discard_changes; $gchild1_2->attach_rightmost_child($ggchild1); $gchild1_2->discard_changes; @children = $gchild1_2->children; is_deeply( [map { $_->id} @children], [map { $_->id} $ggchild1, ], 'Test if ggchild has been put back', ); $tree1->discard_changes; $test_tree->structure($tree1, "after moving ggchild back"); done_testing(); exit; DBIx-Class-Tree-NestedSet-0.10/t/13-private.t0000755446137100000240000001526511636645473020117 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use Test::Exception; use DBICx::TestDatabase; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); # Create the tree my $tree = $trees->create({ content => '1 tree root', root_id => 10}); my $child1 = $tree->add_to_children({ content => '1 child 1' }); my $child2 = $tree->add_to_children({ content => '1 child 2' }); my $child3 = $tree->add_to_children({ content => '1 child 3' }); my $child4 = $tree->add_to_children({ content => '1 child 4' }); my $gchild1 = $child2->add_to_children({ content => '1 g-child 1' }); my $gchild2 = $child2->add_to_children({ content => '1 g-child 2' }); my $gchild3 = $child4->add_to_children({ content => '1 g-child 3' }); my $gchild4 = $child4->add_to_children({ content => '1 g-child 4' }); my $ggchild = $gchild2->add_to_children({ content => '1 gg-child 1' }); # Check that the test tree is constructed correctly is_deeply( [map { $_->id} $tree->nodes], [map { $_->id} $tree, $child1, $child2, $gchild1, $gchild2, $ggchild, $child3, $child4, $gchild3, $gchild4], 'Test Tree is organised correctly.', ); $child2->discard_changes; $child2->_move_to_end; is_deeply( [map { $_->id} $tree->nodes], [map { $_->id} $tree, $child1, $child3, $child4, $gchild3, $gchild4, $child2, $gchild1, $gchild2, $ggchild], 'Tree is organised correctly after move_to_end', ); # Try moving it to the right again $child2->discard_changes; $child2->_move_to_end; is_deeply( [map { $_->id} $tree->nodes], [map { $_->id} $tree, $child1, $child3, $child4, $gchild3, $gchild4, $child2, $gchild1, $gchild2, $ggchild], 'Tree is organised correctly after move_to_end 2', ); # Move a leaf node to the right $ggchild->discard_changes; $ggchild->_move_to_end; is_deeply( [map { $_->id} $tree->nodes], [map { $_->id} $tree, $child1, $child3, $child4, $gchild3, $gchild4, $child2, $gchild1, $gchild2, $ggchild], 'Tree is organised correctly after move_to_end ggchild', ); $tree->discard_changes; is($ggchild->level, 1, "GG Child is now a child of root"); is($ggchild->rgt, $tree->rgt - 1, "GG Child is now a child of root with right rgt"); # Move leftmost child to the right $child1->discard_changes; $child1->_move_to_end; is_deeply( [map { $_->id} $tree->nodes], [map { $_->id} $tree, $child3, $child4, $gchild3, $gchild4, $child2, $gchild1, $gchild2, $ggchild, $child1, ], 'Tree is organised correctly after move_to_end ggchild', ); # Create another test tree my $tree2 = $trees->create({ content => '2 tree root', root_id => 20}); my $child2_1 = $tree2->add_to_children({ content => '2 child 1' }); my $child2_2 = $tree2->add_to_children({ content => '2 child 2' }); my $child2_3 = $tree2->add_to_children({ content => '2 child 3' }); my $child2_4 = $tree2->add_to_children({ content => '2 child 4' }); my $gchild2_1 = $child2_3->add_to_children({ content => '2 g-child 1' }); my $gchild2_2 = $child2_3->add_to_children({ content => '2 g-child 2' }); my $gchild2_3 = $child2_4->add_to_children({ content => '2 g-child 3' }); my $gchild2_4 = $child2_4->add_to_children({ content => '2 g-child 4' }); my $ggchild2 = $gchild2_3->add_to_children({ content => '2 gg-child 1' }); # Create a small test tree to graft my $tree3 = $trees->create({ content => "3 tree root", root_id => 30}); # Try grafting it in as child of ggchild2 $ggchild2->_graft_branch({ node => $tree3, lft => 15, level => 4 }); $tree3->discard_changes; is_deeply( [map { $_->id} $tree2->nodes], [map { $_->id} $tree2, $child2_1, $child2_2, $child2_3, $gchild2_1, $gchild2_2, $child2_4, $gchild2_3, $ggchild2, $tree3, $gchild2_4 ], 'Tree is organised correctly after graft_branch tree3', ); # graft rightmost child of root as left sibling of child 1 $child2_4->discard_changes; $child2_2->discard_changes; $child2_2->_graft_branch({ node => $child2_4, lft => 4, level => 1}); $tree2->discard_changes; is_deeply( [map { $_->id} $tree2->nodes], [map { $_->id} $tree2, $child2_1, $child2_4, $gchild2_3, $ggchild2, $tree3, $gchild2_4, $child2_2, $child2_3, $gchild2_1, $gchild2_2, ], 'Tree is organised correctly after _graft_branch', ); # Now test the _attach_node which does both _move_to_end and _graft_branch # Try to move it relative to itself $child2_4->discard_changes; throws_ok(sub { $child2_4->_attach_node($child2_4, {left_delta => 1, level => 2}); }, qr/Cannot _attach_node to it.s own descendant/, 'attaching node to itself'); $gchild2_4->discard_changes; throws_ok(sub { $gchild2_4->_attach_node($child2_4, {left_delta => 1, level => 2}); }, qr/Cannot _attach_node to it.s own descendant/, 'attaching node to its own descendant'); # Attach a node back to its current place $tree2->discard_changes; $child2_1->discard_changes; $tree2->_attach_node($child2_1, {left_delta => 1, level => 1}); $tree2->discard_changes; is_deeply( [map { $_->id} $tree2->nodes], [map { $_->id} $tree2, $child2_1, $child2_4, $gchild2_3, $ggchild2, $tree3, $gchild2_4, $child2_2, $child2_3, $gchild2_1, $gchild2_2, ], 'Tree is organised correctly after _attach_node back to current place', ); # Attach a node at a lower level $child2_3->discard_changes; $gchild2_4->discard_changes; $child2_3->_attach_node($gchild2_4, {left_delta => 1, level => $child2_3->level + 1}); is_deeply( [map { $_->id} $tree2->nodes], [map { $_->id} $tree2, $child2_1, $child2_4, $gchild2_3, $ggchild2, $tree3, $child2_2, $child2_3, $gchild2_4, $gchild2_1, $gchild2_2, ], 'Tree is organised correctly after _attach_node at a lower level', ); # Promote a gg-child to be a child $child2_4->discard_changes; $tree3->discard_changes; $child2_4->_attach_node($tree3, {left_delta => 1, level => $child2_4->level + 1}); is_deeply( [map { $_->id} $tree2->nodes], [map { $_->id} $tree2, $child2_1, $child2_4, $tree3, $gchild2_3, $ggchild2, $child2_2, $child2_3, $gchild2_4, $gchild2_1, $gchild2_2, ], 'Tree is organised correctly after _attach_node gg-child to be a child', ); # Attach as the right-most child of root $child2_3->discard_changes; $tree2->discard_changes; $tree2->_attach_node($child2_3, {left_delta => $tree2->rgt + 1 - $tree2->lft, level => $tree2->level + 1}); is_deeply( [map { $_->id} $tree2->nodes], [map { $_->id} $tree2, $child2_1, $child2_4, $tree3, $gchild2_3, $ggchild2, $child2_2, $child2_3, $gchild2_4, $gchild2_1, $gchild2_2, ], 'Tree is organised correctly after _attach_node as right-most child of root', ); done_testing(); exit; DBIx-Class-Tree-NestedSet-0.10/t/09-multi.t0000644446137100000240000000450511636645473017574 0ustar dochertigamesuse strict; use warnings; use Test::More tests => 32; use DBICx::TestDatabase; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); my $root = $trees->create({ content => 'foo' }); isa_ok($root, 'DBIx::Class::Row'); is($root->parent, undef, 'root has no parent'); is($root->root->id, $root->id, 'root field gets set automatically'); is($root->descendants->count, 0, 'no descendants, initially'); is($root->nodes->count, 1, 'nodes include self'); ok( $root->is_root, 'is_root()'); ok( $root->is_leaf, 'is_leaf()'); ok(!$root->is_branch, 'is_branch()'); my $child = $root->add_to_children({ content => 'bar' }); is($child->root->id, $root->id, 'root set for descendants'); is($child->ancestors->count, 1, 'child got one parent'); is($child->parent->id, $root->id, 'parent rel works'); is($root->descendants->count, 1, 'now one child'); is($root->nodes->count, 2, '... and two related nodes'); ok( $root->is_root, 'is_root()'); ok(!$root->is_leaf, 'is_leaf()'); ok( $root->is_branch, 'is_branch()'); ok(!$child->is_root, 'is_root()'); ok( $child->is_leaf, 'is_leaf()'); ok(!$child->is_branch, 'is_branch()'); my $child2 = $root->add_to_children({ content => 'kooh' }); my $subchild = $child->add_to_children({ content => 'moo' }); my $subchild2 = $child->add_to_children({ content => 'right' }); is($subchild->root->id, $root->id, 'root set for subchilds'); is($root->descendants->count, 4, 'root now four descendants'); is($root->nodes->count, 5, '... and five related nodes'); is($child->descendants->count, 2, 'subnode has two descendants'); is($child->nodes->count, 5, '... and five related nodes as well'); is($subchild->descendants->count, 0, 'subchild does not have descendants yet'); is($subchild->ancestors->count, 2, '... but two ancestors'); is($subchild->parent->id, $child->id, 'direct parent is correct'); is_deeply( [map { $_->id } $subchild->ancestors], [map { $_->id } $child, $root], 'ancestors are ordered correctly', ); is_deeply( [map { $_->id } $root->descendants], [map { $_->id } $child, $subchild, $subchild2, $child2], 'roots descendants are ordered correctly', ); DBIx-Class-Tree-NestedSet-0.10/t/12-append.t0000755446137100000240000000575511636645473017716 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use DBICx::TestDatabase; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); my $tree1 = $trees->create({ content => 'tree1 root', root_id => 10}); my $tree2 = $trees->create({ content => 'tree2 root', root_id => 20}); is($tree1->ancestors->count, 0, "Root has no ancestors"); is($tree2->parent, undef, "Root has no parent"); # Add children to tree1 my $child1_0 = $tree1->create_rightmost_child({ content => 'child1_0'}); my $child1_1 = $child1_0->create_right_sibling({ content => 'child1-1'}); my $child1_2 = $child1_0->create_right_sibling({ content => 'child1-2'}); my $child1_3 = $child1_0->create_right_sibling({ content => 'child1-3'}); my $g_child10_1 = $child1_0->add_to_children({ content => 'g_child10_1'}); my $g_child10_2 = $child1_0->add_to_children({ content => 'g_child10_2'}); my $gg_child102_1 = $g_child10_2->add_to_children({ content => 'g_child102_1'}); $tree1->discard_changes; $child1_0->discard_changes; $child1_1->discard_changes; $child1_2->discard_changes; $child1_3->discard_changes; $g_child10_1->discard_changes; $g_child10_2->discard_changes; $gg_child102_1->discard_changes; is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_0, $g_child10_1, $g_child10_2, $gg_child102_1, $child1_3, $child1_2, $child1_1], 'Tree 1 is organised correctly.', ); #Test deleting a leaf node $child1_2->delete; $tree1->discard_changes; $child1_0->discard_changes; $child1_1->discard_changes; $child1_3->discard_changes; $g_child10_1->discard_changes; $g_child10_2->discard_changes; $gg_child102_1->discard_changes; is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_0, $g_child10_1, $g_child10_2, $gg_child102_1, $child1_3, $child1_1], 'Tree 1 is organised correctly after deletion.', ); # Test deleting a branch $g_child10_2->delete; $tree1->discard_changes; $child1_0->discard_changes; $child1_1->discard_changes; $child1_3->discard_changes; $g_child10_1->discard_changes; is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_0, $g_child10_1, $child1_3, $child1_1], 'Tree 1 is organised correctly after deletion.', ); # Delete remaining nodes except root $child1_0->delete; $child1_1->delete; $child1_3->delete; is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1], 'Tree 1 is organised correctly after deletion of all nodes.', ); # Delete the root $tree1->delete; my $tree1_count = $trees->search({root_id => 10})->count; is($tree1_count, 0, "There are no more nodes in tree1"); my $tree2_count = $trees->search({root_id => 20})->count; is($tree2_count, 1, "Tree2 still exists"); done_testing(); exit; DBIx-Class-Tree-NestedSet-0.10/t/17-cutting.t0000755446137100000240000000433511636647316020120 0ustar dochertigames#!/usr/bin/env perl use strict; use warnings; use Test::More; use Test::Exception; use DBICx::TestDatabase; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; use TestTree; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $test_tree = TestTree->new({schema => $schema}); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); # Create the tree # taken from t/16-siblings.t my $tree1 = $trees->create({ content => '1 tree root'}); my $child1_1 = $tree1->add_to_children({ content => '1 child 1' }); my $child1_2 = $tree1->add_to_children({ content => '1 child 2' }); my $child1_3 = $tree1->add_to_children({ content => '1 child 3' }); my $child1_4 = $tree1->add_to_children({ content => '1 child 4' }); my $gchild1_1 = $child1_2->add_to_children({ content => '1 g-child 1' }); my $gchild1_2 = $child1_2->add_to_children({ content => '1 g-child 2' }); my $gchild1_3 = $child1_4->add_to_children({ content => '1 g-child 3' }); my $gchild1_4 = $child1_4->add_to_children({ content => '1 g-child 4' }); my $ggchild1 = $gchild1_2->add_to_children({ content => '1 gg-child 1' }); sub refresh { for ($tree1, $child1_1, $child1_2, $child1_3, $child1_4, $gchild1_1, $gchild1_2, $gchild1_3, $gchild1_4, $ggchild1) { $_->discard_changes; } } refresh(); # Check that the test tree is constructed correctly is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_1, $child1_2, $gchild1_1, $gchild1_2, $ggchild1, $child1_3, $child1_4, $gchild1_3, $gchild1_4], 'Test Tree is organised correctly.', ); my $subtree = $child1_2->take_cutting; refresh(); is_deeply( [map { $_->id } $subtree->nodes], [map { $_->id } $child1_2, $gchild1_1, $gchild1_2, $ggchild1], 'cut out tree is organised correctly.'); is_deeply( [map { $_->id } $tree1->nodes], [map { $_->id } $tree1, $child1_1, $child1_3, $child1_4, $gchild1_3, $gchild1_4], 'remainder of tree intact.'); $subtree->dissolve; refresh(); for ($subtree, $child1_2, $gchild1_1, $gchild1_2, $ggchild1) { ok $_->id == $_->root_id, 'dissolved node stands alone' } done_testing; DBIx-Class-Tree-NestedSet-0.10/t/15-attach.t0000755446137100000240000000221411636645473017701 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use Test::Exception; use DBICx::TestDatabase; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); # Create the tree my $tree1 = $trees->create({ content => '1 tree root', root_id => 10}); my $child1_1 = $tree1->add_to_children({ content => '1 child 1' }); my $child1_2 = $tree1->add_to_children({ content => '1 child 2' }); # Check that the test tree is constructed correctly is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_1, $child1_2], 'Test Tree is organised correctly.', ); $child1_1->discard_changes; $tree1->discard_changes; $child1_1->_move_to_end(); $tree1->attach_rightmost_child($child1_1); my @children = $tree1->children; is($children[1]->id, $child1_1->id, "Moved child to last child of root"); done_testing(); exit; DBIx-Class-Tree-NestedSet-0.10/t/10-multi.t0000755446137100000240000000567311636645473017576 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use DBICx::TestDatabase; use Data::Dumper; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); my $tree1 = $trees->create({ content => 'tree1 root', root_id => 10}); my $tree2 = $trees->create({ content => 'tree2 root', root_id => 20}); # Add children to tree1 my $child1_1 = $tree1->add_to_children({ content => 'child1-1'}); my $child1_2 = $tree1->add_to_children({ content => 'child1-2'}); my $child1_3 = $tree1->add_to_children({ content => 'child1-3'}); # Add children to tree2 my $child2_1 = $tree2->add_to_children({ content => 'child2-1'}); my $child2_2 = $tree2->add_to_children({ content => 'child2-2'}); # Add grand-children to tree1 my $g_child1_1 = $child1_1->add_to_children({ content => 'g_child1-1'}); my $g_child1_2 = $child1_1->add_to_children({ content => 'g_child1-2'}); my $g_child1_3 = $child1_3->add_to_children({ content => 'g_child1-3'}); # Add grand-children to tree2 my $g_child2_1 = $child2_2->add_to_children({ content => 'g_child2-1'}); my $g_child2_2 = $child2_2->add_to_children({ content => 'g_child2-2'}); is_deeply( [map { $_->id} $tree1->nodes], [map { $_->id} $tree1, $child1_1, $g_child1_1, $g_child1_2, $child1_2, $child1_3, $g_child1_3], 'Tree 1 is organised correctly.', ); is_deeply( [map { $_->id} $tree2->nodes], [map { $_->id} $tree2, $child2_1, $child2_2, $g_child2_1, $g_child2_2], 'Tree 2 is organised correctly.', ); my $tree3 = $trees->create({ content => 'tree1 root', root_id => 30}); my $child3_1 = $tree3->add_to_nodes({ content => 'child3-1'}); my $new_parent = $child3_1->add_to_ancestors({ content => 'new parent'}); # The need for the following worries me. Creating a new node will cause the # database to be updated (possibly every node) so what happens about instances # such as $tree3 etc. which are modified in the database but not in memory? $tree3->discard_changes; $child3_1->discard_changes; $new_parent->discard_changes; is_deeply( [map {$_->id, $_->lft, $_->rgt, $_->level, $_->content} $tree3->nodes], [map {$_->id, $_->lft, $_->rgt, $_->level, $_->content} $tree3, $new_parent, $child3_1], 'Tree 3 is organised correctly.', ); # See if we can re-root the tree my $new_root = $tree3->add_to_ancestors({ content => 'new root'}); $new_root->discard_changes; $tree3->discard_changes; $child3_1->discard_changes; $new_parent->discard_changes; is_deeply( [map {$_->id, $_->lft, $_->rgt, $_->level, $_->content} $new_root->nodes], [map {$_->id, $_->lft, $_->rgt, $_->level, $_->content} $new_root, $tree3, $new_parent, $child3_1], 'Tree 3 is organised correctly.', ); done_testing(); exit; DBIx-Class-Tree-NestedSet-0.10/t/07-tree.t0000644446137100000240000002453611636645472017404 0ustar dochertigames#!/usr/bin/env perl # # $Id: $ # $Revision: $ # $Author: $ # $Source: $ # # $Log: $ # use strict; use warnings; use Test::More; use Data::Dumper; use File::Temp 'tempfile'; use DBICx::TestDatabase; use FindBin; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/tlib"; BEGIN { use_ok('TestSchema') } my $schema = DBICx::TestDatabase->new('TestSchema'); isa_ok($schema, 'DBIx::Class::Schema'); my $trees = $schema->resultset('MultiTree'); isa_ok($trees, 'DBIx::Class::ResultSet'); my $root = $trees->create({ id => 1, content => 'root' }); isa_ok($root, 'DBIx::Class::Row'); is($root->id, 1, 'root has correct ID'); is($root->content, 'root', 'root has correct content'); is($root->parent, undef, 'root has no parent'); is($root->root->id, $root->id, 'root field gets set automatically'); is($root->descendants->count, 0, 'no descendants, initially'); is($root->nodes->count, 1, 'nodes include self'); ok( $root->is_root, 'is_root()'); ok( $root->is_leaf, 'is_leaf()'); ok(!$root->is_branch, 'is_branch()'); my $auto_inc = 1; # Create a test tree my $test_tree = [ [1, 1, 20, 'root', undef, 0], [2, 2, 13, 'A', 1, 1], [3, 3, 4, 'B', 2, 2], [4, 5, 10, 'C', 2, 2], [5, 11, 12, 'D', 2, 2], [6, 6, 7, 'E', 4, 3], [7, 8, 9, 'F', 4, 3], [8, 14, 19, 'G', 1, 1], [9, 15, 16, 'H', 8, 2], [10, 17, 18, 'I', 8, 2], ]; my ($success, $child, $parent); $root = create_tree(1, $test_tree); test_tree($root, "Initial Tree", $test_tree); # Test the 'is_root' interface my $is_root = $root->is_root; is($is_root, 1, "Root is root"); # Test for descendants of leaf nodes for my $id (3,6,7,5,9,10) { $parent = $schema->resultset('MultiTree')->find($id); # list context my @descendants = $parent->descendants; is(scalar @descendants, 0, "List - descendants of leaf node $id"); # scalar context $child = $parent->descendants; is($child, 0, "Scalar - descendants of leaf node $id"); } # Test for descendants of branches for my $test ([1,[2,3,4,6,7,5,8,9,10]],[2,[3,4,6,7,5]],[4,[6,7]],[8,[9,10]]) { my $parent_id = $test->[0]; my @descendants_ids= @{$test->[1]}; # list context $parent = $schema->resultset('MultiTree')->find($parent_id); is($parent->descendants->count, scalar(@descendants_ids), "Number of descendants for $parent_id"); my @descendants = $parent->descendants; my $index = 0; for my $child (@descendants) { is($child->id, $descendants_ids[$index++], "List - Child $index of $parent_id"); } # scalar context $parent = $schema->resultset('MultiTree')->find($parent_id); my $descendants = $parent->descendants; $index = 0; while (my $child = $descendants->next) { is($child->id, $descendants_ids[$index++], "Scalar - Child $index of $parent_id"); } } # Test for ancestors of root # list context $child = $schema->resultset('MultiTree')->find(1); my @ancestors = $child->ancestors; is(@ancestors, 0, "Scalar - ancestors of root node"); # scalar context $child = $schema->resultset('MultiTree')->find(1); my $ancestors = $child->ancestors; is ($ancestors, 0, "List - ancestors of root node"); # Test for ancestors for my $test ([2,[1]],[3,[2,1]],[4,[2,1]],[6,[4,2,1]],[7,[4,2,1]],[5,[2,1]],[9,[8,1]],[10,[8,1]],[8,[1]]) { my $child_id = $test->[0]; my @ancestor_ids= @{$test->[1]}; # list context $child = $schema->resultset('MultiTree')->find($child_id); my @ancestors = $child->ancestors; my $index = 0; for my $ancestor (@ancestors) { is($ancestor->id, $ancestor_ids[$index++], "List - Ancestor $index of $child_id"); } # scalar context $child = $schema->resultset('MultiTree')->find($child_id); my $ancestors = $child->ancestors; $index = 0; while (my $ancestor = $ancestors->next) { is($ancestor->id, $ancestor_ids[$index++], "Scalar - Ancestor $index of $child_id"); } } done_testing(); exit; #### insert tests #### # Insert a left child of the root node $root = create_tree($test_tree); $root->prepend_child({ parent_id => 11, name => 'left_child', }); $child = $schema->resultset('MultiTree')->create({ id => 11, title => 'left child', }); $success = $root->insert_left_child($child); ok($success, "Left child inserted to root"); test_tree("Left Child to Root",[ [1, 1, 22, 'root'], [2, 4, 15, 'A'], [3, 5, 6, 'B'], [4, 7, 12, 'C'], [5, 13, 14, 'D'], [6, 8, 9, 'E'], [7, 10, 11, 'F'], [8, 16, 21, 'G'], [9, 17, 18, 'H'], [10, 19, 20, 'I'], [11, 2, 3, 'left child'], ]); # Insert a left child of a leaf node $root = create_tree($test_tree); $child = $schema->resultset('MultiTree')->create({ id => 11, title => 'left child', }); ($parent) = $schema->resultset('MultiTree')->search({id => 10,}); $success = $parent->insert_left_child($child); ok($success, "Left child inserted to 10"); test_tree("Left Child to Leaf", [ [1, 1, 22, 'root'], [2, 2, 13, 'A'], [3, 3, 4, 'B'], [4, 5, 10, 'C'], [5, 11, 12, 'D'], [6, 6, 7, 'E'], [7, 8, 9, 'F'], [8, 14, 21, 'G'], [9, 15, 16, 'H'], [10, 17, 20, 'I'], [11, 18, 19, 'left child'], ]); # Insert a left child of a non leaf node $root = create_tree($test_tree); $child = $schema->resultset('MultiTree')->create({ id => 11, title => 'left child', }); ($parent) = $schema->resultset('MultiTree')->search({id => 8,}); $success = $parent->insert_left_child($child); ok($success, "Left child inserted to 8"); test_tree("Left Child to non Leaf",[ [1, 1, 22, 'root'], [2, 2, 13, 'A'], [3, 3, 4, 'B'], [4, 5, 10, 'C'], [5, 11, 12, 'D'], [6, 6, 7, 'E'], [7, 8, 9, 'F'], [8, 14, 21, 'G'], [9, 17, 18, 'H'], [10, 19, 20, 'I'], [11, 15, 16, 'left child'], ]); #### insert_right_child tests #### # Insert a right child of the root node $root = create_tree($test_tree); $child = $schema->resultset('MultiTree')->create({ id => 11, title => 'right child', }); $success = $root->insert_right_child($child); ok($success, "Right child inserted to root"); test_tree("Right Child to Root",[ [1, 1, 22, 'root'], [2, 2, 13, 'A'], [3, 3, 4, 'B'], [4, 5, 10, 'C'], [5, 11, 12, 'D'], [6, 6, 7, 'E'], [7, 8, 9, 'F'], [8, 14, 19, 'G'], [9, 15, 16, 'H'], [10, 17, 18, 'I'], [11, 20, 21, 'right child'], ]); # Insert a right child of a leaf node $root = create_tree($test_tree); $child = $schema->resultset('MultiTree')->create({ id => 11, title => 'right child', }); ($parent) = $schema->resultset('MultiTree')->search({id => 9,}); $success = $parent->insert_right_child($child); ok($success, "Right child inserted to 9"); test_tree("Right Child to Leaf",[ [1, 1, 22, 'root'], [2, 2, 13, 'A'], [3, 3, 4, 'B'], [4, 5, 10, 'C'], [5, 11, 12, 'D'], [6, 6, 7, 'E'], [7, 8, 9, 'F'], [8, 14, 21, 'G'], [9, 15, 18, 'H'], [10, 19, 20, 'I'], [11, 16, 17, 'right child'], ]); # Insert a right child to a non leaf node $root = create_tree($test_tree); $child = $schema->resultset('MultiTree')->create({ id => 11, title => 'right child', }); ($parent) = $schema->resultset('MultiTree')->search({id => 8,}); $success = $parent->insert_right_child($child); ok($success, "Right child inserted to 8"); test_tree("Right Child to non Leaf",[ [1, 1, 22, 'root'], [2, 2, 13, 'A'], [3, 3, 4, 'B'], [4, 5, 10, 'C'], [5, 11, 12, 'D'], [6, 6, 7, 'E'], [7, 8, 9, 'F'], [8, 14, 21, 'G'], [9, 15, 16, 'H'], [10, 17, 18, 'I'], [11, 19, 20, 'right child'], ]); # Test for ancestors $root = create_tree($test_tree); for my $test ([2,1],[3,2],[4,2],[6,4],[7,4],[5,2],[9,8],[10,8],[8,1]) { my $child_id = $test->[0]; my $parent_id = $test->[1]; my $child = $schema->resultset('MultiTree')->find($child_id); my $parent = $child->parent; is($parent->id, $parent_id, "Parent of $child_id is $parent_id"); } # Test for ancestors $root = create_tree($test_tree); for my $test ([2,[1]],[3,[1,2]],[4,[1,2]],[6,[1,2,4]],[7,[1,2,4]],[5,[1,2]],[9,[1,8]],[10,[1,8]],[8,[1]]) { my $child_id = $test->[0]; my @ancestor_ids= @{$test->[1]}; # list context $child = $schema->resultset('MultiTree')->find($child_id); my @ancestors = $child->ancestors; my $index = 0; for my $ancestor (@ancestors) { is($ancestor->id, $ancestor_ids[$index++], "List - Ancestor $index of $child_id"); } # scalar context $child = $schema->resultset('MultiTree')->find($child_id); my $ancestors = $child->ancestors; $index = 0; while (my $ancestor = $ancestors->next) { is($ancestor->id, $ancestor_ids[$index++], "Scalar - Ancestor $index of $child_id"); } } #============== subroutines ==================== # Test an array of nodes, each contains an array of id, lft and rgt sub test_tree { my ( $root, $test_name, $array_ref) = @_; # left and right extents must be unique my @extents = []; for my $node (@$array_ref) { my ($id, $left, $right, $content) = @$node; my $node = $schema->resultset('MultiTree')->find($id); ok($node, "$test_name: Node $id found"); is($node->lft, $left, "$test_name: Left Extent is $left"); is($node->rgt, $right, "$test_name: Right Extent is $right"); is($node->content, $content, "$test_name: Name is $content"); } } # Create a complete tree sub create_tree { my ($root_id, $array_ref) = @_; $auto_inc = $root->id + 1; $schema->resultset('MultiTree')->delete; for my $node (@$array_ref) { my ($id, $left, $right, $content, $parent, $level) = @$node; my $node = $schema->resultset('MultiTree')->create({ id => $id, root_id => $root->id, lft => $left, rgt => $right, content => $content, level => $level, }); if ($id >= $auto_inc) { $auto_inc = $id + 1; } } my $root = $schema->resultset('MultiTree')->find($root_id); return $root; } DBIx-Class-Tree-NestedSet-0.10/lib/0000755446137100000240000000000011636651763016325 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/lib/DBIx/0000755446137100000240000000000011636651763017113 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/lib/DBIx/Class/0000755446137100000240000000000011636651763020160 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/lib/DBIx/Class/Tree/0000755446137100000240000000000011636651763021057 5ustar dochertigamesDBIx-Class-Tree-NestedSet-0.10/lib/DBIx/Class/Tree/NestedSet.pm0000755446137100000240000011622711636651253023321 0ustar dochertigamespackage DBIx::Class::Tree::NestedSet; use strict; use warnings; use Carp qw/croak/; use base 'DBIx::Class'; our $VERSION = '0.10'; $VERSION = eval $VERSION; __PACKAGE__->mk_classdata( _tree_columns => {} ); # specify the tree columns and define the relationships # sub tree_columns { my ($class, $args) = @_; if (defined $args) { my ($root, $left, $right, $level) = map { my $col = $args->{"${_}_column"}; croak("required param $_ not specified") if !defined $col; $col; } qw/root left right level/; my $table = $class->table; my %join_cond = ( "foreign.$root" => "self.$root" ); $class->belongs_to( 'root' => $class, \%join_cond,{ where => \"me.$left = 1", #" }, ); $class->belongs_to( 'parent' => $class, \%join_cond,{ where => \"child.$left > me.$left AND child.$right < me.$right AND me.$level = child.$level - 1", #" from => "$table me, $table child", }, ); $class->has_many( 'nodes' => $class, \%join_cond,{ order_by => "me.$left", cascade_delete => 0, }, ); $class->has_many( 'descendants' => $class, \%join_cond, { where => \"me.$left > parent.$left AND me.$right < parent.$right", #" order_by => "me.$left", from => "$table me, $table parent", cascade_delete => 0, }, ); $class->has_many( 'children' => $class, \%join_cond, { where => \"me.$left > parent.$left AND me.$right < parent.$right AND me.$level = parent.$level + 1", #" order_by => "me.$left", from => "$table me, $table parent", cascade_delete => 0, }, ); $class->has_many( 'ancestors' => $class, \%join_cond, { where => \"child.$left > me.$left AND child.$right < me.$right", #" order_by => "me.$right", from => "$table me, $table child", cascade_delete => 0, }, ); $class->_tree_columns($args); } return $class->_tree_columns; } # Insert a new node. # # If the 'right' column is not defined it assumes that we are inserting a root # node. # sub insert { my ($self, @args) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if (!$self->$right) { $self->set_columns({ $left => 1, $right => 2, $level => 0, }); } my $row; my $get_row = $self->next::can; $self->result_source->schema->txn_do(sub { $row = $get_row->($self, @args); # If the root column is not defined, it uses the primary key so long as it is a # single column primary key if (!defined $row->$root) { my @primary_columns = $row->result_source->primary_columns; if (scalar @primary_columns > 1) { croak('Only single column primary keys are supported for default root selection in nested set tree classes'); } $row->update({ $root => \"$primary_columns[0]", #" }); $row->discard_changes; } }); return $row; } # Delete the current node, and all sub-nodes. # sub delete { my ($self) = shift; my ($root, $left, $right, $level) = $self->_get_columns; my $p_lft = $self->$left; my $p_rgt = $self->$right; my $del_row = $self->next::can; $self->result_source->schema->txn_do(sub { $self->discard_changes; my $descendants = $self->descendants; while (my $descendant = $descendants->next) { $del_row->($descendant); } $self->nodes_rs->update({ $left => \"CASE WHEN $left > $p_rgt THEN $left - 2 ELSE $left END", #" $right => \"CASE WHEN $right > $p_rgt THEN $right - 2 ELSE $right END", #" }); $del_row->($self); }); } # Create a related node with special handling for relationships # sub create_related { my ($self, $rel, $col_data) = @_; if (! grep {$rel eq $_} qw(descendants children nodes ancestors)) { return $self->next::method($rel => $col_data); } my ($root, $left, $right, $level) = $self->_get_columns; my $row; my $get_row = $self->next::can; $self->result_source->schema->txn_do(sub { $self->discard_changes; # With create related ancestor, make it a parent of this child if ($rel eq 'ancestors') { my $p_lft = $self->$left; my $p_rgt = $self->$right; my $p_level = $self->$level; # Update all the nodes to the right of this sub-tree $self->nodes_rs->update({ $left => \"CASE WHEN $left > $p_rgt THEN $left + 2 ELSE $left END", #" $right => \"CASE WHEN $right > $p_rgt THEN $right + 2 ELSE $right END", #" }); # Update all the nodes of this sub-tree $self->nodes_rs->search({ $left => { '>=', $p_lft }, $right => { '<=', $p_rgt } })->update({ $left => \"$left + 1", #" $right => \"$right + 1", #" $level => \"$level + 1", #" }); $self->discard_changes; $col_data->{$root} = $self->$root; $col_data->{$left} = $p_lft; $col_data->{$right} = $p_rgt+2; $col_data->{$level} = $p_level; } else { # insert a descendant, node or a child as a right-most child my $p_rgt = $self->$right; # Update all the nodes to the right of this sub-tree $self->nodes_rs->update({ $left => \"CASE WHEN $left > $p_rgt THEN $left + 2 ELSE $left END", #" $right => \"CASE WHEN $right >= $p_rgt THEN $right + 2 ELSE $right END", #" }); $self->discard_changes; $col_data->{$root} = $self->$root; $col_data->{$left} = $p_rgt; $col_data->{$right} = $p_rgt+1; $col_data->{$level} = $self->$level+1; } $row = $get_row->($self, $rel => $col_data); }); return $row; } # search_related with special handling for relationships # sub search_related { my ($self, $rel, $cond, @rest) = @_; my $pk = ($self->result_source->primary_columns)[0]; $cond ||= {}; if ($rel eq 'descendants' || $rel eq 'children') { $cond->{"parent.$pk"} = $self->$pk, } elsif ($rel eq 'ancestors' || $rel eq 'parent') { $cond->{"child.$pk"} = $self->$pk, } return $self->next::method($rel, $cond, @rest); } *search_related_rs = \&search_related; # Insert a node anywhere in the tree # left # right # level # other_args # sub _insert_node { my ($self, $args) = @_; my $rset = $self->result_source->resultset; my $schema = $self->result_source->schema; my ($root, $left, $right, $level) = $self->_get_columns; # our special arguments my $o_args = delete $args->{other_args}; my $pivot = $args->{$left}; # Use same level as self by default $args->{$level} = $self->$level unless defined $args->{$level}; $args->{$root} = $self->$root unless defined $args->{$root}; # make room and create it my $new_record; $schema->txn_do(sub { $self->discard_changes; $rset->search({ "me.$right" => {'>=', $pivot}, $root => $self->$root, })->update({ $right => \"$right + 2", #" }); $rset->search({ "me.$left" => {'>=', $pivot}, $root => $self->$root, })->update({ $left => \"$left + 2", #" }); $self->discard_changes; $new_record = $rset->create({%$o_args, %$args}); }); return $new_record; } # Attach a node anywhere in the tree # node # left_delta (relative to $self->$left # (or) right_delta (relative to $self->$right # level # sub _attach_node { my ($self, $node, $args) = @_; my $rset = $self->result_source->resultset; my $schema = $self->result_source->schema; my ($root, $left, $right, $level) = $self->_get_columns; # $self cannot be a descendant of $node or $node itself if ($self->$root == $node->$root && $self->$left >= $node->$left && $self->$right <= $node->$right) { croak("Cannot _attach_node to it's own descendant "); } $schema->txn_do(sub { $self->discard_changes; $node->discard_changes; # Move the node to the end (right most child of root) $node->_move_to_end; $self->discard_changes; $node->discard_changes; # Graft the node to the specified location my $left_val; if (defined $args->{left_delta}) { $left_val = $self->$left + $args->{left_delta}; } else { $left_val = $self->$right + $args->{right_delta}; } $self->_graft_branch({ node => $node, $left => $left_val, $level => $args->{$level} }); }); } # Graft a branch of nodes (or a leaf) at this point # The assumption made here is that the nodes being moved here are # either a root node of another tree or the rightmost child of # this or another trees root (see _move_to_end) # sub _graft_branch { my ($self, $args) = @_; my ($root, $left, $right, $level) = $self->_get_columns; my $rset = $self->result_source->resultset; my $node = $args->{node}; my $arg_left = $args->{$left}; my $arg_level = $args->{$level}; my $node_is_root = $node->is_root; my $node_root = $node->root; if ($node_is_root) { # Cannot graft our own root croak "Cannot graft our own root node!" if $node->$root == $self->$root; } else { # Node must be rightmost child of it's root croak "Can only graft rightmost child of root!" if $node->$right + 1 != $node_root->$right; } # If the position we are grafting to is the rightmost child of root then there is nothing to do if ($self->$root == $node->$root && $self->is_root && $self->$left + $arg_left > $node_root->$right) { return; } # Determine the size of the branch to add in. my $offset = $node->$right + 1 - $node->$left; # Make a hole in the tree to accept the graft $self->discard_changes; $rset->search({ "me.$right" => {'>=', $arg_left}, $root => $self->$root, })->update({ $right => \"$right + $offset", #" }); $rset->search({ "me.$left" => {'>=', $arg_left}, $root => $self->$root, })->update({ $left => \"$left + $offset", #" }); # make the graft $node->discard_changes; my $node_left = $node->$left; my $node_right = $node->$right; my $level_offset= $arg_level - $node->$level; my $graft_offset= $arg_left - $node->$left; $self->discard_changes; $rset->search({ "me.$left" => {'>=', $node_left}, "me.$right" => {'<=', $node_right}, $root => $node->$root, })->update({ $left => \"$left + $graft_offset", #" $right => \"$right + $graft_offset", #" $level => \"$level + $level_offset", #" $root => $self->$root, }); # adjust the right value of the root node to take into account the # moved nodes if (! $node_is_root) { $node_root->discard_changes; $node_root->$right($node_root->$right - $offset); $node_root->update; } $self->discard_changes; $node->discard_changes; } # Move nodes to end of tree # This will help make it easier to prune the nodes from # the tree since there will be nothing to the right of them # sub _move_to_end { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; my $rset = $self->result_source->resultset; my $root_node = $self->root; my $old_left = $self->$left; my $old_right = $self->$right; my $offset = $root_node->$right - $self->$left; my $level_offset= $self->$level - 1; # If it is the root or already on the right, do nothing if ($self->is_root || $old_right + 1 == $root_node->$right) { return; } # Move all sub-nodes to the right (adjusting their level) $self->discard_changes; $rset->search({ "me.$left" => {'>=', $old_left}, "me.$right" => {'<=', $old_right}, $root => $self->$root, })->update({ $left => \"$left + $offset", #" $right => \"$right + $offset", #" $level => \"$level - $level_offset", #" }); # Now move everything (except the root) back to fill in the gap $offset = $self->$right + 1 - $self->$left; $rset->search({ "me.$right" => {'>=', $old_right}, $left => {'!=', 1}, # Root needs no adjustment $root => $self->$root, })->update({ $right => \"$right - $offset", #" }); $rset->search({ "me.$left" => {'>=', $old_right}, $root => $self->$root, })->update({ $left => \"$left - $offset", #" }); $self->discard_changes; } # Convenience routine to get the names of the table columns # sub _get_columns { my ($self) = @_; my ($root, $left, $right, $level) = map { $self->tree_columns->{"${_}_column"} } qw/root left right level/; return ($root, $left, $right, $level); } # Attach a node as the rightmost child of the current node # sub attach_rightmost_child { my $self = shift; my ($root, $left, $right, $level) = $self->_get_columns; foreach my $node (@_) { $self->_attach_node($node, { right_delta => 0, $level => $self->$level + 1, }); } return $self; } *append_child = \&attach_rightmost_child; # Attach a node as the leftmost child of the current node # sub attach_leftmost_child { my $self = shift; my ($root, $left, $right, $level) = $self->_get_columns; foreach my $node (@_) { $self->_attach_node($node, { left_delta => 1, $level => $self->$level + 1, }); } return $self; } *prepend_child = \&attach_leftmost_child; # Attach a node as a sibling to the right of self # sub attach_right_sibling { my $self = shift; my ($root, $left, $right, $level) = $self->_get_columns; foreach my $node (@_) { $self->_attach_node($node, { right_delta => 1, $level => $self->$level, }); } return $self; } *attach_after = \&attach_right_sibling; # Attach a node as a sibling to the left of self # sub attach_left_sibling { my $self = shift; my ($root, $left, $right, $level) = $self->_get_columns; foreach my $node (@_) { $self->_attach_node($node, { left_delta => 0, $level => $self->$level, }); } return $self; } *attach_before = \&attach_left_sibling; # take_cutting # Given a node, cut it from it's current tree and make it the root of a new tree # NOTE2: The root ID must be specified for multi-key primary keys # otherwise it comes from the primary key # sub take_cutting { my $self = shift; my ($root, $left, $right, $level) = $self->_get_columns; $self->result_source->schema->txn_do(sub { my $p_lft = $self->$left; my $p_rgt = $self->$right; return $self if $p_lft == $p_rgt + 1; my $pk = ($self->result_source->primary_columns)[0]; $self->discard_changes; my $root_id = $self->$root; my $p_diff = $p_rgt - $p_lft; my $l_diff = $self->$level - 1; my $new_id = $self->$pk; # I'd love to use $self->descendants->update(...), # but it dies with "_strip_cond_qualifiers() is unable to # handle a condition reftype SCALAR". # tough beans. $self->nodes_rs->search({ $root => $root_id, $left => {'>=' => $p_lft }, $right => {'<=' => $p_rgt }, })->update({ $left => \"$left - $p_lft + 1", #" $right => \"$right - $p_lft + 1", #" $root => $new_id, $level => \"$level - $l_diff", #" }); # fix up the rest of the tree $self->nodes_rs->search({ $root => $root_id, $left => { '>=' => $p_rgt}, })->update({ $left => \"$left - $p_diff", #" $right => \"$right - $p_diff", #" }); }); return $self; } sub dissolve { my $self = shift; my ($root, $left, $right, $level) = $self->_get_columns; my $pk = ($self->result_source->primary_columns)[0]; $self->nodes_rs->search({$root => $self->$root})->update({ $level => 1, $left => 1, $right => 2, $root => \"$pk", #" }); return $self; } # Move a node to the left # Swap position with the sibling on the left # returns the node it exchanged with on success, undef if it is already leftmost sibling # sub move_left { my ($self) = @_; my $previous = $self->left_sibling; if (! $previous) { return; } $previous->attach_left_sibling($self); return $previous; } *move_previous = \&move_left; # Move a node to the right # Swap position with the sibling on the right # returns the node it exchanged with on success, undef if it is already rightmost sibling # sub move_right { my ($self) = @_; my $next = $self->right_sibling; if (! $next) { return; } $next->attach_right_sibling($self); return $next; } *move_next = \&move_right; # Move a node to be the leftmost child # Make this node the leftmost sibling # returns the node it exchanged with on success, undef if it is already leftmost sibling sub move_leftmost { my ($self) = @_; my $first = $self->leftmost_sibling; if (! $first) { return; } $first->attach_left_sibling($self); return $first; } *move_first = \&move_leftmost; # Make this node the rightmost sibling # returns 1 on success, 0 if it is already rightmost sibling sub move_rightmost { my ($self) = @_; my $last = $self->rightmost_sibling; if (! $last) { return; } $last->attach_right_sibling($self); return $last; } *move_last = \&move_rightmost; # Move this node to the specified position # Returns 1 on success, 0 if it is already in that position # sub move_to { } # Return a resultset of all siblings excluding the one called on # sub siblings { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if ($self->is_root) { # Root has no siblings return; } if (wantarray()) { my @siblings = $self->parent->children({ "me.$left" => {'!=', $self->$left }, }); return @siblings; } my $siblings_rs = $self->parent->children({ "me.$left" => {'!=', $self->$left }, }); return $siblings_rs; } # Returns a resultset of all siblings to the left of this one # sub left_siblings { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if ($self->is_root) { # Root has no siblings return; } if (wantarray()) { my @siblings = $self->parent->children({ "me.$left" => {'<', $self->$left }, }); return @siblings; } my $siblings_rs = $self->parent->children({ "me.$left" => {'<', $self->$left }, }); return $siblings_rs; } *previous_siblings = \&left_siblings; # Returns a resultset of all siblings to the right of this one # sub right_siblings { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if ($self->is_root) { # Root has no siblings return; } if (wantarray()) { my @siblings = $self->parent->children({ "me.$left" => {'>', $self->$left }, }); return @siblings; } my $siblings_rs = $self->parent->children({ "me.$left" => {'>', $self->$left }, }); return $siblings_rs; } *next_siblings = \&right_siblings; # return the sibling to the left of this one # sub left_sibling { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if ($self->is_root) { # Root has no siblings return; } my $sibling = $self->left_siblings->search({ "me.$right" => $self->$left - 1, },{ rows => 1, })->first; return $sibling; } *previous_sibling = \&left_sibling; # return the sibling to the right of this one # sub right_sibling { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if ($self->is_root) { # Root has no siblings return; } my $sibling = $self->right_siblings->search({ "me.$left" => $self->$right + 1, },{ rows => 1, })->first; return $sibling; } *next_sibling = \&right_sibling; # Returns the leftmost sibling or undef if this is the first sibling # sub leftmost_sibling { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if ($self->is_root) { # Root has no siblings return; } my $sibling = $self->left_siblings->search({},{ order_by => "me.$left", rows => 1, })->first; return $sibling; } *first_sibling = \&leftmost_sibling; # Returns the rightmost sibling or undef if this is the rightmost sibling # sub rightmost_sibling { my ($self) = @_; my ($root, $left, $right, $level) = $self->_get_columns; if ($self->is_root) { # Root has no siblings return; } my $sibling = $self->right_siblings->search({},{ order_by => "me.$left desc", rows => 1, })->first; return $sibling; } *last_sibling = \&rightmost_sibling; # Insert a sibling to the right of this one # sub create_right_sibling { my ($self, $args) = @_; my ($root, $left, $right, $level) = $self->_get_columns; return $self->_insert_node({ $left => $self->$right + 1, $right => $self->$right + 2, $level => $self->$level, other_args => $args, }); } # Insert a sibling to the left of this one # sub create_left_sibling { my ($self, $args) = @_; my ($root, $left, $right, $level) = $self->_get_columns; return $self->_insert_node({ $left => $self->$left, $right => $self->$left + 1, $level => $self->$level, other_args => $args, }); } # Insert a rightmost child # sub create_rightmost_child { my ($self, $args) = @_; my ($root, $left, $right, $level) = $self->_get_columns; return $self->_insert_node({ $left => $self->$right, $right => $self->$right + 1, $level => $self->$level + 1, other_args => $args, }); } # Insert a leftmost child # sub create_leftmost_child { my ($self, $args) = @_; my ($root, $left, $right, $level) = $self->_get_columns; return $self->_insert_node({ $left => $self->$left + 1, $right => $self->$left + 2, $level => $self->$level + 1, other_args => $args, }); } # Given a primary key, determine if it is a descendant of # this object # sub has_descendant { my ($self) = shift; my $descendant = $self->result_source->resultset->find(@_); if (! $descendant) { return; } my ($root, $left, $right, $level) = $self->_get_columns; if ($descendant->$left > $self->$left && $descendant->$right < $self->$right) { return 1; } return; } # Given a primary key, determine if it is an ancestor of # this object # sub has_ancestor { my ($self) = shift; my $ancestor = $self->result_source->resultset->find(@_); if (! $ancestor) { return; } my ($root, $left, $right, $level) = $self->_get_columns; if ($self->$left > $ancestor->$left && $self->$right < $ancestor->$right) { return 1; } return; } # returns true if this node is a root node # sub is_root { my ($self) = @_; if ($self->get_column( $self->tree_columns->{level_column} ) == 0) { return 1; } return; } # returns true if this node is a leaf node (no children) # sub is_leaf { my ($self) = @_; if ($self->get_column( $self->tree_columns->{right_column}) - $self->get_column( $self->tree_columns->{left_column}) == 1) { return 1; } return; } # returns true if this node is a branch (has children) # sub is_branch { my ($self) = @_; return !$self->is_leaf; } 1; =head1 NAME DBIx::Class::Tree::NestedSet - Manage trees of data using the nested set model =head1 SYNOPSIS Create a table for your tree data. CREATE TABLE Department ( id INTEGER PRIMARY KEY AUTOINCREMENT, root_id integer, lft integer NOT NULL, rgt integer NOT NULL, level integer NOT NULL, name text NOT NULL, ); In your Schema or DB class add Tree::NestedSet to the top of the component list. __PACKAGE__->load_components(qw( Tree::NestedSet ... )); Specify the columns required by the module. package My::Department; __PACKAGE__->tree_columns({ root_column => 'root_id', left_column => 'lft', right_column => 'rgt', level_column => 'level', }); Using it: my $root = My::Department->create({ ... }); my $child = $root->add_to_children({ ... }); my $rs = $root->children; my @descendants = $root->children; my $parent = $child->parent; my $rs = $child->ancestors; my @ancestors = $child->ancestors; =head1 DESCRIPTION This module provides methods for working with nested set trees. The nested tree model is a way of representing hierarchical information in a database. This takes a different approach to the Adjacency List implementation. (see L which uses C relationships in a recursive manner. The NestedSet implementation can be more efficient for most searches than the Adjacency List Implementation, for example, to obtain all descendants requires recursive queries in the Adjacency List implementation but is a single query in the NestedSet implementation. The trade-off is that NestedSet inserts are more expensive so it is most useful if you have an application that does many reads but few inserts. More about NestedSets can be found at L Oh, and although I give some code examples of familial relationships (where there are usually two parents), both Adjacency List and NestedSet implementations can only have one parent. =head1 RELATIONS This module automatically creates several relationships. =head2 root $root_node = $node->root; A belongs_to relation to the root of C<$node>s tree. =head2 nodes $all_nodes = $node->nodes; $new_node = $node->add_to_nodes({name => 'Mens Wear'}); A has_many relationship to all the nodes of C<$node>s tree. Adding to this relationship creates a rightmost child to C<$node>. =head2 parent $parent = $node->parent; A belongs_to relationship to the parent node of C<$node>s tree. Note that only the root node does not have a parent. =head2 children $rs = $node->children; @children = $node->children; $child = $node->add_to_children({name => 'Toys'}); A has_many relation to the children of C<$node>. Adding to this relationship creates a rightmost child to C<$node>. =head2 descendants $rs = $node->descendants; @descendants = $node->descendants; $child = $node->add_to_descendants({name => 'Mens Wear'}); A has_many relation to the descendants of C<$node>. Adding to this relationship creates a rightmost child to C<$node>. =head2 ancestors $rs = $node->ancestors; @ancestors = $node->ancestors; $parent = $node->add_to_ancestors({name => 'Head office'}); A has_many relation to the ancestors of C<$node>. Adding to this relationship creates a new node in place of C<$node> and makes it the parent of C<$node>. All descendants of C<$node> will likewise be pushed town the hierarchy. =head1 METHODS Many methods have alternative names, e.g. C and C This is in deference to the L module which uses terms C C C and C. Similarly L uses terms C, C, C and C However, my preference to use terms C and C consistently when using this module. However, the other names are available if you are more familiar with those modules. =head2 tree_columns __PACKAGE__->tree_columns({ left_column => 'lft', right_column => 'rgt', root_column => 'root_id', level_column => 'level', }); Declare the name of the columns defined in the database schema. None of these columns should be modified outside if this module. left_column and right_column are unlikely to be of any use to your application. They should be integer fields. Multiple trees are allowed in the same table, each tree will have a unique value in the root_column. In the current implementation this should be an integer field The level_column may be of use in your application, it defines the depth of each node in the tree (with the root at level zero). =head2 create my $tree = $schema->resultset('My::Department')->create({ name = 'Head Office', }); my $tree = $schema->resultset('My::Department')->create({ name = 'UK Office', root_id = $uk_office_ident, }); Creates a new root node. If the root_column (root_id) is not provided then it defaults to producing a node where the root_column has the same value as the primary key. This will croak if the table is defined with multiple key primary index. Note that no checks (yet) are made to stop you creating another key with the same root_id as an existing tree. If you do so you will get into a terrible mess! =head2 delete $department->delete; This will delete the node and all descendants. Cascade Delete is turned off in the has_many relationships C C C so that delete DTRT. =head2 is_root if ($node->is_root) { print "Node is a root\n"; } Returns true if the C<$node> is a root node =head2 is_branch $has_children = $node->is_branch; Returns true if the node is a branche (i.e. has children) =head2 is_leaf $is_terminal_node = $node->is_leaf; Returns true if the node is a leaf (i.e. it has no children) =head2 siblings @siblings = $node->siblings; $siblings_rs = $node->siblings; Returns all siblings of this C<$node> excluding C<$node> itself. Since a root node has no siblings it returns undef. =head2 left_siblings (or previous_siblings) @younger_siblings = $node->left_siblings; $younger_siblings_rs = $node->left_siblings; Returns all siblings of this C<$node> to the left this C<$node>. Since a root node has no siblings it returns undef. =head2 right_siblings (or next_siblings) @older_siblings = $node->right_siblings; $older_siblings_rs = $node->right_siblings; Returns all siblings of this C<$node> to the right of this C<$node>. Since a root node has no siblings it returns undef. =head2 left_sibling (or previous_sibling) $younger_sibling = $node->left_sibling; Returns the sibling immediately to the left of this C<$node> (if any). =head2 right_sibling (or next_sibling) $older_sibling = $node->right_sibling; Returns the sibling immediately to the right of this C<$node> (if any). =head2 leftmost_sibling (or first_sibling) $youngest_sibling = $node->leftmost_sibling; Returns the left most sibling relative to this C<$node> (if any). Does not return this C<$node> if this node is the leftmost sibling. =head2 rightmost_sibling (or last_sibling) $oldest_sibling = $node->rightmost_sibling; Returns the right most sibling relative to this C<$node> (if any). Does not return this C<$node> if this node is the rightmost sibling. =head2 CREATE METHODS The following create methods create a new node in relation to an existing node. =head2 create_right_sibling $bart->create_right_sibling({ name => 'Lisa' }); Create a new node as a right sibling to C<$bart>. =head2 create_left_sibling $bart->create_left_sibling({ name => 'Maggie' }); Create a new node as a left sibling to C<$bart>. =head2 create_rightmost_child $homer->create_rightmost_child({ name => 'Lisa' }); Create a new node as a rightmost child to C<$homer> =head2 create_leftmost_child $homer->create_leftmost_child({ name => 'Maggie' }); Create a new node as a leftmost child to C<$homer> =head2 ATTACH METHODS The following attach methods take an existing node (and all of it's descendants) and attaches them to the tree in relation to an existing node. The node being inserted can either be from the same tree (as identified by the root_column) or from another tree. If the root of another tree is attached then the whole of that tree becomes a sub-tree of this node's tree. The only restriction is that the node being attached cannot be an ancestor of this node. When attaching multiple nodes we try to DWIM so that the order they are specified in the call represents the order they appear in the siblings list. e.g. if we had a parent with children A,B,C,D,E and we attached nodes 1,2,3 in the following calls, we expect the following results. $parent->attach_rightmost_child 1,2,3 gives us children A,B,C,D,E,1,2,3 $parent->attach_leftmost_child 1,2,3 gives us children 1,2,3,A,B,C,D,E $child_C->attach_right_sibling 1,2,3 gives us children A,B,C,1,2,3,D,E $child_C->attach_left_sibling 1,2,3 gives us children A,B,1,2,3,C,D,E $child_C->attach_rightmost_sibling 1,2,3 gives us children A,B,C,D,E,1,2,3 $child_C->attach_leftmost_sibling 1,2,3 gives us children 1,2,3,A,B,C,D,E =head2 attach_rightmost_child (or append_child) $parent->attach_rightmost_child($other_node); $parent->attach_rightmost_child($other_node_1, $other_node_2, ...); Attaches the other_nodes to C<$parent> as the rightmost children. =head2 attach_leftmost_child $parent->attach_leftmost_child($other_node); $parent->attach_leftmost_child($other_node_1, $other_node_2, ...); Attaches the other_nodes to C<$parent> as the leftmost children. =head2 attach_right_sibling (or attach_after) $node->attach_right_sibling($other_node); $node->attach_right_sibling($other_node_1, $other_node_2, ...); Attaches the other_nodes to C<$node> as it's siblings. =head2 attach_left_sibling $node->attach_left_sibling($other_node); $node->attach_left_sibling($other_node_1, $other_node_2, ...); Attaches the other_nodes to C<$node> as it's left siblings. =head2 attach_rightmost_sibling $node->attach_rightmost_sibling($other_node); $node->attach_rightmost_sibling($other_node_1, $other_node_2, ...); Attaches the other_nodes to C<$node> as it's rightmost siblings. =head2 attach_leftmost_sibling $node->attach_leftmost_sibling($other_node); $node->attach_leftmost_sibling($other_node_1, $other_node_2, ...); Attaches the other_nodes to C<$node> as it's leftmost siblings. =head2 move_left (or move_previous) $node->move_left; Exchange the C<$node> with the sibling immediately to the left and return the node it exchanged with. If the C<$node> is already the leftmost node then no exchange takes place and the method returns undef. =head2 move_right (or move_next) $node->move_right; Exchange the C<$node> with the sibling immediately to the right and return the node it exchanged with. If the C<$node> is already the rightmost node then no exchange takes place and the method returns undef. =head2 move_leftmost (or move_first) $node->move_leftmost; Exchange the C<$node> with the leftmost sibling and return the node it exchanged with. If the C<$node> is already the leftmost node then no exchange takes place and the method returns undef. =head2 move_rightmost (or move_last) $node->move_rightmost; Exchange the C<$node> with the rightmost sibling and return the node it exchanged with. If the C<$node> is already the rightmost node then no exchange takes place and the method returns undef. =head2 CUTTING METHODS =head2 take_cutting Cuts the invocant and its descendants out of the tree they are in, making the invocant the root of a new tree. Returns the modified invocant. =head2 dissolve Dissolves the entire thread, that is turn each node of the thread into a single-item tree of its own. =head1 CAVEATS =head2 Multiple Column Primary Keys Support for Multiple Column Primary Keys is limited (mainly because I rarely use them) but I have tried to make it possible to use them. Please let me know if this does not work as well as you expect. =head2 discard_changes By the nature of Nested Set implementations, moving, inserting or deleting nodes in the tree will potentially update many (sometimes most) other nodes. Even if you have preloaded some of the objects, if you make a change to one object the other objects will not reflect their new value until you have reloaded them from the database. (see L) A simple demonstration of this $grampa = $schema->schema->resultset('Simpsons')->create({ name => 'Abraham' }); $homer = $grampa->add_children({name => 'Homer'}); $bart = $homer->add_children({name => 'Bart'}); The methods in this module will do their best to keep instances that they know about updated. For example the first call to C in the above example will update C<$grampa> and C<$homer> with the latest changes to the database. However, the second call to C only knows about C<$homer> and C<$bart> and in adding a new node to the tree it will update the C<$grampa> node in the database. To ensure you have the latest changes do the following. $grampa->discard_changes. Not doing so will have unpredictable results. =head1 AUTHORS Code by Ian Docherty Epause@iandocherty.comE Based on original code by Florian Ragwitz Erafl@debian.orgE Incorporating ideas and code from Pedro Melo Emelo@simplicidade.orgE Special thanks to Moritz Lenz who sent in lots of patches and changes for version 0.08 =head1 COPYRIGHT AND LICENSE Copyright (c) 2009-2011 The above authors This is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available. =cut DBIx-Class-Tree-NestedSet-0.10/Changes0000755446137100000240000000231211636645732017052 0ustar dochertigamesRevision history for Perl extension DBIx::Class::Tree::NestedSet. 0.10 Thu Sep 22 15:50:00 2011 - got rid of namespace::autoclean and Moose dependencies (both were only used for testing) - implemented take_cutting method, which removes a subtree from a tree - implemented dissolve method, which disasembles a tree into single nodes. - fixed [cpan #63346], Can't attach an independent root node - fixed [cpan #59319], I can't add a root node! - fixed [cpan #59020], typos in code examples - got rid of perl-5.10 dependency - seems to work fine on 5.8 with minor changes. 0.09 Thu Sep 22 09:40:00 2011 - Fixed several document and code bugs reported on RT 0.08 Thu Sep 22 09:02:00 2011 - added take_cutting method provided by Moritz Lenz 0.07 Mon May 31 10:15:00 2010 - fixed another unexpected dependency 0.06 Sat May 29 11:00:00 2010 - Replaced might_have relationship to root with belongs_to 0.05 Sat May 29 07:57:00 2010 - fixed another dependency in test code 0.04 Thu May 27 10:34:00 2010 - fixed dependencies issue 0.01 Wed Jan 9 19:30:30 2010 - original version; created by h2xs 1.23 with options -AX DBIx::Class::Tree::NestedSet DBIx-Class-Tree-NestedSet-0.10/MANIFEST0000644446137100000240000000060711636651445016710 0ustar dochertigamesChanges lib/DBIx/Class/Tree/NestedSet.pm Makefile.PL MANIFEST META.yml README t/07-tree.t t/08-exception.t t/09-multi.t t/10-multi.t t/11-multi-pk.t t/12-append.t t/13-private.t t/14-attach.t t/15-attach.t t/16-siblings.t t/17-cutting.t t/tlib/FailSchema.pm t/tlib/FailSchema/MissingCols.pm t/tlib/TestSchema.pm t/tlib/TestSchema/MultiPK.pm t/tlib/TestSchema/MultiTree.pm t/tlib/TestTree.pm DBIx-Class-Tree-NestedSet-0.10/META.yml0000644446137100000240000000115411636651764017032 0ustar dochertigames--- #YAML:1.0 name: DBIx-Class-Tree-NestedSet version: 0.10 abstract: Manage trees of data using the nested set model author: - Ian Docherty license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: DBICx::TestDatabase: 0.01 DBIx::Class: 0.08 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 DBIx-Class-Tree-NestedSet-0.10/README0000755446137100000240000000251611636647052016442 0ustar dochertigamesDBIx::Class::NestedSet::ResultSet version 0.10 ============================================== INSTALLATION To install this module type the following: perl Makefile.PL make make test make install DEPENDENCIES This module requires these other modules and libraries: DBIx::Class TODO There are some issues about the current implementation that need consideration Is it possible to support multiple key indexes? Yes but we need to specify the root value when we create the root node Also search_related currently only works with single key PK In the event that a root column is not defined, should we default to using the primary key One reason why not, because we may want to add a new node as the new root or to add one tree to another Is the 'CASE' sql in NestedSet.pm compatible with all databases? What do we do if we have several objects (e.g. root) instances when we add new nodes. Do we 'discard_changes'? delete method seems to be ineficient for deleting a sub-tree I am not quite sure what 'has_descendent' should do. COPYRIGHT AND LICENCE Copyright (C) 2010-2011 by Ian Docherty This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available. DBIx-Class-Tree-NestedSet-0.10/Makefile.PL0000755446137100000240000000123511636645472017535 0ustar dochertigamesuse 5.008003; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( NAME => 'DBIx::Class::Tree::NestedSet', VERSION_FROM => 'lib/DBIx/Class/Tree/NestedSet.pm', # finds $VERSION PREREQ_PM => { DBICx::TestDatabase => 0.01, DBIx::Class => 0.08000, }, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'lib/DBIx/Class/Tree/NestedSet.pm', # retrieve abstract AUTHOR => 'Ian Docherty ') : ()), );