X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FCDBICompat%2FColumnCase.pm;h=0f847dbfc00855a91cf57ce2c3dfc8e5cd477c89;hb=4c9f72c1b6c90bdba16fbaa481c5eb77f73a43e6;hp=05ab20f1f3fa538d835218239fa20b090f4d4963;hpb=be7fff92e36a407ac727ce8044af3d323a28037c;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/CDBICompat/ColumnCase.pm b/lib/DBIx/Class/CDBICompat/ColumnCase.pm index 05ab20f..0f847db 100644 --- a/lib/DBIx/Class/CDBICompat/ColumnCase.pm +++ b/lib/DBIx/Class/CDBICompat/ColumnCase.pm @@ -1,97 +1,155 @@ -package DBIx::Class::CDBICompat::ColumnCase; +package # hide from PAUSE + DBIx::Class::CDBICompat::ColumnCase; use strict; use warnings; -use NEXT; + +use base qw/DBIx::Class/; sub _register_column_group { my ($class, $group, @cols) = @_; - return $class->NEXT::ACTUAL::_register_column_group($group => map lc, @cols); + return $class->next::method($group => map lc, @cols); } -sub _register_columns { +sub add_columns { my ($class, @cols) = @_; - return $class->NEXT::ACTUAL::_register_columns(map lc, @cols); + $class->mk_group_accessors(column => @cols); + $class->result_source_instance->add_columns(map lc, @cols); } sub has_a { my ($class, $col, @rest) = @_; - $class->NEXT::ACTUAL::has_a(lc($col), @rest); - $class->mk_group_accessors('has_a' => $col); + $class->next::method(lc($col), @rest); + $class->mk_group_accessors('inflated_column' => $col); return 1; } sub has_many { my ($class, $rel, $f_class, $f_key, @rest) = @_; - return $class->NEXT::ACTUAL::has_many($rel, $f_class, ( ref($f_key) ? - $f_key : + return $class->next::method($rel, $f_class, ( ref($f_key) ? + $f_key : lc($f_key) ), @rest); } -sub get_has_a { +sub get_inflated_column { my ($class, $get, @rest) = @_; - return $class->NEXT::ACTUAL::get_has_a(lc($get), @rest); + return $class->next::method(lc($get), @rest); } -sub store_has_a { +sub store_inflated_column { my ($class, $set, @rest) = @_; - return $class->NEXT::ACTUAL::store_has_a(lc($set), @rest); + return $class->next::method(lc($set), @rest); } -sub set_has_a { +sub set_inflated_column { my ($class, $set, @rest) = @_; - return $class->NEXT::ACTUAL::set_has_a(lc($set), @rest); + return $class->next::method(lc($set), @rest); } sub get_column { my ($class, $get, @rest) = @_; - return $class->NEXT::ACTUAL::get_column(lc($get), @rest); + return $class->next::method(lc($get), @rest); } sub set_column { my ($class, $set, @rest) = @_; - return $class->NEXT::ACTUAL::set_column(lc($set), @rest); + return $class->next::method(lc($set), @rest); } sub store_column { my ($class, $set, @rest) = @_; - return $class->NEXT::ACTUAL::store_column(lc($set), @rest); + return $class->next::method(lc($set), @rest); } sub find_column { my ($class, $col) = @_; - return $class->NEXT::ACTUAL::find_column(lc($col)); + return $class->next::method(lc($col)); } -sub _mk_group_accessors { - my ($class, $type, $group, @fields) = @_; - #warn join(', ', map { ref $_ ? (@$_) : ($_) } @fields); - my @extra; - foreach (@fields) { - my ($acc, $field) = ref $_ ? @$_ : ($_, $_); - #warn "$acc ".lc($acc)." $field"; - next if defined &{"${class}::${acc}"}; - push(@extra, [ lc $acc => $field ]); - } - return $class->NEXT::ACTUAL::_mk_group_accessors($type, $group, - @fields, @extra); +# _build_query +# +# Build a query hash for find, et al. Overrides Retrieve::_build_query. + +sub _build_query { + my ($self, $query) = @_; + + my %new_query; + $new_query{lc $_} = $query->{$_} for keys %$query; + + return \%new_query; } -sub _cond_key { - my ($class, $attrs, $key, @rest) = @_; - return $class->NEXT::ACTUAL::_cond_key($attrs, lc($key), @rest); + +# CDBI will never overwrite an accessor, but it only uses one +# accessor for all column types. DBIC uses many different +# accessor types so, for example, if you declare a column() +# and then a has_a() for that same column it must overwrite. +# +# To make this work CDBICompat has decide if an accessor +# method was put there by itself and only then overwrite. +{ + my %our_accessors; + + sub _has_custom_accessor { + my($class, $name) = @_; + + no strict 'refs'; + my $existing_accessor = *{$class .'::'. $name}{CODE}; + return $existing_accessor && !$our_accessors{$existing_accessor}; + } + + sub _deploy_accessor { + my($class, $name, $accessor) = @_; + + return if $class->_has_custom_accessor($name); + + for my $name ($name, lc $name) { + no strict 'refs'; + no warnings 'redefine'; + *{$class .'::'. $name} = $accessor; + } + + $our_accessors{$accessor}++; + + return 1; + } } -sub _cond_value { - my ($class, $attrs, $key, @rest) = @_; - return $class->NEXT::ACTUAL::_cond_value($attrs, lc($key), @rest); +sub _mk_group_accessors { + my ($class, $type, $group, @fields) = @_; + + # So we don't have to do lots of lookups inside the loop. + my $maker = $class->can($type) unless ref $type; + + # warn "$class $type $group\n"; + foreach my $field (@fields) { + if( $field eq 'DESTROY' ) { + carp("Having a data accessor named DESTROY in ". + "'$class' is unwise."); + } + + my $name = $field; + + ($name, $field) = @$field if ref $field; + + my $accessor = $class->$maker($group, $field); + my $alias = "_${name}_accessor"; + + # warn " $field $alias\n"; + { + no strict 'refs'; + + $class->_deploy_accessor($name, $accessor); + $class->_deploy_accessor($alias, $accessor); + } + } } sub new { my ($class, $attrs, @rest) = @_; my %att; $att{lc $_} = $attrs->{$_} for keys %$attrs; - return $class->NEXT::ACTUAL::new(\%att, @rest); + return $class->next::method(\%att, @rest); } 1;