Merge branch 'master' into custom_column_info
[dbsrgits/DBIx-Class-Schema-Loader.git] / lib / DBIx / Class / Schema / Loader / Base.pm
index 25b8f86..4f57523 100644 (file)
@@ -14,9 +14,10 @@ use Digest::MD5 qw//;
 use Lingua::EN::Inflect::Number qw//;
 use File::Temp qw//;
 use Class::Unload;
+use Class::Inspector ();
 require DBIx::Class;
 
-our $VERSION = '0.04999_13';
+our $VERSION = '0.05002';
 
 __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 schema
@@ -32,13 +33,13 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 skip_relationships
                                 skip_load_external
                                 moniker_map
+                                custom_column_info
                                 inflect_singular
                                 inflect_plural
                                 debug
                                 dump_directory
                                 dump_overwrite
                                 really_erase_my_files
-                                result_namespace
                                 resultset_namespace
                                 default_resultset_class
                                 schema_base_class
@@ -54,6 +55,8 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 monikers
                                 dynamic
                                 naming
+                                datetime_timezone
+                                datetime_locale
 /);
 
 
@@ -62,7 +65,13 @@ __PACKAGE__->mk_group_accessors('simple', qw/
                                 schema_version_to_dump
                                 _upgrading_from
                                 _upgrading_from_load_classes
+                                _downgrading_to_load_classes
+                                _rewriting_result_namespace
                                 use_namespaces
+                                result_namespace
+                                generate_pod
+                                pod_comment_mode
+                                pod_comment_spillover_length
 /);
 
 =head1 NAME
@@ -156,6 +165,48 @@ next major version upgrade:
 
     __PACKAGE__->naming('v5');
 
+=head2 generate_pod
+
+By default POD will be generated for columns and relationships, using database
+metadata for the text if available and supported.
+
+Reading database metadata (e.g. C<COMMENT ON TABLE some_table ...>) is only
+supported for Postgres right now.
+
+Set this to C<0> to turn off all POD generation.
+
+=head2 pod_comment_mode
+
+Controls where table comments appear in the generated POD. Smaller table
+comments are appended to the C<NAME> section of the documentation, and larger
+ones are inserted into C<DESCRIPTION> instead. You can force a C<DESCRIPTION>
+section to be generated with the comment always, only use C<NAME>, or choose
+the length threshold at which the comment is forced into the description.
+
+=over 4
+
+=item name
+
+Use C<NAME> section only.
+
+=item description
+
+Force C<DESCRIPTION> always.
+
+=item auto
+
+Use C<DESCRIPTION> if length > L</pod_comment_spillover_length>, this is the
+default.
+
+=back
+
+=head2 pod_comment_spillover_length
+
+When pod_comment_mode is set to C<auto>, this is the length of the comment at
+which it will be forced into a separate description section.
+
+The default is C<60>
+
 =head2 relationship_attrs
 
 Hashref of attributes to pass to each generated relationship, listed
@@ -331,6 +382,54 @@ made to Loader-generated code.
 Again, you should be using version control on your schema classes.  Be
 careful with this option.
 
