X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper%2FModel%2FDBIC%2FSchema.pm;h=85af98f7721f7a1c09dbe1767c6aa45d289b6869;hb=34f036a0c5aaa1854536e1c46fbee334b025cfd1;hp=86d5686a3fc27d94a8cb0431c5b8d49f09b44746;hpb=6116daed7cd87d13d4a9217ab86d9f221a08537d;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 86d5686..85af98f 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -2,6 +2,9 @@ package Catalyst::Helper::Model::DBIC::Schema; use strict; use warnings; + +our $VERSION = '0.21'; + use Carp; use UNIVERSAL::require; @@ -19,20 +22,20 @@ Helper for the DBIC Schema Models. =head2 Arguments: -C< CatalystModelName > is the short name for the Catalyst Model class -being generated (i.e. callable with C< $c->model > +C is the short name for the Catalyst Model class +being generated (i.e. callable with C<$c-Emodel('CatalystModelName')>). -C< MyApp::SchemaClass > is the fully qualified classname of your Schema, +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< create=dynamic > instructs this Helper to generate the named Schema +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< create=static > instructs this Helper to generate the named Schema +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. @@ -41,7 +44,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< connect_info > arguments are the same as what +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 @@ -56,6 +59,12 @@ Use of either of the C options requires L. # and a Model which references it: script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static dbi:mysql:foodb myuname mypass + # Same, but with extra Schema::Loader args such as db_schema: + script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static db_schema=foodb dbi:Pg:dbname=foodb myuname mypass + + # Likewise, for multiple values such as a components: + script/myapp_create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass create=static components=Some::Component components=Some::OtherComponent dbi:Pg:dbname=foodb myuname mypass + # 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 @@ -67,9 +76,9 @@ Use of either of the C options requires L. # 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 @@ -109,6 +118,20 @@ sub mk_compclass { DBIx::Class::Schema::Loader->use("dump_to_dir:$schema_dir", 'make_schema_at') or croak "Cannot load DBIx::Class::Schema::Loader: $@"; + my %extra_args; + while (@connect_info && $connect_info[0] !~ /^dbi:/) { + my ($key, $val) = split /=/, shift(@connect_info); + + if (exists $extra_args{$key}) { + $extra_args{$key} = [ $extra_args{$key} ] + unless ref $extra_args{$key}; + + push @{ $extra_args{$key} }, $val; + } else { + $extra_args{$key} = $val; + } + } + my @loader_connect_info = @connect_info; my $num = 6; # argument number on the commandline for "dbi:..." for(@loader_connect_info) { @@ -119,9 +142,39 @@ sub mk_compclass { $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} }; + } + make_schema_at( $schema_class, - { relationships => 1 }, + { + relationships => 1, + (%extra_args ? %extra_args : ()), + (!$compatible ? ( + use_namespaces => 1 + ) : ()), + (@components ? ( + components => \@components + ) : ()) + }, \@loader_connect_info, ); }