1 package # hide from pause
2 SQL::Translator::Schema::Graph;
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'
15 use base 'Class::Base';
18 local $Data::Dumper::Maxdepth = 3;
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;
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';
32 use Class::MakeMethods::Template::Hash (
33 'new --and_then_init' => 'new',
35 'translator' => {class => 'SQL::Translator'},
37 'hash' => [ qw( node ) ],
38 'number --counter' => [ qw( order ) ],
42 $DEBUG = 0 unless defined $DEBUG;
48 # build package objects
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];
54 my $node = Node->new();
56 $self->node_push($table->name => $node);
58 if ($table->is_trivial_link) { $node->is_trivial_link(1); }
59 else { $node->is_trivial_link(0); }
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] );
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 )
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);
80 #this means we have an incomplete schema
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])
92 $node->edgecount($that->name, $node->edgecount($that->name)+1);
94 $node->has($that->name, $node->has($that->name)+1);
95 $that->many($node->name, $that->many($node->name)+1);
97 $that->edgecount($node->name, $that->edgecount($node->name)+1);
99 #warn "\t" . $node->name . "\t" . $node->edgecount($that->name);
100 $node->push_edges( $edge );
101 $that->push_edges( $edge->flip );
105 #warn Dumper($node->edgecount());
110 # type MM relationships
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;
119 my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges;
120 next unless @inode1_imports;
122 foreach my $inode2 (sort $self->node_values){
123 #linknode can't link to itself
124 next if $inode2 eq $lnode;
126 #identify tables that import keys to linknode
127 my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges;
129 if(scalar(keys %i) == 1) {
131 last if $inode1 eq $inode2;
134 my @inode2_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges;
135 next unless @inode2_imports;
137 my $cedge = CompoundEdge->new();
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;
146 grep {$_->type eq 'import'
148 ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)
152 if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){
153 $cedge->type('one2one');
155 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
156 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
158 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) == 1){
159 $cedge->type('many2one');
161 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
162 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
164 elsif(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) > 1){
167 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) > 1){
168 $cedge->type('many2many');
170 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
171 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
173 #warn Dumper($cedge);
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;
188 my $graph = $self; #hack
193 # this code needs to move to Graph.pm
194 foreach my $node_from ($graph->node_values) {
196 next unless $node_from->table->is_data or !$node_from->table->is_trivial_link;
198 foreach my $cedge ( $node_from->compoundedges ) {
200 my $hyperedge = SQL::Translator::Schema::Graph::HyperEdge->new();
203 foreach my $edge ($cedge->edges) {
204 if ($edge->thisnode->name eq $node_from->name) {
205 $hyperedge->vianode($edge->thatnode);
207 if ($edge->thatnode->name ne $cedge->via->name) {
208 $node_to ||= $graph->node($edge->thatnode->table->name);
211 $hyperedge->push_thisnode($edge->thisnode);
212 $hyperedge->push_thisfield($edge->thisfield);
213 $hyperedge->push_thisviafield($edge->thatfield);
216 if ($edge->thisnode->name ne $cedge->via->name) {
217 $node_to ||= $graph->node($edge->thisnode->table->name);
219 $hyperedge->push_thatnode($edge->thisnode);
220 $hyperedge->push_thatfield($edge->thisfield);
221 $hyperedge->push_thatviafield($edge->thatfield);
223 $self->debug($edge->thisfield->name);
224 $self->debug($edge->thatfield->name);
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');
237 $self->debug($_) foreach sort keys %::SQL::Translator::Schema::Graph::HyperEdge::;
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);
244 if (scalar($hyperedge->thisnode) > 1) {
245 $self->debug($hyperedge->type ." via ". $hyperedge->vianode->name);
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"
259 #warn Dumper($hyperedge) if $hyperedge->type eq 'many2many';
260 $node_from->push_hyperedges($hyperedge);