DBIx::Tree::Persist - Play with Tree and Tree::Persist a la DBIx::Tree
First, edit lib/DBIx/Tree/Persist/.htdbix.tree.persist.conf.
Then run the scripts in this order (see scripts/test.sh):
Drop tables one and two.
Of course, you only run this after running create.tables.pl.
Create tables one and two.
Some notes regarding the ways tables one and two are declared (in DBIx::Tree::Persist::Create):
DBIx::Tree::Persist::Create
parent_id is not 'not null', because Tree::Persist stores a null as the parent of the root.
If parent_id is 'references two(id)', then it cannot be set to 0 for the root, because id 0 does not exist.
However, by omitting 'references two(id)', the parent_id of the root can be (manually) set to 0, and Tree::Persist still reads in the tree properly.
Populate table two from the text file data/two.txt.
The data comes from the docs for DBIx::Tree.
populate.tables.pl uses neither Tree nor Tree::Persist.
The code in DBIx::Tree::Persist::Create uses 0 as the parent_id of the root, whereas Tree::Persist uses null.
This is both to demonstrate the point made above that Tree::Persist handles this, and to adhere to my convention to use 'not null' whenever possible. Clearly, this is not possible when it's Tree::Persist writing to the database. Hence table two which I write can use 'not null', but table one can't use it, since table one is populated by Tree::Persist.
This convention is adopted from:
Joe Celko's SQL for Smarties 2nd edition Morgan Kaufmann 1-55860-576-2 Section 6.9, page 120, Design Advice for NULLs
Report the record counts from tables one and two.
Traverse and print table two.
This run uses Tree::Persist, and Tree.
Copy table two to table one.
Copy table two to table one, again. Table one now contains 2 independent trees.
Traverse and print table one, starting from id = 1.
Traverse and print table one, starting from id = 21.
The tree structures for the 2 trees printed by the last 2 commands will be the same. However, since the trees are stored at different offsets within table one, the ids associated with each corresponding node will be different.
Use the -data_structure option to call the build_structure() method, and to output that structure instead of pretty-printing the tree.
build_structure()
DBIx::Tree::Persist provides sample code for playing with Tree and Tree::Persist a la DBIx::Tree.
This module is available as a Unix-style distro (*.tgz).
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing distros.
Returns a Perl data structure which can be turned into JSON.
The -data_structure option to scripts/tree.pl gives you access to this feature.
If copy_name is used to pass a table name to new(), sub run() calls sub copy_table().
If copy_name is not used, sub run() calls sub traverse().
sub copy_table() shows how to build a tree based on a linear scan of a dataset.
See scripts/tree.pl for how to pass sample parameters to new() via a command-line program.
new() takes a hash of parameters:
new()
copy_name is optional.
If specified, the code copies the data from the table named with the -t option to the table named with the -c option.
dbh is optional.
If specified, the code uses the $dbh provided.
If not specified, the code reads the config file lib/DBIx/Tree/Persist/.htdbix.tree.persist.conf to get parameters and calls DBI -> connect() to generate a dbh.
This is mainly used for testing. See t/test.t.
starting_id is optional.
If specified, a tree is read from the table named with the -t option, starting at the id given here.
If not specified, starting_id defaults to 1.
table_name is mandatory.
The table named with the -t option is always used as input.
It will (probably) have been populated with scripts/populate.tables.pl.
verbose is optional.
If specified and > 0, if provides more progress reports.
If not specified, it defaults to 0, which minimizes output.
Print the tree nicely. This method is called from traverse() if the -data_structure option is not used.
traverse()
After calling new(...), you have to call run(). See scripts/tree.pl for sample code.
sub traverse() shows how to build a tree from a disk file, and to then process that tree.
if the -data_structure option (to scripts/tree.pl) is used, the tree is converted to a data structure, which is then printed using the Dumper() method of Data::Dumper::Concise.
Dumper()
If the -data_structure option is not used, the tree is pretty-printed by calling the method pretty_print().
pretty_print()
Email the author, or log a bug on RT:
https://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Tree-Persist.
Data::NestedSet. This module has its own list of See Also references.
DBIx::Tree::NestedSet. This module has its own list of See Also references.
DBIx::Tree.
Tree.
Tree::Persist.
Tree::DAG_Node.
Tree::DAG_Node::Persist.
DBIx::Tree::Persist was written by Ron Savage <ron@savage.net.au> in 2010.
Home page: http://savage.net.au/index.html.
Australian copyright (c) 2010, Ron Savage.
All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Artistic License, a copy of which is available at: http://www.opensource.org/licenses/index.html.
To install DBIx::Tree::Persist, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Tree::Persist
CPAN shell
perl -MCPAN -e shell install DBIx::Tree::Persist
For more information on module installation, please visit the detailed CPAN module installation guide.