Eric Veldhuyzen wrote:
> I am using SPOPS with PostgreSQL and so far it works great. I created a
> base class to have a global_datasource_handle and a lexical variable
> $DBH to contain the database handle, and all the real SPOPS classes use
> this class as a base class, just like in one of the examples.
>
> But now I have the challange to copy some of the data from one database
> to another, and I would like to do that with SPOPS, but I don't want to
> create a separate database connection for each table (that wouldn't work
> anyway). So, does anyone here have any suggestions to solve this
> challange in a nice way?
You should just need one connection for each database. The easiest
thing to do would probably be create two separate base classes with
'global_datasource_handle' defined, each pointing to a different
database. Then add the appropriate class as a parent in the 'isa'
configuration list of the SPOPS class. For instance:
package My::DBOne;
use strict;
use DBI;
my ( $DBH );
sub global_datasource_handle {
return $DBH if ( $DBH );
$DBH = DBI->connect( ... );
return $DBH;
}
package My::DBTwo;
use strict;
use DBI;
my ( $DBH );
sub global_datasource_handle {
return $DBH if ( $DBH );
$DBH = DBI->connect( ... );
return $DBH;
}
Then:
my $config = {
one => {
class => 'originalOne',
isa => [ 'My::DBOne', 'SPOPS::DBI::Pg', 'SPOPS::DBI' ],
...
},
two => {
class => 'copyTwo',
isa => [ 'My::DBTwo', 'SPOPS::DBI::Pg', 'SPOPS::DBI' ],
...
},
};
SPOPS::Initialize->process({ config => $config });
foreach my $orig ( @{ $one->fetch_group } ) {
copyTwo->new( $orig )->save();
}
Hope this makes sense...
Chris
--
Chris Winters (ch...@cw...)
Building enterprise-capable snack solutions since 1988.
|