Class::DBI::Loader::GraphViz - Graph tables and relationships


Class-DBI-Loader-GraphViz documentation Contained in the Class-DBI-Loader-GraphViz distribution.

Index


Code Index:

NAME

Top

Class::DBI::Loader::GraphViz - Graph tables and relationships

SYNOPSIS

Top

    my $loader = Class::DBI::Loader->new(
        namespace => "BeerDB",
        dsn => "dbi:SQLite:dbname=t/test.db");
    BeerDB::Beer->has_a(brewery => "BeerDB::Brewery");
    # ...

    my GraphViz $g = $loader->graph_tables;
    my $dot = $g->as_dot;

DESCRIPTION

Top

This module bridges Class::DBI::Loader and GraphViz::DBI, to allow GraphViz::DBI to know about Class::DBI's has_a relationships.

It provides one method in Class::DBI::Loader, graph_tables which returns a graphviz object.

SEE ALSO

Top

Class::DBI::Loader, GraphViz::DBI.

AUTHOR

Top

Simon Cozens, <simon@cpan.org>

COPYRIGHT AND LICENSE

Top


Class-DBI-Loader-GraphViz documentation Contained in the Class-DBI-Loader-GraphViz distribution.

package Class::DBI::Loader::GraphViz;
use Class::DBI::Loader;
use base 'GraphViz::DBI';
use strict;
use warnings;
use Carp qw(croak);
our $VERSION = "1.0";

sub new {
	my ($class, $loader) = @_;
	my $self = $class->SUPER::new();
	$self->{loader} = $loader;
	# Pick the DBH from a random table
	my $table = ($self->get_tables)[0]
		or croak "Don't seem to have any tables";
	$self->set_dbh($loader->_table2class($table)->db_Main);
}

sub get_tables { shift->{loader}->tables }

# Music::CD->has_many(tracks => 'Music::Track');
# Music::CD->has_a(artist => 'Music::Artist');
# is_foreign_key("track", "cd") => "cd"
# is_foreign_key("cd", "artist") => "artist"   
sub is_foreign_key {
	my ($self, $table, $field) = @_;
	my $class = $self->{loader}->_table2class($table);
    return unless $class->can("__hasa_rels");
    my $hasa = $class->__hasa_rels;
	if (exists $hasa->{$field}) {
		return $hasa->{$field}->[0]->table;
	}
	return;
}
	
package Class::DBI::Loader::Generic;

sub graph_tables { return Class::DBI::Loader::GraphViz->new(shift)->graph_tables; }

1;