1 package SQL::Translator::Schema::Graph;
6 $Data::Dumper::Maxdepth = 3;
8 use SQL::Translator::Schema::Graph::Node;
9 use SQL::Translator::Schema::Graph::Edge;
10 use SQL::Translator::Schema::Graph::Port;
11 use SQL::Translator::Schema::Graph::CompoundEdge;
12 use SQL::Translator::Schema::Graph::HyperEdge;
14 use constant Node => 'SQL::Translator::Schema::Graph::Node';
15 use constant Edge => 'SQL::Translator::Schema::Graph::Edge';
16 use constant Port => 'SQL::Translator::Schema::Graph::Port';
17 use constant CompoundEdge => 'SQL::Translator::Schema::Graph::CompoundEdge';
18 use constant HyperEdge => 'SQL::Translator::Schema::Graph::HyperEdge';
20 use Class::MakeMethods::Template::Hash (
21 'new --and_then_init' => 'new',
23 'translator' => {class => 'SQL::Translator'},
25 'hash' => [ qw( node ) ],
26 'scalar' => [ qw( baseclass ) ],
27 'number --counter' => [ qw( order ) ],
33 # build package objects
35 foreach my $table ($self->translator->schema->get_tables){
36 die __PACKAGE__." table ".$table->name." doesn't have a primary key!" unless $table->primary_key;
37 die __PACKAGE__." table ".$table->name." can't have a composite primary key!" if ($table->primary_key->fields)[1];
39 my $node = Node->new();
41 $self->node_push($table->name => $node);
43 if ($table->is_trivial_link) { $node->is_trivial_link(1); }
44 else { $node->is_trivial_link(0); }
46 $node->order($self->order_incr());
47 $node->name( $self->translator->format_package_name($table->name) );
48 $node->base( $self->baseclass );
49 $node->table( $table );
50 $node->primary_key( ($table->primary_key->fields)[0] );
52 # Primary key may have a differenct accessor method name
53 $node->primary_key_accessor(
54 defined($self->translator->format_pk_name)
55 ? $self->translator->format_pk_name->( $node->name, $node->primary_key )
60 foreach my $node ($self->node_values){
61 foreach my $field ($node->table->get_fields){
62 if (!$field->is_foreign_key && !$field->is_primary_key) { $node->data_fields->{$field->name} = 1; }
63 elsif($field->is_foreign_key) {
64 my $that = $self->node($field->foreign_key_reference->reference_table);
66 #this means we have an incomplete schema
74 #can you believe this sh*t just to get a field obj?
75 thatfield => $self->translator->schema->get_table($field->foreign_key_reference->reference_table)->get_field(($field->foreign_key_reference->reference_fields)[0])
78 $node->edgecount($that->name, $node->edgecount($that->name)+1);
80 $node->has($that->name, $node->has($that->name)+1);
81 $that->many($node->name, $that->many($node->name)+1);
83 $that->edgecount($node->name, $that->edgecount($node->name)+1);
85 #warn "\t" . $node->name . "\t" . $node->edgecount($that->name);
86 $node->push_edges( $edge );
87 $that->push_edges( $edge->flip );
91 #warn Dumper($node->edgecount());
96 # type MM relationships
99 foreach my $lnode (sort $self->node_values){
100 next if $lnode->table->is_data;
101 foreach my $inode1 (sort $self->node_values){
102 #linknode can't link to itself
103 next if $inode1 eq $lnode;
105 my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges;
106 next unless @inode1_imports;
108 foreach my $inode2 (sort $self->node_values){
109 #linknode can't link to itself
110 next if $inode2 eq $lnode;
112 #identify tables that import keys to linknode
113 my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges;
115 if(scalar(keys %i) == 1) {
117 last if $inode1 eq $inode2;
120 my @inode2_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges;
121 next unless @inode2_imports;
123 my $cedge = CompoundEdge->new();
126 #warn join ' ', map {$_->thisfield->name} map {$_->flip} $lnode->edges;
127 #warn join ' ', map {$_->thisfield->name} $lnode->edges;
128 #warn join ' ', map {$_->thisfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
129 #warn join ' ', map {$_->thatfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
132 grep {$_->type eq 'import'
134 ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)
138 if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){
139 $cedge->type('one2one');
141 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
142 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
144 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) == 1){
145 $cedge->type('many2one');
147 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
148 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
150 elsif(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) > 1){
153 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) > 1){
154 $cedge->type('many2many');
156 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
157 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
159 #warn Dumper($cedge);
161 $inode1->push_compoundedges($cedge);
162 $inode2->push_compoundedges($cedge) unless $inode1 eq $inode2;
163 # if($inode1->name ne $inode2->name){
164 # my $flipped_cedge = $cedge;
165 # foreach my $flipped_cedge_edge ($flipped_cedge->edges){
166 # warn Dumper $flipped_cedge_edge;
167 # warn "\t". Dumper $flipped_cedge_edge->flip;