1 package SQL::Translator::Schema::Graph;
6 use base 'Class::Base';
9 local $Data::Dumper::Maxdepth = 3;
11 use SQL::Translator::Schema::Graph::Node;
12 use SQL::Translator::Schema::Graph::Edge;
13 use SQL::Translator::Schema::Graph::Port;
14 use SQL::Translator::Schema::Graph::CompoundEdge;
15 use SQL::Translator::Schema::Graph::HyperEdge;
17 use constant Node => 'SQL::Translator::Schema::Graph::Node';
18 use constant Edge => 'SQL::Translator::Schema::Graph::Edge';
19 use constant Port => 'SQL::Translator::Schema::Graph::Port';
20 use constant CompoundEdge => 'SQL::Translator::Schema::Graph::CompoundEdge';
21 use constant HyperEdge => 'SQL::Translator::Schema::Graph::HyperEdge';
23 use Class::MakeMethods::Template::Hash (
24 'new --and_then_init' => 'new',
26 'translator' => {class => 'SQL::Translator'},
28 'hash' => [ qw( node ) ],
29 'number --counter' => [ qw( order ) ],
33 $DEBUG = 0 unless defined $DEBUG;
39 # build package objects
41 foreach my $table ($self->translator->schema->get_tables){
42 die __PACKAGE__." table ".$table->name." doesn't have a primary key!" unless $table->primary_key;
43 die __PACKAGE__." table ".$table->name." can't have a composite primary key!" if ($table->primary_key->fields)[1];
45 my $node = Node->new();
47 $self->node_push($table->name => $node);
49 if ($table->is_trivial_link) { $node->is_trivial_link(1); }
50 else { $node->is_trivial_link(0); }
52 $node->order($self->order_incr());
53 $node->name( $self->translator->format_package_name($table->name) );
54 $node->table( $table );
55 $node->primary_key( ($table->primary_key->fields)[0] );
57 # Primary key may have a differenct accessor method name
58 $node->primary_key_accessor(
59 defined($self->translator->format_pk_name)
60 ? $self->translator->format_pk_name->( $node->name, $node->primary_key )
65 foreach my $node ($self->node_values){
66 foreach my $field ($node->table->get_fields){
67 if (!$field->is_foreign_key && !$field->is_primary_key) { $node->data_fields->{$field->name} = 1; }
68 elsif($field->is_foreign_key) {
69 my $that = $self->node($field->foreign_key_reference->reference_table);
71 #this means we have an incomplete schema
79 #can you believe this sh*t just to get a field obj?
80 thatfield => $self->translator->schema->get_table($field->foreign_key_reference->reference_table)->get_field(($field->foreign_key_reference->reference_fields)[0])
83 $node->edgecount($that->name, $node->edgecount($that->name)+1);
85 $node->has($that->name, $node->has($that->name)+1);
86 $that->many($node->name, $that->many($node->name)+1);
88 $that->edgecount($node->name, $that->edgecount($node->name)+1);
90 #warn "\t" . $node->name . "\t" . $node->edgecount($that->name);
91 $node->push_edges( $edge );
92 $that->push_edges( $edge->flip );
96 #warn Dumper($node->edgecount());
101 # type MM relationships
104 foreach my $lnode (sort $self->node_values){
105 next if $lnode->table->is_data;
106 foreach my $inode1 (sort $self->node_values){
107 #linknode can't link to itself
108 next if $inode1 eq $lnode;
110 my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges;
111 next unless @inode1_imports;
113 foreach my $inode2 (sort $self->node_values){
114 #linknode can't link to itself
115 next if $inode2 eq $lnode;
117 #identify tables that import keys to linknode
118 my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges;
120 if(scalar(keys %i) == 1) {
122 last if $inode1 eq $inode2;
125 my @inode2_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges;
126 next unless @inode2_imports;
128 my $cedge = CompoundEdge->new();
131 #warn join ' ', map {$_->thisfield->name} map {$_->flip} $lnode->edges;
132 #warn join ' ', map {$_->thisfield->name} $lnode->edges;
133 #warn join ' ', map {$_->thisfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
134 #warn join ' ', map {$_->thatfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
137 grep {$_->type eq 'import'
139 ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)
143 if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){
144 $cedge->type('one2one');
146 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
147 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
149 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) == 1){
150 $cedge->type('many2one');
152 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
153 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
155 elsif(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) > 1){
158 elsif(scalar(@inode1_imports) > 1 and scalar(@inode2_imports) > 1){
159 $cedge->type('many2many');
161 $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
162 $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
164 #warn Dumper($cedge);
166 $inode1->push_compoundedges($cedge);
167 $inode2->push_compoundedges($cedge) unless $inode1 eq $inode2;
168 # if($inode1->name ne $inode2->name){
169 # my $flipped_cedge = $cedge;
170 # foreach my $flipped_cedge_edge ($flipped_cedge->edges){
171 # warn Dumper $flipped_cedge_edge;
172 # warn "\t". Dumper $flipped_cedge_edge->flip;
179 my $graph = $self; #hack
184 # this code needs to move to Graph.pm
185 foreach my $node_from ($graph->node_values) {
187 next unless $node_from->table->is_data or !$node_from->table->is_trivial_link;
189 foreach my $cedge ( $node_from->compoundedges ) {
191 my $hyperedge = SQL::Translator::Schema::Graph::HyperEdge->new();
194 foreach my $edge ($cedge->edges) {
195 if ($edge->thisnode->name eq $node_from->name) {
196 $hyperedge->vianode($edge->thatnode);
198 if ($edge->thatnode->name ne $cedge->via->name) {
199 $node_to ||= $graph->node($edge->thatnode->table->name);
202 $hyperedge->push_thisnode($edge->thisnode);
203 $hyperedge->push_thisfield($edge->thisfield);
204 $hyperedge->push_thisviafield($edge->thatfield);
207 if ($edge->thisnode->name ne $cedge->via->name) {
208 $node_to ||= $graph->node($edge->thisnode->table->name);
210 $hyperedge->push_thatnode($edge->thisnode);
211 $hyperedge->push_thatfield($edge->thisfield);
212 $hyperedge->push_thatviafield($edge->thatfield);
214 $self->debug($edge->thisfield->name);
215 $self->debug($edge->thatfield->name);
218 if ($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode == 1) {
219 $hyperedge->type('one2one');
220 } elsif ($hyperedge->count_thisnode > 1 and $hyperedge->count_thatnode == 1) {
221 $hyperedge->type('many2one');
222 } elsif ($hyperedge->count_thisnode == 1 and $hyperedge->count_thatnode > 1) {
223 $hyperedge->type('one2many');
224 } elsif ($hyperedge->count_thisnode > 1 and $hyperedge->count_thatnode > 1) {
225 $hyperedge->type('many2many');
228 $self->debug($_) foreach sort keys %::SQL::Translator::Schema::Graph::HyperEdge::;
230 #node_to won't always be defined b/c of multiple edges to a single other node
231 if (defined($node_to)) {
232 $self->debug($node_from->name);
233 $self->debug($node_to->name);
235 if (scalar($hyperedge->thisnode) > 1) {
236 $self->debug($hyperedge->type ." via ". $hyperedge->vianode->name);
238 foreach my $thisnode ( $hyperedge->thisnode ) {
239 $self->debug($thisnode->name .' '.
240 $hyperedge->thisfield_index(0)->name .' -> '.
241 $hyperedge->thisviafield_index($i)->name .' '.
242 $hyperedge->vianode->name .' '.
243 $hyperedge->thatviafield_index(0)->name .' <- '.
244 $hyperedge->thatfield_index(0)->name .' '.
245 $hyperedge->thatnode_index(0)->name ."\n"
250 #warn Dumper($hyperedge) if $hyperedge->type eq 'many2many';
251 $node_from->push_hyperedges($hyperedge);