Commit | Line | Data |
ad91060a |
1 | package Catalyst::Helper::Model::DBIC::Schema; |
2 | |
3 | use strict; |
0f2fd2c0 |
4 | use warnings; |
ffbf1967 |
5 | no warnings 'uninitialized'; |
018eb0e2 |
6 | |
2201c2e4 |
7 | our $VERSION = '0.24'; |
8 | |
9 | use parent 'Class::Accessor::Fast'; |
018eb0e2 |
10 | |
0f2fd2c0 |
11 | use Carp; |
781c6876 |
12 | use UNIVERSAL::require; |
2201c2e4 |
13 | use Tie::IxHash (); |
14 | use Data::Dumper (); |
15 | use List::Util (); |
16 | |
17 | __PACKAGE__->mk_accessors(qw/ |
18 | helper schema_class loader_args connect_info _old_schema |
19 | /); |
ad91060a |
20 | |
21 | =head1 NAME |
22 | |
23 | Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models |
24 | |
25 | =head1 SYNOPSIS |
26 | |
2201c2e4 |
27 | script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \ |
28 | [ create=dynamic | create=static ] [ Schema::Loader opts ] \ |
29 | [ dsn user pass ] [ other connect_info arguments ] |
0f2fd2c0 |
30 | |
d89e6c8a |
31 | =head1 DESCRIPTION |
32 | |
33 | Helper for the DBIC Schema Models. |
34 | |
35 | =head2 Arguments: |
36 | |
018eb0e2 |
37 | C<CatalystModelName> is the short name for the Catalyst Model class |
38 | being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>). |
6116daed |
39 | |
018eb0e2 |
40 | C<MyApp::SchemaClass> is the fully qualified classname of your Schema, |
6116daed |
41 | which might or might not yet exist. Note that you should have a good |
42 | reason to create this under a new global namespace, otherwise use an |
43 | existing top level namespace for your schema class. |
44 | |
018eb0e2 |
45 | C<create=dynamic> instructs this Helper to generate the named Schema |
6116daed |
46 | class for you, basing it on L<DBIx::Class::Schema::Loader> (which |
47 | means the table information will always be dynamically loaded at |
48 | runtime from the database). |
49 | |
018eb0e2 |
50 | C<create=static> instructs this Helper to generate the named Schema |
6116daed |
51 | class for you, using L<DBIx::Class::Schema::Loader> in "one shot" |
52 | mode to create a standard, manually-defined L<DBIx::Class::Schema> |
53 | setup, based on what the Loader sees in your database at this moment. |
54 | A Schema/Model pair generated this way will not require |
55 | L<DBIx::Class::Schema::Loader> at runtime, and will not automatically |
56 | adapt itself to changes in your database structure. You can edit |
57 | the generated classes by hand to refine them. |
58 | |
018eb0e2 |
59 | C<connect_info> arguments are the same as what |
6116daed |
60 | DBIx::Class::Schema::connect expects, and are storage_type-specific. |
61 | For DBI-based storage, these arguments are the dsn, username, |
62 | password, and connect options, respectively. These are optional for |
63 | existing Schemas, but required if you use either of the C<create=> |
64 | options. |
d89e6c8a |
65 | |
66 | Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>. |
0b2a7108 |
67 | |
68 | =head1 TYPICAL EXAMPLES |
69 | |
d89e6c8a |
70 | # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema, |
71 | # and a Model which references it: |
2201c2e4 |
72 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
73 | MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass |
74 | |
75 | # Same, with extra connect_info args |
76 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
77 | MyApp::SchemaClass create=static dbi:SQLite:foo.db '' '' \ |
78 | AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \ |
79 | on_connect_do='["select 1", "select 2"]' |
0b2a7108 |
80 | |
ca14239e |
81 | # Same, but with extra Schema::Loader args (separate multiple values by commas): |
2201c2e4 |
82 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
83 | MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \ |
84 | exclude='^wibble|wobble$' moniker_map='{ foo => "FFFFUUUU" }' \ |
85 | dbi:Pg:dbname=foodb myuname mypass |
34f036a0 |
86 | |
ca14239e |
87 | # See DBIx::Class::Schema::Loader::Base for list of options |
34f036a0 |
88 | |
d89e6c8a |
89 | # Create a dynamic DBIx::Class::Schema::Loader-based Schema, |
90 | # and a Model which references it: |
2201c2e4 |
91 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
92 | MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass |
ad91060a |
93 | |
d89e6c8a |
94 | # Reference an existing Schema of any kind, and provide some connection information for ->config: |
2201c2e4 |
95 | script/myapp_create.pl model CatalystModelName DBIC::Schema \ |
96 | MyApp::SchemaClass dbi:mysql:foodb myuname mypass |
ad91060a |
97 | |
d89e6c8a |
98 | # Same, but don't supply connect information yet (you'll need to do this |
99 | # in your app config, or [not recommended] in the schema itself). |
100 | script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass |
ad91060a |
101 | |
018eb0e2 |
102 | =head1 METHODS |
ad91060a |
103 | |
018eb0e2 |
104 | =head2 mk_compclass |
ad91060a |
105 | |
0fbbc8d5 |
106 | This is called by L<Catalyst::Helper> with the commandline args to generate the |
107 | files. |
108 | |
ad91060a |
109 | =cut |
110 | |
111 | sub mk_compclass { |
2201c2e4 |
112 | my ($package, $helper, $schema_class, @args) = @_; |
113 | |
114 | my $self = $package->new; |
0f2fd2c0 |
115 | |
d89e6c8a |
116 | $helper->{schema_class} = $schema_class |
592cd3ae |
117 | or croak "Must supply schema class name"; |
d89e6c8a |
118 | |
2201c2e4 |
119 | $self->schema_class($schema_class); |
120 | $self->helper($helper); |
121 | |
d89e6c8a |
122 | my $create = ''; |
2201c2e4 |
123 | if ($args[0] && $args[0] =~ /^create=(dynamic|static)$/) { |
d89e6c8a |
124 | $create = $1; |
2201c2e4 |
125 | shift @args; |
126 | |
127 | if (@args) { |
128 | $self->_parse_loader_args(\@args); |
129 | |
130 | if (List::Util::first { /dbi:/ } @args) { |
131 | $helper->{setup_connect_info} = 1; |
132 | |
133 | $helper->{connect_info} = |
134 | $self->_build_helper_connect_info(\@args); |
135 | |
136 | $self->_parse_connect_info(\@args) if $create eq 'static'; |
137 | } |
138 | } |
d89e6c8a |
139 | } |
0f2fd2c0 |
140 | |
2201c2e4 |
141 | $helper->{generator} = ref $self; |
142 | $helper->{generator_version} = $VERSION; |
143 | |
144 | if ($create eq 'dynamic') { |
145 | $self->helper->{loader_args} = $self->_build_helper_loader_args; |
146 | $self->_gen_dynamic_schema; |
147 | } elsif ($create eq 'static') { |
148 | $self->_gen_static_schema; |
149 | } |
150 | |
151 | $self->_gen_model; |
152 | } |
153 | |
154 | sub _parse_loader_args { |
155 | my ($self, $args) = @_; |
156 | |
157 | my %loader_args = $self->_read_loader_args($args); |
158 | |
159 | while (my ($key, $val) = each %loader_args) { |
160 | next if $key =~ /^(?:components|constraint|exclude)\z/; |
161 | |
162 | $loader_args{$key} = eval $val; |
0fbbc8d5 |
163 | croak "syntax error for loader args key '$key' with value '$val': $@" |
2201c2e4 |
164 | if $@; |
165 | } |
166 | |
167 | my @components = |
168 | $self->_build_loader_components(delete $loader_args{components}); |
169 | |
170 | for my $re_opt (qw/constraint exclude/) { |
171 | $loader_args{$re_opt} = qr/$loader_args{$re_opt}/ |
172 | if exists $loader_args{$re_opt}; |
173 | } |
174 | |
175 | tie my %result, 'Tie::IxHash'; |
176 | |
177 | %result = ( |
178 | relationships => 1, |
179 | (%loader_args ? %loader_args : ()), |
180 | (!$self->_is_old_schema ? ( |
181 | use_namespaces => 1 |
182 | ) : ()), |
183 | (@components ? ( |
184 | components => \@components |
185 | ) : ()) |
186 | ); |
187 | |
188 | $self->loader_args(\%result); |
189 | |
190 | wantarray ? %result : \%result; |
191 | } |
192 | |
193 | sub _read_loader_args { |
194 | my ($self, $args) = @_; |
195 | |
196 | my %loader_args; |
197 | |
198 | while (@$args && $args->[0] !~ /^dbi:/) { |
199 | my ($key, $val) = split /=/, shift(@$args), 2; |
ca14239e |
200 | |
201 | if ((my @vals = split /,/ => $val) > 1) { |
2201c2e4 |
202 | $loader_args{$key} = \@vals; |
ca14239e |
203 | } else { |
2201c2e4 |
204 | $loader_args{$key} = $val; |
ca14239e |
205 | } |
206 | } |
207 | |
2201c2e4 |
208 | wantarray ? %loader_args : \%loader_args; |
209 | } |
210 | |
211 | sub _build_helper_loader_args { |
212 | my $self = shift; |
213 | |
214 | my $args = $self->loader_args; |
215 | |
216 | tie my %loader_args, 'Tie::IxHash'; |
217 | |
218 | while (my ($arg, $val) = each %$args) { |
219 | if (ref $val) { |
220 | $loader_args{$arg} = $self->_data_struct_to_string($val); |
221 | } else { |
222 | $loader_args{$arg} = qq{'$val'}; |
0b2a7108 |
223 | } |
0f2fd2c0 |
224 | } |
225 | |
2201c2e4 |
226 | \%loader_args |
227 | } |
228 | |
229 | sub _build_loader_components { |
230 | my ($self, $components) = @_; |
d89e6c8a |
231 | |
2201c2e4 |
232 | my @components = $self->_is_old_schema ? () : ('InflateColumn::DateTime'); |
d89e6c8a |
233 | |
2201c2e4 |
234 | if ($components) { |
235 | $components = [ $components ] if !ref $components; |
236 | push @components, @$components; |
d89e6c8a |
237 | } |
2201c2e4 |
238 | |
239 | wantarray ? @components : \@components; |
240 | } |
241 | |
242 | sub _build_helper_connect_info { |
243 | my ($self, $connect_info) = @_; |
244 | |
245 | my @connect_info = @$connect_info; |
246 | |
247 | my ($dsn, $user, $password) = splice @connect_info, 0, 3; |
248 | |
249 | tie my %helper_connect_info, 'Tie::IxHash'; |
250 | |
251 | %helper_connect_info = ( |
252 | dsn => qq{'$dsn'}, |
253 | user => qq{'$user'}, |
254 | password => qq{'$password'} |
255 | ); |
256 | |
257 | for (@connect_info) { |
258 | if (/^\s*{.*}\s*\z/) { |
259 | my $hash = eval $_; |
0fbbc8d5 |
260 | croak "Syntax errorr in connect_info hash: $_: $@" if $@; |
2201c2e4 |
261 | my %hash = %$hash; |
262 | |
263 | for my $key (keys %hash) { |
264 | my $val = $hash{$key}; |
265 | |
266 | if (ref $val) { |
267 | $val = $self->_data_struct_to_string($val); |
268 | } else { |
269 | $val = qq{'$val'}; |
270 | } |
271 | |
272 | $helper_connect_info{$key} = $val; |
592cd3ae |
273 | } |
2201c2e4 |
274 | |
275 | next; |
592cd3ae |
276 | } |
277 | |
2201c2e4 |
278 | my ($key, $val) = split /=/, $_, 2; |
1d155058 |
279 | |
2201c2e4 |
280 | $helper_connect_info{$key} = $self->_quote_unless_struct($val); |
281 | } |
1d155058 |
282 | |
2201c2e4 |
283 | \%helper_connect_info |
284 | } |
1d155058 |
285 | |
2201c2e4 |
286 | sub _data_struct_to_string { |
287 | my ($self, $data) = @_; |
34f036a0 |
288 | |
2201c2e4 |
289 | local $Data::Dumper::Terse = 1; |
290 | local $Data::Dumper::Quotekeys = 0; |
291 | local $Data::Dumper::Indent = 0; |
292 | local $Data::Dumper::Useqq = 1; |
34f036a0 |
293 | |
2201c2e4 |
294 | return Data::Dumper->Dump([$data]); |
295 | } |
34f036a0 |
296 | |
2201c2e4 |
297 | sub _parse_connect_info { |
298 | my ($self, $connect_info) = @_; |
299 | |
300 | my @connect_info = @$connect_info; |
301 | |
302 | my ($dsn, $user, $password) = splice @connect_info, 0, 3; |
303 | |
304 | tie my %connect_info, 'Tie::IxHash'; |
305 | @connect_info{qw/dsn user password/} = ($dsn, $user, $password); |
306 | |
307 | for (@connect_info) { |
308 | if (/^\s*{.*}\s*\z/) { |
309 | my $hash = eval $_; |
0fbbc8d5 |
310 | croak "Syntax errorr in connect_info hash: $_: $@" if $@; |
2201c2e4 |
311 | |
312 | %connect_info = (%connect_info, %$hash); |
ca14239e |
313 | |
2201c2e4 |
314 | next; |
ca14239e |
315 | } |
316 | |
2201c2e4 |
317 | my ($key, $val) = split /=/, $_, 2; |
318 | |
319 | $connect_info{$key} = eval $val; |
0fbbc8d5 |
320 | croak "syntax error for connect_info key '$key' with value '$val': $@" |
2201c2e4 |
321 | if $@; |
d89e6c8a |
322 | } |
202d09c8 |
323 | |
2201c2e4 |
324 | $self->connect_info(\%connect_info); |
325 | |
326 | \%connect_info |
327 | } |
328 | |
329 | sub _quote_unless_struct { |
330 | my ($self, $val) = @_; |
331 | |
332 | $val = qq{'$val'} if $val !~ /^\s*[[{]/; |
333 | |
334 | $val; |
335 | } |
336 | |
337 | sub _gen_dynamic_schema { |
338 | my $self = shift; |
339 | |
340 | my $helper = $self->helper; |
341 | |
342 | my @schema_parts = split(/\:\:/, $self->schema_class); |
343 | my $schema_file_part = pop @schema_parts; |
344 | |
345 | my $schema_dir = File::Spec->catfile( |
346 | $helper->{base}, 'lib', @schema_parts |
347 | ); |
348 | my $schema_file = File::Spec->catfile( |
349 | $schema_dir, $schema_file_part . '.pm' |
350 | ); |
351 | |
352 | $helper->mk_dir($schema_dir); |
353 | $helper->render_file('schemaclass', $schema_file); |
354 | } |
355 | |
356 | sub _gen_static_schema { |
357 | my $self = shift; |
358 | |
0fbbc8d5 |
359 | croak "cannot load schema without connect info" unless $self->connect_info; |
2201c2e4 |
360 | |
361 | my $helper = $self->helper; |
362 | |
363 | my $schema_dir = File::Spec->catfile($helper->{base}, 'lib'); |
364 | |
365 | DBIx::Class::Schema::Loader->use( |
366 | "dump_to_dir:$schema_dir", 'make_schema_at' |
367 | ) or croak "Cannot load DBIx::Class::Schema::Loader: $@"; |
368 | |
369 | make_schema_at( |
370 | $self->schema_class, |
371 | $self->loader_args, |
372 | [$self->connect_info] |
373 | ); |
374 | } |
375 | |
376 | sub _is_old_schema { |
377 | my $self = shift; |
378 | |
379 | return $self->_old_schema if defined $self->_old_schema; |
380 | |
381 | my @schema_pm = split '::', $self->schema_class; |
382 | $schema_pm[-1] .= '.pm'; |
383 | my $schema_file = |
384 | File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm); |
385 | |
386 | if (-f $schema_file) { |
387 | my $schema_code = do { local (@ARGV, $/) = $schema_file; <> }; |
388 | $self->_old_schema(1) if $schema_code =~ /->load_classes/; |
389 | } else { |
390 | $self->_old_schema(0); |
391 | } |
392 | |
393 | return $self->_old_schema; |
394 | } |
395 | |
396 | sub _gen_model { |
397 | my $self = shift; |
398 | my $helper = $self->helper; |
399 | |
400 | $helper->render_file('compclass', $helper->{file} ); |
ad91060a |
401 | } |
402 | |
403 | =head1 SEE ALSO |
404 | |
7b39f3f0 |
405 | General Catalyst Stuff: |
406 | |
ad91060a |
407 | L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>, |
7b39f3f0 |
408 | L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>, |
409 | |
410 | Stuff related to DBIC and this Model style: |
411 | |
412 | L<DBIx::Class>, L<DBIx::Class::Schema>, |
ef91bcf9 |
413 | L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema> |
ad91060a |
414 | |
415 | =head1 AUTHOR |
416 | |
417 | Brandon L Black, C<blblack@gmail.com> |
418 | |
419 | =head1 LICENSE |
420 | |
421 | This library is free software, you can redistribute it and/or modify |
422 | it under the same terms as Perl itself. |
423 | |
424 | =cut |
425 | |
dce0dfe8 |
426 | 1; |
427 | |
ad91060a |
428 | __DATA__ |
429 | |
5d11d759 |
430 | =begin pod_to_ignore |
431 | |
d89e6c8a |
432 | __schemaclass__ |
433 | package [% schema_class %]; |
434 | |
435 | use strict; |
436 | use base qw/DBIx::Class::Schema::Loader/; |
437 | |
438 | __PACKAGE__->loader_options( |
2201c2e4 |
439 | [%- FOREACH key = loader_args.keys %] |
440 | [% key %] => [% loader_args.${key} %], |
441 | [%- END -%] |
442 | |
d89e6c8a |
443 | ); |
444 | |
445 | =head1 NAME |
446 | |
2201c2e4 |
447 | [% schema_class %] - L<DBIx::Class::Schema::Loader> class |
d89e6c8a |
448 | |
449 | =head1 SYNOPSIS |
450 | |
451 | See L<[% app %]> |
452 | |
453 | =head1 DESCRIPTION |
454 | |
2201c2e4 |
455 | Dynamic L<DBIx::Class::Schema::Loader> schema for use in L<[% class %]> |
456 | |
457 | =head1 GENERATED BY |
458 | |
459 | [% generator %] - [% generator_version %] |
d89e6c8a |
460 | |
461 | =head1 AUTHOR |
462 | |
2201c2e4 |
463 | [% author.replace(',+$', '') %] |
d89e6c8a |
464 | |
465 | =head1 LICENSE |
466 | |
467 | This library is free software, you can redistribute it and/or modify |
468 | it under the same terms as Perl itself. |
469 | |
470 | =cut |
471 | |
472 | 1; |
473 | |
ad91060a |
474 | __compclass__ |
475 | package [% class %]; |
476 | |
477 | use strict; |
478 | use base 'Catalyst::Model::DBIC::Schema'; |
479 | |
480 | __PACKAGE__->config( |
0f2fd2c0 |
481 | schema_class => '[% schema_class %]', |
2201c2e4 |
482 | [% IF setup_connect_info %]connect_info => { |
483 | [%- FOREACH key = connect_info.keys %] |
484 | [% key %] => [% connect_info.${key} %], |
485 | [%- END -%] |
486 | |
487 | }[% END %] |
ad91060a |
488 | ); |
489 | |
490 | =head1 NAME |
491 | |
1aeb6e1e |
492 | [% class %] - Catalyst DBIC Schema Model |
2201c2e4 |
493 | |
ad91060a |
494 | =head1 SYNOPSIS |
495 | |
496 | See L<[% app %]> |
497 | |
498 | =head1 DESCRIPTION |
499 | |
d89e6c8a |
500 | L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]> |
ad91060a |
501 | |
2201c2e4 |
502 | =head1 GENERATED BY |
503 | |
504 | [% generator %] - [% generator_version %] |
505 | |
ad91060a |
506 | =head1 AUTHOR |
507 | |
2201c2e4 |
508 | [% author.replace(',+$', '') %] |
ad91060a |
509 | |
510 | =head1 LICENSE |
511 | |
512 | This library is free software, you can redistribute it and/or modify |
513 | it under the same terms as Perl itself. |
514 | |
515 | =cut |
516 | |
517 | 1; |