[Dbix-class] Making DBD::Pg to return Postgres arrays as Perl arrays
Will Crawford
billcrawford1970 at gmail.com
Thu May 7 09:29:44 GMT 2015
{ data_type => "integer[]", ... }
works for me?
On 7 May 2015 at 10:17, Adam Sjøgren <adsj at novozymes.com> wrote:
> 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
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@lists.scsys.co.uk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20150507/bb995e6e/attachment.htm>
More information about the DBIx-Class
mailing list