minor changes
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / Helper / Model / DBIC / Schema.pm
index b560900..2895c86 100644 (file)
@@ -9,6 +9,10 @@ use Carp;
 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 Moose::Autobox;
+use List::MoreUtils 'firstidx';
 
 use namespace::clean -except => 'meta';
 
@@ -63,66 +67,71 @@ password, and connect options, respectively.  These are optional for
 existing Schemas, but required if you use either of the C<create=>
 options.
 
+username and password can be omitted for C<SQLite> dsns.
+
 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:
+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
+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='"'
+
+B<ON WINDOWS COMMAND LINES QUOTING RULES ARE DIFFERENT>
+
+In C<cmd.exe> the above example would be:
+
   script/myapp_create.pl model CatalystModelName DBIC::Schema \
-    MyApp::SchemaClass create=static dbi:SQLite:foo.db '' '' \
+    MyApp::SchemaClass create=static dbi:SQLite:foo.db \
     AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \
-    on_connect_do='["select 1", "select 2"]'
+    on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""
+
+Same, but with extra Schema::Loader args (separate multiple values by commas):
 
-  # 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$' moniker_map='{ foo => "FFFFUUUU" }' \
+    exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \
     dbi:Pg:dbname=foodb myuname mypass
 
-  # See DBIx::Class::Schema::Loader::Base for list of options
+See L<DBIx::Class::Schema::Loader::Base> for a list of options
+
+Create a dynamic DBIx::Class::Schema::Loader-based Schema,
+and a Model which references it (B<DEPRECATED>):
 
-  # 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
 
