1 package DBIx::Class::UTF8Columns;
4 use base qw/DBIx::Class/;
7 __PACKAGE__->mk_classdata( '_utf8_columns' );
11 DBIx::Class::UTF8Columns - Force UTF8 (Unicode) flag on columns
16 use base 'DBIx::Class::Core';
18 __PACKAGE__->load_components(qw/UTF8Columns/);
19 __PACKAGE__->utf8_columns(qw/name description/);
21 # then belows return strings with utf8 flag
23 $artist->get_column('description');
27 This module allows you to get columns data that have utf8 (Unicode) flag.
31 Note that this module overloads L<DBIx::Class::Row/store_column> in a way
32 that may prevent other components overloading the same method from working
33 correctly. This component must be the last one before L<DBIx::Class::Row>
34 (which is provided by L<DBIx::Class::Core>). DBIx::Class will detect such
35 incorrect component order and issue an appropriate warning, advising which
36 components need to be loaded differently.
40 L<Template::Stash::ForceUTF8>, L<DBIx::Class::UUIDColumns>.
51 foreach my $col (@_) {
52 $self->throw_exception("column $col doesn't exist")
53 unless $self->has_column($col);
55 return $self->_utf8_columns({ map { $_ => 1 } @_ });
57 return $self->_utf8_columns;
61 =head1 EXTENDED METHODS
68 my ( $self, $column ) = @_;
69 my $value = $self->next::method($column);
71 utf8::decode($value) if (
72 defined $value and $self->_is_utf8_column($column) and ! utf8::is_utf8($value)
84 my %data = $self->next::method(@_);
86 foreach my $col (keys %data) {
87 utf8::decode($data{$col}) if (
88 exists $data{$col} and defined $data{$col} and $self->_is_utf8_column($col) and ! utf8::is_utf8($data{$col})
100 my ( $self, $column, $value ) = @_;
102 # the dirtyness comparison must happen on the non-encoded value
105 if ( defined $value and $self->_is_utf8_column($column) and utf8::is_utf8($value) ) {
107 utf8::encode($value);
110 $self->next::method( $column, $value );
112 return $copy || $value;
115 # override this if you want to force everything to be encoded/decoded
116 sub _is_utf8_column {
117 return (shift->utf8_columns || {})->{shift};
122 See L<DBIx::Class/CONTRIBUTORS>.
126 You may distribute this code under the same terms as Perl itself.