5062ec1c8cdfa62d1100ecea984318dbde0b590e
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
1 package Catalyst::Helper::Model::DBIC::Schema;
2
3 use namespace::autoclean;
4 use Moose;
5 no warnings 'uninitialized';
6
7 our $VERSION = '0.51';
8 $VERSION = eval $VERSION;
9
10 use Carp;
11 use Tie::IxHash ();
12 use Data::Dumper ();
13 use List::Util 'first';
14 use MooseX::Types::Moose qw/Str HashRef Bool ArrayRef/;
15 use Catalyst::Model::DBIC::Schema::Types 'CreateOption';
16 use List::MoreUtils 'firstidx';
17 use Scalar::Util 'looks_like_number';
18 use File::Find 'finddepth';
19 use Try::Tiny;
20 use Cwd 'getcwd';
21
22 =head1 NAME
23
24 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
25
26 =head1 SYNOPSIS
27
28   script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \
29     [ create=dynamic | create=static ] [ traits=trait1,trait2... ] \
30     [ Schema::Loader opts ] [ dsn user pass ] \
31     [ other connect_info args ]
32
33 =head1 DESCRIPTION
34
35 Helper for the DBIC Schema Models.
36
37 =head2 Arguments:
38
39 C<CatalystModelName> is the short name for the Catalyst Model class
40 being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>).
41
42 C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
43 which might or might not yet exist.  Note that you should have a good
44 reason to create this under a new global namespace, otherwise use an
45 existing top level namespace for your schema class.
46
47 C<create=dynamic> instructs this Helper to generate the named Schema
48 class for you, basing it on L<DBIx::Class::Schema::Loader> (which
49 means the table information will always be dynamically loaded at
50 runtime from the database).
51
52 C<create=static> instructs this Helper to generate the named Schema
53 class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
54 mode to create a standard, manually-defined L<DBIx::Class::Schema>
55 setup, based on what the Loader sees in your database at this moment.
56 A Schema/Model pair generated this way will not require
57 L<DBIx::Class::Schema::Loader> at runtime, and will not automatically
58 adapt itself to changes in your database structure.  You can edit
59 the generated classes by hand to refine them.
60
61 C<traits> is the list of traits to apply to the model, see
62 L<Catalyst::Model::DBIC::Schema> for details.
63
64 C<Schema::Loader opts> are documented in L<DBIx::Class::Schema::Loader::Base>
65 and some examples are given in L</TYPICAL EXAMPLES> below.
66
67 C<connect_info> arguments are the same as what L<DBIx::Class::Schema/connect>
68 expects, and are storage_type-specific. They are documented in
69 L<DBIx::Class::Storage::DBI/connect_info>. For DBI-based storage, these
70 arguments are the dsn, username, password, and connect options, respectively.
71 These are optional for existing Schemas, but required if you use either of the
72 C<create=> options.
73
74 username and password can be omitted for C<SQLite> dsns.
75
76 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
77
78 =head1 TYPICAL EXAMPLES
79
80 Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
81 and a Model which references it:
82
83   script/myapp_create.pl model CatalystModelName DBIC::Schema \
84     MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
85
86 Same, with extra connect_info args
87 user and pass can be omitted for sqlite, since they are always empty
88
89   script/myapp_create.pl model CatalystModelName DBIC::Schema \
90     MyApp::SchemaClass create=static dbi:SQLite:foo.db \
91     AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
92     on_connect_do='["select 1", "select 2"]' quote_names=1
93
94 B<ON WINDOWS COMMAND LINES QUOTING RULES ARE DIFFERENT>
95
96 In C<cmd.exe> the above example would be:
97
98   script/myapp_create.pl model CatalystModelName DBIC::Schema \
99     MyApp::SchemaClass create=static dbi:SQLite:foo.db \
100     AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
101     on_connect_do="[\"select 1\", \"select 2\"]" quote_names=1
102
103 Same, but with extra Schema::Loader args (separate multiple values by commas):
104
105   script/myapp_create.pl model CatalystModelName DBIC::Schema \
106     MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \
107     exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \
108     dbi:Pg:dbname=foodb myuname mypass
109
110 Coderefs are also supported:
111
112   script/myapp_create.pl model CatalystModelName DBIC::Schema \
113     MyApp::SchemaClass create=static \
114     inflect_singular='sub { $_[0] =~ /\A(.+?)(_id)?\z/; $1 }' \
115     moniker_map='sub { join(q{}, map ucfirst, split(/[\W_]+/, lc $_[0])); }' \
116     dbi:mysql:foodb myuname mypass
117
118 See L<DBIx::Class::Schema::Loader::Base> for a list of options
119
120 Create a dynamic DBIx::Class::Schema::Loader-based Schema,
121 and a Model which references it (B<DEPRECATED>):
122
123   script/myapp_create.pl model CatalystModelName DBIC::Schema \
124     MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
125
126 Reference an existing Schema of any kind, and provide some connection information for ->config:
127
128   script/myapp_create.pl model CatalystModelName DBIC::Schema \
129     MyApp::SchemaClass dbi:mysql:foodb myuname mypass
130
131 Same, but don't supply connect information yet (you'll need to do this
132 in your app config, or [not recommended] in the schema itself).
133
134   script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
135
136 =cut
137
138 has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1);
139 has create => (is => 'rw', isa => CreateOption);
140 has args => (is => 'ro', isa => ArrayRef);
141 has traits => (is => 'rw', isa => ArrayRef);
142 has schema_class => (is => 'ro', isa => Str, required => 1);
143 has loader_args => (is => 'rw', isa => HashRef);
144 has connect_info => (is => 'rw', isa => HashRef);
145 has old_schema => (is => 'rw', isa => Bool, lazy_build => 1);
146 has is_moose_schema => (is => 'rw', isa => Bool, lazy_build => 1);
147 has result_namespace => (is => 'rw', isa => Str, lazy_build => 1);
148 has components => (is => 'rw', isa => ArrayRef);
149
150 =head1 METHODS
151
152 =head2 mk_compclass
153
154 This is called by L<Catalyst::Helper> with the commandline args to generate the
155 files.
156
157 =cut
158
159 sub mk_compclass {
160     my ($package, $helper, $schema_class, @args) = @_;
161
162     my $self = $package->new(
163         helper => $helper,
164         schema_class => $schema_class,
165         args => \@args
166     );
167
168     $self->run;
169 }
170
171 sub BUILD {
172     my $self   = shift;
173     my $helper = $self->helper;
174     my @args   = @{ $self->args || [] };
175
176     $helper->{schema_class} = $self->schema_class;
177
178     @args = $self->_cleanup_args(\@args);
179
180     my ($traits_idx, $traits);
181     if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) {
182         my @traits = split /,/ => $traits;
183
184         $self->traits(\@traits);
185
186         $helper->{traits} = '['
187             .(join ',' => map { qq{'$_'} } @traits)
188             .']';
189
190         splice @args, $traits_idx, 1, ();
191     }
192
193     if ($args[0] && $args[0] =~ /^create=(\S*)\z/) {
194         $self->create($1);
195         shift @args;
196
197         if (@args) {
198             $self->_parse_loader_args(\@args);
199
200             $helper->{loader_args} = $self->_build_helper_loader_args;
201         }
202     }
203
204     my $dbi_dsn_part;
205     if (first { ($dbi_dsn_part) = /^(dbi):/i } @args) {
206         die
207 qq{DSN must start with 'dbi:' not '$dbi_dsn_part' (case matters!)}
208             if $dbi_dsn_part ne 'dbi';
209
210         $helper->{setup_connect_info} = 1;
211
212         $helper->{connect_info} =
213             $self->_build_helper_connect_info(\@args);
214
215         $self->_parse_connect_info(\@args);
216     }
217
218     $helper->{generator} = ref $self;
219     $helper->{generator_version} = $VERSION;
220 }
221
222 =head2 run
223
224 Can be called on an instance to generate the files.
225
226 =cut
227
228 sub run {
229     my $self = shift;
230
231     if ($self->create eq 'dynamic') {
232         $self->_print_dynamic_deprecation_warning;
233         $self->_gen_dynamic_schema;
234     } elsif ($self->create eq 'static') {
235         $self->_gen_static_schema;
236     }
237
238     $self->_gen_model;
239 }
240
241 sub _parse_loader_args {
242     my ($self, $args) = @_;
243
244     my %loader_args = $self->_read_loader_args($args);
245
246     while (my ($key, $val) = each %loader_args) {
247         next if $key =~ /^(?:components|constraint|exclude)\z/;
248
249         $loader_args{$key} = $self->_eval($val);
250         die "syntax error for loader args key '$key' with value '$val': $@"
251             if $@;
252     }
253
254     my @components = $self->_build_loader_components(
255         delete $loader_args{components},
256         $loader_args{use_namespaces},
257     );
258
259     $self->components(\@components);
260
261     for my $re_opt (qw/constraint exclude/) {
262         $loader_args{$re_opt} = qr/$loader_args{$re_opt}/
263         if exists $loader_args{$re_opt};
264     }
265
266     tie my %result, 'Tie::IxHash';
267
268     %result = (
269         relationships => 1,
270         use_moose => $self->is_moose_schema ? 1 : 0,
271         col_collision_map => 'column_%s',
272         (!$self->old_schema ? (
273                 use_namespaces => 1
274             ) : ()),
275         (@components ? (
276                 components => \@components
277             ) : ()),
278         (%loader_args ? %loader_args : ()),
279     );
280
281     $self->loader_args(\%result);
282
283     wantarray ? %result : \%result;
284 }
285
286 sub _read_loader_args {
287     my ($self, $args) = @_;
288
289     my %loader_args;
290
291     while (@$args && $args->[0] !~ /^dbi:/i) {
292         my ($key, $val) = split /=/, shift(@$args), 2;
293
294         if ($self->_is_struct($val)) {
295             $loader_args{$key} = $val;
296         } elsif ((my @vals = split /,/ => $val) > 1) {
297             $loader_args{$key} = \@vals;
298         } else {
299             $loader_args{$key} = $val;
300         }
301     }
302
303     # Use args after connect_info as loader args as well, because people always
304     # get the order confused.
305     my $i = 1;
306     if ($args->[0] =~ /sqlite/i) {
307         $i++ if $args->[$i] eq '';
308         $i++ if $args->[$i] eq '';
309     }
310     else {
311         $i += 2;
312     }
313
314     while (defined $args->[$i]) {
315         my ($key, $val) = split /=/, $args->[$i++], 2;
316
317         if ($self->_is_struct($val)) {
318             $loader_args{$key} = $val;
319         } elsif ((my @vals = split /,/ => $val) > 1) {
320             $loader_args{$key} = \@vals;
321         } else {
322             $loader_args{$key} = $val;
323         }
324     }
325
326     wantarray ? %loader_args : \%loader_args;
327 }
328
329 sub _build_helper_loader_args {
330     my $self = shift;
331
332     my $args = $self->loader_args;
333
334     tie my %loader_args, 'Tie::IxHash';
335
336     while (my ($arg, $val) = each %$args) {
337         if (ref $val) {
338             $loader_args{$arg} = $self->_data_struct_to_string($val);
339         } else {
340             $loader_args{$arg} = qq{'$val'};
341         }
342     }
343
344     \%loader_args
345 }
346
347 sub _build_loader_components {
348     my ($self, $components, $use_namespaces) = @_;
349
350     my @components = $self->old_schema && (not $use_namespaces) ? ()
351         : ('InflateColumn::DateTime');
352
353     if ($components) {
354         $components = [ $components ] if !ref $components;
355         push @components, @$components;
356     }
357
358     wantarray ? @components : \@components;
359 }
360
361 sub _build_helper_connect_info {
362     my ($self, $connect_info) = @_;
363
364     my @connect_info = @$connect_info;
365
366     my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info);
367
368     tie my %helper_connect_info, 'Tie::IxHash';
369
370     %helper_connect_info = (
371         dsn => qq{'$dsn'},
372         user => qq{'$user'},
373         password => qq{'$password'}
374     );
375
376     for (@connect_info) {
377         if (/^\s*{.*}\s*\z/) {
378             my $hash = $self->_eval($_);
379             die "Syntax errorr in connect_info hash: $_: $@" if $@;
380             my %hash = %$hash;
381
382             for my $key (keys %hash) {
383                 my $val = $hash{$key};
384
385                 if (ref $val) {
386                     $val = $self->_data_struct_to_string($val);
387                 } else {
388                     $val = $self->_quote($val);
389                 }
390
391                 $helper_connect_info{$key} = $val;
392             }
393
394             next;
395         }
396
397         my ($key, $val) = split /=/, $_, 2;
398
399         if ($key eq 'quote_char') {
400             $helper_connect_info{$key} = length($val) == 1 ?
401                 $self->_quote($val) :
402                 $self->_data_struct_to_string([split //, $val]);
403         } else {
404             $helper_connect_info{$key} = $self->_quote_unless_struct($val);
405         }
406     }
407
408     \%helper_connect_info
409 }
410
411 sub _build_old_schema {
412     my $self = shift;
413
414     return $self->result_namespace eq '' ? 1 : 0;
415 }
416
417 sub _build_is_moose_schema {
418     my $self = shift;
419
420     my @schema_parts = split '::', $self->schema_class;
421
422     my $result_dir = File::Spec->catfile(
423         $self->helper->{base}, 'lib', @schema_parts, $self->result_namespace
424     );
425
426     # assume yes for new schemas
427     return 1 if not -d $result_dir;
428
429     my $uses_moose = 1;
430
431     my $cwd = getcwd;
432
433     try {
434         finddepth(sub {
435             return if $File::Find::name !~ /\.pm\z/;
436
437             open my $fh, '<', $File::Find::name
438                 or die "Could not open $File::Find::name: $!";
439
440             my $code = do { local $/; <$fh> };
441             close $fh;
442
443             $uses_moose = 0 if $code !~ /\nuse Moose;\n/;
444
445             die;
446         }, $result_dir);
447     };
448
449     chdir $cwd;
450
451     return $uses_moose;
452 }
453
454 sub _build_result_namespace {
455     my $self = shift;
456
457     my @schema_parts = split '::', $self->schema_class;
458     my $schema_pm =
459         File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts) . '.pm';
460
461     if (not -f $schema_pm) {
462         eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
463
464         return 'Result' if $@;
465
466         return (try { DBIx::Class::Schema::Loader->VERSION('0.05') }) ? 'Result' : '';
467     }
468
469     open my $fh, '<', $schema_pm or die "Could not open $schema_pm: $!";
470     my $code = do { local $/; <$fh> };
471     close $fh;
472
473     my ($result_namespace) = $code =~ /result_namespace => '([^']+)'/;
474
475     return $result_namespace if $result_namespace;
476
477     return '' if $code =~ /->load_classes/;
478
479     return 'Result';
480 }
481
482 sub _data_struct_to_string {
483     my ($self, $data) = @_;
484
485     local $Data::Dumper::Terse = 1;
486     local $Data::Dumper::Quotekeys = 0;
487     local $Data::Dumper::Indent = 0;
488     local $Data::Dumper::Useqq = 1;
489
490     return Data::Dumper->Dump([$data]);
491 }
492
493 sub _get_dsn_user_pass {
494     my ($self, $connect_info) = @_;
495
496     my $dsn = shift @$connect_info;
497     my ($user, $password);
498
499     if ($dsn =~ /sqlite/i) {
500         ($user, $password) = ('', '');
501         shift @$connect_info while @$connect_info and $connect_info->[0] eq '';
502     } else {
503         ($user, $password) = splice @$connect_info, 0, 2;
504     }
505     
506     ($dsn, $user, $password)
507 }
508
509 sub _parse_connect_info {
510     my ($self, $connect_info) = @_;
511
512     my @connect_info = @$connect_info;
513
514     my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info);
515
516     tie my %connect_info, 'Tie::IxHash';
517     @connect_info{qw/dsn user password/} = ($dsn, $user, $password);
518
519     for (@connect_info) {
520         if (/^\s*{.*}\s*\z/) {
521             my $hash = $self->_eval($_);
522             die "Syntax errorr in connect_info hash: $_: $@" if $@;
523
524             %connect_info = (%connect_info, %$hash);
525
526             next;
527         }
528
529         my ($key, $val) = split /=/, $_, 2;
530
531         if ($key eq 'quote_char') {
532             $connect_info{$key} = length($val) == 1 ? $val : [split //, $val];
533         } elsif ($key =~ /^(?:name_sep|limit_dialect)\z/) {
534             $connect_info{$key} = $val;
535         } else {
536             $connect_info{$key} = $self->_eval($val);
537         }
538
539         die "syntax error for connect_info key '$key' with value '$val': $@"
540             if $@;
541     }
542
543     $self->connect_info(\%connect_info);
544
545     \%connect_info
546 }
547
548 sub _is_struct {
549     my ($self, $val) = @_;
550
551     return $val =~ /^\s*(?:sub|[[{])/;
552 }
553
554 sub _quote {
555     my ($self, $val) = @_;
556
557     return 'q{'.$val.'}';
558 }
559
560 sub _quote_unless_struct {
561     my ($self, $val) = @_;
562
563     $val = $self->_quote($val) if not $self->_is_struct($val);
564
565     return $val;
566 }
567
568 sub _eval {
569     my ($self, $code) = @_;
570
571     return $code if looks_like_number $code;
572
573     return $code if not $self->_is_struct($code);
574
575     return eval "{no strict; $code}";
576 }
577
578 sub _gen_dynamic_schema {
579     my $self = shift;
580
581     my $helper = $self->helper;
582
583     my @schema_parts = split(/\:\:/, $self->schema_class);
584     my $schema_file_part = pop @schema_parts;
585
586     my $schema_dir  = File::Spec->catfile(
587         $helper->{base}, 'lib', @schema_parts
588     );
589     my $schema_file = File::Spec->catfile(
590         $schema_dir, $schema_file_part . '.pm'
591     );
592
593     $helper->mk_dir($schema_dir);
594     $helper->render_file('schemaclass', $schema_file);
595 }
596
597 sub _gen_static_schema {
598     my $self = shift;
599
600     die "cannot load schema without connect info" unless $self->connect_info;
601
602     my $helper = $self->helper;
603
604     my $schema_dir = File::Spec->catfile($helper->{base}, 'lib');
605
606     try {
607         Class::MOP::load_class('DBIx::Class::Schema::Loader')
608     }
609     catch {
610         die "Cannot load DBIx::Class::Schema::Loader: $_";
611     };
612
613     DBIx::Class::Schema::Loader->import(
614         "dump_to_dir:$schema_dir", 'make_schema_at'
615     );
616
617     make_schema_at(
618         $self->schema_class,
619         $self->loader_args,
620         [$self->connect_info]
621     );
622
623     require lib;
624     lib->import($schema_dir);
625
626     Class::MOP::load_class($self->schema_class);
627
628     my @sources = $self->schema_class->sources;
629
630     if (not @sources) {
631         warn <<'EOF';
632 WARNING: No tables found, did you forget to specify db_schema?
633 EOF
634     }
635 }
636
637 sub _gen_model {
638     my $self = shift;
639     my $helper = $self->helper;
640
641     $helper->render_file('compclass', $helper->{file} );
642 }
643
644 sub _print_dynamic_deprecation_warning {
645     warn <<EOF;
646 ************************************ WARNING **********************************
647 * create=dynamic is DEPRECATED, please use create=static instead.             *
648 *******************************************************************************
649 EOF
650     print "Continue? [y/n]: ";
651     chomp(my $response = <STDIN>);
652     exit 0 if $response =~ /^n(o)?\z/;
653 }
654
655 sub _cleanup_args {
656     my ($self, $args) = @_;
657
658 # remove blanks, ie. someoned doing foo \  bar
659     my @res = grep !/^\s+\z/, @$args;
660
661 # remove leading whitespace, ie. foo \ bar
662     s/^\s*// for @res;
663
664     @res
665 }
666
667 =head1 SEE ALSO
668
669 General Catalyst Stuff:
670
671 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
672 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
673
674 Stuff related to DBIC and this Model style:
675
676 L<DBIx::Class>, L<DBIx::Class::Schema>,
677 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
678
679 =head1 AUTHOR
680
681 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
682 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
683
684 =head1 COPYRIGHT
685
686 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
687
688 =head1 LICENSE
689
690 This library is free software, you can redistribute it and/or modify
691 it under the same terms as Perl itself.
692
693 =cut
694
695 1;
696
697 __DATA__
698
699 =begin pod_to_ignore
700
701 __schemaclass__
702 package [% schema_class %];
703
704 use strict;
705 use base qw/DBIx::Class::Schema::Loader/;
706
707 __PACKAGE__->loader_options(
708     [%- FOREACH key = loader_args.keys %]
709     [% key %] => [% loader_args.${key} %],
710     [%- END -%]
711
712 );
713
714 =head1 NAME
715
716 [% schema_class %] - L<DBIx::Class::Schema::Loader> class
717
718 =head1 SYNOPSIS
719
720 See L<[% app %]>
721
722 =head1 DESCRIPTION
723
724 Dynamic L<DBIx::Class::Schema::Loader> schema for use in L<[% class %]>
725
726 =head1 GENERATED BY
727
728 [% generator %] - [% generator_version %]
729
730 =head1 AUTHOR
731
732 [% author.replace(',+$', '') %]
733
734 =head1 LICENSE
735
736 This library is free software, you can redistribute it and/or modify
737 it under the same terms as Perl itself.
738
739 =cut
740
741 1;
742
743 __compclass__
744 package [% class %];
745
746 use strict;
747 use base 'Catalyst::Model::DBIC::Schema';
748
749 __PACKAGE__->config(
750     schema_class => '[% schema_class %]',
751     [% IF traits %]traits => [% traits %],[% END %]
752     [% IF setup_connect_info %]connect_info => {
753         [%- FOREACH key = connect_info.keys %]
754         [% key %] => [% connect_info.${key} %],
755         [%- END -%]
756
757     }[% END %]
758 );
759
760 =head1 NAME
761
762 [% class %] - Catalyst DBIC Schema Model
763
764 =head1 SYNOPSIS
765
766 See L<[% app %]>
767
768 =head1 DESCRIPTION
769
770 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
771
772 =head1 GENERATED BY
773
774 [% generator %] - [% generator_version %]
775
776 =head1 AUTHOR
777
778 [% author.replace(',+$', '') %]
779
780 =head1 LICENSE
781
782 This library is free software, you can redistribute it and/or modify
783 it under the same terms as Perl itself.
784
785 =cut
786
787 1;
788 __END__
789 # vim:sts=4 sw=4: