From: David Kamholz Date: Fri, 7 Jul 2006 02:05:03 +0000 (+0000) Subject: utf8columns fix X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=404939a4ab4a2bece2fd2a1f44abac7e435e464e;p=dbsrgits%2FDBIx-Class-Historic.git utf8columns fix --- diff --git a/Changes b/Changes index 9f5bf4b..c1644c8 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for DBIx::Class + - cleanup UTF8Columns and make more efficient - rename DBIX_CLASS_STORAGE_DBI_DEBUG to DBIC_TRACE - rename _parent_rs to _parent_source in ResultSet diff --git a/lib/DBIx/Class/UTF8Columns.pm b/lib/DBIx/Class/UTF8Columns.pm index 71c1013..4bdcd8d 100644 --- a/lib/DBIx/Class/UTF8Columns.pm +++ b/lib/DBIx/Class/UTF8Columns.pm @@ -5,7 +5,7 @@ use base qw/DBIx::Class/; use Encode; -__PACKAGE__->mk_classdata( force_utf8_columns => [] ); +__PACKAGE__->mk_classdata( 'force_utf8_columns' ); =head1 NAME @@ -37,11 +37,11 @@ L, L. sub utf8_columns { my $self = shift; - for (@_) { - $self->throw_exception("column $_ doesn't exist") - unless $self->has_column($_); + foreach my $col (@_) { + $self->throw_exception("column $col doesn't exist") + unless $self->has_column($col); } - $self->force_utf8_columns( \@_ ); + $self->force_utf8_columns({ map { $_ => 1 } @_ }); } =head1 EXTENDED METHODS @@ -54,7 +54,7 @@ sub get_column { my ( $self, $column ) = @_; my $value = $self->next::method($column); - if ( { map { $_ => 1 } @{ $self->force_utf8_columns } }->{$column} ) { + if ( defined $value and $self->force_utf8_columns->{$column} ) { Encode::_utf8_on($value) unless Encode::is_utf8($value); } @@ -69,8 +69,8 @@ sub get_columns { my $self = shift; my %data = $self->next::method(@_); - for (@{ $self->force_utf8_columns }) { - Encode::_utf8_on($data{$_}) if $data{$_} and !Encode::is_utf8($_); + foreach my $col (grep { defined $data{$_} } keys %{ $self->force_utf8_columns }) { + Encode::_utf8_on($data{$col}) unless Encode::is_utf8($data{$col}); } %data; @@ -83,7 +83,7 @@ sub get_columns { sub store_column { my ( $self, $column, $value ) = @_; - if ( { map { $_ => 1 } @{ $self->force_utf8_columns } }->{$column} ) { + if ( defined $value and $self->force_utf8_columns->{$column} ) { Encode::_utf8_off($value) if Encode::is_utf8($value); } diff --git a/t/85utf8.t b/t/85utf8.t index 562efb4..9a621db 100644 --- a/t/85utf8.t +++ b/t/85utf8.t @@ -10,18 +10,19 @@ my $schema = DBICTest->init_schema(); eval 'use Encode ; 1' or plan skip_all => 'Install Encode run this test'; -plan tests => 2; +plan tests => 3; -DBICTest::Schema::Artist->load_components('UTF8Columns'); -DBICTest::Schema::Artist->utf8_columns('name'); +DBICTest::Schema::CD->load_components('UTF8Columns'); +DBICTest::Schema::CD->utf8_columns('title'); Class::C3->reinitialize(); -my $artist = $schema->resultset("Artist")->create( { name => 'uni' } ); -ok( Encode::is_utf8( $artist->name ), 'got name with utf8 flag' ); +my $cd = $schema->resultset('CD')->create( { artist => 1, title => 'uni', year => 'foo' } ); +ok( Encode::is_utf8( $cd->title ), 'got title with utf8 flag' ); +ok( !Encode::is_utf8( $cd->year ), 'got year without utf8 flag' ); my $utf8_char = 'uniuni'; Encode::_utf8_on($utf8_char); -$artist->name($utf8_char); -ok( !Encode::is_utf8( $artist->{_column_data}->{name} ), - 'store utf8 less chars' ); +$cd->title($utf8_char); +ok( !Encode::is_utf8( $cd->{_column_data}{title} ), + 'store utf8-less chars' );