« CentOSでDBIC最新版を使うときの注意 | Home | 2007 クラブワールドカップ レッズ - ミラン »

[Perl]   DBIC::InflateColumnで勘違い

DBIC::Inflateを作ってこんなコードを作ってたのに動かなかった。

__PACKAGE__->inflate_column(
    name => {
        inflate => sub {
            my ( $value, $obj ) = @_;
            warn "inflate name\n";
            return "inflate: $value";
        },
        deflate => sub {
            my ( $value, $obj ) = @_;
            warn "deflate name\n";
            return "deflate: $value";
        },
    }
);

inflateは動くけどdeflateは動かない。

で、よくよく調べてみました。
というかDBIx::Class::InflateColumnのpod読みました。

It will handle all types of references except scalar references. It will not handle scalar values, these are ignored and thus passed through to SQL::Abstract. This is to allow setting raw values to "just work". Scalar references are passed through to the database to deal with, to allow such settings as \'year + 1' and \'DEFAULT' to work.

つまりscalarは無視するよと。
さらに

If you want to filter plain scalar values and replace them with something else, contribute a filtering component.

もしscalar valueをフィルタリングしたり置換したいならフィルタリングコンポーネントとして作りなさいよと。

恥ずかしながら知りませんでした。

実際ソースを見てみると

sub _deflated_column {
  my ($self, $col, $value) = @_;
#  return $value unless ref $value && blessed($value); # If it's not an object, don't touch it
  ## Leave scalar refs (ala SQL::Abstract literal SQL), untouched, deflate all other refs
  return $value unless (ref $value && ref($value) ne 'SCALAR');

... snip ...

てな感じで、いきなりreturnされてます。
_inflated_columnではこのような処理をされていないので、ちょっと混乱しました。

これでDBIx::Class::UTF8ColumnsやDBIx::Class::DigestColumnsがInflateColumnsを使っていない理由がわかりました。

Trackbacks:

このエントリーのトラックバックURL:

コメントを投稿