use strict;
use warnings;
use base qw/DBIx::Class/;
+use utf8;
-use Encode;
-
-__PACKAGE__->mk_classdata( force_utf8_columns => [] );
+__PACKAGE__->mk_classdata( '_utf8_columns' );
=head1 NAME
=head1 SYNOPSIS
package Artist;
- __PACKAGE__->load_components(qw/UTF8Columns Core/);
+ use base 'DBIx::Class::Core';
+
+ __PACKAGE__->load_components(qw/UTF8Columns/);
__PACKAGE__->utf8_columns(qw/name description/);
-
+
# then belows return strings with utf8 flag
$artist->name;
$artist->get_column('description');
This module allows you to get columns data that have utf8 (Unicode) flag.
+=head2 Warning
+
+Note that this module overloads L<DBIx::Class::Row/store_column> in a way
+that may prevent other components overloading the same method from working
+correctly. This component must be the last one before L<DBIx::Class::Row>
+(which is provided by L<DBIx::Class::Core>). DBIx::Class will detect such
+incorrect component order and issue an appropriate warning, advising which
+components need to be loaded differently.
+
=head1 SEE ALSO
L<Template::Stash::ForceUTF8>, L<DBIx::Class::UUIDColumns>.
sub utf8_columns {
my $self = shift;
- for (@_) {
- $self->throw_exception("column $_ doesn't exist")
- unless $self->has_column($_);
+ if (@_) {
+ foreach my $col (@_) {
+ $self->throw_exception("column $col doesn't exist")
+ unless $self->has_column($col);
+ }
+ return $self->_utf8_columns({ map { $_ => 1 } @_ });
+ } else {
+ return $self->_utf8_columns;
}
- $self->force_utf8_columns( \@_ );
}
=head1 EXTENDED METHODS
my ( $self, $column ) = @_;
my $value = $self->next::method($column);
- if ( { map { $_ => 1 } @{ $self->force_utf8_columns } }->{$column} ) {
- Encode::_utf8_on($value) unless Encode::is_utf8($value);
- }
+ utf8::decode($value) if (
+ defined $value and $self->_is_utf8_column($column) and ! utf8::is_utf8($value)
+ );
- $value;
+ return $value;
}
=head2 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 (keys %data) {
+ utf8::decode($data{$col}) if (
+ exists $data{$col} and defined $data{$col} and $self->_is_utf8_column($col) and ! utf8::is_utf8($data{$col})
+ );
}
- %data;
+ return %data;
}
=head2 store_column
sub store_column {
my ( $self, $column, $value ) = @_;
- if ( { map { $_ => 1 } @{ $self->force_utf8_columns } }->{$column} ) {
- Encode::_utf8_off($value) if Encode::is_utf8($value);
+ # the dirtyness comparison must happen on the non-encoded value
+ my $copy;
+
+ if ( defined $value and $self->_is_utf8_column($column) and utf8::is_utf8($value) ) {
+ $copy = $value;
+ utf8::encode($value);
}
$self->next::method( $column, $value );
+
+ return $copy || $value;
}
-=head1 AUTHOR
+# override this if you want to force everything to be encoded/decoded
+sub _is_utf8_column {
+ return (shift->utf8_columns || {})->{shift};
+}
-Daisuke Murase <typester@cpan.org>
+=head1 AUTHORS
-=head1 COPYRIGHT
+See L<DBIx::Class/CONTRIBUTORS>.
-This program is free software; you can redistribute
-it and/or modify it under the same terms as Perl itself.
+=head1 LICENSE
-The full text of the license can be found in the
-LICENSE file included with this module.
+You may distribute this code under the same terms as Perl itself.
=cut
1;
-