1 package SQL::Translator::Schema::Graph;
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'
14 use base 'Class::Base';
17 local $Data::Dumper::Maxdepth = 3;
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;
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';
31 use Class::MakeMethods::Template::Hash (
32 'new --and_then_init' => 'new',
34 'translator' => {class => 'SQL::Translator'},
36 'hash' => [ qw( node ) ],
37 'number --counter' => [ qw( order ) ],
41 $DEBUG = 0 unless defined $DEBUG;
47 # build package objects
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];
53 my $node = Node->new();
55 $self->node_push($table->name => $node);
57 if ($table->is_trivial_link) { $node->is_trivial_link(1); }
58 else { $node->is_trivial_link(0); }
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] );
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 )
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);
79 #this means we have an incomplete schema
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])
91 $node->edgecount($that->name, $node->edgecount($that->name)+1);
93 $node->has($that->name, $node->has($that->name)+1);
94 $that->many($node->name, $that->many($node->name)+1);
96 $that->edgecount($node->name, $that->edgecount($node->name)+1);
98 #warn "\t" . $node->name . "\t" . $node->edgecount($that->name);
99 $node->push_edges( $edge );
100 $that->push_edges( $edge->flip );
104 #warn Dumper($node->edgecount());
109 # type MM relationships
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;
118 my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges;
119 next unless @inode1_imports;
121 foreach my $inode2 (sort $self->node_values){
122 #linknode can't link to itself
123 next if $inode2 eq $lnode;
125 #identify tables that import keys to linknode
126 my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges;
128 if(scalar(keys %i) == 1) {
130 last if $inode1 eq $inode2;
133 my @inode2_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges;
134 next unless @inode2_imports;
136 my $cedge = CompoundEdge->new();
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;
145 grep {$_->type eq 'import'
147 ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)
151 if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){
152 $cedge->type('one2one');
154 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
155 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
157 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) == 1){
158 $cedge->type('many2one');
160 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
161 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
163 elsif(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) > 1){
166 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) > 1){
167 $cedge->type('many2many');
169 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
170 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
172 #warn Dumper($cedge);
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;
187 my $graph = $self; #hack
192 # this code needs to move to Graph.pm
193 foreach my $node_from ($graph->node_values) {
195 next unless $node_from->table->is_data or !$node_from->table->is_trivial_link;
197 foreach my $cedge ( $node_from->compoundedges ) {
199 my $hyperedge = SQL::Translator::Schema::Graph::HyperEdge->new();
202 foreach my $edge ($cedge->edges) {
203 if ($edge->thisnode->name eq $node_from->name) {
204 $hyperedge->vianode($edge->thatnode);
206 if ($edge->thatnode->name ne $cedge->via->name) {
207 $node_to ||= $graph->node($edge->thatnode->table->name);
210 $hyperedge->push_thisnode($edge->thisnode);
211 $hyperedge->push_thisfield($edge->thisfield);
212 $hyperedge->push_thisviafield($edge->thatfield);
215 if ($edge->thisnode->name ne $cedge->via->name) {
216 $node_to ||= $graph->node($edge->thisnode->table->name);
218 $hyperedge->push_thatnode($edge->thisnode);
219 $hyperedge->push_thatfield($edge->thisfield);
220 $hyperedge->push_thatviafield($edge->thatfield);
222 $self->debug($edge->thisfield->name);
223 $self->debug($edge->thatfield->name);
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');
236 $self->debug($_) foreach sort keys %::SQL::Translator::Schema::Graph::HyperEdge::;
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);
243 if (scalar($hyperedge->thisnode) > 1) {
244 $self->debug($hyperedge->type ." via ". $hyperedge->vianode->name);
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"
258 #warn Dumper($hyperedge) if $hyperedge->type eq 'many2many';
259 $node_from->push_hyperedges($hyperedge);