sqlt parser now respected a relationship attribute of is_foreign_key_constraint
[dbsrgits/DBIx-Class.git] / lib / SQL / Translator / Parser / DBIx / Class.pm
CommitLineData
75d07914 1package # hide from PAUSE
c0e7b4e5 2 SQL::Translator::Parser::DBIx::Class;
b02b20b5 3
4# AUTHOR: Jess Robinson
5
1d996af5 6# Some mistakes the fault of Matt S Trout
7
b02b20b5 8use strict;
9use warnings;
10use vars qw($DEBUG $VERSION @EXPORT_OK);
11$DEBUG = 0 unless defined $DEBUG;
12$VERSION = sprintf "%d.%02d", q$Revision 1.0$ =~ /(\d+)\.(\d+)/;
13
14use Exporter;
15use Data::Dumper;
16use SQL::Translator::Utils qw(debug normalize_name);
17
18use base qw(Exporter);
19
20@EXPORT_OK = qw(parse);
21
22# -------------------------------------------------------------------
23# parse($tr, $data)
24#
880a1a0c 25# Note that $data, in the case of this parser, is not useful.
b02b20b5 26# We're working with DBIx::Class Schemas, not data streams.
27# -------------------------------------------------------------------
28sub parse {
499adf63 29 my ($tr, $data) = @_;
30 my $args = $tr->parser_args;
31 my $dbixschema = $args->{'DBIx::Schema'} || $data;
32 $dbixschema ||= $args->{'package'};
33 my $limit_sources = $args->{'sources'};
b02b20b5 34
35 die 'No DBIx::Schema' unless ($dbixschema);
36 if (!ref $dbixschema) {
37 eval "use $dbixschema;";
38 die "Can't load $dbixschema ($@)" if($@);
39 }
40
41 my $schema = $tr->schema;
42 my $table_no = 0;
43
44# print Dumper($dbixschema->registered_classes);
45
0009fa49 46 #foreach my $tableclass ($dbixschema->registered_classes)
38e48163 47
48 my %seen_tables;
49
499adf63 50 my @monikers = $dbixschema->sources;
51 if ($limit_sources) {
52 my $ref = ref $limit_sources || '';
5bdf1b3d 53 die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH';
499adf63 54
55 # limit monikers to those specified in
56 my $sources;
57 if ($ref eq 'ARRAY') {
58 $sources->{$_} = 1 for (@$limit_sources);
59 } else {
60 $sources = $limit_sources;
61 }
62 @monikers = grep { $sources->{$_} } @monikers;
63 }
64
65
66 foreach my $moniker (@monikers)
b02b20b5 67 {
0009fa49 68 #eval "use $tableclass";
69 #print("Can't load $tableclass"), next if($@);
70 my $source = $dbixschema->source($moniker);
b02b20b5 71
38e48163 72 next if $seen_tables{$source->name}++;
73
b02b20b5 74 my $table = $schema->add_table(
75 name => $source->name,
76 type => 'TABLE',
77 ) || die $schema->error;
78 my $colcount = 0;
79 foreach my $col ($source->columns)
80 {
81 # assuming column_info in dbix is the same as DBI (?)
82 # data_type is a number, column_type is text?
83 my %colinfo = (
84 name => $col,
b02b20b5 85 size => 0,
86 is_auto_increment => 0,
87 is_foreign_key => 0,
88 is_nullable => 0,
89 %{$source->column_info($col)}
90 );
0009fa49 91 if ($colinfo{is_nullable}) {
92 $colinfo{default} = '' unless exists $colinfo{default};
93 }
b02b20b5 94 my $f = $table->add_field(%colinfo) || die $table->error;
95 }
96 $table->primary_key($source->primary_columns);
97
7b90bb13 98 my @primary = $source->primary_columns;
99 my %unique_constraints = $source->unique_constraints;
100 foreach my $uniq (keys %unique_constraints) {
de60a93d 101 if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 102 $table->add_constraint(
103 type => 'unique',
104 name => "$uniq",
105 fields => $unique_constraints{$uniq}
106 );
107 }
108 }
109
b02b20b5 110 my @rels = $source->relationships();
499adf63 111
112 my %created_FK_rels;
113
b02b20b5 114 foreach my $rel (@rels)
115 {
116 my $rel_info = $source->relationship_info($rel);
637ca936 117
637ca936 118 # Ignore any rel cond that isn't a straight hash
119 next unless ref $rel_info->{cond} eq 'HASH';
120
de60a93d 121 my $othertable = $source->related_source($rel);
122 my $rel_table = $othertable->name;
123
637ca936 124 # Get the key information, mapping off the foreign/self markers
125 my @cond = keys(%{$rel_info->{cond}});
126 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
127 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
128
129 if($rel_table)
75d07914 130 {
637ca936 131
de60a93d 132 my $reverse_rels = $source->reverse_relationship_info($rel);
133 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
134
135 my $on_delete = '';
136 my $on_update = '';
137
138 if (defined $otherrelationship) {
139 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
140 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
141 }
142
499adf63 143 # Make sure we dont create the same foreign key constraint twice
144 my $key_test = join("\x00", @keys);
145
637ca936 146 #Decide if this is a foreign key based on whether the self
147 #items are our primary columns.
3d618782 148 $DB::single = 1 if $moniker eq 'Tests::MBTI::Result';
637ca936 149
637ca936 150 # If the sets are different, then we assume it's a foreign key from
151 # us to another table.
3d618782 152 # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation
153 if ( ! exists $created_FK_rels{$rel_table}->{$key_test} &&
154 ( exists $rel_info->{attrs}{is_foreign_key_constraint} &&
155 $rel_info->{attrs}{is_foreign_key_constraint} ||
156 !$source->compare_relationship_keys(\@keys, \@primary)
157 )
158 )
159 {
499adf63 160 $created_FK_rels{$rel_table}->{$key_test} = 1;
637ca936 161 $table->add_constraint(
162 type => 'foreign_key',
163 name => "fk_$keys[0]",
164 fields => \@keys,
165 reference_fields => \@refkeys,
166 reference_table => $rel_table,
de60a93d 167 on_delete => $on_delete,
168 on_update => $on_update
637ca936 169 );
170 }
b02b20b5 171 }
172 }
173 }
637ca936 174 return 1;
75d07914 175}
b02b20b5 176
1771;
de60a93d 178