
DBIx::Class::Graph - Represent a graph in a relational database using DBIC

package MySchema::Graph;
use strict;
use warnings;
use base 'DBIx::Class';
__PACKAGE__->load_components("Graph", "Core");
__PACKAGE__->table("client_groups");
__PACKAGE__->add_columns("id", "name", "parent_id");
__PACKAGE__->connect_graph(predecessor => "parent_id");
# predecessor must be a column of this row
my $g = $rs->get_graph;
my @children = $g->successors($rs->get_vertex($id));
# do other cool stuff like calculating distances etc.

This module allows to create and interact with a directed graph. It will take care of storing the information in a relational database. It uses Graph for calculations. This module extends the DBIx::Class::ResultSet. Some methods are added to the resultset, some to the row objects.

The first argument is the relation to the next vertex. Possible values: "predecessor" and "successor"
The name of the relation to the next vertex is defined by the second argument.

finds a vertex by searching the underlying resultset for $id in the primary key column (only single primary keys are supported). It's not as smart as the original "find" in DBIx::Class::ResultSet because it looks on the primary key(s) for $id only.

Graph lacks some useful methods
Returns all successors of a vertex $v. It's a recusive method. It avoids infinite loops.
Returns all predecessors of a vertex $v. It's a recusive method. It avoids infinite loops.

Simply sort the resultset
$rs->search(undef, {order_by => "title ASC"})->get_graph;

It might be possible that some database actions are not recognized by the graph object and thus do not represent the correct status of the graph. To make sure you are working with the correct graph object reload it after editing the graph. If you see such a behaviour please report it to me.
you should ommit creating multigraphs. Most graph algorithms expect a simple graph and may break if they get a multigraph.
you should consider caching the output of your scripts since retrieving and creating a Graph is not very fast.

This would allow graphs which have multiple parents and multiple children

DBIx::Class::Tree, DBIx::Class::NestedSet

See "CAVEATS"

Moritz Onken, <onken@houseofdesign.de>
I am also avaiable on the DBIx::Class mailinglist

Copyright (C) 2008 by Moritz Onken
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.