X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper%2FModel%2FDBIC%2FSchema.pm;h=d35bfb89face5a4963203cc8b5ae89c359119bb6;hb=510629748f0214f9e8bed35502a90edf7b6d589e;hp=7564960131ede7d889a0b787bd4a4bb2012cd19c;hpb=5f9a34d7a46dbf7b4870820d2fe46a226a99b491;p=catagits%2FCatalyst-Model-DBIC-Schema.git diff --git a/lib/Catalyst/Helper/Model/DBIC/Schema.pm b/lib/Catalyst/Helper/Model/DBIC/Schema.pm index 7564960..d35bfb8 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -1,9 +1,11 @@ package Catalyst::Helper::Model::DBIC::Schema; +use namespace::autoclean; use Moose; no warnings 'uninitialized'; -our $VERSION = '0.24'; +our $VERSION = '0.40'; +$VERSION = eval $VERSION; use Carp; use Tie::IxHash (); @@ -11,12 +13,9 @@ use Data::Dumper (); use List::Util 'first'; use MooseX::Types::Moose qw/Str HashRef Bool ArrayRef/; use Catalyst::Model::DBIC::Schema::Types 'CreateOption'; -use Moose::Autobox; use List::MoreUtils 'firstidx'; use Scalar::Util 'looks_like_number'; -use namespace::clean -except => 'meta'; - =head1 NAME Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models @@ -24,7 +23,7 @@ Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models =head1 SYNOPSIS script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \ - [ create=dynamic | create=static ] [ roles=role1,role2... ] \ + [ create=dynamic | create=static ] [ traits=trait1,trait2... ] \ [ Schema::Loader opts ] [ dsn user pass ] \ [ other connect_info args ] @@ -56,7 +55,7 @@ L at runtime, and will not automatically adapt itself to changes in your database structure. You can edit the generated classes by hand to refine them. -C is the list of roles to apply to the model, see +C is the list of traits to apply to the model, see L for details. C are described in L below. @@ -88,6 +87,10 @@ user and pass can be omitted for sqlite, since they are always empty AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \ on_connect_do='["select 1", "select 2"]' quote_char='"' +If using a 2 character quote_char: + + script/myapp_create.pl ... quote_char='[]' + B In C the above example would be: @@ -104,6 +107,14 @@ Same, but with extra Schema::Loader args (separate multiple values by commas): exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \ dbi:Pg:dbname=foodb myuname mypass +Coderefs are also supported: + + script/myapp_create.pl model CatalystModelName DBIC::Schema \ + MyApp::SchemaClass create=static \ + inflect_singular='sub { $_[0] =~ /\A(.+?)(_id)?\z/; $1 }' \ + moniker_map='sub { join(q{}, map ucfirst, split(/[\W_]+/, lc $_[0])); }' \ + dbi:mysql:foodb myuname mypass + See L for a list of options Create a dynamic DBIx::Class::Schema::Loader-based Schema, @@ -127,7 +138,7 @@ in your app config, or [not recommended] in the schema itself). has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1); has create => (is => 'rw', isa => CreateOption); has args => (is => 'ro', isa => ArrayRef); -has roles => (is => 'rw', isa => ArrayRef); +has traits => (is => 'rw', isa => ArrayRef); has schema_class => (is => 'ro', isa => Str, required => 1); has loader_args => (is => 'rw', isa => HashRef); has connect_info => (is => 'rw', isa => HashRef); @@ -158,23 +169,23 @@ sub mk_compclass { sub BUILD { my $self = shift; my $helper = $self->helper; - my @args = $self->args->flatten if $self->args; + my @args = @{ $self->args || [] }; $helper->{schema_class} = $self->schema_class; @args = $self->_cleanup_args(\@args); - my ($roles_idx, $roles); - if (($roles_idx = firstidx { ($roles) = /^roles=(\S*)\z/ } @args) != -1) { - my @roles = split /,/ => $roles; + my ($traits_idx, $traits); + if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) { + my @traits = split /,/ => $traits; - $self->roles(\@roles); + $self->traits(\@traits); - $helper->{roles} = '[' - .(join ',' => map { qq{'$_'} } ($self->roles->flatten)) + $helper->{traits} = '[' + .(join ',' => map { qq{'$_'} } @traits) .']'; - splice @args, $roles_idx, 1, (); + splice @args, $traits_idx, 1, (); } if ($args[0] && $args[0] =~ /^create=(\S*)\z/) { @@ -185,16 +196,21 @@ sub BUILD { $self->_parse_loader_args(\@args); $helper->{loader_args} = $self->_build_helper_loader_args; + } + } - if (first { /^dbi:/i } @args) { - $helper->{setup_connect_info} = 1; + my $dbi_dsn_part; + if (first { ($dbi_dsn_part) = /^(dbi):/i } @args) { + die +qq{DSN must start with 'dbi:' not '$dbi_dsn_part' (case matters!)} + if $dbi_dsn_part ne 'dbi'; - $helper->{connect_info} = - $self->_build_helper_connect_info(\@args); + $helper->{setup_connect_info} = 1; - $self->_parse_connect_info(\@args); - } - } + $helper->{connect_info} = + $self->_build_helper_connect_info(\@args); + + $self->_parse_connect_info(\@args); } $helper->{generator} = ref $self; @@ -233,8 +249,10 @@ sub _parse_loader_args { if $@; } - my @components = - $self->_build_loader_components(delete $loader_args{components}); + my @components = $self->_build_loader_components( + delete $loader_args{components}, + $loader_args{use_namespaces}, + ); $self->components(\@components); @@ -247,13 +265,13 @@ sub _parse_loader_args { %result = ( relationships => 1, - (%loader_args ? %loader_args : ()), (!$self->old_schema ? ( use_namespaces => 1 ) : ()), (@components ? ( components => \@components - ) : ()) + ) : ()), + (%loader_args ? %loader_args : ()), ); $self->loader_args(\%result); @@ -266,7 +284,7 @@ sub _read_loader_args { my %loader_args; - while (@$args && $args->[0] !~ /^dbi:/) { + while (@$args && $args->[0] !~ /^dbi:/i) { my ($key, $val) = split /=/, shift(@$args), 2; if ($self->_is_struct($val)) { @@ -300,9 +318,10 @@ sub _build_helper_loader_args { } sub _build_loader_components { - my ($self, $components) = @_; + my ($self, $components, $use_namespaces) = @_; - my @components = $self->old_schema ? () : ('InflateColumn::DateTime'); + my @components = $self->old_schema && (not $use_namespaces) ? () + : ('InflateColumn::DateTime'); if ($components) { $components = [ $components ] if !ref $components; @@ -339,7 +358,7 @@ sub _build_helper_connect_info { if (ref $val) { $val = $self->_data_struct_to_string($val); } else { - $val = 'q{'.$val.'}'; + $val = $self->_quote($val); } $helper_connect_info{$key} = $val; @@ -350,7 +369,13 @@ sub _build_helper_connect_info { my ($key, $val) = split /=/, $_, 2; - $helper_connect_info{$key} = $self->_quote_unless_struct($val); + if ($key eq 'quote_char') { + $helper_connect_info{$key} = length($val) == 1 ? + $self->_quote($val) : + $self->_data_struct_to_string([split //, $val]); + } else { + $helper_connect_info{$key} = $self->_quote_unless_struct($val); + } } \%helper_connect_info @@ -391,7 +416,7 @@ sub _get_dsn_user_pass { if ($dsn =~ /sqlite/i) { ($user, $password) = ('', ''); - shift @$connect_info while $connect_info->[0] eq ''; + shift @$connect_info while @$connect_info and $connect_info->[0] eq ''; } else { ($user, $password) = splice @$connect_info, 0, 2; } @@ -421,7 +446,9 @@ sub _parse_connect_info { my ($key, $val) = split /=/, $_, 2; - if ($key =~ /^(?:quote_char|name_sep|limit_dialect)\z/) { + if ($key eq 'quote_char') { + $connect_info{$key} = length($val) == 1 ? $val : [split //, $val]; + } elsif ($key =~ /^(?:name_sep|limit_dialect)\z/) { $connect_info{$key} = $val; } else { $connect_info{$key} = $self->_eval($val); @@ -439,15 +466,21 @@ sub _parse_connect_info { sub _is_struct { my ($self, $val) = @_; - return $val =~ /^\s*[[{]/; + return $val =~ /^\s*(?:sub|[[{])/; +} + +sub _quote { + my ($self, $val) = @_; + + return 'q{'.$val.'}'; } sub _quote_unless_struct { my ($self, $val) = @_; - $val = 'q{'.$val.'}' if not $self->_is_struct($val); + $val = $self->_quote($val) if not $self->_is_struct($val); - $val; + return $val; } sub _eval { @@ -455,7 +488,7 @@ sub _eval { return $code if looks_like_number $code; - return $code if $code =~ m{^[\w;:/]*\z}; + return $code if not $self->_is_struct($code); return eval "{no strict; $code}"; } @@ -546,11 +579,12 @@ L, L =head1 AUTHOR -Brandon L Black, C +See L and +L. -Contributors: +=head1 COPYRIGHT -Rafael Kitover, C<> +See L. =head1 LICENSE @@ -615,7 +649,7 @@ use base 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config( schema_class => '[% schema_class %]', - [% IF roles %]roles => [% roles %],[% END %] + [% IF traits %]traits => [% traits %],[% END %] [% IF setup_connect_info %]connect_info => { [%- FOREACH key = connect_info.keys %] [% key %] => [% connect_info.${key} %],