DBIx::Class::EncodeColumns - Handle column encodings


DBIx-Class-EncodeColumns documentation Contained in the DBIx-Class-EncodeColumns distribution.

Index


Code Index:

NAME

Top

DBIx::Class::EncodeColumns - Handle column encodings

SYNOPSIS

Top

    package Artist;
    __PACKAGE__->load_components(qw/EncodeColumns Core/);
    __PACKAGE__->decode_columns('latin-1');
    __PACKAGE__->encode_columns('utf-8');




DESCRIPTION

Top

This module allows you to handle column encodings

METHODS

Top

decode_columns($encoding)

Use this function to set the default encoding of all your columns. The data of all columns will be decoded to internal encoding of perl.

encode_columns($encoding)

Before returning the data form a column, it will be encoded using this encoding type.

EXTENDED METHODS

Top

get_column

get_columns

store_column

AUTHOR

Top

Sascha Kiefer <esskar@cpan.org>

COPYRIGHT

Top


DBIx-Class-EncodeColumns documentation Contained in the DBIx-Class-EncodeColumns distribution.
package DBIx::Class::EncodeColumns;

use strict;
use warnings;
use vars qw/$VERSION/;
$VERSION = '0.02';

use base qw/DBIx::Class/;

use Encode;

__PACKAGE__->mk_classdata( decode_columns => '' );
__PACKAGE__->mk_classdata( encode_columns => '' );

sub get_column {
    my ( $self, $column ) = @_;
    my $value = $self->next::method($column);

    if( defined $value ) {

        $value = decode( $self->decode_columns, $value )
          if $self->decode_columns;

        $value = encode( $self->encode_columns, $value )
          if $self->encode_columns;
    }

    return $value;
}

sub get_columns {
    my $self = shift;
    my %data = $self->next::method(@_);

    foreach my $col (keys %data) {

        if(defined(my $value = $data{$col}) ) {

            $value = decode( $self->decode_columns, $value )
              if $self->decode_columns;

            $value = encode( $self->encode_columns, $value )
              if $self->encode_columns;

            $data{$col} = $value;
        }
    }

    return %data;
}

sub store_column {
    my ( $self, $column, $value ) = @_;

    $value = decode( $self->encode_columns, $value )
      if $self->encode_columns;

    $value = encode( $self->decode_columns, $value )
      if $self->decode_columns;

    return $self->next::method( $column, $value );
}

1;