Commit | Line | Data |
75d07914 |
1 | package # 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 |
10 | use strict; |
11 | use warnings; |
b7e303a8 |
12 | use vars qw($DEBUG @EXPORT_OK); |
b02b20b5 |
13 | $DEBUG = 0 unless defined $DEBUG; |
b02b20b5 |
14 | |
15 | use Exporter; |
16 | use Data::Dumper; |
17 | use SQL::Translator::Utils qw(debug normalize_name); |
18 | |
19 | use 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 | # ------------------------------------------------------------------- |
32 | sub 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 | |
1d521afd |
68 | my(@table_monikers, @view_monikers); |
69 | for my $moniker (@monikers){ |
70 | my $source = $dbicschema->source($moniker); |
9774f48b |
71 | next if $source->is_virtual; |
1d521afd |
72 | if ( $source->isa('DBIx::Class::ResultSource::Table') || |
73 | $source->isa('DBIx::Class::ResultSourceProxy::Table') ) { |
74 | push(@table_monikers, $moniker); |
75 | } elsif( $source->isa('DBIx::Class::ResultSource::View') || |
76 | $source->isa('DBIx::Class::ResultSourceProxy::View') ){ |
77 | push(@view_monikers, $moniker); |
78 | } |
79 | } |
499adf63 |
80 | |
1d521afd |
81 | foreach my $moniker (sort @table_monikers) |
b02b20b5 |
82 | { |
b7e303a8 |
83 | my $source = $dbicschema->source($moniker); |
90c11f68 |
84 | |
85 | # Skip custom query sources |
86 | next if ref($source->name); |
b02b20b5 |
87 | |
b7e303a8 |
88 | # Its possible to have multiple DBIC source using same table |
38e48163 |
89 | next if $seen_tables{$source->name}++; |
90 | |
b02b20b5 |
91 | my $table = $schema->add_table( |
92 | name => $source->name, |
93 | type => 'TABLE', |
94 | ) || die $schema->error; |
95 | my $colcount = 0; |
96 | foreach my $col ($source->columns) |
97 | { |
d6c79cb3 |
98 | # assuming column_info in dbic is the same as DBI (?) |
b02b20b5 |
99 | # data_type is a number, column_type is text? |
100 | my %colinfo = ( |
101 | name => $col, |
b02b20b5 |
102 | size => 0, |
103 | is_auto_increment => 0, |
104 | is_foreign_key => 0, |
105 | is_nullable => 0, |
106 | %{$source->column_info($col)} |
107 | ); |
0009fa49 |
108 | if ($colinfo{is_nullable}) { |
109 | $colinfo{default} = '' unless exists $colinfo{default}; |
110 | } |
b02b20b5 |
111 | my $f = $table->add_field(%colinfo) || die $table->error; |
112 | } |
113 | $table->primary_key($source->primary_columns); |
114 | |
7b90bb13 |
115 | my @primary = $source->primary_columns; |
116 | my %unique_constraints = $source->unique_constraints; |
b7e303a8 |
117 | foreach my $uniq (sort keys %unique_constraints) { |
de60a93d |
118 | if (!$source->compare_relationship_keys($unique_constraints{$uniq}, \@primary)) { |
7b90bb13 |
119 | $table->add_constraint( |
120 | type => 'unique', |
a7e65bb5 |
121 | name => $uniq, |
7b90bb13 |
122 | fields => $unique_constraints{$uniq} |
123 | ); |
124 | } |
125 | } |
126 | |
b02b20b5 |
127 | my @rels = $source->relationships(); |
499adf63 |
128 | |
129 | my %created_FK_rels; |
2581038c |
130 | |
131 | # global add_fk_index set in parser_args |
132 | my $add_fk_index = (exists $args->{add_fk_index} && ($args->{add_fk_index} == 0)) ? 0 : 1; |
499adf63 |
133 | |
454a5a42 |
134 | foreach my $rel (sort @rels) |
b02b20b5 |
135 | { |
136 | my $rel_info = $source->relationship_info($rel); |
637ca936 |
137 | |
637ca936 |
138 | # Ignore any rel cond that isn't a straight hash |
139 | next unless ref $rel_info->{cond} eq 'HASH'; |
140 | |
de60a93d |
141 | my $othertable = $source->related_source($rel); |
142 | my $rel_table = $othertable->name; |
143 | |
e377d723 |
144 | my $reverse_rels = $source->reverse_relationship_info($rel); |
145 | my ($otherrelname, $otherrelationship) = each %{$reverse_rels}; |
146 | |
d1b264d3 |
147 | # Force the order of @cond to match the order of ->add_columns |
148 | my $idx; |
149 | my %other_columns_idx = map {'foreign.'.$_ => ++$idx } $othertable->columns; |
150 | my @cond = sort { $other_columns_idx{$a} cmp $other_columns_idx{$b} } keys(%{$rel_info->{cond}}); |
151 | |
637ca936 |
152 | # Get the key information, mapping off the foreign/self markers |
637ca936 |
153 | my @refkeys = map {/^\w+\.(\w+)$/} @cond; |
154 | my @keys = map {$rel_info->{cond}->{$_} =~ /^\w+\.(\w+)$/} @cond; |
155 | |
e377d723 |
156 | # determine if this relationship is a self.fk => foreign.pk (i.e. belongs_to) |
157 | my $fk_constraint; |
de60a93d |
158 | |
e377d723 |
159 | #first it can be specified explicitly |
160 | if ( exists $rel_info->{attrs}{is_foreign_key_constraint} ) { |
161 | $fk_constraint = $rel_info->{attrs}{is_foreign_key_constraint}; |
162 | } |
163 | # it can not be multi |
164 | elsif ( $rel_info->{attrs}{accessor} eq 'multi' ) { |
165 | $fk_constraint = 0; |
166 | } |
167 | # if indeed single, check if all self.columns are our primary keys. |
168 | # this is supposed to indicate a has_one/might_have... |
169 | # where's the introspection!!?? :) |
170 | else { |
171 | $fk_constraint = not $source->compare_relationship_keys(\@keys, \@primary); |
172 | } |
de60a93d |
173 | |
e377d723 |
174 | my $cascade; |
175 | for my $c (qw/delete update/) { |
176 | if (exists $rel_info->{attrs}{"on_$c"}) { |
177 | if ($fk_constraint) { |
178 | $cascade->{$c} = $rel_info->{attrs}{"on_$c"}; |
179 | } |
180 | else { |
181 | warn "SQLT attribute 'on_$c' was supplied for relationship '$moniker/$rel', which does not appear to be a foreign constraint. " |
182 | . "If you are sure that SQLT must generate a constraint for this relationship, add 'is_foreign_key_constraint => 1' to the attributes.\n"; |
183 | } |
184 | } |
185 | elsif (defined $otherrelationship and $otherrelationship->{attrs}{$c eq 'update' ? 'cascade_copy' : 'cascade_delete'}) { |
186 | $cascade->{$c} = 'CASCADE'; |
de60a93d |
187 | } |
e377d723 |
188 | } |
189 | |
190 | if($rel_table) |
191 | { |
192 | # Constraints are added only if applicable |
193 | next unless $fk_constraint; |
194 | |
195 | # Make sure we dont create the same foreign key constraint twice |
196 | my $key_test = join("\x00", @keys); |
197 | next if $created_FK_rels{$rel_table}->{$key_test}; |
de60a93d |
198 | |
45f1a484 |
199 | my $is_deferrable = $rel_info->{attrs}{is_deferrable}; |
2581038c |
200 | |
201 | # global parser_args add_fk_index param can be overridden on the rel def |
202 | my $add_fk_index_rel = (exists $rel_info->{attrs}{add_fk_index}) ? $rel_info->{attrs}{add_fk_index} : $add_fk_index; |
13de943d |
203 | |
7b5d0b84 |
204 | |
205 | $created_FK_rels{$rel_table}->{$key_test} = 1; |
206 | if (scalar(@keys)) { |
207 | $table->add_constraint( |
0d865134 |
208 | type => 'foreign_key', |
a7e65bb5 |
209 | name => join('_', $table->name, 'fk', @keys), |
0d865134 |
210 | fields => \@keys, |
211 | reference_fields => \@refkeys, |
212 | reference_table => $rel_table, |
e377d723 |
213 | on_delete => $cascade->{delete}, |
214 | on_update => $cascade->{update}, |
e394339b |
215 | (defined $is_deferrable ? ( deferrable => $is_deferrable ) : ()), |
7b5d0b84 |
216 | ); |
0d865134 |
217 | |
2581038c |
218 | if ($add_fk_index_rel) { |
0e2c6809 |
219 | my $index = $table->add_index( |
220 | name => join('_', $table->name, 'idx', @keys), |
221 | fields => \@keys, |
222 | type => 'NORMAL', |
223 | ); |
224 | } |
225 | } |
b02b20b5 |
226 | } |
227 | } |
0e2c6809 |
228 | |
aaf2403d |
229 | if ($source->result_class->can('sqlt_deploy_hook')) { |
230 | $source->result_class->sqlt_deploy_hook($table); |
231 | } |
b02b20b5 |
232 | } |
d6c79cb3 |
233 | |
1d521afd |
234 | foreach my $moniker (sort @view_monikers) |
235 | { |
236 | my $source = $dbicschema->source($moniker); |
237 | # Skip custom query sources |
238 | next if ref($source->name); |
239 | |
240 | # Its possible to have multiple DBIC source using same table |
241 | next if $seen_tables{$source->name}++; |
242 | |
47344132 |
243 | my $view = $schema->add_view( |
1d521afd |
244 | name => $source->name, |
245 | fields => [ $source->columns ], |
246 | ($source->view_definition ? $source->view_definition : ()) |
247 | ); |
248 | if ($source->result_class->can('sqlt_deploy_hook')) { |
47344132 |
249 | $source->result_class->sqlt_deploy_hook($view); |
1d521afd |
250 | } |
251 | } |
252 | |
253 | |
b7e303a8 |
254 | if ($dbicschema->can('sqlt_deploy_hook')) { |
255 | $dbicschema->sqlt_deploy_hook($schema); |
d6c79cb3 |
256 | } |
257 | |
637ca936 |
258 | return 1; |
75d07914 |
259 | } |
b02b20b5 |
260 | |
0da8b7da |
261 | 1; |