1 package DBIx::Class::UTF8Columns;
4 use base qw/DBIx::Class/;
6 __PACKAGE__->mk_classdata( '_utf8_columns' );
10 DBIx::Class::UTF8Columns - Force UTF8 (Unicode) flag on columns
15 use base 'DBIx::Class::Core';
17 __PACKAGE__->load_components(qw/UTF8Columns/);
18 __PACKAGE__->utf8_columns(qw/name description/);
20 # then belows return strings with utf8 flag
22 $artist->get_column('description');
26 This module allows you to get columns data that have utf8 (Unicode) flag.
30 Note that this module overloads L<DBIx::Class::Row/store_column> in a way
31 that may prevent other components overloading the same method from working
32 correctly. This component must be the last one before L<DBIx::Class::Row>
33 (which is provided by L<DBIx::Class::Core>). DBIx::Class will detect such
34 incorrect component order and issue an appropriate warning, advising which
35 components need to be loaded differently.
39 L<Template::Stash::ForceUTF8>, L<DBIx::Class::UUIDColumns>.
50 foreach my $col (@_) {
51 $self->throw_exception("column $col doesn't exist")
52 unless $self->has_column($col);
54 return $self->_utf8_columns({ map { $_ => 1 } @_ });
56 return $self->_utf8_columns;
60 =head1 EXTENDED METHODS
67 my ( $self, $column ) = @_;
68 my $value = $self->next::method($column);
70 utf8::decode($value) if (
71 defined $value and $self->_is_utf8_column($column) and ! utf8::is_utf8($value)
83 my %data = $self->next::method(@_);
85 foreach my $col (keys %data) {
86 utf8::decode($data{$col}) if (
87 exists $data{$col} and defined $data{$col} and $self->_is_utf8_column($col) and ! utf8::is_utf8($data{$col})
99 my ( $self, $column, $value ) = @_;
101 # the dirtyness comparison must happen on the non-encoded value
104 if ( defined $value and $self->_is_utf8_column($column) and utf8::is_utf8($value) ) {
106 utf8::encode($value);
109 $self->next::method( $column, $value );
111 return $copy || $value;
114 # override this if you want to force everything to be encoded/decoded
115 sub _is_utf8_column {
116 # my ($self, $col) = @_;
117 return ($_[0]->utf8_columns || {})->{$_[1]};
122 See L<DBIx::Class/CONTRIBUTORS>.
126 You may distribute this code under the same terms as Perl itself.