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