Merge 'trunk' into 'replication_dedux'
[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
c385ecea 8# Others the fault of Ash Berlin
9
b02b20b5 10use strict;
11use warnings;
b7e303a8 12use vars qw($DEBUG @EXPORT_OK);
b02b20b5 13$DEBUG = 0 unless defined $DEBUG;
b02b20b5 14
15use Exporter;
16use Data::Dumper;
17use SQL::Translator::Utils qw(debug normalize_name);
18
19use base qw(Exporter);
20
21@EXPORT_OK = qw(parse);
22
23# -------------------------------------------------------------------
24# parse($tr, $data)
25#
0e2c6809 26# setting parser_args => { add_fk_index => 0 } will prevent
27# the auto-generation of an index for each FK.
28#
880a1a0c 29# Note that $data, in the case of this parser, is not useful.
b02b20b5 30# We're working with DBIx::Class Schemas, not data streams.
31# -------------------------------------------------------------------
32sub parse {
499adf63 33 my ($tr, $data) = @_;
34 my $args = $tr->parser_args;
b7e303a8 35 my $dbicschema = $args->{'DBIx::Class::Schema'} || $args->{"DBIx::Schema"} ||$data;
36 $dbicschema ||= $args->{'package'};
499adf63 37 my $limit_sources = $args->{'sources'};
b02b20b5 38
b7e303a8 39 die 'No DBIx::Class::Schema' unless ($dbicschema);
40 if (!ref $dbicschema) {
41 eval "use $dbicschema;";
42 die "Can't load $dbicschema ($@)" if($@);
b02b20b5 43 }
44
45 my $schema = $tr->schema;
46 my $table_no = 0;
47
b7e303a8 48 $schema->name( ref($dbicschema) . " v" . ($dbicschema->VERSION || '1.x'))
49 unless ($schema->name);
38e48163 50
51 my %seen_tables;
52
b7e303a8 53 my @monikers = sort $dbicschema->sources;
499adf63 54 if ($limit_sources) {
55 my $ref = ref $limit_sources || '';
5bdf1b3d 56 die "'sources' parameter must be an array or hash ref" unless $ref eq 'ARRAY' || ref eq 'HASH';
499adf63 57
58 # limit monikers to those specified in
59 my $sources;
60 if ($ref eq 'ARRAY') {
61 $sources->{$_} = 1 for (@$limit_sources);
62 } else {
63 $sources = $limit_sources;
64 }
65 @monikers = grep { $sources->{$_} } @monikers;
66 }
67
68
454a5a42 69 foreach my $moniker (sort @monikers)
b02b20b5 70 {
b7e303a8 71 my $source = $dbicschema->source($moniker);
b02b20b5 72
b7e303a8 73 # Its possible to have multiple DBIC source using same table
38e48163 74 next if $seen_tables{$source->name}++;
75
b02b20b5 76 my $table = $schema->add_table(
77 name => $source->name,
78 type => 'TABLE',
79 ) || die $schema->error;
80 my $colcount = 0;
81 foreach my $col ($source->columns)
82 {
d6c79cb3 83 # assuming column_info in dbic is the same as DBI (?)
b02b20b5 84 # data_type is a number, column_type is text?
85 my %colinfo = (
86 name => $col,
b02b20b5 87 size => 0,
88 is_auto_increment => 0,
89 is_foreign_key => 0,
90 is_nullable => 0,
91 %{$source->column_info($col)}
92 );
0009fa49 93 if ($colinfo{is_nullable}) {
94 $colinfo{default} = '' unless exists $colinfo{default};
95 }
b02b20b5 96 my $f = $table->add_field(%colinfo) || die $table->error;
97 }
98 $table->primary_key($source->primary_columns);
99
7b90bb13 100 my @primary = $source->primary_columns;
101 my %unique_constraints = $source->unique_constraints;
b7e303a8 102 foreach my $uniq (sort keys %unique_constraints) {
de60a93d 103 if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) {
7b90bb13 104 $table->add_constraint(
105 type => 'unique',
a7e65bb5 106 name => $uniq,
7b90bb13 107 fields => $unique_constraints{$uniq}
108 );
109 }
110 }
111
b02b20b5 112 my @rels = $source->relationships();
499adf63 113
114 my %created_FK_rels;
2581038c 115
116 # global add_fk_index set in parser_args
117 my $add_fk_index = (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) ? 0 : 1;
499adf63 118
454a5a42 119 foreach my $rel (sort @rels)
b02b20b5 120 {
121 my $rel_info = $source->relationship_info($rel);
637ca936 122
637ca936 123 # Ignore any rel cond that isn't a straight hash
124 next unless ref $rel_info->{cond} eq 'HASH';
125
de60a93d 126 my $othertable = $source->related_source($rel);
127 my $rel_table = $othertable->name;
128
d1b264d3 129 # Force the order of @cond to match the order of ->add_columns
130 my $idx;
131 my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $othertable->columns;
132 my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}});
133
637ca936 134 # Get the key information, mapping off the foreign/self markers
637ca936 135 my @refkeys = map {/^\w+\.(\w+)$/} @cond;
136 my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond;
137
138 if($rel_table)
75d07914 139 {
de60a93d 140 my $reverse_rels = $source->reverse_relationship_info($rel);
141 my ($otherrelname, $otherrelationship) = each %{$reverse_rels};
142
143 my $on_delete = '';
144 my $on_update = '';
145
146 if (defined $otherrelationship) {
147 $on_delete = $otherrelationship->{'attrs'}->{cascade_delete} ? 'CASCADE' : '';
148 $on_update = $otherrelationship->{'attrs'}->{cascade_copy} ? 'CASCADE' : '';
149 }
150
45f1a484 151 my $is_deferrable = $rel_info->{attrs}{is_deferrable};
2581038c 152
153 # global parser_args add_fk_index param can be overridden on the rel def
154 my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index;
13de943d 155
499adf63 156 # Make sure we dont create the same foreign key constraint twice
157 my $key_test = join("\x00", @keys);
158
637ca936 159 #Decide if this is a foreign key based on whether the self
160 #items are our primary columns.
161
637ca936 162 # If the sets are different, then we assume it's a foreign key from
163 # us to another table.
3d618782 164 # OR: If is_foreign_key_constraint attr is explicity set (or set to false) on the relation
7b5d0b84 165 next if ( exists $created_FK_rels{$rel_table}->{$key_test} );
166 if ( exists $rel_info->{attrs}{is_foreign_key_constraint}) {
167 # not is this attr set to 0 but definitely if set to 1
168 next unless ($rel_info->{attrs}{is_foreign_key_constraint});
169 } else {
170 # not if might have
171 # next if ($rel_info->{attrs}{accessor} eq 'single' && exists $rel_info->{attrs}{join_type} && uc($rel_info->{attrs}{join_type}) eq 'LEFT');
172 # not sure about this one
173 next if $source->compare_relationship_keys(\@keys, \@primary);
174 }
175
176 $created_FK_rels{$rel_table}->{$key_test} = 1;
177 if (scalar(@keys)) {
178 $table->add_constraint(
0d865134 179 type => 'foreign_key',
a7e65bb5 180 name => join('_', $table->name, 'fk', @keys),
0d865134 181 fields => \@keys,
182 reference_fields => \@refkeys,
183 reference_table => $rel_table,
184 on_delete => $on_delete,
28f21a8f 185 on_update => $on_update,
e394339b 186 (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()),
7b5d0b84 187 );
0d865134 188
2581038c 189 if ($add_fk_index_rel) {
0e2c6809 190 my $index = $table->add_index(
191 name => join('_', $table->name, 'idx', @keys),
192 fields => \@keys,
193 type => 'NORMAL',
194 );
195 }
196 }
b02b20b5 197 }
198 }
0e2c6809 199
aaf2403d 200 if ($source->result_class->can('sqlt_deploy_hook')) {
201 $source->result_class->sqlt_deploy_hook($table);
202 }
b02b20b5 203 }
d6c79cb3 204
b7e303a8 205 if ($dbicschema->can('sqlt_deploy_hook')) {
206 $dbicschema->sqlt_deploy_hook($schema);
d6c79cb3 207 }
208
637ca936 209 return 1;
75d07914 210}
b02b20b5 211
0da8b7da 2121;