+=head2 custom_column_info
+
+Must be a coderef, returing a hashref with the custom column informations.
+
+Example:
+
+    custom_column_info => sub {
+        my $info = shift;
+        # Example from $info hashref:
+        # $info = {
+        #           'DECIMAL_DIGITS' => undef,
+        #           'COLUMN_DEF' => undef,
+        #           'TABLE_CAT' => undef,
+        #           'NUM_PREC_RADIX' => undef,
+        #           'TABLE_SCHEM' => 'TESTS',
+        #           'BUFFER_LENGTH' => '8',
+        #           'CHAR_OCTET_LENGTH' => undef,
+        #           'IS_NULLABLE' => 'NO',
+        #           'REMARKS' => undef,
+        #           'COLUMN_SIZE' => '8',
+        #           'ORDINAL_POSITION' => '1',
+        #           'COLUMN_NAME' => 'LOADER_TEST9',
+        #           'TYPE_NAME' => 'VARCHAR2',
+        #           'NULLABLE' => '0',
+        #           'DATA_TYPE' => '12',
+        #           'TABLE_NAME' => 'LOADER_TEST9',
+        #           'SQL_DATA_TYPE' => '12',
+        #           'SQL_DATETIME_SUB' => undef
+        #         };
+        
+        if ( $info->{TYPE_NAME} eq 'DATE' ){
+            return { timezone => "Europe/Berlin" };
+        }
+        return;
+    }
+
+Add to all columns with type DATE the attribute timezone => "Europe/Berlin". 
+
+=head2 datetime_timezone
+
+Set timezone attribute for L<DBIx::Class::InflateColumn::DateTime> 
+to all columns with the type DATE.
+
+=head2 datetime_locale
+
+Set local attribute for L<DBIx::Class::InflateColumn::DateTime> 
+to all columns with the type DATE.
+
 =head1 METHODS
 
 None of these methods are intended for direct invocation by regular
@@ -339,7 +438,12 @@ can also be found via standard L<DBIx::Class::Schema> methods somehow.
 
 =cut
 
-use constant CURRENT_V => 'v5';
+use constant CURRENT_V  => 'v5';
+
+use constant CLASS_ARGS => qw(
+    schema_base_class result_base_class additional_base_classes
+    left_base_classes additional_classes components resultset_components
+);
 
 # ensure that a peice of object data is a valid arrayref, creating
 # an empty one or encapsulating whatever's there.
@@ -374,6 +478,8 @@ sub new {
                                resultset_components
                               /);
 
+    $self->_validate_class_args;
+
     push(@{$self->{components}}, 'ResultSetManager')
         if @{$self->{resultset_components}};
 
@@ -417,6 +523,9 @@ sub new {
     $self->_check_back_compat;
 
     $self->use_namespaces(1) unless defined $self->use_namespaces;
+    $self->generate_pod(1)   unless defined $self->generate_pod;
+    $self->pod_comment_mode('auto')         unless defined $self->pod_comment_mode;
+    $self->pod_comment_spillover_length(60) unless defined $self->pod_comment_spillover_length;
 
     $self;
 }
@@ -437,6 +546,8 @@ Dynamic schema detected, will run in 0.04006 mode.
 Set the 'naming' attribute or the SCHEMA_LOADER_BACKCOMPAT environment variable
 to disable this warning.
 
+Also consider setting 'use_namespaces => 1' if/when upgrading.
+
 See perldoc DBIx::Class::Schema::Loader::Manual::UpgradingFromV4 for more
 details.
 EOF
@@ -448,7 +559,12 @@ EOF
         $self->naming->{relationships} ||= 'v4';
         $self->naming->{monikers}      ||= 'v4';
 
-        $self->use_namespaces(0) unless defined $self->use_namespaces;
+        if ($self->use_namespaces) {
+            $self->_upgrading_from_load_classes(1);
+        }
+        else {
+            $self->use_namespaces(0);
+        }
 
         return;
     }
@@ -460,21 +576,55 @@ EOF
     open(my $fh, '<', $filename)
         or croak "Cannot open '$filename' for reading: $!";
 
