From: Robert Bohne Date: Fri, 19 Feb 2010 06:44:23 +0000 (+0100) Subject: Bettering custom/datetime_column_info parameters X-Git-Tag: 0.05003~5^2~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=23d1f36b238acbceaa1ecd71743ce4f5b8e8fd7e;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Bettering custom/datetime_column_info parameters Use DBIx::Class column_info instead if DBI column_info. The DBIx::Class column_info is more generic than the DBI column_info. --- diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 33642f0..c93fa90 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -1611,19 +1611,18 @@ sub _quote_table_name { sub _is_case_sensitive { 0 } sub _custom_column_info { - my ( $self, $info ) = @_; + my ( $self, $table_name, $column_name, $column_info ) = @_; if( ref $self->custom_column_info eq 'CODE' ) { - return $self->custom_column_info->($info); + return $self->custom_column_info->( $table_name, $column_name, $column_info ); } return {}; } sub _datetime_column_info { - my ( $self, $info, $column_info) = @_; + my ( $self, $table_name, $column_name, $column_info ) = @_; my $return = {}; - my $type = lc ( $info->{TYPE_NAME} ); - + 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} ) diff --git a/lib/DBIx/Class/Schema/Loader/DBI.pm b/lib/DBIx/Class/Schema/Loader/DBI.pm index df76ae5..fb2669e 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI.pm @@ -282,19 +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) || {}; - my $custom_info = $self->_custom_column_info($info) || {}; - my $datetime_info = $self->_datetime_column_info($info, - { %column_info, %$extra_info, %$custom_info }) || {}; - $result{$col_name} = { %column_info, %$extra_info, %$custom_info, %$datetime_info }; + $column_info = { %$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; }; @@ -306,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; diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index 35cb49a..b209fe8 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -58,20 +58,22 @@ sub _monikerize { } sub _custom_column_info { - my $info = shift; + my ( $table_name, $column_name, $column_info ) = @_; + $table_name = lc ( $table_name ); + $column_name = lc ( $column_name ); - if ( lc( $info->{TABLE_NAME} ) eq 'loader_test11' - and lc( $info->{COLUMN_NAME} ) eq 'loader_test10' + 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 ( lc( $info->{TABLE_NAME} ) eq 'loader_test36' ){ + if ( $table_name eq 'loader_test36' ){ return { inflate_datetime => 1 } if - ( lc( $info->{COLUMN_NAME} ) eq 'b_char_as_data' ); + ( $column_name eq 'b_char_as_data' ); return { inflate_date => 1 } if - ( lc( $info->{COLUMN_NAME} ) eq 'c_char_as_data' ); + ( $column_name eq 'c_char_as_data' ); } return;