Merge branch 'master' into custom_column_info
Rafael Kitover [Fri, 19 Feb 2010 06:49:07 +0000 (01:49 -0500)]
lib/DBIx/Class/Schema/Loader/Base.pm
lib/DBIx/Class/Schema/Loader/DBI.pm
t/lib/dbixcsl_common_tests.pm

index 08a685f..4f57523 100644 (file)
@@ -33,6 +33,7 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 skip_relationships
                                 skip_load_external
                                 moniker_map
+                                custom_column_info
                                 inflect_singular
                                 inflect_plural
                                 debug
@@ -54,6 +55,8 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/
                                 monikers
                                 dynamic
                                 naming
+                                datetime_timezone
+                                datetime_locale
 /);
 
 
@@ -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
@@ -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;
index 0aac5ed..fb2669e 100644 (file)
@@ -282,17 +282,24 @@ sub _columns_info_for {
         eval {
             my $sth = $dbh->column_info( undef, $self->db_schema, $table, '%' );
             while ( my $info = $sth->fetchrow_hashref() ){
-                my %column_info;
-                $column_info{data_type}   = $info->{TYPE_NAME};
-                $column_info{size}      = $info->{COLUMN_SIZE};
-                $column_info{is_nullable}   = $info->{NULLABLE} ? 1 : 0;
-                $column_info{default_value} = $info->{COLUMN_DEF};
+                my $column_info = {};
+                $column_info->{data_type}   = $info->{TYPE_NAME};
+                $column_info->{size}      = $info->{COLUMN_SIZE};
+                $column_info->{is_nullable}   = $info->{NULLABLE} ? 1 : 0;
+                $column_info->{default_value} = $info->{COLUMN_DEF};
                 my $col_name = $info->{COLUMN_NAME};
                 $col_name =~ s/^\"(.*)\"$/$1/;
 
                 my $extra_info = $self->_extra_column_info($info) || {};
+                $column_info = { %$column_info, %$extra_info };
 
-                $result{$col_name} = { %column_info, %$extra_info };
+                my $custom_info = $self->_custom_column_info( $table, $col_name, $column_info ) || {};
+                $column_info = { %$column_info, %$custom_info };
+
+                my $datetime_info = $self->_datetime_column_info( $table, $col_name, $column_info )  || {};
+                $column_info = { %$column_info, %$datetime_info };
+
+                $result{$col_name} = $column_info;
             }
             $sth->finish;
         };
@@ -304,19 +311,26 @@ sub _columns_info_for {
     $sth->execute;
     my @columns = @{$sth->{NAME_lc}};
     for my $i ( 0 .. $#columns ){
-        my %column_info;
-        $column_info{data_type} = $sth->{TYPE}->[$i];
-        $column_info{size} = $sth->{PRECISION}->[$i];
-        $column_info{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
-
-        if ($column_info{data_type} =~ m/^(.*?)\((.*?)\)$/) {
-            $column_info{data_type} = $1;
-            $column_info{size}    = $2;
+        my $column_info = {};
+        $column_info->{data_type} = $sth->{TYPE}->[$i];
+        $column_info->{size} = $sth->{PRECISION}->[$i];
+        $column_info->{is_nullable} = $sth->{NULLABLE}->[$i] ? 1 : 0;
+
+        if ($column_info->{data_type} =~ m/^(.*?)\((.*?)\)$/) {
+            $column_info->{data_type} = $1;
+            $column_info->{size}    = $2;
         }
 
         my $extra_info = $self->_extra_column_info($table, $columns[$i], $sth, $i) || {};
+        $column_info = { %$column_info, %$extra_info };
+
+        my $custom_info = $self->_custom_column_info( $table, $columns[$i], $column_info ) || {};
+        $column_info = { %$column_info, %$custom_info };
+
+        my $datetime_info = $self->_datetime_column_info( $table, $columns[$i], $column_info )  || {};
+        $column_info = { %$column_info, %$datetime_info };
 
-        $result{$columns[$i]} = { %column_info, %$extra_info };
+        $result{$columns[$i]} = $column_info;
     }
     $sth->finish;
 
index 7fa28cb..478cf52 100644 (file)
@@ -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();
 
@@ -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};
@@ -125,7 +150,7 @@ sub setup_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} || [] };
@@ -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" );
@@ -457,6 +486,7 @@ sub test_schema {
         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);
@@ -605,11 +635,24 @@ sub test_schema {
         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' });
 
@@ -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 = (
@@ -1245,6 +1297,7 @@ sub drop_tables {
         LOADER_TEST23
         LoAdEr_test24
         loader_test35
+        loader_test36
     /;
     
     my @tables_auto_inc = (