Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: how to use Graphviz with sql

by andreas1234567 (Vicar)
on Jan 25, 2008 at 08:08 UTC ( #664240=note: print w/replies, xml ) Need Help??

in reply to how to use Graphviz with sql

I once did something like this. Below is a sample that creates a simple graph with nodes and edges showing relationships between them:
#!/usr/bin/perl use strict; use warnings; use DBI; use GraphViz; use Data::Dumper; use Getopt::Long; use Log::Log4perl; use Pod::Usage; my $conf = undef; my $log = undef; my $help = undef; my $db = undef; my $dbuser = undef; my $dbpass = undef; $conf = q( log4perl.category.Script = DEBUG, ScreenAppender log4perl.appender.ScreenAppender = Log::Log4perl::Appender:: +Screen log4perl.appender.ScreenAppender.stderr = 1 log4perl.appender.ScreenAppender.layout = PatternLayout log4perl.appender.ScreenAppender.layout.ConversionPattern=[%p] %d %F +:%L - %m%n ); Log::Log4perl::init( \$conf ); $log = Log::Log4perl::->get_logger(q(Script)); $log->debug("start") if $log->is_debug(); # GetOptions GetOptions("help|?" => \$help, "db=s" => \$db, "dbuser=s" => \$dbuser, "dbpass=s" => \$dbpass) or pod2usage(2); pod2usage(1) if $help; pod2usage(1) if !$db; pod2usage(1) if !$dbuser; pod2usage(1) if !$dbpass; my $dbh = DBI->connect ('DBI:mysql:' . $db, $dbuser, $dbpass); my $sth = $dbh->prepare( q{select name, friend from friends} ); $sth->execute() or $log->logdie($dbh->errstr); my %friends = (); while (my $hashref = $sth->fetchrow_hashref()) { my ($name, $friend) = ($hashref->{name}, $hashref->{friend}); $log->debug(qq{$name, $friend}) if $log->is_debug(); push @{$friends{$hashref->{name}}}, $friend; } $sth->finish(); $log->debug(Dumper(\%friends)) if $log->is_debug(); my $g = GraphViz->new(layout => 'fdp', ratio => 'compress'); # 1st iteration : Add nodes foreach my $name (keys %friends) { $g->add_node( "$name", label => $name, shape => 'box', style => 'filled', ) or $log->logdie("add_node failed"); } # 2nd iteration : Add edges foreach my $name (keys %friends) { foreach my $friend ( @{$friends{$name}}) { $log->debug(qq{$name => $friend}) if $log->is_debug(); $g->add_edge("$name" => "$friend", label => q{loves}) or $log->logdie("add_edge failed"); } } # Write to file my $fh = undef; open($fh, "> friends.png") or die "open failed:$!"; print $fh $g->as_png; close $fh or die "close failed:$!"; __END__ =head1 NAME simpsons - Graphviz DBI example =head1 SYNOPSIS ./ --db DB --dbuser DBUSER --dbpass DBPASS =head1 MySQL DDL, DML create table friends (name varchar(32) not null, friend varchar(32) no +t null); alter table friends add unique key (name, friend); insert into friends values ('Homer', 'Marge'), ('Homer', 'Bart'), ('Homer', 'Lisa'),('Marge', 'Homer'),('Bart', 'Santa\'s Little Helper'),('Lisa', 'Snowball II'); =cut

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://664240]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-01-21 22:37 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (59 votes). Check out past polls.