Added support for unique constraints to SQLT::Parser::DBIx::Class
[dbsrgits/DBIx-Class.git] / lib / SQL / Translator / Parser / DBIx / Class.pm
1 package # hide from PAUSE
2     SQL::Translator::Parser::DBIx::Class;
3
4 # AUTHOR: Jess Robinson
5
6 # Some mistakes the fault of Matt S Trout
7
8 use strict;
9 use warnings;
10 use vars qw($DEBUG $VERSION @EXPORT_OK);
11 $DEBUG = 0 unless defined $DEBUG;
12 $VERSION = sprintf "%d.%02d", q$Revision 1.0$ =~ /(\d+)\.(\d+)/;
13
14 use Exporter;
15 use Data::Dumper;
16 use SQL::Translator::Utils qw(debug normalize_name);
17
18 use base qw(Exporter);
19
20 @EXPORT_OK = qw(parse);
21
22 # -------------------------------------------------------------------
23 # parse($tr, $data)
24 #
25 # Note that $data, in the case of this parser, is not useful.
26 # We're working with DBIx::Class Schemas, not data streams.
27 # -------------------------------------------------------------------
28 sub parse {
29     my ($tr, $data) = @_;
30     my $args        = $tr->parser_args;
31     my $dbixschema  = $args->{'DBIx::Schema'} || $data;
32     $dbixschema   ||= $args->{'package'};
33     
34     die 'No DBIx::Schema' unless ($dbixschema);
35     if (!ref $dbixschema) {
36       eval "use $dbixschema;";
37       die "Can't load $dbixschema ($@)" if($@);
38     }
39
40     my $schema      = $tr->schema;
41     my $table_no    = 0;
42
43 #    print Dumper($dbixschema->registered_classes);
44
45     #foreach my $tableclass ($dbixschema->registered_classes)
46     foreach my $moniker ($dbixschema->sources)
47     {
48         #eval "use $tableclass";
49         #print("Can't load $tableclass"), next if($@);
50         my $source = $dbixschema->source($moniker);
51
52         my $table = $schema->add_table(
53                                        name => $source->name,
54                                        type => 'TABLE',
55                                        ) || die $schema->error;
56         my $colcount = 0;
57         foreach my $col ($source->columns)
58         {
59             # assuming column_info in dbix is the same as DBI (?)
60             # data_type is a number, column_type is text?
61             my %colinfo = (
62               name => $col,
63               size => 0,
64               is_auto_increment => 0,
65               is_foreign_key => 0,
66               is_nullable => 0,
67               %{$source->column_info($col)}
68             );
69             if ($colinfo{is_nullable}) {
70               $colinfo{default} = '' unless exists $colinfo{default};
71             }
72             my $f = $table->add_field(%colinfo) || die $table->error;
73         }
74         $table->primary_key($source->primary_columns);
75
76         my @primary = $source->primary_columns;
77         my %unique_constraints = $source->unique_constraints;
78         foreach my $uniq (keys %unique_constraints) {
79             if (!equal_keys($unique_constraints{$uniq}, \@primary)) {
80                 $table->add_constraint(
81                             type             => 'unique',
82                             name             => "$uniq",
83                             fields           => $unique_constraints{$uniq}
84                 );
85             }
86         }
87
88         my @rels = $source->relationships();
89         foreach my $rel (@rels)
90         {
91             my $rel_info = $source->relationship_info($rel);
92
93             my $rel_table = $source->related_source($rel)->name;
94
95             # Ignore any rel cond that isn't a straight hash
96             next unless ref $rel_info->{cond} eq 'HASH';
97
98             # Get the key information, mapping off the foreign/self markers
99             my @cond = keys(%{$rel_info->{cond}});
100             my @refkeys = map {/^\w+\.(\w+)$/} @cond;
101             my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
102
103             if($rel_table)
104             {
105
106                 #Decide if this is a foreign key based on whether the self
107                 #items are our primary columns.
108
109                 # If the sets are different, then we assume it's a foreign key from
110                 # us to another table.
111                 if (!equal_keys(\@keys, \@primary)) {
112                     $table->add_constraint(
113                                 type             => 'foreign_key',
114                                 name             => "fk_$keys[0]",
115                                 fields           => \@keys,
116                                 reference_fields => \@refkeys,
117                                 reference_table  => $rel_table,
118                     );
119                 }
120             }
121         }
122     }
123     return 1;
124 }
125
126 # -------------------------------------------------------------------
127 # equal_keys($key1, $key2)
128 #
129 # See if the set of keys in $key1 is equal to the set of keys in $key2
130 # -------------------------------------------------------------------
131 sub equal_keys {
132     my ($key1, $key2) = @_;
133
134     # Make sure every key1 is in key2
135     my $found;
136     foreach my $key (@$key1) {
137         $found = 0;
138         foreach my $prim (@$key2) {
139             if ($prim eq $key) {
140                 $found = 1;
141                 last;
142             }
143         }
144         last unless $found;
145     }
146
147     # Make sure every key2 is in key1
148     if ($found) {
149         foreach my $prim (@$key2) {
150             $found = 0;
151             foreach my $key (@$key1) {
152                 if ($prim eq $key) {
153                     $found = 1;
154                     last;
155                 }
156             }
157             last unless $found;
158         }
159     }
160
161     return $found;
162 }
163
164 1;