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