-    my $load_classes = 0;
+    my $load_classes     = 0;
+    my $result_namespace = '';
 
     while (<$fh>) {
         if (/^__PACKAGE__->load_classes;/) {
             $load_classes = 1;
-        } elsif (/^# Created by DBIx::Class::Schema::Loader v((\d+)\.(\d+))/) {
-            my $real_ver = $1;
+        } elsif (/result_namespace => '([^']+)'/) {
+            $result_namespace = $1;
+        } elsif (my ($real_ver) =
+                /^# Created by DBIx::Class::Schema::Loader v(\d+\.\d+)/) {
+
+            if ($load_classes && (not defined $self->use_namespaces)) {
+                warn <<"EOF"  unless $ENV{SCHEMA_LOADER_BACKCOMPAT};
+
+'load_classes;' static schema detected, turning off 'use_namespaces'.
+
+Set the 'use_namespaces' attribute or the SCHEMA_LOADER_BACKCOMPAT environment
+variable to disable this warning.
+
+See perldoc DBIx::Class::Schema::Loader::Manual::UpgradingFromV4 for more
+details.
+EOF
+                $self->use_namespaces(0);
+            }
+            elsif ($load_classes && $self->use_namespaces) {
+                $self->_upgrading_from_load_classes(1);
+            }
+            elsif ((not $load_classes) && defined $self->use_namespaces
+                                       && (not $self->use_namespaces)) {
+                $self->_downgrading_to_load_classes(
+                    $result_namespace || 'Result'
+                );
+            }
+            elsif ((not defined $self->use_namespaces)
+                   || $self->use_namespaces) {
+                if (not $self->result_namespace) {
+                    $self->result_namespace($result_namespace || 'Result');
+                }
+                elsif ($result_namespace ne $self->result_namespace) {
+                    $self->_rewriting_result_namespace(
+                        $result_namespace || 'Result'
+                    );
+                }
+            }
 
             # XXX when we go past .0 this will need fixing
             my ($v) = $real_ver =~ /([1-9])/;
             $v = "v$v";
 
-            $self->_upgrading_from_load_classes($load_classes)
-                unless defined $self->use_namespaces;
-
             last if $v eq CURRENT_V || $real_ver =~ /^0\.\d\d999/;
 
             if (not %{ $self->naming }) {
@@ -499,22 +649,49 @@ EOF
 
             $self->schema_version_to_dump($real_ver);
 
-            $self->use_namespaces(0) unless defined $self->use_namespaces;
-
             last;
         }
     }
     close $fh;
 }
 
