C::Helper::DBIC::Schema -- parse extra Schema::Loader args
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
index dbbb03c..85af98f 100644 (file)
@@ -2,7 +2,11 @@ package Catalyst::Helper::Model::DBIC::Schema;
 
 use strict;
 use warnings;
+
+our $VERSION = '0.21';
+
 use Carp;
+use UNIVERSAL::require;
 
 =head1 NAME
 
@@ -10,36 +14,169 @@ Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models
 
 =head1 SYNOPSIS
 
-    script/create.pl model Foo DBIC::Schema Foo::SchemaClass [ dsn user password ]
-
-    Where:
-      Foo is the short name for the Model class being generated
-      Foo::SchemaClass is the fully qualified classname of your Schema,
-        which isa DBIx::Class::Schema defined elsewhere.
-      dsn, user, and password are optional if connection info is already
-        defined in your Schema class (as it would be in the case of
-        DBIx::Class::Schema::Loader).
+  script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass [ create=dynamic | create=static ] [ connect_info arguments ]
 
 =head1 DESCRIPTION
 
 Helper for the DBIC Schema Models.
 
-=head2 METHODS
+=head2 Arguments:
+
+C<CatalystModelName> is the short name for the Catalyst Model class
+being generated (i.e. callable with C<$c-E<gt>model('CatalystModelName')>).
+
+C<MyApp::SchemaClass> 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
+class for you, basing it on L<DBIx::Class::Schema::Loader> (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
+class for you, using L<DBIx::Class::Schema::Loader> in "one shot"
+mode to create a standard, manually-defined L<DBIx::Class::Schema>
+setup, based on what the Loader sees in your database at this moment.
+A Schema/Model pair generated this way will not require
+L<DBIx::Class::Schema::Loader> 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
+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<create=>
+options.
+
+Use of either of the C<create=> options requires L<DBIx::Class::Schema::Loader>.
+
+=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
+
+  # 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
 
-=head3 mk_compclass
+  # 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).
+  script/myapp_create.pl model ModelName DBIC::Schema My::SchemaClass
+
+=head1 METHODS
+
+=head2 mk_compclass
 
 =cut
 
 sub mk_compclass {
-    my ( $self, $helper, $schema_class, $dsn, $user, $pass ) = @_;
+    my ( $self, $helper, $schema_class, @connect_info) = @_;
 
-    $helper->{schema_class} = $schema_class || '';
+    $helper->{schema_class} = $schema_class
+        or croak "Must supply schema class name";
 
-    if(defined($dsn)) {
+    my $create = '';
+    if($connect_info[0] && $connect_info[0] =~ /^create=(dynamic|static)$/) {
+        $create = $1;
+        shift @connect_info;
+    }
+
+    if(@connect_info) {
         $helper->{setup_connect_info} = 1;
-        $helper->{dsn}         = $dsn  || '';
-        $helper->{user}        = $user || '';
-        $helper->{pass}        = $pass || '';
+        my @helper_connect_info = @connect_info;
+        for(@helper_connect_info) {
+            $_ = qq{'$_'} if $_ !~ /^\s*[[{]/;
+        }
+        $helper->{connect_info} = \@helper_connect_info;
+    }
+
+    if($create eq 'dynamic') {
+        my @schema_parts = split(/\:\:/, $helper->{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 );
+    }
+    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 %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) {
+            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} };
+        }
+
+        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};
@@ -56,8 +193,7 @@ L<Catalyst::Response>, L<Catalyst::Helper>, L<Catalyst>,
 Stuff related to DBIC and this Model style:
 
 L<DBIx::Class>, L<DBIx::Class::Schema>,
-L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>,
-L<Catalyst::Helper::Model::DBIC::SchemaLoader>
+L<DBIx::Class::Schema::Loader>, L<Catalyst::Model::DBIC::Schema>
 
 =head1 AUTHOR
 
@@ -72,9 +208,45 @@ it under the same terms as Perl itself.
 
 1;
 
+__DATA__
+
 =begin pod_to_ignore
 
-__DATA__
+__schemaclass__
+package [% schema_class %];
+
+use strict;
+use base qw/DBIx::Class::Schema::Loader/;
+
+__PACKAGE__->loader_options(
+    relationships => 1,
+    # debug => 1,
+);
+
+=head1 NAME
+
+[% schema_class %] - DBIx::Class::Schema::Loader class
+
+=head1 SYNOPSIS
+
+See L<[% app %]>
+
+=head1 DESCRIPTION
+
+Generated by L<Catalyst::Model::DBIC::Schema> for use in L<[% class %]>
+
+=head1 AUTHOR
+
+[% author %]
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;
 
 __compclass__
 package [% class %];
@@ -84,33 +256,22 @@ use base 'Catalyst::Model::DBIC::Schema';
 
 __PACKAGE__->config(
     schema_class => '[% schema_class %]',
-    [% IF setup_connect_info %]
-    connect_info => [ '[% dsn %]',
-                      '[% user %]',
-                      '[% pass %]',
-                      {
-                          RaiseError         => 1,
-                          PrintError         => 0,
-                          ShowErrorStatement => 1,
-                          TraceLevel         => 0,
-                          AutoCommit         => 1,
-                      }
-                    ],
-    [% END %]
+    [% IF setup_connect_info %]connect_info => [
+        [% FOREACH arg = connect_info %][% arg %],
+        [% END %]
+    ],[% END %]
 );
 
 =head1 NAME
 
 [% class %] - Catalyst DBIC Schema Model
-
 =head1 SYNOPSIS
 
 See L<[% app %]>
 
 =head1 DESCRIPTION
 
-L<Catalyst::Model::DBIC::Schema> Model using schema
-L<[% schema_class %]>
+L<Catalyst::Model::DBIC::Schema> Model using schema L<[% schema_class %]>
 
 =head1 AUTHOR