[Dbix-class] Making DBD::Pg to return Postgres arrays as Perl arrays

Adam Sjøgren adsj at novozymes.com
Thu May 7 09:17:15 GMT 2015


  Hi.

I have a Postgres database with some fields that are defined as arrays
in the database.

When I insert new rows where the field has a Perl-array as the value,
DBIx::Class+DBD::Pg automatically stores them in the database, as hoped
- nice!

But when I read the field again, I get the textual "Postgres-encoded"
representation back (i.e. a string like '{"(a,b,c)","(d,e,f)"}').

It looks to me like DBD::Pg can expand these to Perl arrays, given the
correct type of the field.

My question is: How do I specify this with DBIx::Class?

I have tried:

    __PACKAGE__->add_columns($field_name=>{ data_type=>'array' });
    __PACKAGE__->inflate_column($field_name=>{ inflate=>sub { ... },. deflate=>sub {...} );

in various combinations (pg_type, data_type)=>('array', 'pg_anyarray',
DBD::Pg::PG_ANYARRAY), but I seem to get the textual representation in
the inflator sub regardless.

What I would really like, is to get the array back as a Perl array.

I can do the parsing myself, but I think DBD::Pg would be better and
faster at it.

(I need the inflate_column() call, because after getting the array in, I
need to do some rearranging.)

I have browsed in the documentation of DBD::Pg and DBIx::Class, but my
search-fu is failing me.

(E.g. the cookbook mentions how to store arrays:

 * https://metacpan.org/pod/DBIx::Class::Manual::Cookbook#Working-with-PostgreSQL-array-types

but not how to read them.)

Any pointers?

  Thanks,

   Adam

-- 
                                                          Adam Sjøgren
                                                    adsj at novozymes.com



More information about the DBIx-Class mailing list