1 package Catalyst::Helper::Model::DBIC::Schema;
4 no warnings 'uninitialized';
11 use List::Util 'first';
12 use MooseX::Types::Moose qw/Str HashRef Bool ArrayRef/;
13 use Catalyst::Model::DBIC::Schema::Types 'CreateOption';
15 use List::MoreUtils 'firstidx';
16 use Scalar::Util 'looks_like_number';
18 use namespace::clean -except => 'meta';
22 Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
26 script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \
27 [ create=dynamic | create=static ] [ traits=trait1,trait2... ] \
28 [ Schema::Loader opts ] [ dsn user pass ] \
29 [ other connect_info args ]
33 Helper for the DBIC Schema Models.
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')>).
40 C<MyApp::SchemaClass> is the fully qualified classname of your Schema,
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.
45 C<create=dynamic> instructs this Helper to generate the named Schema
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).
50 C<create=static> instructs this Helper to generate the named Schema
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.
59 C<traits> is the list of traits to apply to the model, see
60 L<Catalyst::Model::DBIC::Schema> for details.
62 C<Schema::Loader opts> are described in L</TYPICAL EXAMPLES> below.
64 C<connect_info> arguments are the same as what
65 DBIx::Class::Schema::connect expects, and are storage_type-specific.
66 For DBI-based storage, these arguments are the dsn, username,
67 password, and connect options, respectively. These are optional for
68 existing Schemas, but required if you use either of the C<create=>
71 username and password can be omitted for C<SQLite> dsns.
73 Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
75 =head1 TYPICAL EXAMPLES
77 Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema,
78 and a Model which references it:
80 script/myapp_create.pl model CatalystModelName DBIC::Schema \
81 MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass
83 Same, with extra connect_info args
84 user and pass can be omitted for sqlite, since they are always empty
86 script/myapp_create.pl model CatalystModelName DBIC::Schema \
87 MyApp::SchemaClass create=static dbi:SQLite:foo.db \
88 AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
89 on_connect_do='["select 1", "select 2"]' quote_char='"'
91 If using a 2 character quote_char:
93 script/myapp_create.pl ... quote_char='[]'
95 B<ON WINDOWS COMMAND LINES QUOTING RULES ARE DIFFERENT>
97 In C<cmd.exe> the above example would be:
99 script/myapp_create.pl model CatalystModelName DBIC::Schema \
100 MyApp::SchemaClass create=static dbi:SQLite:foo.db \
101 AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
102 on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""
104 Same, but with extra Schema::Loader args (separate multiple values by commas):
106 script/myapp_create.pl model CatalystModelName DBIC::Schema \
107 MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \
108 exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \
109 dbi:Pg:dbname=foodb myuname mypass
111 See L<DBIx::Class::Schema::Loader::Base> for a list of options
113 Create a dynamic DBIx::Class::Schema::Loader-based Schema,
114 and a Model which references it (B<DEPRECATED>):
116 script/myapp_create.pl model CatalystModelName DBIC::Schema \
117 MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass
119 Reference an existing Schema of any kind, and provide some connection information for ->config:
121 script/myapp_create.pl model CatalystModelName DBIC::Schema \
122 MyApp::SchemaClass dbi:mysql:foodb myuname mypass
124 Same, but don't supply connect information yet (you'll need to do this
125 in your app config, or [not recommended] in the schema itself).
127 script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
131 has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1);
132 has create => (is => 'rw', isa => CreateOption);
133 has args => (is => 'ro', isa => ArrayRef);
134 has traits => (is => 'rw', isa => ArrayRef);
135 has schema_class => (is => 'ro', isa => Str, required => 1);
136 has loader_args => (is => 'rw', isa => HashRef);
137 has connect_info => (is => 'rw', isa => HashRef);
138 has old_schema => (is => 'rw', isa => Bool, lazy_build => 1);
139 has components => (is => 'rw', isa => ArrayRef);
145 This is called by L<Catalyst::Helper> with the commandline args to generate the
151 my ($package, $helper, $schema_class, @args) = @_;
153 my $self = $package->new(
155 schema_class => $schema_class,
164 my $helper = $self->helper;
165 my @args = $self->args->flatten if $self->args;
167 $helper->{schema_class} = $self->schema_class;
169 @args = $self->_cleanup_args(\@args);
171 my ($traits_idx, $traits);
172 if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) {
173 my @traits = split /,/ => $traits;
175 $self->traits(\@traits);
177 $helper->{traits} = '['
178 .(join ',' => map { qq{'$_'} } ($self->traits->flatten))
181 splice @args, $traits_idx, 1, ();
184 if ($args[0] && $args[0] =~ /^create=(\S*)\z/) {
189 $self->_parse_loader_args(\@args);
191 $helper->{loader_args} = $self->_build_helper_loader_args;
193 if (first { /^dbi:/i } @args) {
194 $helper->{setup_connect_info} = 1;
196 $helper->{connect_info} =
197 $self->_build_helper_connect_info(\@args);
199 $self->_parse_connect_info(\@args);
204 $helper->{generator} = ref $self;
205 $helper->{generator_version} = $VERSION;
210 Can be called on an instance to generate the files.
217 if ($self->create eq 'dynamic') {
218 $self->_print_dynamic_deprecation_warning;
219 $self->_gen_dynamic_schema;
220 } elsif ($self->create eq 'static') {
221 $self->_gen_static_schema;
227 sub _parse_loader_args {
228 my ($self, $args) = @_;
230 my %loader_args = $self->_read_loader_args($args);
232 while (my ($key, $val) = each %loader_args) {
233 next if $key =~ /^(?:components|constraint|exclude)\z/;
235 $loader_args{$key} = $self->_eval($val);
236 die "syntax error for loader args key '$key' with value '$val': $@"
241 $self->_build_loader_components(delete $loader_args{components});
243 $self->components(\@components);
245 for my $re_opt (qw/constraint exclude/) {
246 $loader_args{$re_opt} = qr/$loader_args{$re_opt}/
247 if exists $loader_args{$re_opt};
250 tie my %result, 'Tie::IxHash';
254 (%loader_args ? %loader_args : ()),
255 (!$self->old_schema ? (
259 components => \@components
263 $self->loader_args(\%result);
265 wantarray ? %result : \%result;
268 sub _read_loader_args {
269 my ($self, $args) = @_;
273 while (@$args && $args->[0] !~ /^dbi:/) {
274 my ($key, $val) = split /=/, shift(@$args), 2;
276 if ($self->_is_struct($val)) {
277 $loader_args{$key} = $val;
278 } elsif ((my @vals = split /,/ => $val) > 1) {
279 $loader_args{$key} = \@vals;
281 $loader_args{$key} = $val;
285 wantarray ? %loader_args : \%loader_args;
288 sub _build_helper_loader_args {
291 my $args = $self->loader_args;
293 tie my %loader_args, 'Tie::IxHash';
295 while (my ($arg, $val) = each %$args) {
297 $loader_args{$arg} = $self->_data_struct_to_string($val);
299 $loader_args{$arg} = qq{'$val'};
306 sub _build_loader_components {
307 my ($self, $components) = @_;
309 my @components = $self->old_schema ? () : ('InflateColumn::DateTime');
312 $components = [ $components ] if !ref $components;
313 push @components, @$components;
316 wantarray ? @components : \@components;
319 sub _build_helper_connect_info {
320 my ($self, $connect_info) = @_;
322 my @connect_info = @$connect_info;
324 my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info);
326 tie my %helper_connect_info, 'Tie::IxHash';
328 %helper_connect_info = (
331 password => qq{'$password'}
334 for (@connect_info) {
335 if (/^\s*{.*}\s*\z/) {
336 my $hash = $self->_eval($_);
337 die "Syntax errorr in connect_info hash: $_: $@" if $@;
340 for my $key (keys %hash) {
341 my $val = $hash{$key};
344 $val = $self->_data_struct_to_string($val);
346 $val = $self->_quote($val);
349 $helper_connect_info{$key} = $val;
355 my ($key, $val) = split /=/, $_, 2;
357 if ($key eq 'quote_char') {
358 $helper_connect_info{$key} = length($val) == 1 ?
359 $self->_quote($val) :
360 $self->_data_struct_to_string([split //, $val]);
362 $helper_connect_info{$key} = $self->_quote_unless_struct($val);
366 \%helper_connect_info
369 sub _build_old_schema {
372 my @schema_pm = split '::', $self->schema_class;
373 $schema_pm[-1] .= '.pm';
375 File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm);
377 if (-f $schema_file) {
378 my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
379 return 1 if $schema_code =~ /->load_classes/;
385 sub _data_struct_to_string {
386 my ($self, $data) = @_;
388 local $Data::Dumper::Terse = 1;
389 local $Data::Dumper::Quotekeys = 0;
390 local $Data::Dumper::Indent = 0;
391 local $Data::Dumper::Useqq = 1;
393 return Data::Dumper->Dump([$data]);
396 sub _get_dsn_user_pass {
397 my ($self, $connect_info) = @_;
399 my $dsn = shift @$connect_info;
400 my ($user, $password);
402 if ($dsn =~ /sqlite/i) {
403 ($user, $password) = ('', '');
404 shift @$connect_info while $connect_info->[0] eq '';
406 ($user, $password) = splice @$connect_info, 0, 2;
409 ($dsn, $user, $password)
412 sub _parse_connect_info {
413 my ($self, $connect_info) = @_;
415 my @connect_info = @$connect_info;
417 my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info);
419 tie my %connect_info, 'Tie::IxHash';
420 @connect_info{qw/dsn user password/} = ($dsn, $user, $password);
422 for (@connect_info) {
423 if (/^\s*{.*}\s*\z/) {
424 my $hash = $self->_eval($_);
425 die "Syntax errorr in connect_info hash: $_: $@" if $@;
427 %connect_info = (%connect_info, %$hash);
432 my ($key, $val) = split /=/, $_, 2;
434 if ($key eq 'quote_char') {
435 $connect_info{$key} = length($val) == 1 ? $val : [split //, $val];
436 } elsif ($key =~ /^(?:name_sep|limit_dialect)\z/) {
437 $connect_info{$key} = $val;
439 $connect_info{$key} = $self->_eval($val);
442 die "syntax error for connect_info key '$key' with value '$val': $@"
446 $self->connect_info(\%connect_info);
452 my ($self, $val) = @_;
454 return $val =~ /^\s*[[{]/;
458 my ($self, $val) = @_;
460 return 'q{'.$val.'}';
463 sub _quote_unless_struct {
464 my ($self, $val) = @_;
466 $val = $self->_quote($val) if not $self->_is_struct($val);
472 my ($self, $code) = @_;
474 return $code if looks_like_number $code;
476 return $code if $code =~ m{^[\w;:/]*\z};
478 return eval "{no strict; $code}";
481 sub _gen_dynamic_schema {
484 my $helper = $self->helper;
486 my @schema_parts = split(/\:\:/, $self->schema_class);
487 my $schema_file_part = pop @schema_parts;
489 my $schema_dir = File::Spec->catfile(
490 $helper->{base}, 'lib', @schema_parts
492 my $schema_file = File::Spec->catfile(
493 $schema_dir, $schema_file_part . '.pm'
496 $helper->mk_dir($schema_dir);
497 $helper->render_file('schemaclass', $schema_file);
500 sub _gen_static_schema {
503 die "cannot load schema without connect info" unless $self->connect_info;
505 my $helper = $self->helper;
507 my $schema_dir = File::Spec->catfile($helper->{base}, 'lib');
509 eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
510 die "Cannot load DBIx::Class::Schema::Loader: $@" if $@;
512 DBIx::Class::Schema::Loader->import(
513 "dump_to_dir:$schema_dir", 'make_schema_at'
519 [$self->connect_info]
525 my $helper = $self->helper;
527 $helper->render_file('compclass', $helper->{file} );
530 sub _print_dynamic_deprecation_warning {
532 ************************************ WARNING **********************************
533 * create=dynamic is DEPRECATED, please use create=static instead. *
534 *******************************************************************************
536 print "Continue? [y/n]: ";
537 chomp(my $response = <STDIN>);
538 exit 0 if $response =~ /^n(o)?\z/;
542 my ($self, $args) = @_;
544 # remove blanks, ie. someoned doing foo \ bar
545 my @res = grep !/^\s+\z/, @$args;
547 # remove leading whitespace, ie. foo \ bar
555 General Catalyst Stuff:
557 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
558 L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
560 Stuff related to DBIC and this Model style:
562 L<DBIx::Class>, L<DBIx::Class::Schema>,
563 L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
567 Brandon L Black, C<blblack@gmail.com>
571 Rafael Kitover, C<rkitover at cpan.org>
575 This library is free software, you can redistribute it and/or modify
576 it under the same terms as Perl itself.
587 package [% schema_class %];
590 use base qw/DBIx::Class::Schema::Loader/;
592 __PACKAGE__->loader_options(
593 [%- FOREACH key = loader_args.keys %]
594 [% key %] => [% loader_args.${key} %],
601 [% schema_class %] - L<DBIx::Class::Schema::Loader> class
609 Dynamic L<DBIx::Class::Schema::Loader> schema for use in L<[% class %]>
613 [% generator %] - [% generator_version %]
617 [% author.replace(',+$', '') %]
621 This library is free software, you can redistribute it and/or modify
622 it under the same terms as Perl itself.
632 use base 'Catalyst::Model::DBIC::Schema';
635 schema_class => '[% schema_class %]',
636 [% IF traits %]traits => [% traits %],[% END %]
637 [% IF setup_connect_info %]connect_info => {
638 [%- FOREACH key = connect_info.keys %]
639 [% key %] => [% connect_info.${key} %],
647 [% class %] - Catalyst DBIC Schema Model
655 L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
659 [% generator %] - [% generator_version %]
663 [% author.replace(',+$', '') %]
667 This library is free software, you can redistribute it and/or modify
668 it under the same terms as Perl itself.