take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Graph.pm
1 package SQL::Translator::Schema::Graph;
2
3 use strict;
4
5 use base 'Class::Base';
6
7 use Data::Dumper;
8 local $Data::Dumper::Maxdepth = 3;
9
10 use SQL::Translator::Schema::Graph::Node;
11 use SQL::Translator::Schema::Graph::Edge;
12 use SQL::Translator::Schema::Graph::Port;
13 use SQL::Translator::Schema::Graph::CompoundEdge;
14 use SQL::Translator::Schema::Graph::HyperEdge;
15
16 use constant Node => 'SQL::Translator::Schema::Graph::Node';
17 use constant Edge => 'SQL::Translator::Schema::Graph::Edge';
18 use constant Port => 'SQL::Translator::Schema::Graph::Port';
19 use constant CompoundEdge => 'SQL::Translator::Schema::Graph::CompoundEdge';
20 use constant HyperEdge => 'SQL::Translator::Schema::Graph::HyperEdge';
21
22 use Class::MakeMethods::Template::Hash (
23   'new --and_then_init' => 'new',
24   object => [
25    'translator' => {class => 'SQL::Translator'},
26   ],
27   'hash' => [ qw( node ) ],
28   'number --counter' => [ qw( order ) ],
29 );
30
31 use vars qw/$DEBUG/;
32 $DEBUG = 0 unless defined $DEBUG;
33
34 sub init {
35   my $self = shift;
36
37   #
38   # build package objects
39   #
40   foreach my $table ($self->translator->schema->get_tables){
41    die __PACKAGE__." table ".$table->name." doesn't have a primary key!" unless $table->primary_key;
42    die __PACKAGE__." table ".$table->name." can't have a composite primary key!" if ($table->primary_key->fields)[1];
43
44    my $node = Node->new();
45
46    $self->node_push($table->name => $node);
47
48    if ($table->is_trivial_link) { $node->is_trivial_link(1); }
49    else { $node->is_trivial_link(0); }
50
51    $node->order($self->order_incr());
52    $node->name( $self->translator->format_package_name($table->name) );
53    $node->table( $table );
54    $node->primary_key( ($table->primary_key->fields)[0] );
55
56    # Primary key may have a differenct accessor method name
57    $node->primary_key_accessor(
58                         defined($self->translator->format_pk_name)
59                         ? $self->translator->format_pk_name->( $node->name, $node->primary_key )
60                         : undef
61                         );
62   }
63
64   foreach my $node ($self->node_values){
65    foreach my $field ($node->table->get_fields){
66      if (!$field->is_foreign_key && !$field->is_primary_key) { $node->data_fields->{$field->name} = 1; }
67      elsif($field->is_foreign_key) {
68      my $that = $self->node($field->foreign_key_reference->reference_table);
69
70      #this means we have an incomplete schema
71      next unless $that;
72
73      my $edge = Edge->new(
74                      type => 'import',
75                      thisnode => $node,
76                      thisfield => $field,
77                      thatnode => $that,
78                      #can you believe this sh*t just to get a field obj?
79                      thatfield => $self->translator->schema->get_table($field->foreign_key_reference->reference_table)->get_field(($field->foreign_key_reference->reference_fields)[0])
80                     );
81
82      $node->edgecount($that->name, $node->edgecount($that->name)+1);
83
84      $node->has($that->name, $node->has($that->name)+1);
85      $that->many($node->name, $that->many($node->name)+1);
86
87      $that->edgecount($node->name, $that->edgecount($node->name)+1);
88
89           #warn "\t" . $node->name . "\t" . $node->edgecount($that->name);
90      $node->push_edges( $edge );
91      $that->push_edges( $edge->flip );
92       }
93    }
94
95     #warn Dumper($node->edgecount());
96     #warn "*****";
97   }
98
99   #
100   # type MM relationships
101   #
102   #foreach linknode
103   foreach my $lnode (sort $self->node_values){
104    next if $lnode->table->is_data;
105    foreach my $inode1 (sort $self->node_values){
106      #linknode can't link to itself
107      next if $inode1 eq $lnode;
108
109      my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges;
110      next unless @inode1_imports;
111
112      foreach my $inode2 (sort $self->node_values){
113       #linknode can't link to itself
114       next if $inode2 eq $lnode;
115
116       #identify tables that import keys to linknode
117       my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges;
118
119       if(scalar(keys %i) == 1) {
120       } else {
121         last if $inode1 eq $inode2;
122       }
123
124       my @inode2_imports =  grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges;
125       next unless @inode2_imports;
126
127       my $cedge = CompoundEdge->new();
128       $cedge->via($lnode);
129
130       #warn join ' ', map {$_->thisfield->name} map {$_->flip} $lnode->edges;
131       #warn join ' ', map {$_->thisfield->name} $lnode->edges;
132       #warn join ' ', map {$_->thisfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
133       #warn join ' ', map {$_->thatfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
134       $cedge->push_edges(
135                      map {$_->flip}
136                      grep {$_->type eq 'import'
137                            and
138                          ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)
139                          } $lnode->edges
140                     );
141
142       if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){
143         $cedge->type('one2one');
144
145         $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
146         $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
147       }
148       elsif(scalar(@inode1_imports)  > 1 and scalar(@inode2_imports) == 1){
149         $cedge->type('many2one');
150
151         $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
152         $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
153       }
154       elsif(scalar(@inode1_imports) == 1 and scalar(@inode2_imports)  > 1){
155         #handled above
156       }
157       elsif(scalar(@inode1_imports)  > 1 and scalar(@inode2_imports)  > 1){
158         $cedge->type('many2many');
159
160         $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
161         $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
162       }
163 #warn Dumper($cedge);
164
165       $inode1->push_compoundedges($cedge);
166       $inode2->push_compoundedges($cedge) unless $inode1 eq $inode2;
167 #        if($inode1->name ne $inode2->name){
168 #          my $flipped_cedge = $cedge;
169 #          foreach my $flipped_cedge_edge ($flipped_cedge->edges){
170 #            warn Dumper $flipped_cedge_edge;
171 #            warn "\t". Dumper $flipped_cedge_edge->flip;
172 #          }
173 #        }
174      }
175    }
176   }
177
178   my $graph = $self; #hack
179
180   #
181   # create methods
182   #
183   # this code needs to move to Graph.pm
184   foreach my $node_from ($graph->node_values) {
185
186     next unless $node_from->table->is_data or !$node_from->table->is_trivial_link;
187
188     foreach my $cedge ( $node_from->compoundedges ) {
189
190       my $hyperedge = SQL::Translator::Schema::Graph::HyperEdge->new();
191
192       my $node_to;
193       foreach my $edge ($cedge->edges) {
194         if ($edge->thisnode->name eq $node_from->name) {
195           $hyperedge->vianode($edge->thatnode);
196
197           if ($edge->thatnode->name ne $cedge->via->name) {
198             $node_to ||= $graph->node($edge->thatnode->table->name);
199           }
200
201           $hyperedge->push_thisnode($edge->thisnode);
202           $hyperedge->push_thisfield($edge->thisfield);
203           $hyperedge->push_thisviafield($edge->thatfield);
204
205         } else {
206           if ($edge->thisnode->name ne $cedge->via->name) {
207             $node_to ||= $graph->node($edge->thisnode->table->name);
208           }
209           $hyperedge->push_thatnode($edge->thisnode);
210           $hyperedge->push_thatfield($edge->thisfield);
211           $hyperedge->push_thatviafield($edge->thatfield);
212         }
213         $self->debug($edge->thisfield->name);
214         $self->debug($edge->thatfield->name);
215       }
216
217       if ($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode == 1) {
218         $hyperedge->type('one2one');
219       } elsif ($hyperedge->count_thisnode  > 1 and $hyperedge->count_thatnode == 1) {
220         $hyperedge->type('many2one');
221       } elsif ($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode  > 1) {
222         $hyperedge->type('one2many');
223       } elsif ($hyperedge->count_thisnode  > 1 and $hyperedge->count_thatnode  > 1) {
224         $hyperedge->type('many2many');
225       }
226
227       $self->debug($_) foreach sort keys %::SQL::Translator::Schema::Graph::HyperEdge::;
228
229       #node_to won't always be defined b/c of multiple edges to a single other node
230       if (defined($node_to)) {
231         $self->debug($node_from->name);
232         $self->debug($node_to->name);
233
234         if (scalar($hyperedge->thisnode) > 1) {
235           $self->debug($hyperedge->type ." via ". $hyperedge->vianode->name);
236           my $i = 0;
237           foreach my $thisnode ( $hyperedge->thisnode ) {
238             $self->debug($thisnode->name .' '.
239                         $hyperedge->thisfield_index(0)->name .' -> '.
240                         $hyperedge->thisviafield_index($i)->name .' '.
241                         $hyperedge->vianode->name .' '.
242                         $hyperedge->thatviafield_index(0)->name .' <- '.
243                         $hyperedge->thatfield_index(0)->name .' '.
244                         $hyperedge->thatnode_index(0)->name ."\n"
245                        );
246             $i++;
247           }
248         }
249         #warn Dumper($hyperedge) if $hyperedge->type eq 'many2many';
250         $node_from->push_hyperedges($hyperedge);
251       }
252     }
253   }
254
255 }
256
257 1;