this is pretty stable, and is ( i think ) generating usable class::dbi
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Graph.pm
1 package SQL::Translator::Schema::Graph;
2
3 use strict;
4
5 use Data::Dumper;
6
7 use SQL::Translator::Schema::Graph::Node;
8 use SQL::Translator::Schema::Graph::Edge;
9 use SQL::Translator::Schema::Graph::Port;
10 use SQL::Translator::Schema::Graph::CompoundEdge;
11 use SQL::Translator::Schema::Graph::HyperEdge;
12
13 use constant Node => 'SQL::Translator::Schema::Graph::Node';
14 use constant Edge => 'SQL::Translator::Schema::Graph::Edge';
15 use constant Port => 'SQL::Translator::Schema::Graph::Port';
16 use constant CompoundEdge => 'SQL::Translator::Schema::Graph::CompoundEdge';
17 use constant HyperEdge => 'SQL::Translator::Schema::Graph::HyperEdge';
18
19 use 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
29 sub 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
42         $node->order($self->order_incr());
43         $node->name( $self->translator->format_package_name($table->name) );
44         $node->base( $self->baseclass );
45         $node->table( $table );
46         $node->primary_key( ($table->primary_key->fields)[0] );
47
48         # Primary key may have a differenct accessor method name
49         $node->primary_key_accessor(
50                                                                 defined($self->translator->format_pk_name)
51                                                                 ? $self->translator->format_pk_name->( $node->name, $node->primary_key )
52                                                                 : undef
53                                                            );
54   }
55
56   foreach my $node ($self->node_values){
57         foreach my $field ($node->table->get_fields){
58           next unless $field->is_foreign_key;
59
60           my $that = $self->node($field->foreign_key_reference->reference_table);
61
62           #this means we have an incomplete schema
63           next unless $that;
64
65           my $edge = Edge->new(
66                                                    type => 'import',
67                                                    thisnode => $node,
68                                                    thisfield => $field,
69                                                    thatnode => $that,
70                                                    #can you believe this sh*t just to get a field obj?
71                                                    thatfield => $self->translator->schema->get_table($field->foreign_key_reference->reference_table)->get_field(($field->foreign_key_reference->reference_fields)[0])
72                                                   );
73
74           $node->edgecount($that->name, $node->edgecount($that->name)+1);
75
76           $node->has($that->name, $node->has($that->name)+1);
77           $that->many($node->name, $that->many($node->name)+1);
78
79           $that->edgecount($node->name, $that->edgecount($node->name)+1);
80 warn $node->name . "\t" . $that->edgecount($node->name);
81           $node->push_edges( $edge );
82           $that->push_edges( $edge->flip );
83         }
84   }
85
86   #
87   # type MM relationships
88   #
89   #foreach linknode
90   foreach my $lnode (sort $self->node_values){
91         next if $lnode->table->is_data;
92         foreach my $inode1 (sort $self->node_values){
93           #linknode can't link to itself
94           next if $inode1 eq $lnode;
95
96           my @inode1_imports = grep { $_->type eq 'import' and $_->thatnode eq $inode1 } $lnode->edges;
97           next unless @inode1_imports;
98
99           foreach my $inode2 (sort $self->node_values){
100                 #linknode can't link to itself
101                 next if $inode2 eq $lnode;
102
103                 #identify tables that import keys to linknode
104                 my %i = map {$_->thatnode->name => 1} grep { $_->type eq 'import'} $lnode->edges;
105
106                 if(scalar(keys %i) == 1) {
107                 } else {
108                   last if $inode1 eq $inode2;
109                 }
110
111                 my @inode2_imports =  grep { $_->type eq 'import' and $_->thatnode eq $inode2 } $lnode->edges;
112                 next unless @inode2_imports;
113
114                 my $cedge = CompoundEdge->new();
115                 $cedge->via($lnode);
116
117                 #warn join ' ', map {$_->thisfield->name} map {$_->flip} $lnode->edges;
118                 #warn join ' ', map {$_->thisfield->name} $lnode->edges;
119                 #warn join ' ', map {$_->thisfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
120                 #warn join ' ', map {$_->thatfield->name} map {$_->flip} grep {$_->type eq 'import'} $lnode->edges;
121                 $cedge->push_edges(
122                                                    map {$_->flip}
123                                                    grep {$_->type eq 'import'
124                                                                    and
125                                                                  ($_->thatnode eq $inode1 or $_->thatnode eq $inode2)
126                                                             } $lnode->edges
127                                                   );
128
129                 if(scalar(@inode1_imports) == 1 and scalar(@inode2_imports) == 1){
130                   $cedge->type('one2one');
131
132                   $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
133                   $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
134                 }
135                 elsif(scalar(@inode1_imports)  > 1 and scalar(@inode2_imports) == 1){
136                   $cedge->type('many2one');
137
138                   $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
139                   $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
140                 }
141                 elsif(scalar(@inode1_imports) == 1 and scalar(@inode2_imports)  > 1){
142                   #handled above
143                 }
144                 elsif(scalar(@inode1_imports)  > 1 and scalar(@inode2_imports)  > 1){
145                   $cedge->type('many2many');
146
147                   $inode1->via($inode2->name,$inode1->via($inode2->name)+1);
148                   $inode2->via($inode1->name,$inode2->via($inode1->name)+1);
149                 }
150
151                 $inode1->push_compoundedges($cedge);
152                 $inode2->push_compoundedges($cedge) unless $inode1 eq $inode2;
153           }
154         }
155   }
156 }
157
158 1;