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 | |
8 | use SQL::Translator::Schema::Graph::Node; |
9 | use SQL::Translator::Schema::Graph::Edge; |
10 | use SQL::Translator::Schema::Graph::Port; |
11 | use SQL::Translator::Schema::Graph::CompoundEdge; |
12 | use SQL::Translator::Schema::Graph::HyperEdge; |
13 | |
14 | use constant Node => 'SQL::Translator::Schema::Graph::Node'; |
15 | use constant Edge => 'SQL::Translator::Schema::Graph::Edge'; |
16 | use constant Port => 'SQL::Translator::Schema::Graph::Port'; |
17 | use constant CompoundEdge => 'SQL::Translator::Schema::Graph::CompoundEdge'; |
18 | use constant HyperEdge => 'SQL::Translator::Schema::Graph::HyperEdge'; |
19 | |
20 | use Class::MakeMethods::Template::Hash ( |
21 | 'new --and_then_init' => 'new', |
22 | object => [ |
23 | 'translator' => {class => 'SQL::Translator'}, |
24 | ], |
25 | 'hash' => [ qw( node ) ], |
26 | 'scalar' => [ qw( baseclass ) ], |
27 | 'number --counter' => [ qw( order ) ], |
28 | ); |
29 | |
30 | sub init { |
31 | my $self = shift; |
32 | # |
33 | # build package objects |
34 | # |
35 | foreach my $table ($self->translator->schema->get_tables){ |
36 | die __PACKAGE__." table ".$table->name." doesn't have a primary key!" unless $table->primary_key; |
37 | die __PACKAGE__." table ".$table->name." can't have a composite primary key!" if ($table->primary_key->fields)[1]; |
38 | |
39 | my $node = Node->new(); |
40 | |
41 | $self->node_push($table->name => $node); |
42 | |
a0249514 |
43 | if ($table->is_trivial_link) { $node->is_trivial_link(1); } |
44 | else { $node->is_trivial_link(0); } |
45 | |
0caaf4c3 |
46 | $node->order($self->order_incr()); |
47 | $node->name( $self->translator->format_package_name($table->name) ); |
48 | $node->base( $self->baseclass ); |
49 | $node->table( $table ); |
50 | $node->primary_key( ($table->primary_key->fields)[0] ); |
51 | |
52 | # Primary key may have a differenct accessor method name |
53 | $node->primary_key_accessor( |
54 | defined($self->translator->format_pk_name) |
55 | ? $self->translator->format_pk_name->( $node->name, $node->primary_key ) |
56 | : undef |
57 | ); |
58 | } |
59 | |
60 | foreach my $node ($self->node_values){ |
61 | foreach my $field ($node->table->get_fields){ |
a0249514 |
62 | if (!$field->is_foreign_key && !$field->is_primary_key) { $node->data_fields->{$field->name} = 1; } |
63 | elsif($field->is_foreign_key) { |
0caaf4c3 |
64 | my $that = $self->node($field->foreign_key_reference->reference_table); |
65 | |
66 | #this means we have an incomplete schema |
67 | next unless $that; |
68 | |
69 | my $edge = Edge->new( |
70 | type => 'import', |
71 | thisnode => $node, |
72 | thisfield => $field, |
73 | thatnode => $that, |
65157eda |
74 | #can you believe this sh*t just to get a field obj? |
75 | thatfield => $self->translator->schema->get_table($field->foreign_key_reference->reference_table)->get_field(($field->foreign_key_reference->reference_fields)[0]) |
0caaf4c3 |
76 | ); |
77 | |
65157eda |
78 | $node->edgecount($that->name, $node->edgecount($that->name)+1); |
0caaf4c3 |
79 | |
80 | $node->has($that->name, $node->has($that->name)+1); |
81 | $that->many($node->name, $that->many($node->name)+1); |
82 | |
65157eda |
83 | $that->edgecount($node->name, $that->edgecount($node->name)+1); |
7a1eb8c0 |
84 | |
85 | #warn "\t" . $node->name . "\t" . $node->edgecount($that->name); |
0caaf4c3 |
86 | $node->push_edges( $edge ); |
87 | $that->push_edges( $edge->flip ); |
a0249514 |
88 | } |
0caaf4c3 |
89 | } |
7a1eb8c0 |
90 | |
91 | #warn Dumper($node->edgecount()); |
92 | #warn "*****"; |
0caaf4c3 |
93 | } |
94 | |
95 | # |
96 | # type MM relationships |
97 | # |
65157eda |
98 | #foreach linknode |
0caaf4c3 |
99 | foreach my $lnode (sort $self->node_values){ |
100 | next if $lnode->table->is_data; |
101 | foreach my $inode1 (sort $self->node_values){ |
65157eda |
102 | #linknode can't link to itself |
0caaf4c3 |
103 | next if $inode1 eq $lnode; |
104 | |
105 | my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges; |
106 | next unless @inode1_imports; |
107 | |
108 | foreach my $inode2 (sort $self->node_values){ |
65157eda |
109 | #linknode can't link to itself |
110 | next if $inode2 eq $lnode; |
111 | |
112 | #identify tables that import keys to linknode |
0caaf4c3 |
113 | my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges; |
65157eda |
114 | |
0caaf4c3 |
115 | if(scalar(keys %i) == 1) { |
116 | } else { |
117 | last if $inode1 eq $inode2; |
118 | } |
119 | |
0caaf4c3 |
120 | my @inode2_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges; |
121 | next unless @inode2_imports; |
122 | |
123 | my $cedge = CompoundEdge->new(); |
124 | $cedge->via($lnode); |
125 | |
65157eda |
126 | #warn join ' ', map {$_->thisfield->name} map {$_->flip} $lnode->edges; |
127 | #warn join ' ', map {$_->thisfield->name} $lnode->edges; |
128 | #warn join ' ', map {$_->thisfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges; |
129 | #warn join ' ', map {$_->thatfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges; |
130 | $cedge->push_edges( |
131 | map {$_->flip} |
132 | grep {$_->type eq 'import' |
133 | and |
134 | ($_->thatnode eq $inode1 or $_->thatnode eq $inode2) |
135 | } $lnode->edges |
136 | ); |
0caaf4c3 |
137 | |
138 | if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){ |
139 | $cedge->type('one2one'); |
140 | |
141 | $inode1->via($inode2->name,$inode1->via($inode2->name)+1); |
142 | $inode2->via($inode1->name,$inode2->via($inode1->name)+1); |
143 | } |
144 | elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) == 1){ |
145 | $cedge->type('many2one'); |
146 | |
147 | $inode1->via($inode2->name,$inode1->via($inode2->name)+1); |
148 | $inode2->via($inode1->name,$inode2->via($inode1->name)+1); |
149 | } |
150 | elsif(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) > 1){ |
151 | #handled above |
152 | } |
153 | elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) > 1){ |
154 | $cedge->type('many2many'); |
155 | |
156 | $inode1->via($inode2->name,$inode1->via($inode2->name)+1); |
157 | $inode2->via($inode1->name,$inode2->via($inode1->name)+1); |
158 | } |
0be4a1f8 |
159 | #warn Dumper($cedge); |
0caaf4c3 |
160 | |
161 | $inode1->push_compoundedges($cedge); |
162 | $inode2->push_compoundedges($cedge) unless $inode1 eq $inode2; |
0be4a1f8 |
163 | # if($inode1->name ne $inode2->name){ |
164 | # my $flipped_cedge = $cedge; |
165 | # foreach my $flipped_cedge_edge ($flipped_cedge->edges){ |
166 | # warn Dumper $flipped_cedge_edge; |
167 | # warn "\t". Dumper $flipped_cedge_edge->flip; |
168 | # } |
169 | # } |
0caaf4c3 |
170 | } |
171 | } |
172 | } |
173 | } |
174 | |
175 | 1; |