[Dbix-class] SELECT from $schema1, INSERT into $schema2 ?

Emanuele Zeppieri ema_zep at libero.it
Mon Feb 25 11:45:55 GMT 2008


Chisel Wright wrote:

> Morning all,
> 
> I'm just about to embark on an exercise that's a sync of sorts from a Pg
> database to a mysql database (backend app to public website).
> 
> The table(s) I'm interested in do not comprise the whole database, I'm
> just pushing data (on demand) for a record and relevant join data.
> 
> The table schemas match on both sides, so no evil column mapping to
> worry about.
> 
> With this in mind, I was wondering if there was some way of doing
> something like the psudo-ish code below:
> 
>   $record = $schema_pg->resultset('MainTable')->find($id);
>   $coldata_hashref = $record->MAGIC_FUNCTION();
>   $schema_mysql->create_or_update($coldata_hashref);
> 
> I've had a quick run through the docs for ::ResultSet and
> ::Manual::Cookbook but nothing leaps out as being MAGIC_FUNCTION()

A simple find() will do what you are asking for, provided that you've 
set DBIx::Class::ResultClass::HashRefInflator as your result class:

my $rs = $schema_pg->resultset('MainTable');
$rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
my $hash_ref = $rs->find($id);
$schema_mysql->create_or_update($hash_ref);

http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/Manual/Cookbook.pod#Skip_object_creation_for_faster_results

This is more efficient than a map() on the returned row object, since it 
skips the object creation altogether.

Cheers,
Emanuele.

P.S.
There is bug that emerges in certain cases (not in the code shown above 
anyway), for which a patch is arriving ;-)



More information about the DBIx-Class mailing list