Commit | Line | Data |
18fca96a |
1 | package DBIx::Class::Schema::Loader::Generic; |
a78e3fed |
2 | |
3 | use strict; |
a4a19f3c |
4 | use warnings; |
5 | |
6 | use base qw/DBIx::Class::Schema/; |
7 | |
a78e3fed |
8 | use Carp; |
9 | use Lingua::EN::Inflect; |
a4a19f3c |
10 | |
a78e3fed |
11 | require DBIx::Class::Core; |
a4a19f3c |
12 | |
13 | __PACKAGE__->mk_classdata('loader_data'); |
14 | |
a78e3fed |
15 | =head1 NAME |
16 | |
18fca96a |
17 | DBIx::Class::Schema::Loader::Generic - Generic DBIx::Class::Schema::Loader Implementation. |
a78e3fed |
18 | |
19 | =head1 SYNOPSIS |
20 | |
18fca96a |
21 | See L<DBIx::Class::Schema::Loader> |
a78e3fed |
22 | |
23 | =head1 DESCRIPTION |
24 | |
25 | =head2 OPTIONS |
26 | |
27 | Available constructor options are: |
28 | |
29 | =head3 additional_base_classes |
30 | |
31 | List of additional base classes your table classes will use. |
32 | |
33 | =head3 left_base_classes |
34 | |
35 | List of additional base classes, that need to be leftmost. |
36 | |
37 | =head3 additional_classes |
38 | |
39 | List of additional classes which your table classes will use. |
40 | |
41 | =head3 constraint |
42 | |
43 | Only load tables matching regex. |
44 | |
45 | =head3 exclude |
46 | |
47 | Exclude tables matching regex. |
48 | |
49 | =head3 debug |
50 | |
51 | Enable debug messages. |
52 | |
53 | =head3 dsn |
54 | |
55 | DBI Data Source Name. |
56 | |
a78e3fed |
57 | =head3 password |
58 | |
59 | Password. |
60 | |
61 | =head3 relationships |
62 | |
63 | Try to automatically detect/setup has_a and has_many relationships. |
64 | |
65 | =head3 inflect |
66 | |
67 | An hashref, which contains exceptions to Lingua::EN::Inflect::PL(). |
68 | Useful for foreign language column names. |
69 | |
70 | =head3 user |
71 | |
72 | Username. |
73 | |
74 | =head2 METHODS |
75 | |
76 | =cut |
77 | |
78 | =head3 new |
79 | |
80 | Not intended to be called directly. This is used internally by the |
18fca96a |
81 | C<new()> method in L<DBIx::Class::Schema::Loader>. |
a78e3fed |
82 | |
83 | =cut |
84 | |
a4a19f3c |
85 | sub _load_from_connection { |
a78e3fed |
86 | my ( $class, %args ) = @_; |
87 | if ( $args{debug} ) { |
88 | no strict 'refs'; |
a4a19f3c |
89 | *{"$class\::debug_loader"} = sub { 1 }; |
a78e3fed |
90 | } |
91 | my $additional = $args{additional_classes} || []; |
92 | $additional = [$additional] unless ref $additional eq 'ARRAY'; |
93 | my $additional_base = $args{additional_base_classes} || []; |
94 | $additional_base = [$additional_base] |
95 | unless ref $additional_base eq 'ARRAY'; |
96 | my $left_base = $args{left_base_classes} || []; |
97 | $left_base = [$left_base] unless ref $left_base eq 'ARRAY'; |
a4a19f3c |
98 | $class->loader_data({ |
a78e3fed |
99 | _datasource => |
100 | [ $args{dsn}, $args{user}, $args{password}, $args{options} ], |
a78e3fed |
101 | _additional => $additional, |
102 | _additional_base => $additional_base, |
103 | _left_base => $left_base, |
104 | _constraint => $args{constraint} || '.*', |
105 | _exclude => $args{exclude}, |
106 | _relationships => $args{relationships}, |
107 | _inflect => $args{inflect}, |
af96f52e |
108 | _db_schema => $args{db_schema} || '', |
a4a19f3c |
109 | _drop_db_schema => $args{drop_db_schema}, |
af6c2665 |
110 | TABLE_CLASSES => {}, |
111 | MONIKERS => {}, |
a4a19f3c |
112 | }); |
113 | |
114 | $class->connection(@{$class->loader_data->{_datasource}}); |
fbd83464 |
115 | warn qq/\### START DBIx::Class::Schema::Loader dump ###\n/ if $class->debug_loader; |
a4a19f3c |
116 | $class->_load_classes; |
117 | $class->_relationships if $class->loader_data->{_relationships}; |
fbd83464 |
118 | warn qq/\### END DBIx::Class::Schema::Loader dump ###\n/ if $class->debug_loader; |
a4a19f3c |
119 | $class->storage->dbh->disconnect; # XXX this should be ->storage->disconnect later? |
120 | |
121 | 1; |
a78e3fed |
122 | } |
123 | |
af6c2665 |
124 | # The original table class name during Loader, |
125 | sub _find_table_class { |
a4a19f3c |
126 | my ( $class, $table ) = @_; |
127 | return $class->loader_data->{TABLE_CLASSES}->{$table}; |
a78e3fed |
128 | } |
129 | |
af6c2665 |
130 | # Returns the moniker for a given table name, |
131 | # for use in $conn->resultset($moniker) |
fbd83464 |
132 | |
133 | =head3 moniker |
134 | |
135 | Returns the moniker for a given literal table name. Used |
136 | as $schema->resultset($moniker), etc. |
137 | |
138 | =cut |
af6c2665 |
139 | sub moniker { |
a4a19f3c |
140 | my ( $class, $table ) = @_; |
141 | return $class->loader_data->{MONIKERS}->{$table}; |
a78e3fed |
142 | } |
143 | |
fbd83464 |
144 | =head3 debug_loader |
a78e3fed |
145 | |
fbd83464 |
146 | Overload to enable Loader debug messages. |
a78e3fed |
147 | |
148 | =cut |
149 | |
fbd83464 |
150 | sub debug_loader { 0 } |
a78e3fed |
151 | |
152 | =head3 tables |
153 | |
154 | Returns a sorted list of tables. |
155 | |
156 | my @tables = $loader->tables; |
157 | |
158 | =cut |
159 | |
160 | sub tables { |
a4a19f3c |
161 | my $class = shift; |
162 | return sort keys %{ $class->loader_data->{MONIKERS} }; |
a78e3fed |
163 | } |
164 | |
165 | # Overload in your driver class |
166 | sub _db_classes { croak "ABSTRACT METHOD" } |
167 | |
168 | # Setup has_a and has_many relationships |
169 | sub _belongs_to_many { |
a4a19f3c |
170 | my ( $class, $table, $column, $other, $other_column ) = @_; |
171 | my $table_class = $class->_find_table_class($table); |
172 | my $other_class = $class->_find_table_class($other); |
a78e3fed |
173 | |
fbd83464 |
174 | warn qq/\# Belongs_to relationship\n/ if $class->debug_loader; |
a78e3fed |
175 | |
176 | if($other_column) { |
177 | warn qq/$table_class->belongs_to( '$column' => '$other_class',/ |
178 | . qq/ { "foreign.$other_column" => "self.$column" },/ |
179 | . qq/ { accessor => 'filter' });\n\n/ |
fbd83464 |
180 | if $class->debug_loader; |
a78e3fed |
181 | $table_class->belongs_to( $column => $other_class, |
182 | { "foreign.$other_column" => "self.$column" }, |
183 | { accessor => 'filter' } |
184 | ); |
185 | } |
186 | else { |
187 | warn qq/$table_class->belongs_to( '$column' => '$other_class' );\n\n/ |
fbd83464 |
188 | if $class->debug_loader; |
a78e3fed |
189 | $table_class->belongs_to( $column => $other_class ); |
190 | } |
191 | |
192 | my ($table_class_base) = $table_class =~ /.*::(.+)/; |
193 | my $plural = Lingua::EN::Inflect::PL( lc $table_class_base ); |
a4a19f3c |
194 | $plural = $class->loader_data->{_inflect}->{ lc $table_class_base } |
195 | if $class->loader_data->{_inflect} |
196 | and exists $class->loader_data->{_inflect}->{ lc $table_class_base }; |
a78e3fed |
197 | |
fbd83464 |
198 | warn qq/\# Has_many relationship\n/ if $class->debug_loader; |
a78e3fed |
199 | |
200 | if($other_column) { |
201 | warn qq/$other_class->has_many( '$plural' => '$table_class',/ |
202 | . qq/ { "foreign.$column" => "self.$other_column" } );\n\n/ |
fbd83464 |
203 | if $class->debug_loader; |
a78e3fed |
204 | $other_class->has_many( $plural => $table_class, |
205 | { "foreign.$column" => "self.$other_column" } |
206 | ); |
207 | } |
208 | else { |
209 | warn qq/$other_class->has_many( '$plural' => '$table_class',/ |
210 | . qq/'$other_column' );\n\n/ |
fbd83464 |
211 | if $class->debug_loader; |
a78e3fed |
212 | $other_class->has_many( $plural => $table_class, $column ); |
213 | } |
214 | } |
215 | |
216 | # Load and setup classes |
217 | sub _load_classes { |
a4a19f3c |
218 | my $class = shift; |
af6c2665 |
219 | |
a4a19f3c |
220 | my @tables = $class->_tables(); |
221 | my @db_classes = $class->_db_classes(); |
222 | my $additional = join '', map "use $_;\n", @{ $class->loader_data->{_additional} }; |
a78e3fed |
223 | my $additional_base = join '', map "use base '$_';\n", |
a4a19f3c |
224 | @{ $class->loader_data->{_additional_base} }; |
225 | my $left_base = join '', map "use base '$_';\n", @{ $class->loader_data->{_left_base} }; |
226 | my $constraint = $class->loader_data->{_constraint}; |
227 | my $exclude = $class->loader_data->{_exclude}; |
a78e3fed |
228 | |
a78e3fed |
229 | foreach my $table (@tables) { |
230 | next unless $table =~ /$constraint/; |
231 | next if ( defined $exclude && $table =~ /$exclude/ ); |
af6c2665 |
232 | |
af6c2665 |
233 | my ($db_schema, $tbl) = split /\./, $table; |
af96f52e |
234 | my $tablename = lc $table; |
a78e3fed |
235 | if($tbl) { |
af96f52e |
236 | $tablename = $class->loader_data->{_drop_db_schema} ? $tbl : lc $table; |
af6c2665 |
237 | } |
238 | |
65644119 |
239 | my $table_moniker = $class->_table2moniker($db_schema, $tbl); |
240 | my $table_class = "$class\::$table_moniker"; |
af6c2665 |
241 | |
a4a19f3c |
242 | $class->inject_base( $table_class, 'DBIx::Class::Core' ); |
a78e3fed |
243 | $_->require for @db_classes; |
a4a19f3c |
244 | $class->inject_base( $table_class, $_ ) for @db_classes; |
af96f52e |
245 | warn qq/\# Initializing table "$tablename" as "$table_class"\n/ if $class->debug_loader; |
246 | $table_class->table(lc $tablename); |
af6c2665 |
247 | |
af96f52e |
248 | my ( $cols, $pks ) = $class->_table_info($table); |
a78e3fed |
249 | carp("$table has no primary key") unless @$pks; |
a4a19f3c |
250 | $table_class->add_columns(@$cols); |
251 | $table_class->set_primary_key(@$pks) if @$pks; |
af6c2665 |
252 | |
a4a19f3c |
253 | my $code = "package $table_class;\n$additional_base$additional$left_base"; |
fbd83464 |
254 | warn qq/$code/ if $class->debug_loader; |
af96f52e |
255 | warn qq/$table_class->table('$tablename');\n/ if $class->debug_loader; |
a78e3fed |
256 | my $columns = join "', '", @$cols; |
fbd83464 |
257 | warn qq/$table_class->add_columns('$columns')\n/ if $class->debug_loader; |
a78e3fed |
258 | my $primaries = join "', '", @$pks; |
fbd83464 |
259 | warn qq/$table_class->set_primary_key('$primaries')\n/ if $class->debug_loader && @$pks; |
a78e3fed |
260 | eval $code; |
261 | croak qq/Couldn't load additional classes "$@"/ if $@; |
a4a19f3c |
262 | unshift @{"$table_class\::ISA"}, $_ foreach ( @{ $class->loader_data->{_left_base} } ); |
af6c2665 |
263 | |
65644119 |
264 | $class->register_class($table_moniker, $table_class); |
af96f52e |
265 | $class->loader_data->{TABLE_CLASSES}->{lc $tablename} = $table_class; |
65644119 |
266 | $class->loader_data->{MONIKERS}->{lc $tablename} = $table_moniker; |
a78e3fed |
267 | } |
268 | } |
269 | |
270 | # Find and setup relationships |
271 | sub _relationships { |
a4a19f3c |
272 | my $class = shift; |
273 | my $dbh = $class->storage->dbh; |
274 | foreach my $table ( $class->tables ) { |
a78e3fed |
275 | my $quoter = $dbh->get_info(29) || q{"}; |
af96f52e |
276 | if ( my $sth = $dbh->foreign_key_info( '', $class->loader_data->{_db_schema}, '', '', '', $table ) ) { |
a78e3fed |
277 | for my $res ( @{ $sth->fetchall_arrayref( {} ) } ) { |
af96f52e |
278 | my $column = lc $res->{FK_COLUMN_NAME}; |
279 | my $other = lc $res->{UK_TABLE_NAME}; |
280 | my $other_column = lc $res->{UK_COLUMN_NAME}; |
a78e3fed |
281 | $column =~ s/$quoter//g; |
282 | $other =~ s/$quoter//g; |
283 | $other_column =~ s/$quoter//g; |
a4a19f3c |
284 | eval { $class->_belongs_to_many( $table, $column, $other, |
a78e3fed |
285 | $other_column ) }; |
286 | warn qq/\# belongs_to_many failed "$@"\n\n/ |
fbd83464 |
287 | if $@ && $class->debug_loader; |
a78e3fed |
288 | } |
289 | } |
290 | } |
291 | } |
292 | |
65644119 |
293 | # Make a moniker from a table |
294 | sub _table2moniker { |
a4a19f3c |
295 | my ( $class, $db_schema, $table ) = @_; |
af6c2665 |
296 | |
af96f52e |
297 | my $db_schema_ns; |
af6c2665 |
298 | |
af96f52e |
299 | if($table) { |
300 | $db_schema = ucfirst lc $db_schema; |
65644119 |
301 | $db_schema_ns = $db_schema if(!$class->loader_data->{_drop_db_schema}); |
af96f52e |
302 | } else { |
303 | $table = $db_schema; |
a78e3fed |
304 | } |
af6c2665 |
305 | |
65644119 |
306 | my $moniker = join '', map ucfirst, split /[\W_]+/, lc $table; |
307 | $moniker = $db_schema_ns ? $db_schema_ns . $moniker : $moniker; |
af96f52e |
308 | |
65644119 |
309 | return $moniker; |
a78e3fed |
310 | } |
311 | |
312 | # Overload in driver class |
313 | sub _tables { croak "ABSTRACT METHOD" } |
314 | |
315 | sub _table_info { croak "ABSTRACT METHOD" } |
316 | |
317 | =head1 SEE ALSO |
318 | |
18fca96a |
319 | L<DBIx::Class::Schema::Loader> |
a78e3fed |
320 | |
321 | =cut |
322 | |
323 | 1; |