+sub _validate_class_args {
+    my $self = shift;
+    my $args = shift;
+    
+    foreach my $k (CLASS_ARGS) {
+        next unless $self->$k;
+
+        my @classes = ref $self->$k eq 'ARRAY' ? @{ $self->$k } : $self->$k;
+        foreach my $c (@classes) {
+            # components default to being under the DBIx::Class namespace unless they
+            # are preceeded with a '+'
+            if ( $k =~ m/components$/ && $c !~ s/^\+// ) {
+                $c = 'DBIx::Class::' . $c;
+            }
+
+            # 1 == installed, 0 == not installed, undef == invalid classname
+            my $installed = Class::Inspector->installed($c);
+            if ( defined($installed) ) {
+                if ( $installed == 0 ) {
+                    croak qq/$c, as specified in the loader option "$k", is not installed/;
+                }
+            } else {
+                croak qq/$c, as specified in the loader option "$k", is an invalid class name/;
+            }
+        }
+    }
+}
+
 sub _find_file_in_inc {
     my ($self, $file) = @_;
 
     foreach my $prefix (@INC) {
         my $fullpath = File::Spec->catfile($prefix, $file);
         return $fullpath if -f $fullpath
-            and Cwd::abs_path($fullpath) ne
-               (Cwd::abs_path(File::Spec->catfile($self->dump_directory, $file)) || '');
+            # abs_path throws on Windows for nonexistant files
+            and eval { Cwd::abs_path($fullpath) } ne
+               (eval { Cwd::abs_path(File::Spec->catfile($self->dump_directory, $file)) } || '');
     }
 
     return;
@@ -536,10 +713,20 @@ sub _find_class_in_inc {
     return $self->_find_file_in_inc($self->_class_path($class));
 }
 
+sub _rewriting {
+    my $self = shift;
+
+    return $self->_upgrading_from
+        || $self->_upgrading_from_load_classes
+        || $self->_downgrading_to_load_classes
+        || $self->_rewriting_result_namespace
+    ;
+}
+
 sub _rewrite_old_classnames {
     my ($self, $code) = @_;
 
-    return $code unless $self->_upgrading_from;
+    return $code unless $self->_rewriting;
 
     my %old_classes = reverse %{ $self->_upgrading_classes };
 
@@ -562,7 +749,7 @@ sub _load_external {
     my $real_inc_path = $self->_find_class_in_inc($class);
 
     my $old_class = $self->_upgrading_classes->{$class}
-        if $self->_upgrading_from;
+        if $self->_rewriting;
 
     my $old_real_inc_path = $self->_find_class_in_inc($old_class)
         if $old_class && $old_class ne $class;
@@ -905,6 +1092,23 @@ sub _dump_to_dir {
         $self->_write_classfile($src_class, $src_text);
     }
 
+    # remove Result dir if downgrading from use_namespaces, and there are no
+    # files left.
+    if (my $result_ns = $self->_downgrading_to_load_classes
+                        || $self->_rewriting_result_namespace) {
+        my $result_namespace = $self->_result_namespace(
+            $schema_class,
+            $result_ns,
+        );
+
+        (my $result_dir = $result_namespace) =~ s{::}{/}g;
+        $result_dir = $self->dump_directory . '/' . $result_dir;
+
+        unless (my @files = glob "$result_dir/*") {
+            rmdir $result_dir;
+        }
+    }
+
     warn "Schema dump completed.\n" unless $self->{dynamic} or $self->{quiet};
 
 }
@@ -1057,6 +1261,22 @@ sub _inject {
     $self->_raw_stmt($target, "use base qw/ $blist /;") if @_;
 }
 
+sub _result_namespace {
+    my ($self, $schema_class, $ns) = @_;
+    my @result_namespace;
+
+    if ($ns =~ /^\+(.*)/) {
+        # Fully qualified namespace
+        @result_namespace = ($1)
+    }
+    else {
+        # Relative namespace
+        @result_namespace = ($schema_class, $ns);
+    }
+
+    return wantarray ? @result_namespace : join '::', @result_namespace;
+}
+
 # Create class with applicable bases, setup monikers, etc
 sub _make_src_class {
     my ($self, $table) = @_;
@@ -1068,19 +1288,34 @@ sub _make_src_class {
     my @result_namespace = ($schema_class);
     if ($self->use_namespaces) {
         my $result_namespace = $self->result_namespace || 'Result';
-        if ($result_namespace =~ /^\+(.*)/) {
-            # Fully qualified namespace
-            @result_namespace =  ($1)
-        }
-        else {
-            # Relative namespace
-            push @result_namespace, $result_namespace;
-        }
+        @result_namespace = $self->_result_namespace(
+            $schema_class,
+            $result_namespace,
+        );
     }
     my $table_class = join(q{::}, @result_namespace, $table_moniker);
 
-    if (my $upgrading_v = $self->_upgrading_from) {
-        local $self->naming->{monikers} = $upgrading_v;
+    if ((my $upgrading_v = $self->_upgrading_from)
+            || $self->_rewriting) {
+        local $self->naming->{monikers} = $upgrading_v
+            if $upgrading_v;
+
+        my @result_namespace = @result_namespace;
+        if ($self->_upgrading_from_load_classes) {
+            @result_namespace = ($schema_class);
+        }
+        elsif (my $ns = $self->_downgrading_to_load_classes) {
+            @result_namespace = $self->_result_namespace(
+                $schema_class,
+                $ns,
+            );
+        }
+        elsif ($ns = $self->_rewriting_result_namespace) {
+            @result_namespace = $self->_result_namespace(
+                $schema_class,
+                $ns,
+            );
+        }
 
         my $old_class = join(q{::}, @result_namespace,
             $self->_table2moniker($table));
@@ -1262,7 +1497,7 @@ sub _dbic_stmt {
     my $method = shift;
 
     # generate the pod for this statement, storing it with $self->_pod
-    $self->_make_pod( $class, $method, @_ );
+    $self->_make_pod( $class, $method, @_ ) if $self->generate_pod;
 
     my $args = dump(@_);
     $args = '(' . $args . ')' if @_ < 2;
@@ -1282,14 +1517,23 @@ sub _make_pod {
 
     if ( $method eq 'table' ) {
         my ($table) = @_;
-        $self->_pod( $class, "=head1 NAME" );
-        my $table_descr = $class;
+        my $pcm = $self->pod_comment_mode;
+        my ($comment, $comment_overflows, $comment_in_name, $comment_in_desc);
         if ( $self->can('_table_comment') ) {
-            my $comment = $self->_table_comment($table);
-            $table_descr .= " - " . $comment if $comment;
+            $comment = $self->_table_comment($table);
+            $comment_overflows = ($comment and length $comment > $self->pod_comment_spillover_length);
+            $comment_in_name   = ($pcm eq 'name' or ($pcm eq 'auto' and !$comment_overflows));
+            $comment_in_desc   = ($pcm eq 'description' or ($pcm eq 'auto' and $comment_overflows));
         }
+        $self->_pod( $class, "=head1 NAME" );
+        my $table_descr = $class;
+        $table_descr .= " - " . $comment if $comment and $comment_in_name;
         $self->{_class2table}{ $class } = $table;
         $self->_pod( $class, $table_descr );
+        if ($comment and $comment_in_desc) {
+            $self->_pod( $class, "=head1 DESCRIPTION" );
+            $self->_pod( $class, $comment );
+        }
         $self->_pod_cut( $class );
     } elsif ( $method eq 'add_columns' ) {
         $self->_pod( $class, "=head1 ACCESSORS" );
@@ -1301,9 +1545,11 @@ sub _make_pod {
            $self->_pod( $class,
                         join "\n", map {
                             my $s = $attrs->{$_};
-                            $s = !defined $s      ? 'undef'          :
-                                 length($s) == 0  ? '(empty string)' :
-                                                     $s;
+                            $s = !defined $s         ? 'undef'          :
+                                  length($s) == 0     ? '(empty string)' :
+                                  ref($s) eq 'SCALAR' ? $$s              :
+                                                        $s
+                                  ;
 
                             "  $_: $s"
                         } sort keys %$attrs,
@@ -1338,7 +1584,6 @@ sub _pod_cut {
     $self->_raw_stmt( $class, "\n=cut\n" );
 }
 
-
 # Store a raw source line for a class (for dumping purposes)
 sub _raw_stmt {
     my ($self, $class, $stmt) = @_;
@@ -1367,6 +1612,32 @@ sub _quote_table_name {
 
 sub _is_case_sensitive { 0 }
 
+sub _custom_column_info {
+    my ( $self, $table_name, $column_name, $column_info ) = @_;
+
+    if( ref $self->custom_column_info eq 'CODE' ) {
+        return $self->custom_column_info->( $table_name, $column_name, $column_info );
+    }
+    return {};
+}
+
+sub _datetime_column_info {
+    my ( $self, $table_name, $column_name, $column_info ) = @_;
+    my $return = {};
+    my $type = lc ( $column_info->{data_type} );
+    if (
+        ( defined $column_info->{inflate_datetime} and $column_info->{inflate_datetime} )
+        or ( defined $column_info->{inflate_date} and $column_info->{inflate_date} )
+        or ( $type eq 'date')
+        or ( $type eq 'datetime')
+        or ( $type eq 'timestamp')
+    ){
+        $return->{timezone} = $self->datetime_timezone if $self->datetime_timezone;
+        $return->{locale}   = $self->datetime_locale if $self->datetime_locale;
+    }
+    return $return;
+}
+
 # remove the dump dir from @INC on destruction
 sub DESTROY {
     my $self = shift;