6aa22fdd9a324aa947c1a9afc334de91db573a7c
[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
47     my %seen_tables;
48
49     foreach my $moniker ($dbixschema->sources)
50     {
51         #eval "use $tableclass";
52         #print("Can't load $tableclass"), next if($@);
53         my $source = $dbixschema->source($moniker);
54
55         next if $seen_tables{$source->name}++;
56
57         my $table = $schema->add_table(
58                                        name => $source->name,
59                                        type => 'TABLE',
60                                        ) || die $schema->error;
61         my $colcount = 0;
62         foreach my $col ($source->columns)
63         {
64             # assuming column_info in dbix is the same as DBI (?)
65             # data_type is a number, column_type is text?
66             my %colinfo = (
67               name => $col,
68               size => 0,
69               is_auto_increment => 0,
70               is_foreign_key => 0,
71               is_nullable => 0,
72               %{$source->column_info($col)}
73             );
74             if ($colinfo{is_nullable}) {
75               $colinfo{default} = '' unless exists $colinfo{default};
76             }
77             warn "No length set for char field in " . $colinfo{name}
78             if(!$colinfo{size} && $colinfo{data_type} =~ /char/);
79             my $f = $table->add_field(%colinfo) || die $table->error;
80         }
81         $table->primary_key($source->primary_columns);
82
83         my @primary = $source->primary_columns;
84         my %unique_constraints = $source->unique_constraints;
85         foreach my $uniq (keys %unique_constraints) {
86             if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
87                 $table->add_constraint(
88                             type             => 'unique',
89                             name             => "$uniq",
90                             fields           => $unique_constraints{$uniq}
91                 );
92             }
93         }
94
95         my @rels = $source->relationships();
96         foreach my $rel (@rels)
97         {
98             my $rel_info = $source->relationship_info($rel);
99
100             # Ignore any rel cond that isn't a straight hash
101             next unless ref $rel_info->{cond} eq 'HASH';
102
103             my $othertable = $source->related_source($rel);
104             my $rel_table = $othertable->name;
105
106             # Get the key information, mapping off the foreign/self markers
107             my @cond = keys(%{$rel_info->{cond}});
108             my @refkeys = map {/^\w+\.(\w+)$/} @cond;
109             my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
110
111             if($rel_table)
112             {
113
114                 my $reverse_rels = $source->reverse_relationship_info($rel);
115                 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
116
117                 my $on_delete = '';
118                 my $on_update = '';
119
120                 if (defined $otherrelationship) {
121                     $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
122                     $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
123                 }
124
125                 #Decide if this is a foreign key based on whether the self
126                 #items are our primary columns.
127
128                 # If the sets are different, then we assume it's a foreign key from
129                 # us to another table.
130                 if (!$source->compare_relationship_keys(\@keys, \@primary)) {
131                     $table->add_constraint(
132                                 type             => 'foreign_key',
133                                 name             => "fk_$keys[0]",
134                                 fields           => \@keys,
135                                 reference_fields => \@refkeys,
136                                 reference_table  => $rel_table,
137                                 on_delete        => $on_delete,
138                                 on_update        => $on_update
139                     );
140                 }
141             }
142         }
143     }
144     return 1;
145 }
146
147 1;
148