code. still template problems for other output formats.
use strict;
use vars qw[ $VERSION $DEBUG ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
$DEBUG = 1 unless defined $DEBUG;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Schema::Graph;
use SQL::Translator::Schema::Graph::HyperEdge;
-use SQL::Translator::Utils qw(header_comment);
+use Log::Log4perl; Log::Log4perl::init('/etc/log4perl.conf');
use Data::Dumper;
use Template;
-#{
-# local $/;
-# my $data = <SQL::Translator::Producer::Turnkey::DATA>;
-# eval { $data };
-# warn $@ if $@;
-#}
-
my %producer2dsn = (
MySQL => 'mysql',
PostgreSQL => 'Pg',
# -------------------------------------------------------------------
sub produce {
+ my $log = Log::Log4perl->get_logger('sql.translator');
+
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 $graph = SQL::Translator::Schema::Graph->new(translator => $t,
+ baseclass => $baseclass
+ );
my $parser_type = (split /::/, $t->parser_type)[-1];
# create methods
#
foreach my $node_from ($graph->node_values){
- next unless $node_from->table->is_data;
+
+ 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();
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') }
- if(scalar($hyperedge->thisnode) > 1){
-warn $hyperedge;
-warn $hyperedge->type;
- foreach my $thisnode ( $hyperedge->thisnode ){
-#warn $thisnode;
-#warn $hyperedge->thatnode;
-warn $thisnode->name;
-
-eval { $hyperedge->thatnode->name }; warn $@ if $@;
-
-warn $hyperedge->thatnode if(defined($hyperedge->thatnode));
-
-
-#warn $hyperedge->thisfield->name;
-#warn $hyperedge->thatfield->name;
-#warn $hyperedge->thisviafield->name;
-#warn $hyperedge->thatviafield->name;
+#warn join "\n", 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++;
+ }
}
+ $node_from->push_hyperedges($hyperedge);
}
-exit;
-
-#warn $node_from->name ."\t". $node_to->name ."\t". $hyperedge->type ."\t". $hyperedge->vianode->name;
-
- $node_from->push_hyperedges($hyperedge);
}
}
my $tt2;
$tt2 = template($type);
my $template = Template->new({
+ PRE_CHOMP => 1,
+ POST_CHOMP => 0,
EVAL_PERL => 1
});
my $result;
- # specify input filename, or file handle, text reference, etc.
- # process input template, substituting variables
$template->process(\$tt2, $meta, \$result) || die $template->error();
return($result);
}
=head1 AUTHORS
Allen Day E<lt>allenday@ucla.eduE<gt>
-Ying Zhang E<lt>zyolive@yahoo.comE<gt>,
Brian O\'Connor E<lt>brian.oconnor@excite.comE<gt>.
=cut
[% printHasA(node.edges, node) %]
[% printHasMany(node.edges, node) %]
[% printHasCompound(node.compoundedges, node.hyperedges, node.name) %]
-[% printHasFriendly(node) %]
+[% #printHasFriendly(node) %]
[% END %]
[% MACRO printPKAccessors(array, name) BLOCK %]
sub id { shift->[% item %] }
sub [% name %] { shift->[% item %] }
[% END %]
+
[% END %]
[% MACRO printHasA(edges, name) BLOCK %]
#
# Has A
#
+
[% FOREACH edge = edges %]
- [%- IF edge.type == 'import' -%]
+ [% IF edge.type == 'import' %]
[% node.name %]->has_a([% edge.thisfield.name %] => '[% edge.thatnode.name %]');
- [%- IF node.has(edge.thatnode.name) < 2 %]
+ [% IF node.has(edge.thatnode.name) < 2 %]
sub [% edge.thatnode.table.name %] { return shift->[% edge.thisfield.name %] }
- [%- ELSE %]
+ [% ELSE %]
sub [% format_fk(edge.thisnode.table.name,edge.thisfield.name) %] { return shift->[% edge.thisfield.name %] }
- [%- END %]
- [%- END %]
+ [% END %]
+
+ [% END %]
[% END %]
+
[% END %]
[% MACRO printHasMany(edges, node) BLOCK %]
#
# Has Many
#
+
[% FOREACH edge = edges %]
- [%- IF edge.type == 'export' -%]
+ [% IF edge.type == 'export' %]
[% node.name %]->has_many([% edge.thatnode.table.name %]_[% edge.thatfield.name %], '[% edge.thatnode.name %]' => '[% edge.thatfield.name %]');
- [%- IF node.via(edge.thatnode.name) >= 1 %]
+ [% IF node.via(edge.thatnode.name) >= 1 %]
sub [% edge.thatnode.table.name %]_[% format_fk(edge.thatnode.table.name,edge.thatfield.name) %]s { return shift->[% edge.thatnode.table.name %]_[% edge.thatfield.name %] }
- [%- ELSIF edge.thatnode.table.is_data %]
+ [% ELSIF edge.thatnode.table.is_data %]
+ [% IF node.edgecount(edge.thatnode.name) > 1 %]
+sub [% edge.thatnode.table.name %]_[% format_fk(edge.thatnode.name,edge.thatfield.name) %]s { return shift->[% edge.thatnode.table.name %]_[% edge.thatfield.name %] }
+ [% ELSE %]
sub [% edge.thatnode.table.name %]s { return shift->[% edge.thatnode.table.name %]_[% edge.thatfield.name %] }
- [%- END %]
- [%- END %]
+ [% END %]
+ [% END %]
+
+ [% END %]
[% END %]
+
[% END %]
[% MACRO printHasCompound(cedges,hedges,name) BLOCK %]
#
[% FOREACH cedge = cedges %]
[% FOREACH edge = cedge.edges %]
- [%- NEXT IF edge.thisnode.name != name -%]
+ [% NEXT IF edge.thisnode.name != name %]
sub [% cedge.via.table.name %]_[% format_fk(edge.thatnode.table.name,edge.thatfield.name) %]s { return shift->[% cedge.via.table.name %]_[% edge.thatfield.name %] }
[% END %]
[% END %]
+
[% FOREACH h = hedges %]
- [%- NEXT IF h.thisnode.name != name -%]
- [%- IF h.type == 'one2one' %]
-1sub [% h.thatnode.table.name %]s { my \$self = shift; return map \$_->[% h.thatviafield.name %], \$self->[% h.vianode.table.name %]_[% h.thisviafield.name %] }
- [%- ELSIF h.type == 'one2many' %]
+########## [% h.type %] ##########
+ [% IF h.type == 'one2one' %]
+sub [% h.thatnode.table.name %]s { my \$self = shift; return map \$_->[% h.thatviafield.name %], \$self->[% h.vianode.table.name %]_[% h.thisviafield.name %] }
+
+ [% ELSIF h.type == 'one2many' %]
+ [% thisnode = h.thisnode_index(0) %]
+ [% i = 0 %]
+ [% FOREACH thatnode = h.thatnode %]
+#[% thisnode.name %]::[% h.thisfield_index(0).name %] -> [% h.vianode.name %]::[% h.thisviafield_index(i).name %] ... [% h.vianode.name %]::[% h.thatviafield_index(0).name %] <- [% h.thatnode_index(0).name %]::[% h.thatfield_index(0).name %]
+sub [% h.vianode.table.name %]_[% format_fk(h.vianode,h.thatviafield_index(0).name) %]s { my \$self = shift; return map \$_->[% h.thatviafield_index(0).name %], \$self->[% h.vianode.table.name %]_[% h.thisviafield_index(i).name %] }
+ [% i = i + 1 %]
+ [% END %]
+
+ [% ELSIF h.type == 'many2one' %]
+ [% i = 0 %]
[% FOREACH thisnode = h.thisnode %]
-2
- h.thatnode.name=[% h.thatnode.name %]
- h.thatfield.name [% h.thatfield.name %]
- h.thisnode.name=[% thisnode.name %]
- h.thisfield.name=[% thisfield.name %]
-2
+#[% thisnode.name %]::[% h.thisfield_index(0).name %] -> [% h.vianode.name %]::[% h.thisviafield_index(i).name %] ... [% h.vianode.name %]::[% h.thatviafield_index(0).name %] <- [% h.thatnode_index(0).name %]::[% h.thatfield_index(0).name %]
+sub [% h.vianode.table.name %]_[% format_fk(h.vianode,h.thisviafield_index(i).name) %]_[% format_fk(h.vianode,h.thatviafield_index(0).name) %]s { my \$self = shift; return map \$_->[% h.thatviafield_index(0).name %], \$self->[% h.vianode.table.name %]_[% h.thisviafield_index(i).name %] }
+ [% i = i + 1 %]
+
[% END %]
- [%- ELSIF h.type == 'many2one' %]
-3sub [% h.thatnode.table.name %]s { my \$self = shift; return map \$_->[% h.thatviafield.name %], \$self->[% h.vianode.table.name %]_[% h.thisviafield.name %] }
- [%- ELSIF h.type == 'many2many' %]
-4
- [%- END %]
+
+ [% ELSIF h.type == 'many2many' %]
+ [% i = 0 %]
+ [% FOREACH thisnode = h.thisnode %]
+ [% j = 0 %]
+ [% FOREACH thatnode = h.thatnode %]
+#[% thisnode.name %]::[% h.thisfield_index(i).name %] -> [% h.vianode.name %]::[% h.thisviafield_index(i).name %] ... [% h.vianode.name %]::[% h.thatviafield_index(j).name %] <- [% h.thatnode_index(j).name %]::[% h.thatfield_index(j).name %]
+sub [% h.vianode.table.name %]_[% format_fk(h.vianode,h.thisviafield_index(i).name) %]_[% format_fk(h.vianode,h.thatviafield_index(j).name) %]s { my \$self = shift; return map \$_->[% %], \$self->[% %] }
+ [% j = j + 1 %]
+
+ [% END %]
+ [% i = i + 1 %]
+ [% END %]
+ [% END %]
[% END %]
+
[% END %]
[% MACRO printHasFriendly(node) BLOCK %]
package SQL::Translator::Schema::Constraint;
# ----------------------------------------------------------------------
-# $Id: Constraint.pm,v 1.8 2003-08-21 18:11:45 kycl4rk Exp $
+# $Id: Constraint.pm,v 1.9 2003-09-25 01:31:28 allenday Exp $
# ----------------------------------------------------------------------
# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
#
use base 'Class::Base';
use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
my %VALID_CONSTRAINT_TYPE = (
PRIMARY_KEY, 1,
type => 'foreign_key', # type of table constraint
name => 'fk_phone_id', # name of the constraint
fields => 'phone_id', # field in the referring table
- reference_fields => 'phone_id', # referenced table
- reference_table => 'phone', # referenced fields
+ reference_fields => 'phone_id', # referenced field
+ reference_table => 'phone', # referenced table
match_type => 'full', # how to match
on_delete_do => 'cascade', # what to do on deletes
on_update_do => '', # what to do on updates
thisnode => $node,
thisfield => $field,
thatnode => $that,
- thatfield => ($field->foreign_key_reference->reference_fields)[0]
+ #can you believe this sh*t just to get a field obj?
+ thatfield => $self->translator->schema->get_table($field->foreign_key_reference->reference_table)->get_field(($field->foreign_key_reference->reference_fields)[0])
);
+ $node->edgecount($that->name, $node->edgecount($that->name)+1);
$node->has($that->name, $node->has($that->name)+1);
$that->many($node->name, $that->many($node->name)+1);
+ $that->edgecount($node->name, $that->edgecount($node->name)+1);
+warn $node->name . "\t" . $that->edgecount($node->name);
$node->push_edges( $edge );
$that->push_edges( $edge->flip );
}
#
# type MM relationships
#
+ #foreach linknode
foreach my $lnode (sort $self->node_values){
next if $lnode->table->is_data;
foreach my $inode1 (sort $self->node_values){
+ #linknode can't link to itself
next if $inode1 eq $lnode;
my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges;
next unless @inode1_imports;
foreach my $inode2 (sort $self->node_values){
+ #linknode can't link to itself
+ next if $inode2 eq $lnode;
+
+ #identify tables that import keys to linknode
my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges;
+
if(scalar(keys %i) == 1) {
} else {
last if $inode1 eq $inode2;
}
- next if $inode2 eq $lnode;
my @inode2_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges;
next unless @inode2_imports;
my $cedge = CompoundEdge->new();
$cedge->via($lnode);
- $cedge->push_edges( map {$_->flip} grep {$_->type eq 'import' and ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)} $lnode->edges);
+ #warn join ' ', map {$_->thisfield->name} map {$_->flip} $lnode->edges;
+ #warn join ' ', map {$_->thisfield->name} $lnode->edges;
+ #warn join ' ', map {$_->thisfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
+ #warn join ' ', map {$_->thatfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
+ $cedge->push_edges(
+ map {$_->flip}
+ grep {$_->type eq 'import'
+ and
+ ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)
+ } $lnode->edges
+ );
if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){
$cedge->type('one2one');
$inode1->push_compoundedges($cedge);
$inode2->push_compoundedges($cedge) unless $inode1 eq $inode2;
-
}
}
}
sub flip {
my $self = shift;
+
+#warn "self thisfield: ".$self->thisfield->name;
+#warn "self thatfield: ".$self->thatfield->name;
+
return SQL::Translator::Schema::Graph::Edge->new( thisfield => $self->thatfield,
thatfield => $self->thisfield,
thisnode => $self->thatnode,
'array_of_objects -class SQL::Translator::Schema::Graph::Edge' => [ qw( edges ) ],
'array_of_objects -class SQL::Translator::Schema::Graph::CompoundEdge' => [ qw( compoundedges ) ],
'array_of_objects -class SQL::Translator::Schema::Graph::HyperEdge' => [ qw( hyperedges ) ],
- 'hash' => [ qw( many via has ) ],
+ 'hash' => [ qw( many via has edgecount) ],
scalar => [ qw( base name order primary_key primary_key_accessor table ) ],
number => [ qw( order ) ],
);
package SQL::Translator::Schema::Table;
# ----------------------------------------------------------------------
-# $Id: Table.pm,v 1.19 2003-09-02 00:24:07 allenday Exp $
+# $Id: Table.pm,v 1.20 2003-09-25 01:31:28 allenday Exp $
# ----------------------------------------------------------------------
# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
#
use base 'Class::Base';
use vars qw( $VERSION $FIELD_ORDER );
-$VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.20 $ =~ /(\d+)\.(\d+)/;
# ----------------------------------------------------------------------
sub init {
}
# ----------------------------------------------------------------------
+sub is_trivial_link {
+
+=pod
+
+=head2 is_data
+
+=cut
+
+ my $self = shift;
+ return 0 if $self->is_data;
+ return $self->{'is_trivial_link'} if defined $self->{'is_trivial_link'};
+
+ $self->{'is_trivial_link'} = 1;
+
+ my %fk = ();
+
+ foreach my $field ( $self->get_fields ) {
+ next unless $field->is_foreign_key;
+ $fk{$field->foreign_key_reference->reference_table}++;
+ }
+
+ foreach my $referenced (keys %fk){
+ $self->{'is_trivial_link'} = 0 and last if $fk{$referenced} > 1;
+ }
+
+ return $self->{'is_trivial_link'};
+
+}
+
sub is_data {
=pod
$self->{'is_data'} = 0;
- my %fk = ();
-
foreach my $field ( $self->get_fields ) {
if ( !$field->is_primary_key and !$field->is_foreign_key ) {
$self->{'is_data'} = 1;
return $self->{'is_data'};
-# } elsif($field->is_foreign_key) {
-# $fk{$field->foreign_key_reference->reference_table}++;
}
}
-# foreach my $referenced (keys %fk){
-# $self->{'is_data'} = 1 and last if $fk{$referenced} > 1;
-# }
-
return $self->{'is_data'};
}
name text
);
---create table b (
--- b_id serial not null,
--- primary key (b_id),
--- name text
---);
+create table b (
+ b_id serial not null,
+ primary key (b_id),
+ name text
+);
---create table a_b (
--- a_b_id serial not null,
--- primary key (a_b_id),
--- a_id int not null,
--- foreign key (a_id) references a (a_id),
--- b_id int not null,
--- foreign key (b_id) references b (b_id)
---);
+create table a_b (
+ a_b_id serial not null,
+ primary key (a_b_id),
+ a_id int not null,
+ foreign key (a_id) references a (a_id),
+ b_id int not null,
+ foreign key (b_id) references b (b_id)
+);
---create table c (
--- c_id serial not null,
--- primary key (c_id),
--- b_id int not null,
--- foreign key (b_id) references b (b_id),
--- name text
---);
+create table c (
+ c_id serial not null,
+ primary key (c_id),
+ b_id int not null,
+ foreign key (b_id) references b (b_id),
+ name text
+);
---create table d (
--- d_id serial not null,
--- primary key (d_id),
--- c_id int not null,
--- foreign key (c_id) references c (c_id),
--- name text
---);
+create table d (
+ d_id serial not null,
+ primary key (d_id),
+ c_id int not null,
+ foreign key (c_id) references c (c_id),
+ name text
+);
create table e (
e_id serial not null,
name text
);
---create table c_e (
--- c_e_id serial not null,
--- primary key (c_e_id),
--- c_id int not null,
--- foreign key (c_id) references c (c_id),
--- e_id int not null,
--- foreign key (e_id) references e (e_id)
---);
+create table c_e (
+ c_e_id serial not null,
+ primary key (c_e_id),
+ c_id int not null,
+ foreign key (c_id) references c (c_id),
+ e_id int not null,
+ foreign key (e_id) references e (e_id)
+);
---create table f (
--- f_id serial not null,
--- primary key (f_id),
--- e1_id int not null,
--- foreign key (e1_id) references e (e_id),
--- e2_id int not null,
--- foreign key (e2_id) references e (e_id)
---);
+create table f (
+ f_id serial not null,
+ primary key (f_id),
+ el_id int not null,
+ foreign key (el_id) references e (e_id),
+ em_id int not null,
+ foreign key (em_id) references e (e_id),
+ en_id int not null,
+ foreign key (en_id) references e (e_id)
+);
create table g (
g_id serial not null,
foreign key (e2_id) references e (e_id)
);
---create table h (
--- h_id serial not null,
--- primary key (h_id),
--- a1_id int not null,
--- foreign key (a1_id) references a (a_id),
--- a2_id int not null,
--- foreign key (a2_id) references a (a_id),
--- g1_id int not null,
--- foreign key (g1_id) references g (g_id),
--- g2_id int not null,
--- foreign key (g2_id) references g (g_id)
---);
+create table h (
+ h_id serial not null,
+ primary key (h_id),
+ a1_id int not null,
+ foreign key (a1_id) references a (a_id),
+ a2_id int not null,
+ foreign key (a2_id) references a (a_id),
+ g1_id int not null,
+ foreign key (g1_id) references g (g_id),
+ g2_id int not null,
+ foreign key (g2_id) references g (g_id),
+ g3_id int not null,
+ foreign key (g3_id) references g (g_id)
+);
+