X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper%2FModel%2FDBIC%2FSchema.pm;h=b58a09375ec351eba0c752e3e509ffe9a5a57e8a;hb=8e1f4dbf45b813d4f4fd5caddccf3f3deadc410c;hp=5a771cc4f8f4c572ad9a6af8ca805372609af675;hpb=ffbf1967be4a74a07555bad4397bf42952647ce7;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 5a771cc..b58a093 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -1,13 +1,19 @@ package Catalyst::Helper::Model::DBIC::Schema; -use strict; -use warnings; +use namespace::autoclean; +use Moose; no warnings 'uninitialized'; -our $VERSION = '0.23'; +our $VERSION = '0.30'; use Carp; -use UNIVERSAL::require; +use Tie::IxHash (); +use Data::Dumper (); +use List::Util 'first'; +use MooseX::Types::Moose qw/Str HashRef Bool ArrayRef/; +use Catalyst::Model::DBIC::Schema::Types 'CreateOption'; +use List::MoreUtils 'firstidx'; +use Scalar::Util 'looks_like_number'; =head1 NAME @@ -15,7 +21,10 @@ 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 ] [ connect_info arguments ] + script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \ + [ create=dynamic | create=static ] [ traits=trait1,trait2... ] \ + [ Schema::Loader opts ] [ dsn user pass ] \ + [ other connect_info args ] =head1 DESCRIPTION @@ -45,6 +54,11 @@ 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 traits to apply to the model, see +L for details. + +C are described 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, @@ -52,141 +66,491 @@ 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. + Use of either of the C options requires L. =head1 TYPICAL EXAMPLES - # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema, - # and a Model which references it: - script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass +Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema, +and a Model which references it: + + script/myapp_create.pl model CatalystModelName DBIC::Schema \ + MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass + +Same, with extra connect_info args +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='[]' + +B + +In C the above example would be: - # Same, but with extra Schema::Loader args (separate multiple values by commas): - script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar exclude='^wibble|wobble$' dbi:Pg:dbname=foodb myuname mypass + 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="\"" - # See DBIx::Class::Schema::Loader::Base for list of options +Same, but with extra Schema::Loader args (separate multiple values by commas): - # Create a dynamic DBIx::Class::Schema::Loader-based Schema, - # and a Model which references it: - script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass + script/myapp_create.pl model CatalystModelName DBIC::Schema \ + MyApp::SchemaClass create=static db_schema=foodb components=Foo,Bar \ + exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \ + dbi:Pg:dbname=foodb myuname mypass - # Reference an existing Schema of any kind, and provide some connection information for ->config: - script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass dbi:mysql:foodb myuname mypass +See L for a list of options + +Create a dynamic DBIx::Class::Schema::Loader-based Schema, +and a Model which references it (B): + + script/myapp_create.pl model CatalystModelName DBIC::Schema \ + MyApp::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass + +Reference an existing Schema of any kind, and provide some connection information for ->config: + + script/myapp_create.pl model CatalystModelName DBIC::Schema \ + MyApp::SchemaClass dbi:mysql:foodb myuname mypass + +Same, but don't supply connect information yet (you'll need to do this +in your app config, or [not recommended] in the schema itself). - # Same, but don't supply connect information yet (you'll need to do this - # in your app config, or [not recommended] in the schema itself). script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass +=cut + +has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1); +has create => (is => 'rw', isa => CreateOption); +has args => (is => 'ro', 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); +has old_schema => (is => 'rw', isa => Bool, lazy_build => 1); +has components => (is => 'rw', isa => ArrayRef); + =head1 METHODS =head2 mk_compclass +This is called by L with the commandline args to generate the +files. + =cut sub mk_compclass { - my ( $self, $helper, $schema_class, @connect_info) = @_; + my ($package, $helper, $schema_class, @args) = @_; - $helper->{schema_class} = $schema_class - or croak "Must supply schema class name"; + my $self = $package->new( + helper => $helper, + schema_class => $schema_class, + args => \@args + ); - my $create = ''; - if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) { - $create = $1; - shift @connect_info; + $self->run; +} + +sub BUILD { + my $self = shift; + my $helper = $self->helper; + my @args = @{ $self->args || [] }; + + $helper->{schema_class} = $self->schema_class; + + @args = $self->_cleanup_args(\@args); + + my ($traits_idx, $traits); + if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) { + my @traits = split /,/ => $traits; + + $self->traits(\@traits); + + $helper->{traits} = '[' + .(join ',' => map { qq{'$_'} } @traits) + .']'; + + splice @args, $traits_idx, 1, (); } - my %extra_args; - while (@connect_info && $connect_info[0] !~ /^dbi:/) { - my ($key, $val) = split /=/, shift(@connect_info); + if ($args[0] && $args[0] =~ /^create=(\S*)\z/) { + $self->create($1); + shift @args; - if ((my @vals = split /,/ => $val) > 1) { - $extra_args{$key} = \@vals; - } else { - $extra_args{$key} = $val; + if (@args) { + $self->_parse_loader_args(\@args); + + $helper->{loader_args} = $self->_build_helper_loader_args; } } - if(@connect_info) { + 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->{setup_connect_info} = 1; - my @helper_connect_info = @connect_info; - for(@helper_connect_info) { - $_ = qq{'$_'} if $_ !~ /^\s*[[{]/; - } - $helper->{connect_info} = \@helper_connect_info; + + $helper->{connect_info} = + $self->_build_helper_connect_info(\@args); + + $self->_parse_connect_info(\@args); } - if($create eq 'dynamic') { - my @schema_parts = split(/\:\:/, $helper->{schema_class}); - my $schema_file_part = pop @schema_parts; + $helper->{generator} = ref $self; + $helper->{generator_version} = $VERSION; +} + +=head2 run - my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib', @schema_parts ); - my $schema_file = File::Spec->catfile( $schema_dir, $schema_file_part . '.pm' ); +Can be called on an instance to generate the files. - $helper->mk_dir($schema_dir); - $helper->render_file( 'schemaclass', $schema_file ); +=cut + +sub run { + my $self = shift; + + if ($self->create eq 'dynamic') { + $self->_print_dynamic_deprecation_warning; + $self->_gen_dynamic_schema; + } elsif ($self->create eq 'static') { + $self->_gen_static_schema; } - elsif($create eq 'static') { - my $schema_dir = File::Spec->catfile( $helper->{base}, 'lib' ); - DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at') - or croak "Cannot load DBIx::Class::Schema::Loader: $@"; - - my @loader_connect_info = @connect_info; - my $num = 6; # argument number on the commandline for "dbi:..." - for(@loader_connect_info) { - if(/^\s*[[{]/) { - $_ = eval "$_"; - croak "Perl syntax error in commandline argument $num: $@" if $@; - } - $num++; + + $self->_gen_model; +} + +sub _parse_loader_args { + my ($self, $args) = @_; + + my %loader_args = $self->_read_loader_args($args); + + while (my ($key, $val) = each %loader_args) { + next if $key =~ /^(?:components|constraint|exclude)\z/; + + $loader_args{$key} = $self->_eval($val); + die "syntax error for loader args key '$key' with value '$val': $@" + if $@; + } + + my @components = + $self->_build_loader_components(delete $loader_args{components}); + + $self->components(\@components); + + for my $re_opt (qw/constraint exclude/) { + $loader_args{$re_opt} = qr/$loader_args{$re_opt}/ + if exists $loader_args{$re_opt}; + } + + tie my %result, 'Tie::IxHash'; + + %result = ( + relationships => 1, + (%loader_args ? %loader_args : ()), + (!$self->old_schema ? ( + use_namespaces => 1 + ) : ()), + (@components ? ( + components => \@components + ) : ()) + ); + + $self->loader_args(\%result); + + wantarray ? %result : \%result; +} + +sub _read_loader_args { + my ($self, $args) = @_; + + my %loader_args; + + while (@$args && $args->[0] !~ /^dbi:/i) { + my ($key, $val) = split /=/, shift(@$args), 2; + + if ($self->_is_struct($val)) { + $loader_args{$key} = $val; + } elsif ((my @vals = split /,/ => $val) > 1) { + $loader_args{$key} = \@vals; + } else { + $loader_args{$key} = $val; } + } + + wantarray ? %loader_args : \%loader_args; +} -# Check if we need to be backward-compatible. - my $compatible = 0; +sub _build_helper_loader_args { + my $self = shift; - my @schema_pm = split '::', $schema_class; - $schema_pm[-1] .= '.pm'; - my $schema_file = File::Spec->catfile($helper->{base}, 'lib', @schema_pm); + my $args = $self->loader_args; - if (-f $schema_file) { - my $schema_code = do { local (@ARGV, $/) = $schema_file; <> }; - $compatible = 1 if $schema_code =~ /->load_classes/; + tie my %loader_args, 'Tie::IxHash'; + + while (my ($arg, $val) = each %$args) { + if (ref $val) { + $loader_args{$arg} = $self->_data_struct_to_string($val); + } else { + $loader_args{$arg} = qq{'$val'}; } + } + + \%loader_args +} + +sub _build_loader_components { + my ($self, $components) = @_; + + my @components = $self->old_schema ? () : ('InflateColumn::DateTime'); + + if ($components) { + $components = [ $components ] if !ref $components; + push @components, @$components; + } + + wantarray ? @components : \@components; +} + +sub _build_helper_connect_info { + my ($self, $connect_info) = @_; + + my @connect_info = @$connect_info; + + my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info); + + tie my %helper_connect_info, 'Tie::IxHash'; + + %helper_connect_info = ( + dsn => qq{'$dsn'}, + user => qq{'$user'}, + password => qq{'$password'} + ); + + for (@connect_info) { + if (/^\s*{.*}\s*\z/) { + my $hash = $self->_eval($_); + die "Syntax errorr in connect_info hash: $_: $@" if $@; + my %hash = %$hash; - my @components = $compatible ? () : ('InflateColumn::DateTime'); + for my $key (keys %hash) { + my $val = $hash{$key}; - if (exists $extra_args{components}) { - $extra_args{components} = [ $extra_args{components} ] - unless ref $extra_args{components}; + if (ref $val) { + $val = $self->_data_struct_to_string($val); + } else { + $val = $self->_quote($val); + } - push @components, @{ delete $extra_args{components} }; + $helper_connect_info{$key} = $val; + } + + next; } - for my $re_opt (qw/constraint exclude/) { - $extra_args{$re_opt} = qr/$extra_args{$re_opt}/ - if exists $extra_args{$re_opt}; + my ($key, $val) = split /=/, $_, 2; + + 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 +} + +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); + + if (-f $schema_file) { + my $schema_code = do { local (@ARGV, $/) = $schema_file; <> }; + return 1 if $schema_code =~ /->load_classes/; + } - if (exists $extra_args{moniker_map}) { - die "The moniker_map option is not currently supported by this helper, please write your own DBIx::Class::Schema::Loader script if you need it." + 0; +} + +sub _data_struct_to_string { + my ($self, $data) = @_; + + local $Data::Dumper::Terse = 1; + local $Data::Dumper::Quotekeys = 0; + local $Data::Dumper::Indent = 0; + local $Data::Dumper::Useqq = 1; + + return Data::Dumper->Dump([$data]); +} + +sub _get_dsn_user_pass { + my ($self, $connect_info) = @_; + + my $dsn = shift @$connect_info; + my ($user, $password); + + if ($dsn =~ /sqlite/i) { + ($user, $password) = ('', ''); + shift @$connect_info while @$connect_info and $connect_info->[0] eq ''; + } else { + ($user, $password) = splice @$connect_info, 0, 2; + } + + ($dsn, $user, $password) +} + +sub _parse_connect_info { + my ($self, $connect_info) = @_; + + my @connect_info = @$connect_info; + + my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info); + + tie my %connect_info, 'Tie::IxHash'; + @connect_info{qw/dsn user password/} = ($dsn, $user, $password); + + for (@connect_info) { + if (/^\s*{.*}\s*\z/) { + my $hash = $self->_eval($_); + die "Syntax errorr in connect_info hash: $_: $@" if $@; + + %connect_info = (%connect_info, %$hash); + + next; + } + + my ($key, $val) = split /=/, $_, 2; + + 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); } - make_schema_at( - $schema_class, - { - relationships => 1, - (%extra_args ? %extra_args : ()), - (!$compatible ? ( - use_namespaces => 1 - ) : ()), - (@components ? ( - components => \@components - ) : ()) - }, - \@loader_connect_info, - ); + die "syntax error for connect_info key '$key' with value '$val': $@" + if $@; } - my $file = $helper->{file}; - $helper->render_file( 'compclass', $file ); + $self->connect_info(\%connect_info); + + \%connect_info +} + +sub _is_struct { + my ($self, $val) = @_; + + return $val =~ /^\s*[[{]/; +} + +sub _quote { + my ($self, $val) = @_; + + return 'q{'.$val.'}'; +} + +sub _quote_unless_struct { + my ($self, $val) = @_; + + $val = $self->_quote($val) if not $self->_is_struct($val); + + return $val; +} + +sub _eval { + my ($self, $code) = @_; + + return $code if looks_like_number $code; + + return $code if $code =~ m{^[\w;:/]*\z}; + + return eval "{no strict; $code}"; +} + +sub _gen_dynamic_schema { + my $self = shift; + + my $helper = $self->helper; + + my @schema_parts = split(/\:\:/, $self->schema_class); + my $schema_file_part = pop @schema_parts; + + my $schema_dir = File::Spec->catfile( + $helper->{base}, 'lib', @schema_parts + ); + my $schema_file = File::Spec->catfile( + $schema_dir, $schema_file_part . '.pm' + ); + + $helper->mk_dir($schema_dir); + $helper->render_file('schemaclass', $schema_file); +} + +sub _gen_static_schema { + my $self = shift; + + die "cannot load schema without connect info" unless $self->connect_info; + + my $helper = $self->helper; + + 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 $@; + + DBIx::Class::Schema::Loader->import( + "dump_to_dir:$schema_dir", 'make_schema_at' + ); + + make_schema_at( + $self->schema_class, + $self->loader_args, + [$self->connect_info] + ); +} + +sub _gen_model { + my $self = shift; + my $helper = $self->helper; + + $helper->render_file('compclass', $helper->{file} ); +} + +sub _print_dynamic_deprecation_warning { + warn <); + exit 0 if $response =~ /^n(o)?\z/; +} + +sub _cleanup_args { + my ($self, $args) = @_; + +# remove blanks, ie. someoned doing foo \ bar + my @res = grep !/^\s+\z/, @$args; + +# remove leading whitespace, ie. foo \ bar + s/^\s*// for @res; + + @res } =head1 SEE ALSO @@ -205,6 +569,10 @@ L, L Brandon L Black, C +Contributors: + +Rafael Kitover, C + =head1 LICENSE This library is free software, you can redistribute it and/or modify @@ -225,13 +593,15 @@ use strict; use base qw/DBIx::Class::Schema::Loader/; __PACKAGE__->loader_options( - relationships => 1, - # debug => 1, + [%- FOREACH key = loader_args.keys %] + [% key %] => [% loader_args.${key} %], + [%- END -%] + ); =head1 NAME -[% schema_class %] - DBIx::Class::Schema::Loader class +[% schema_class %] - L class =head1 SYNOPSIS @@ -239,11 +609,15 @@ See L<[% app %]> =head1 DESCRIPTION -Generated by L for use in L<[% class %]> +Dynamic L schema for use in L<[% class %]> + +=head1 GENERATED BY + +[% generator %] - [% generator_version %] =head1 AUTHOR -[% author %] +[% author.replace(',+$', '') %] =head1 LICENSE @@ -262,15 +636,19 @@ use base 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config( schema_class => '[% schema_class %]', - [% IF setup_connect_info %]connect_info => [ - [% FOREACH arg = connect_info %][% arg %], - [% END %] - ],[% END %] + [% IF traits %]traits => [% traits %],[% END %] + [% IF setup_connect_info %]connect_info => { + [%- FOREACH key = connect_info.keys %] + [% key %] => [% connect_info.${key} %], + [%- END -%] + + }[% END %] ); =head1 NAME [% class %] - Catalyst DBIC Schema Model + =head1 SYNOPSIS See L<[% app %]> @@ -279,9 +657,13 @@ See L<[% app %]> L Model using schema L<[% schema_class %]> +=head1 GENERATED BY + +[% generator %] - [% generator_version %] + =head1 AUTHOR -[% author %] +[% author.replace(',+$', '') %] =head1 LICENSE @@ -291,3 +673,5 @@ it under the same terms as Perl itself. =cut 1; +__END__ +# vim:sts=4 sw=4: