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