From: Rafael Kitover Date: Thu, 23 Dec 2010 12:29:28 +0000 (-0500) Subject: fix column name case bug X-Git-Tag: 0.07003~17 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=61d1cca103b4dc4dbbdfa0ee2b402eb619be644d;p=dbsrgits%2FDBIx-Class-Schema-Loader.git fix column name case bug --- diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 6754b0e..84cffc0 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -1148,7 +1148,8 @@ sub _reload_class { eval_without_redefine_warnings ("require $class"); } catch { - die "Failed to reload class $class: $_"; + my $source = slurp $self->_get_dump_filename($class); + die "Failed to reload class $class: $_.\n\nCLASS SOURCE:\n\n$source"; }; } @@ -1564,9 +1565,10 @@ sub _resolve_col_accessor_collisions { die $@ if $@; push @methods, @{ Class::Inspector->methods($class) || [] }; - push @methods, @{ Class::Inspector->methods('UNIVERSAL') || [] }; } + push @methods, @{ Class::Inspector->methods('UNIVERSAL') }; + my %methods; @methods{@methods} = (); @@ -1599,10 +1601,6 @@ EOF } } } - - # FIXME: it appears that this method should also check that the - # default accessor (i.e. the column name itself) is not colliding - # with any of these methods } # use the same logic to run moniker_map, column_accessor_map, and @@ -1630,8 +1628,7 @@ sub _default_column_accessor_name { my $accessor_name = $column_name; $accessor_name =~ s/\W+/_/g; - # for backcompat - if( ($self->naming->{column_accessors}||'') =~ /(\d+)/ && $1 < 7 ) { + if ((($self->naming->{column_accessors}||'') =~ /(\d+)/ && $1 < 7) || (not $self->preserve_case)) { # older naming just lc'd the col accessor and that's all. return lc $accessor_name; } @@ -1639,6 +1636,7 @@ sub _default_column_accessor_name { return join '_', map lc, split_name $column_name; } + sub _make_column_accessor_name { my ($self, $column_name, $column_context_info ) = @_; diff --git a/lib/DBIx/Class/Schema/Loader/DBI.pm b/lib/DBIx/Class/Schema/Loader/DBI.pm index 7cb0c66..b23023a 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI.pm @@ -5,6 +5,8 @@ use warnings; use base qw/DBIx::Class::Schema::Loader::Base/; use mro 'c3'; use Carp::Clan qw/^DBIx::Class/; +use Try::Tiny; +use namespace::clean; our $VERSION = '0.07002'; @@ -123,17 +125,16 @@ sub _filter_tables { @tables = grep { ! /$exclude/ } @$tables if defined $exclude; for my $table (@tables) { - eval { + try { my $sth = $self->_sth_for($table, undef, \'1 = 0'); $sth->execute; - }; - if (not $@) { - push @filtered_tables, $table; } - else { - warn "Bad table or view '$table', ignoring: $@\n"; + catch { + warn "Bad table or view '$table', ignoring: $_\n"; $self->_unregister_source_for_table($table); - } + }; + + push @filtered_tables, $table; } return @filtered_tables; @@ -282,46 +283,48 @@ sub _columns_info_for { my $dbh = $self->schema->storage->dbh; + my %result; + if ($dbh->can('column_info')) { - my %result; - eval { - my $sth = $self->_dbh_column_info($dbh, undef, $self->db_schema, $table, '%' ); - while ( my $info = $sth->fetchrow_hashref() ){ - my $column_info = {}; - $column_info->{data_type} = lc $info->{TYPE_NAME}; - - my $size = $info->{COLUMN_SIZE}; - - if (defined $size && defined $info->{DECIMAL_DIGITS}) { - $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}]; - } - elsif (defined $size) { - $column_info->{size} = $size; - } - - $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0; - $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF}; - my $col_name = $info->{COLUMN_NAME}; - $col_name =~ s/^\"(.*)\"$/$1/; - - my $extra_info = $self->_extra_column_info( - $table, $col_name, $column_info, $info - ) || {}; - $column_info = { %$column_info, %$extra_info }; - - $result{$col_name} = $column_info; + my $sth = $self->_dbh_column_info($dbh, undef, $self->db_schema, $table, '%' ); + while ( my $info = $sth->fetchrow_hashref() ){ + my $column_info = {}; + $column_info->{data_type} = lc $info->{TYPE_NAME}; + + my $size = $info->{COLUMN_SIZE}; + + if (defined $size && defined $info->{DECIMAL_DIGITS}) { + $column_info->{size} = [$size, $info->{DECIMAL_DIGITS}]; } - $sth->finish; - }; + elsif (defined $size) { + $column_info->{size} = $size; + } + + $column_info->{is_nullable} = $info->{NULLABLE} ? 1 : 0; + $column_info->{default_value} = $info->{COLUMN_DEF} if defined $info->{COLUMN_DEF}; + my $col_name = $info->{COLUMN_NAME}; + $col_name =~ s/^\"(.*)\"$/$1/; + + $col_name = $self->_lc($col_name); - return \%result if !$@ && scalar keys %result; + my $extra_info = $self->_extra_column_info( + $table, $col_name, $column_info, $info + ) || {}; + $column_info = { %$column_info, %$extra_info }; + + $result{$col_name} = $column_info; + } + $sth->finish; + + return \%result if %result; } - my %result; my $sth = $self->_sth_for($table, undef, \'1 = 0'); $sth->execute; - my @columns = @{ $self->preserve_case ? $sth->{NAME} : $sth->{NAME_lc} }; - for my $i ( 0 .. $#columns ){ + + my @columns = $sth->{NAME}; + + for my $i (0 .. $#columns) { my $column_info = {}; $column_info->{data_type} = lc $sth->{TYPE}->[$i]; @@ -344,7 +347,7 @@ sub _columns_info_for { my $extra_info = $self->_extra_column_info($table, $columns[$i], $column_info) || {}; $column_info = { %$column_info, %$extra_info }; - $result{$columns[$i]} = $column_info; + $result{ $self->_lc($columns[$i]) } = $column_info; } $sth->finish; @@ -352,7 +355,7 @@ sub _columns_info_for { my $colinfo = $result{$col}; my $type_num = $colinfo->{data_type}; my $type_name; - if(defined $type_num && $type_num =~ /^\d+\z/ && $dbh->can('type_info')) { + if (defined $type_num && $type_num =~ /^\d+\z/ && $dbh->can('type_info')) { my $type_info = $dbh->type_info($type_num); $type_name = $type_info->{TYPE_NAME} if $type_info; $colinfo->{data_type} = lc $type_name if $type_name; diff --git a/t/14ora_common.t b/t/14ora_common.t index f8bd668..25805e1 100644 --- a/t/14ora_common.t +++ b/t/14ora_common.t @@ -158,17 +158,15 @@ my $tester = dbixcsl_common_tests->new( } } - SKIP: { - skip 'not running comment tests', 1 unless (my $class = $classes->{oracle_loader_test1}); - my $filename = $schema->_loader->get_dump_filename($class); - my $code = File::Slurp::slurp $filename; + my $class = $classes->{oracle_loader_test1}; + my $filename = $schema->_loader->get_dump_filename($class); + my $code = File::Slurp::slurp $filename; - like $code, qr/^=head1 NAME\n\n^$class - oracle_loader_test1 table comment\n\n^=cut\n/m, - 'table comment'; + like $code, qr/^=head1 NAME\n\n^$class - oracle_loader_test1 table comment\n\n^=cut\n/m, + 'table comment'; - like $code, qr/^=head2 value\n\n(.+:.+\n)+\noracle_loader_test1\.value column comment\n\n/m, - 'column comment and attrs'; - } + like $code, qr/^=head2 value\n\n(.+:.+\n)+\noracle_loader_test1\.value column comment\n\n/m, + 'column comment and attrs'; }, }, ); diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index 7551470..f934b98 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -1056,7 +1056,6 @@ qq| INSERT INTO ${oqt}LoaderTest41${cqt} VALUES (1, 1) |, local $conn->_loader->{preserve_case} = 1; $conn->_loader->_setup; - $self->rescan_without_warnings($conn); if (not $self->{skip_rels}) { @@ -1871,7 +1870,7 @@ sub rescan_without_warnings { sub test_column_accessor_map { my ( $column_name, $default_name, $context ) = @_; - if( $column_name eq 'crumb_crisp_coating' ) { + if( lc($column_name) eq 'crumb_crisp_coating' ) { is( $default_name, 'crumb_crisp_coating', 'column_accessor_map was passed the default name' ); ok( $context->{$_}, "column_accessor_map func was passed the $_" )