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