An update of the Turnkey producer to output with better css support. May be slightly...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Graph.pm
CommitLineData
0caaf4c3 1package SQL::Translator::Schema::Graph;
2
3use strict;
4
5use Data::Dumper;
6
7use SQL::Translator::Schema::Graph::Node;
8use SQL::Translator::Schema::Graph::Edge;
9use SQL::Translator::Schema::Graph::Port;
10use SQL::Translator::Schema::Graph::CompoundEdge;
11use SQL::Translator::Schema::Graph::HyperEdge;
12
13use constant Node => 'SQL::Translator::Schema::Graph::Node';
14use constant Edge => 'SQL::Translator::Schema::Graph::Edge';
15use constant Port => 'SQL::Translator::Schema::Graph::Port';
16use constant CompoundEdge => 'SQL::Translator::Schema::Graph::CompoundEdge';
17use constant HyperEdge => 'SQL::Translator::Schema::Graph::HyperEdge';
18
19use Class::MakeMethods::Template::Hash (
20 'new --and_then_init' => 'new',
21 object => [
22 'translator' => {class => 'SQL::Translator'},
23 ],
24 'hash' => [ qw( node ) ],
25 'scalar' => [ qw( baseclass ) ],
26 'number --counter' => [ qw( order ) ],
27);
28
29sub init {
30 my $self = shift;
31 #
32 # build package objects
33 #
34 foreach my $table ($self->translator->schema->get_tables){
35 die __PACKAGE__." table ".$table->name." doesn't have a primary key!" unless $table->primary_key;
36 die __PACKAGE__." table ".$table->name." can't have a composite primary key!" if ($table->primary_key->fields)[1];
37
38 my $node = Node->new();
39
40 $self->node_push($table->name => $node);
41
a0249514 42 if ($table->is_trivial_link) { $node->is_trivial_link(1); }
43 else { $node->is_trivial_link(0); }
44
0caaf4c3 45 $node->order($self->order_incr());
46 $node->name( $self->translator->format_package_name($table->name) );
47 $node->base( $self->baseclass );
48 $node->table( $table );
49 $node->primary_key( ($table->primary_key->fields)[0] );
50
51 # Primary key may have a differenct accessor method name
52 $node->primary_key_accessor(
53 defined($self->translator->format_pk_name)
54 ? $self->translator->format_pk_name->( $node->name, $node->primary_key )
55 : undef
56 );
57 }
58
59 foreach my $node ($self->node_values){
60 foreach my $field ($node->table->get_fields){
a0249514 61 if (!$field->is_foreign_key && !$field->is_primary_key) { $node->data_fields->{$field->name} = 1; }
62 elsif($field->is_foreign_key) {
0caaf4c3 63 my $that = $self->node($field->foreign_key_reference->reference_table);
64
65 #this means we have an incomplete schema
66 next unless $that;
67
68 my $edge = Edge->new(
69 type => 'import',
70 thisnode => $node,
71 thisfield => $field,
72 thatnode => $that,
65157eda 73 #can you believe this sh*t just to get a field obj?
74 thatfield => $self->translator->schema->get_table($field->foreign_key_reference->reference_table)->get_field(($field->foreign_key_reference->reference_fields)[0])
0caaf4c3 75 );
76
7a1eb8c0 77# $node->edgecount($that->name, $node->edgecount($that->name)+1);
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
7a1eb8c0 83# $that->edgecount($node->name, $that->edgecount($node->name)+1);
65157eda 84 $that->edgecount($node->name, $that->edgecount($node->name)+1);
7a1eb8c0 85
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 }
160
161 $inode1->push_compoundedges($cedge);
162 $inode2->push_compoundedges($cedge) unless $inode1 eq $inode2;
0caaf4c3 163 }
164 }
165 }
166}
167
1681;