X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Model-DBIC-Schema.git;a=blobdiff_plain;f=lib%2FCatalyst%2FHelper%2FModel%2FDBIC%2FSchema.pm;h=f50b2e553e5b92ab2f72ed6b07db72974fb3c5af;hp=049ff7ec852d105e44c4676c7d1e14a54b8206e0;hb=2a5644e8ea19e2db4136eaf8e458030d596071c4;hpb=73f72d2824f088091e1974dce03453d47311af78 diff --git a/lib/Catalyst/Helper/Model/DBIC/Schema.pm b/lib/Catalyst/Helper/Model/DBIC/Schema.pm index 049ff7e..f50b2e5 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -4,7 +4,8 @@ use namespace::autoclean; use Moose; no warnings 'uninitialized'; -our $VERSION = '0.24'; +our $VERSION = '0.65'; +$VERSION = eval $VERSION; use Carp; use Tie::IxHash (); @@ -12,9 +13,12 @@ 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 File::Find 'finddepth'; +use Try::Tiny; +use Cwd 'getcwd'; +use Module::Runtime 'use_module'; =head1 NAME @@ -58,14 +62,15 @@ the generated classes by hand to refine them. C is the list of traits to apply to the model, see L for details. -C are described in L below. +C are documented in L +and some examples are given in L below. -C arguments are the same as what -DBIx::Class::Schema::connect expects, and are storage_type-specific. -For DBI-based storage, these arguments are the dsn, username, -password, and connect options, respectively. These are optional for -existing Schemas, but required if you use either of the C -options. +C arguments are the same as what L +expects, and are storage_type-specific. They are documented in +L. For DBI-based storage, these +arguments are the dsn, username, password, and connect options, respectively. +These are optional for existing Schemas, but required if you use either of the +C options. username and password can be omitted for C dsns. @@ -85,11 +90,7 @@ user and pass can be omitted for sqlite, since they are always empty script/myapp_create.pl model CatalystModelName DBIC::Schema \ MyApp::SchemaClass create=static dbi:SQLite:foo.db \ 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='[]' + on_connect_do='["select 1", "select 2"]' quote_names=1 B @@ -98,7 +99,7 @@ In C the above example would be: script/myapp_create.pl model CatalystModelName DBIC::Schema \ MyApp::SchemaClass create=static dbi:SQLite:foo.db \ AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \ - on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\"" + on_connect_do="[\"select 1\", \"select 2\"]" quote_names=1 Same, but with extra Schema::Loader args (separate multiple values by commas): @@ -107,6 +108,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, @@ -135,6 +144,8 @@ has schema_class => (is => 'ro', isa => Str, required => 1); has loader_args => (is => 'rw', isa => HashRef); has connect_info => (is => 'rw', isa => HashRef); has old_schema => (is => 'rw', isa => Bool, lazy_build => 1); +has is_moose_schema => (is => 'rw', isa => Bool, lazy_build => 1); +has result_namespace => (is => 'rw', isa => Str, lazy_build => 1); has components => (is => 'rw', isa => ArrayRef); =head1 METHODS @@ -161,7 +172,7 @@ 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; @@ -174,7 +185,7 @@ sub BUILD { $self->traits(\@traits); $helper->{traits} = '[' - .(join ',' => map { qq{'$_'} } ($self->traits->flatten)) + .(join ',' => map { qq{'$_'} } @traits) .']'; splice @args, $traits_idx, 1, (); @@ -188,16 +199,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; @@ -236,8 +252,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); @@ -250,13 +268,15 @@ sub _parse_loader_args { %result = ( relationships => 1, - (%loader_args ? %loader_args : ()), + use_moose => $self->is_moose_schema ? 1 : 0, + col_collision_map => 'column_%s', (!$self->old_schema ? ( use_namespaces => 1 ) : ()), (@components ? ( components => \@components - ) : ()) + ) : ()), + (%loader_args ? %loader_args : ()), ); $self->loader_args(\%result); @@ -269,7 +289,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)) { @@ -281,6 +301,47 @@ sub _read_loader_args { } } + # Use args after connect_info as loader args as well, because people always + # get the order confused. + my $i = 1; + if ($args->[0] =~ /sqlite/i) { + $i++ if $args->[$i] eq ''; + $i++ if $args->[$i] eq ''; + } + else { + $i += 2; + } + + my $have_loader = try { + use_module('DBIx::Class::Schema::Loader::Base'); + 1; + }; + + if ($have_loader) { + while (defined $args->[$i]) { + $i++ while $self->_is_struct($args->[$i]); + + last if not defined $args->[$i]; + + my ($key, $val) = split /=/, $args->[$i], 2; + + if (not DBIx::Class::Schema::Loader::Base->can($key)) { + $i++; + next; + } + + if ($self->_is_struct($val)) { + $loader_args{$key} = $val; + } elsif ((my @vals = split /,/ => $val) > 1) { + $loader_args{$key} = \@vals; + } else { + $loader_args{$key} = $val; + } + + splice @$args, $i, 1; + } + } + wantarray ? %loader_args : \%loader_args; } @@ -303,9 +364,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; @@ -368,17 +430,72 @@ sub _build_helper_connect_info { sub _build_old_schema { my $self = shift; - my @schema_pm = split '::', $self->schema_class; - $schema_pm[-1] .= '.pm'; - my $schema_file = - File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm); + return $self->result_namespace eq '' ? 1 : 0; +} + +sub _build_is_moose_schema { + my $self = shift; - if (-f $schema_file) { - my $schema_code = do { local (@ARGV, $/) = $schema_file; <> }; - return 1 if $schema_code =~ /->load_classes/; + my @schema_parts = split '::', $self->schema_class; + + my $result_dir = File::Spec->catfile( + $self->helper->{base}, 'lib', @schema_parts, $self->result_namespace + ); + + # assume yes for new schemas + return 1 if not -d $result_dir; + + my $uses_moose = 1; + + my $cwd = getcwd; + + try { + finddepth(sub { + return if $File::Find::name !~ /\.pm\z/; + + open my $fh, '<', $File::Find::name + or die "Could not open $File::Find::name: $!"; + + my $code = do { local $/; <$fh> }; + close $fh; + + $uses_moose = 0 if $code !~ /\nuse Moose;\n/; + + die; + }, $result_dir); + }; + + chdir $cwd; + + return $uses_moose; +} + +sub _build_result_namespace { + my $self = shift; + + my @schema_parts = split '::', $self->schema_class; + my $schema_pm = + File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts) . '.pm'; + + if (not -f $schema_pm) { + eval { use_module('DBIx::Class::Schema::Loader') }; + + return 'Result' if $@; + + return (try { DBIx::Class::Schema::Loader->VERSION('0.05') }) ? 'Result' : ''; } - 0; + open my $fh, '<', $schema_pm or die "Could not open $schema_pm: $!"; + my $code = do { local $/; <$fh> }; + close $fh; + + my ($result_namespace) = $code =~ /result_namespace => '([^']+)'/; + + return $result_namespace if $result_namespace; + + return '' if $code =~ /->load_classes/; + + return 'Result'; } sub _data_struct_to_string { @@ -386,6 +503,7 @@ sub _data_struct_to_string { local $Data::Dumper::Terse = 1; local $Data::Dumper::Quotekeys = 0; + local $Data::Dumper::Sortkeys = 1; local $Data::Dumper::Indent = 0; local $Data::Dumper::Useqq = 1; @@ -400,7 +518,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; } @@ -450,7 +568,7 @@ sub _parse_connect_info { sub _is_struct { my ($self, $val) = @_; - return $val =~ /^\s*[[{]/; + return $val =~ /^\s*(?:sub|[[{])/; } sub _quote { @@ -472,7 +590,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}"; } @@ -505,8 +623,12 @@ sub _gen_static_schema { my $schema_dir = File::Spec->catfile($helper->{base}, 'lib'); - eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') }; - die "Cannot load DBIx::Class::Schema::Loader: $@" if $@; + try { + use_module('DBIx::Class::Schema::Loader') + } + catch { + die "Cannot load DBIx::Class::Schema::Loader: $_"; + }; DBIx::Class::Schema::Loader->import( "dump_to_dir:$schema_dir", 'make_schema_at' @@ -517,6 +639,19 @@ sub _gen_static_schema { $self->loader_args, [$self->connect_info] ); + + require lib; + lib->import($schema_dir); + + use_module($self->schema_class); + + my @sources = $self->schema_class->sources; + + if (not @sources) { + warn <<'EOF'; +WARNING: No tables found, did you forget to specify db_schema? +EOF + } } sub _gen_model { @@ -563,11 +698,12 @@ L, L =head1 AUTHOR -Brandon L Black, C +See L and +L. -Contributors: +=head1 COPYRIGHT -Rafael Kitover, C +See L. =head1 LICENSE