X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper%2FModel%2FDBIC%2FSchema.pm;h=5a771cc4f8f4c572ad9a6af8ca805372609af675;hb=ffbf1967be4a74a07555bad4397bf42952647ce7;hp=b70b86e7b62e969d9590e19f2e1153f7552b4981;hpb=781c68761e0c4776b34669d20a5a8498e50666cb;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 b70b86e..5a771cc 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -2,6 +2,10 @@ package Catalyst::Helper::Model::DBIC::Schema; use strict; use warnings; +no warnings 'uninitialized'; + +our $VERSION = '0.23'; + use Carp; use UNIVERSAL::require; @@ -11,7 +15,7 @@ Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models =head1 SYNOPSIS - script/create.pl model ModelName DBIC::Schema My::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ] + script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ] =head1 DESCRIPTION @@ -19,30 +23,34 @@ Helper for the DBIC Schema Models. =head2 Arguments: - ModelName is the short name for the Model class being generated - - My::SchemaClass is the fully qualified classname of your Schema, - which might or might not yet exist. - - create=dynamic instructs this Helper to generate the named Schema - class for you, basing it on L (which - means the table information will always be dynamically loaded at - runtime from the database). - - create=static instructs this Helper to generate the named Schema - class for you, using L in "one shot" - mode to create a standard, manually-defined L - setup, based on what the Loader sees in your database at this moment. - A Schema/Model pair generated this way will not require - 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. - - connect_info 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 is the short name for the Catalyst Model class +being generated (i.e. callable with C<$c-Emodel('CatalystModelName')>). + +C is the fully qualified classname of your Schema, +which might or might not yet exist. Note that you should have a good +reason to create this under a new global namespace, otherwise use an +existing top level namespace for your schema class. + +C instructs this Helper to generate the named Schema +class for you, basing it on L (which +means the table information will always be dynamically loaded at +runtime from the database). + +C instructs this Helper to generate the named Schema +class for you, using L in "one shot" +mode to create a standard, manually-defined L +setup, based on what the Loader sees in your database at this moment. +A Schema/Model pair generated this way will not require +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 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. Use of either of the C options requires L. @@ -50,22 +58,27 @@ Use of either of the C options requires L. # Use DBIx::Class::Schema::Loader to create a static DBIx::Class::Schema, # and a Model which references it: - script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass create=static dbi:mysql:foodb myuname mypass + script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass + + # 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 + + # See DBIx::Class::Schema::Loader::Base for list of options # Create a dynamic DBIx::Class::Schema::Loader-based Schema, # and a Model which references it: - script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass create=dynamic dbi:mysql:foodb myuname mypass + 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 ModelName DBIC::Schema My::SchemaClass dbi:mysql:foodb myuname mypass + 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). script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass -=head2 METHODS +=head1 METHODS -=head3 mk_compclass +=head2 mk_compclass =cut @@ -73,7 +86,7 @@ sub mk_compclass { my ( $self, $helper, $schema_class, @connect_info) = @_; $helper->{schema_class} = $schema_class - or die "Must supply schema class name"; + or croak "Must supply schema class name"; my $create = ''; if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) { @@ -81,17 +94,26 @@ sub mk_compclass { shift @connect_info; } + my %extra_args; + while (@connect_info && $connect_info[0] !~ /^dbi:/) { + my ($key, $val) = split /=/, shift(@connect_info); + + if ((my @vals = split /,/ => $val) > 1) { + $extra_args{$key} = \@vals; + } else { + $extra_args{$key} = $val; + } + } + if(@connect_info) { $helper->{setup_connect_info} = 1; - for(@connect_info) { + my @helper_connect_info = @connect_info; + for(@helper_connect_info) { $_ = qq{'$_'} if $_ !~ /^\s*[[{]/; } - $helper->{connect_info} = \@connect_info; + $helper->{connect_info} = \@helper_connect_info; } - my $file = $helper->{file}; - $helper->render_file( 'compclass', $file ); - if($create eq 'dynamic') { my @schema_parts = split(/\:\:/, $helper->{schema_class}); my $schema_file_part = pop @schema_parts; @@ -103,15 +125,68 @@ sub mk_compclass { $helper->render_file( 'schemaclass', $schema_file ); } 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 die "Cannot load DBIx::Class::Schema::Loader: $@"; - make_schema_at( - $schema_class, - { relationships => 1 }, - \@connect_info, - ); + 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++; + } + +# Check if we need to be backward-compatible. + my $compatible = 0; + + my @schema_pm = split '::', $schema_class; + $schema_pm[-1] .= '.pm'; + my $schema_file = File::Spec->catfile($helper->{base}, 'lib', @schema_pm); + + if (-f $schema_file) { + my $schema_code = do { local (@ARGV, $/) = $schema_file; <> }; + $compatible = 1 if $schema_code =~ /->load_classes/; + } + + my @components = $compatible ? () : ('InflateColumn::DateTime'); + + if (exists $extra_args{components}) { + $extra_args{components} = [ $extra_args{components} ] + unless ref $extra_args{components}; + + push @components, @{ delete $extra_args{components} }; + } + + for my $re_opt (qw/constraint exclude/) { + $extra_args{$re_opt} = qr/$extra_args{$re_opt}/ + if exists $extra_args{$re_opt}; + } + + 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." + } + + make_schema_at( + $schema_class, + { + relationships => 1, + (%extra_args ? %extra_args : ()), + (!$compatible ? ( + use_namespaces => 1 + ) : ()), + (@components ? ( + components => \@components + ) : ()) + }, + \@loader_connect_info, + ); } + + my $file = $helper->{file}; + $helper->render_file( 'compclass', $file ); } =head1 SEE ALSO @@ -124,8 +199,7 @@ L, L, L, Stuff related to DBIC and this Model style: L, L, -L, L, -L +L, L =head1 AUTHOR