-  # Reference an existing Schema of any kind, and provide some connection information for ->config:
+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 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 => 1, default => sub {
-    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/;
-    }
-
-    0;
-});
+has create => (is => 'rw', isa => CreateOption);
+has args => (is => 'ro', isa => ArrayRef);
+has roles => (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
 
@@ -136,47 +145,74 @@ files.
 sub mk_compclass {
     my ($package, $helper, $schema_class, @args) = @_;
 
-    my $self = $package->new(helper => $helper, schema_class => $schema_class);
+    my $self = $package->new(
+        helper => $helper,
+        schema_class => $schema_class,
+        args => \@args
+    );
 
-    $helper->{schema_class} = $schema_class
-        or die "Must supply schema class name";
+    $self->run;
+}
+
+sub BUILD {
+    my $self   = shift;
+    my $helper = $self->helper;
+    my @args   = $self->args->flatten if $self->args;
+
+    $helper->{schema_class} = $self->schema_class;
 
     @args = $self->_cleanup_args(\@args);
 
-    my $create = '';
-    if ($args[0] && $args[0] =~ /^create=(dynamic|static)\z/) {
-        $create = $1;
-        shift @args;
+    my ($roles_idx, $roles);
+    if (($roles_idx = firstidx { ($roles) = /^roles=(\S*)\z/ } @args) != -1) {
+        my @roles = split /,/ => $roles;
 
-        if ($args[0] && $args[0] =~ /^roles=(.*)\z/) {
-            $helper->{roles} = '['
-                .(join ',' => map { qq{'$_'} } (split /,/ => $1))
-                .']';
-            shift @args;
-        }
+        $self->roles(\@roles);
+
+        $helper->{roles} = '['
+            .(join ',' => map { qq{'$_'} } ($self->roles->flatten))
+            .']';
+
+        splice @args, $roles_idx, 1, ();
+    }
+
+    if ($args[0] && $args[0] =~ /^create=(\S*)\z/) {
+        $self->create($1);
+        shift @args;
 
         if (@args) {
             $self->_parse_loader_args(\@args);
 
+            $helper->{loader_args} = $self->_build_helper_loader_args;
+
             if (first { /^dbi:/i } @args) {
                 $helper->{setup_connect_info} = 1;
 
                 $helper->{connect_info} =
                     $self->_build_helper_connect_info(\@args);
 
-                $self->_parse_connect_info(\@args) if $create eq 'static';
+                $self->_parse_connect_info(\@args);
             }
         }
     }
 
     $helper->{generator} = ref $self;
     $helper->{generator_version} = $VERSION;
+}
+
+=head2 run
+
+Can be called on an instance to generate the files.
+
+=cut
+
+sub run {
+    my $self = shift;
 
-    if ($create eq 'dynamic') {
+    if ($self->create eq 'dynamic') {
         $self->_print_dynamic_deprecation_warning;
-        $self->helper->{loader_args} = $self->_build_helper_loader_args;
         $self->_gen_dynamic_schema;
-    } elsif ($create eq 'static') {
+    } elsif ($self->create eq 'static') {
         $self->_gen_static_schema;
     }
 
@@ -199,6 +235,8 @@ sub _parse_loader_args {
     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};
@@ -230,7 +268,9 @@ sub _read_loader_args {
     while (@$args && $args->[0] !~ /^dbi:/) {
         my ($key, $val) = split /=/, shift(@$args), 2;
 
-        if ((my @vals = split /,/ => $val) > 1) {
+        if ($self->_is_struct($val)) {
+            $loader_args{$key} = $val;
+        } elsif ((my @vals = split /,/ => $val) > 1) {
             $loader_args{$key} = \@vals;
         } else {
             $loader_args{$key} = $val;
@@ -276,7 +316,7 @@ sub _build_helper_connect_info {
 
     my @connect_info = @$connect_info;
 
-    my ($dsn, $user, $password) = splice @connect_info, 0, 3;
+    my ($dsn, $user, $password) = $self->_get_dsn_user_pass(\@connect_info);
 
     tie my %helper_connect_info, 'Tie::IxHash';
 
@@ -298,7 +338,7 @@ sub _build_helper_connect_info {
                 if (ref $val) {
                     $val = $self->_data_struct_to_string($val);
                 } else {
-                    $val = qq{'$val'};
+                    $val = 'q{'.$val.'}';
                 }
 
                 $helper_connect_info{$key} = $val;
@@ -315,6 +355,22 @@ sub _build_helper_connect_info {
     \%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/;
+    }
+
+    0;
+}
+
 sub _data_struct_to_string {
     my ($self, $data) = @_;
 
@@ -326,12 +382,28 @@ sub _data_struct_to_string {
     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->[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) = splice @connect_info, 0, 3;
+    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);
@@ -348,7 +420,7 @@ sub _parse_connect_info {
 
         my ($key, $val) = split /=/, $_, 2;
 
-        if ($key =~ /^(?:quote_char|name_sep)\z/) {
+        if ($key =~ /^(?:quote_char|name_sep|limit_dialect)\z/) {
             $connect_info{$key} = $val;
         } else {
             $connect_info{$key} = eval $val;
@@ -363,10 +435,16 @@ sub _parse_connect_info {
     \%connect_info
 }
 
+sub _is_struct {
+    my ($self, $val) = @_;
+
+    return $val =~ /^\s*[[{]/;
+}
+
 sub _quote_unless_struct {
     my ($self, $val) = @_;
 
-    $val = qq{'$val'} if $val !~ /^\s*[[{]/;
+    $val = 'q{'.$val.'}' if not $self->_is_struct($val);
 
     $val;
 }
@@ -435,9 +513,7 @@ sub _cleanup_args {
     my ($self, $args) = @_;
 
 # remove blanks, ie. someoned doing foo \  bar
-#    my @res = grep !/^\s*\z/, @$args;
-# bad idea.
-    my @res = @$args;
+    my @res = grep !/^\s+\z/, @$args;
 
 # remove leading whitespace, ie. foo \ bar
     s/^\s*// for @res;
@@ -565,3 +641,5 @@ it under the same terms as Perl itself.
 =cut
 
 1;
+__END__
+# vim:sts=4 sw=4: