Merge branch 'master' into custom_column_info
Robert Bohne [Fri, 19 Feb 2010 06:48:56 +0000 (07:48 +0100)]
1  2 
lib/DBIx/Class/Schema/Loader/Base.pm
t/lib/dbixcsl_common_tests.pm

@@@ -33,7 -33,6 +33,7 @@@ __PACKAGE__->mk_group_ro_accessors('sim
                                  skip_relationships
                                  skip_load_external
                                  moniker_map
 +                                custom_column_info
                                  inflect_singular
                                  inflect_plural
                                  debug
@@@ -55,8 -54,6 +55,8 @@@
                                  monikers
                                  dynamic
                                  naming
 +                                datetime_timezone
 +                                datetime_locale
  /);
  
  
@@@ -382,54 -379,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
@@@ -1545,9 -1494,11 +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,
@@@ -1610,32 -1561,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;
@@@ -57,32 -57,10 +57,32 @@@ sub _monikerize 
      return undef;
  }
  
 +sub _custom_column_info {
 +    my ( $table_name, $column_name, $column_info ) = @_;
 +    $table_name = lc ( $table_name );
 +    $column_name = lc ( $column_name );
 +
 +    if ( $table_name eq 'loader_test11' 
 +        and $column_name eq 'loader_test10' 
 +    ){
 +        return { is_numeric => 1 }
 +    }
 +    # Set inflate_datetime or  inflate_date to check 
 +    #   datetime_timezone and datetime_locale
 +    if ( $table_name eq 'loader_test36' ){
 +        return { inflate_datetime => 1 } if 
 +            ( $column_name eq 'b_char_as_data' );
 +        return { inflate_date => 1 } if 
 +            ( $column_name eq 'c_char_as_data' );
 +    }
 +
 +    return;
 +}
 +
  sub run_tests {
      my $self = shift;
  
 -    plan tests => 145 + ($self->{extra}->{count} || 0);
 +    plan tests => 155 + ($self->{extra}->{count} || 0);
  
      $self->create();
  
@@@ -124,12 -102,9 +124,12 @@@ sub setup_schema 
          inflect_plural          => { loader_test4 => 'loader_test4zes' },
          inflect_singular        => { fkid => 'fkid_singular' },
          moniker_map             => \&_monikerize,
 +        custom_column_info      => \&_custom_column_info,
          debug                   => $debug,
          use_namespaces          => 0,
          dump_directory          => $DUMP_DIR,
 +        datetime_timezone       => 'Europe/Berlin',
 +        datetime_locale         => 'de_DE'
      );
  
      $loader_opts{db_schema} = $self->{db_schema} if $self->{db_schema};
         my $file_count;
         find sub { return if -d; $file_count++ }, $DUMP_DIR;
  
 -       my $expected_count = 35;
 +       my $expected_count = 36;
  
         $expected_count += grep /CREATE (?:TABLE|VIEW)/i,
             @{ $self->{extra}{create} || [] };
  
         $warn_count++ for grep /^Bad table or view/, @loader_warnings;
  
+        $warn_count++ for grep /stripping trailing _id/, @loader_warnings;
         my $vendor = $self->{vendor};
         $warn_count++ for grep /${vendor}_\S+ has no primary key/,
             @loader_warnings;
@@@ -457,10 -434,6 +459,10 @@@ sub test_schema 
          my $class34   = $classes->{loader_test34};
          my $rsobj34   = $conn->resultset($moniker34);
  
 +        my $moniker36 = $monikers->{loader_test36};
 +        my $class36   = $classes->{loader_test36};
 +        my $rsobj36   = $conn->resultset($moniker36);
 +        
          isa_ok( $rsobj3, "DBIx::Class::ResultSet" );
          isa_ok( $rsobj4, "DBIx::Class::ResultSet" );
          isa_ok( $rsobj5, "DBIx::Class::ResultSet" );
          isa_ok( $rsobj32, "DBIx::Class::ResultSet" );
          isa_ok( $rsobj33, "DBIx::Class::ResultSet" );
          isa_ok( $rsobj34, "DBIx::Class::ResultSet" );
 +        isa_ok( $rsobj36, "DBIx::Class::ResultSet" );
  
          # basic rel test
          my $obj4 = $rsobj4->find(123);
          my $class11   = $classes->{loader_test11};
          my $rsobj11   = $conn->resultset($moniker11);
  
 -        isa_ok( $rsobj10, "DBIx::Class::ResultSet" ); 
 +        isa_ok( $rsobj10, "DBIx::Class::ResultSet" );
          isa_ok( $rsobj11, "DBIx::Class::ResultSet" );
  
          ok($class10->column_info('loader_test11')->{is_foreign_key}, 'Foreign key detected');
          ok($class11->column_info('loader_test10')->{is_foreign_key}, 'Foreign key detected');
 +        # Added by custom_column_info
 +        ok($class11->column_info('loader_test10')->{is_numeric}, 'is_numeric detected');
 +
 +        is($class36->column_info('a_date')->{locale},'de_DE','locale is correct');
 +        is($class36->column_info('a_date')->{timezone},'Europe/Berlin','locale is correct');
 +
 +        ok($class36->column_info('b_char_as_data')->{inflate_datetime},'inflate_datetime detected');
 +        is($class36->column_info('b_char_as_data')->{locale},'de_DE','locale is correct');
 +        is($class36->column_info('b_char_as_data')->{timezone},'Europe/Berlin','locale is correct');
 +
 +        ok($class36->column_info('c_char_as_data')->{inflate_date},'inflate_date detected');
 +        is($class36->column_info('c_char_as_data')->{locale},'de_DE','locale is correct');
 +        is($class36->column_info('c_char_as_data')->{timezone},'Europe/Berlin','locale is correct');
  
          my $obj10 = $rsobj10->create({ subject => 'xyzzy' });
  
@@@ -889,15 -848,6 +891,15 @@@ sub create 
                  a_function $self->{default_function_def}
              ) $self->{innodb}
          },
 +
 +        qq{
 +            CREATE TABLE loader_test36 (
 +                id INTEGER NOT NULL PRIMARY KEY,
 +                a_date DATE,
 +                b_char_as_data VARCHAR(100),
 +                c_char_as_data VARCHAR(100)
 +            ) $self->{innodb}
 +        },
      );
  
      @statements_reltests = (
@@@ -1295,7 -1245,6 +1297,7 @@@ sub drop_tables 
          LOADER_TEST23
          LoAdEr_test24
          loader_test35
 +        loader_test36
      /;
      
      my @tables_auto_inc = (