From: Allen Day Date: Wed, 13 Oct 2004 23:14:06 +0000 (+0000) Subject: some refactoring. moving to being able to call $schema->as_graph to do X-Git-Tag: v0.11008~611 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b046b0b9c28f98a00ec4f89b544333f40782b3db;p=dbsrgits%2FSQL-Translator.git some refactoring. moving to being able to call $schema->as_graph to do an OM translation. --- diff --git a/lib/SQL/Translator/Producer/Turnkey.pm b/lib/SQL/Translator/Producer/Turnkey.pm index 40ed06b..de6f7b3 100644 --- a/lib/SQL/Translator/Producer/Turnkey.pm +++ b/lib/SQL/Translator/Producer/Turnkey.pm @@ -1,7 +1,7 @@ package SQL::Translator::Producer::Turnkey; # ------------------------------------------------------------------- -# $Id: Turnkey.pm,v 1.60 2004-10-13 22:31:59 allenday Exp $ +# $Id: Turnkey.pm,v 1.61 2004-10-13 23:14:06 allenday Exp $ # ------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -22,7 +22,7 @@ package SQL::Translator::Producer::Turnkey; use strict; use vars qw[ $VERSION $DEBUG ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.60 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.61 $ =~ /(\d+)\.(\d+)/; $DEBUG = 1 unless defined $DEBUG; use SQL::Translator::Schema::Constants; @@ -41,111 +41,43 @@ my %producer2dsn = ( # ------------------------------------------------------------------- sub produce { - my $log = Log::Log4perl->get_logger('SQL.Translator.Producer.Turnkey'); - - my $t = shift; - my $create = undef; - my $args = $t->producer_args; - my $no_comments = $t->no_comments; - my $baseclass = $args->{'main_pkg_name'} || $t->format_package_name('DBI'); - my $graph = SQL::Translator::Schema::Graph->new(translator => $t, - baseclass => $baseclass - ); - - my $parser_type = (split /::/, $t->parser_type)[-1]; - - local $DEBUG = $t->debug; - - my %meta = ( -concat => $args->{'concat'} || '', - format_fk => $t->format_fk_name, - format_package => $t->format_package_name, - format_table => $t->format_table_name, - template => $args->{'template'} || '', - baseclass => $baseclass, - db_dsn => $args->{'db_dsn'} || '', - db_user => $args->{'db_user'} || '', - db_pass => $args->{'db_pass'} || '', - db_str => $args->{'db_str'} || '', - parser => $t->parser_type, - producer => __PACKAGE__, - dsn => $args->{'dsn'} || sprintf( 'dbi:%s:_', $producer2dsn{ $parser_type } - ? $producer2dsn{ $parser_type } - : $parser_type - ) - ); - - # - # create methods - # - # this code needs to move to Graph.pm - foreach my $node_from ($graph->node_values){ - - next unless $node_from->table->is_data or !$node_from->table->is_trivial_link; - - foreach my $cedge ( $node_from->compoundedges ){ - - my $hyperedge = SQL::Translator::Schema::Graph::HyperEdge->new(); - - my $node_to; - foreach my $edge ($cedge->edges){ - if($edge->thisnode->name eq $node_from->name){ - $hyperedge->vianode($edge->thatnode); - - if($edge->thatnode->name ne $cedge->via->name){ - $node_to ||= $graph->node($edge->thatnode->table->name); - } - - $hyperedge->push_thisnode($edge->thisnode); - $hyperedge->push_thisfield($edge->thisfield); - $hyperedge->push_thisviafield($edge->thatfield); - - } else { - if($edge->thisnode->name ne $cedge->via->name){ - $node_to ||= $graph->node($edge->thisnode->table->name); - } - $hyperedge->push_thatnode($edge->thisnode); - $hyperedge->push_thatfield($edge->thisfield); - $hyperedge->push_thatviafield($edge->thatfield); - } - $log->debug($edge->thisfield->name); - $log->debug($edge->thatfield->name); - } - - if($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode == 1){ $hyperedge->type('one2one') } - elsif($hyperedge->count_thisnode > 1 and $hyperedge->count_thatnode == 1){ $hyperedge->type('many2one') } - elsif($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode > 1){ $hyperedge->type('one2many') } - elsif($hyperedge->count_thisnode > 1 and $hyperedge->count_thatnode > 1){ $hyperedge->type('many2many') } - - $log->debug($_) foreach sort keys %::SQL::Translator::Schema::Graph::HyperEdge::; - - #node_to won't always be defined b/c of multiple edges to a single other node - if(defined($node_to)){ - $log->debug($node_from->name); - $log->debug($node_to->name); - - if(scalar($hyperedge->thisnode) > 1){ - $log->debug($hyperedge->type ." via ". $hyperedge->vianode->name); - my $i = 0; - foreach my $thisnode ( $hyperedge->thisnode ){ - $log->debug($thisnode->name .' '. - $hyperedge->thisfield_index(0)->name .' -> '. - $hyperedge->thisviafield_index($i)->name .' '. - $hyperedge->vianode->name .' '. - $hyperedge->thatviafield_index(0)->name .' <- '. - $hyperedge->thatfield_index(0)->name .' '. - $hyperedge->thatnode_index(0)->name ."\n" - ); - $i++; - } - } -#warn Dumper($hyperedge) if $hyperedge->type eq 'many2many'; - $node_from->push_hyperedges($hyperedge); - } - } - } - $meta{"nodes"} = $graph->node; - return(translateForm($t, \%meta)); + + my $t = shift; + my $create = undef; + my $args = $t->producer_args; + my $no_comments = $t->no_comments; + my $baseclass = $args->{'main_pkg_name'} || $t->format_package_name('DBI'); + my $graph = SQL::Translator::Schema::Graph->new(translator => $t); + + my $parser_type = (split /::/, $t->parser_type)[-1]; + + local $DEBUG = $t->debug; + + my %meta = ( + concat => $args->{'concat'} || '', + format_fk => $t->format_fk_name, + format_package => $t->format_package_name, + format_table => $t->format_table_name, + template => $args->{'template'} || '', + baseclass => $baseclass, + db_dsn => $args->{'db_dsn'} || '', + db_user => $args->{'db_user'} || '', + db_pass => $args->{'db_pass'} || '', + db_str => $args->{'db_str'} || '', + parser => $t->parser_type, + producer => __PACKAGE__, + dsn => $args->{'dsn'} || sprintf( 'dbi:%s:_', $producer2dsn{ $parser_type } + ? $producer2dsn{ $parser_type } + : $parser_type + ) + ); + + foreach my $node ($graph->node()){ + $node->base($baseclass); + } + + $meta{"nodes"} = $graph->node; + return(translateForm($t, \%meta)); } sub translateForm { @@ -165,9 +97,9 @@ sub translateForm { $tt2 = template($type); my $result; - if($type eq 'atomtemplate'){ + if ($type eq 'atomtemplate') { my %result; - foreach my $node (values %{ $meta->{'nodes'} }){ + foreach my $node (values %{ $meta->{'nodes'} }) { $result = ''; my $param = { node => $node }; $template->process(\$tt2, $param, \$result) || die $template->error(); diff --git a/lib/SQL/Translator/Schema.pm b/lib/SQL/Translator/Schema.pm index 4a2458e..36bd610 100644 --- a/lib/SQL/Translator/Schema.pm +++ b/lib/SQL/Translator/Schema.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema; # ---------------------------------------------------------------------- -# $Id: Schema.pm,v 1.15 2004-07-26 13:55:51 kycl4rk Exp $ +# $Id: Schema.pm,v 1.16 2004-10-13 23:14:06 allenday Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -49,12 +49,13 @@ use SQL::Translator::Schema::Procedure; use SQL::Translator::Schema::Table; use SQL::Translator::Schema::Trigger; use SQL::Translator::Schema::View; +use SQL::Translator::Schema::Graph; use SQL::Translator::Utils 'parse_list_arg'; use base 'Class::Base'; use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER $TRIGGER_ORDER $PROC_ORDER ]; -$VERSION = sprintf "%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)\.(\d+)/; # ---------------------------------------------------------------------- sub init { diff --git a/lib/SQL/Translator/Schema/Graph.pm b/lib/SQL/Translator/Schema/Graph.pm index 818b906..097be2b 100644 --- a/lib/SQL/Translator/Schema/Graph.pm +++ b/lib/SQL/Translator/Schema/Graph.pm @@ -23,7 +23,6 @@ use Class::MakeMethods::Template::Hash ( 'translator' => {class => 'SQL::Translator'}, ], 'hash' => [ qw( node ) ], - 'scalar' => [ qw( baseclass ) ], 'number --counter' => [ qw( order ) ], ); @@ -45,7 +44,6 @@ sub init { $node->order($self->order_incr()); $node->name( $self->translator->format_package_name($table->name) ); - $node->base( $self->baseclass ); $node->table( $table ); $node->primary_key( ($table->primary_key->fields)[0] ); @@ -170,6 +168,85 @@ sub init { } } } + + my $graph = $self; #hack + my $log = Log::Log4perl->get_logger('SQL.Translator.Schema.Graph'); + + # + # create methods + # + # this code needs to move to Graph.pm + foreach my $node_from ($graph->node_values) { + + next unless $node_from->table->is_data or !$node_from->table->is_trivial_link; + + foreach my $cedge ( $node_from->compoundedges ) { + + my $hyperedge = SQL::Translator::Schema::Graph::HyperEdge->new(); + + my $node_to; + foreach my $edge ($cedge->edges) { + if ($edge->thisnode->name eq $node_from->name) { + $hyperedge->vianode($edge->thatnode); + + if ($edge->thatnode->name ne $cedge->via->name) { + $node_to ||= $graph->node($edge->thatnode->table->name); + } + + $hyperedge->push_thisnode($edge->thisnode); + $hyperedge->push_thisfield($edge->thisfield); + $hyperedge->push_thisviafield($edge->thatfield); + + } else { + if ($edge->thisnode->name ne $cedge->via->name) { + $node_to ||= $graph->node($edge->thisnode->table->name); + } + $hyperedge->push_thatnode($edge->thisnode); + $hyperedge->push_thatfield($edge->thisfield); + $hyperedge->push_thatviafield($edge->thatfield); + } + $log->debug($edge->thisfield->name); + $log->debug($edge->thatfield->name); + } + + if ($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode == 1) { + $hyperedge->type('one2one'); + } elsif ($hyperedge->count_thisnode > 1 and $hyperedge->count_thatnode == 1) { + $hyperedge->type('many2one'); + } elsif ($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode > 1) { + $hyperedge->type('one2many'); + } elsif ($hyperedge->count_thisnode > 1 and $hyperedge->count_thatnode > 1) { + $hyperedge->type('many2many'); + } + + $log->debug($_) foreach sort keys %::SQL::Translator::Schema::Graph::HyperEdge::; + + #node_to won't always be defined b/c of multiple edges to a single other node + if (defined($node_to)) { + $log->debug($node_from->name); + $log->debug($node_to->name); + + if (scalar($hyperedge->thisnode) > 1) { + $log->debug($hyperedge->type ." via ". $hyperedge->vianode->name); + my $i = 0; + foreach my $thisnode ( $hyperedge->thisnode ) { + $log->debug($thisnode->name .' '. + $hyperedge->thisfield_index(0)->name .' -> '. + $hyperedge->thisviafield_index($i)->name .' '. + $hyperedge->vianode->name .' '. + $hyperedge->thatviafield_index(0)->name .' <- '. + $hyperedge->thatfield_index(0)->name .' '. + $hyperedge->thatnode_index(0)->name ."\n" + ); + $i++; + } + } + #warn Dumper($hyperedge) if $hyperedge->type eq 'many2many'; + $node_from->push_hyperedges($hyperedge); + } + } + } + } 1;