[Catalyst] Switching between different databases during a session

Andrew Peebles peebles at cortina-systems.com
Fri Nov 16 03:01:53 GMT 2007


Andrew Peebles wrote:
> I've been searching and experimenting and have not found a solution ...
>
> I have multiple instances of a database (each in a separate sqlite db 
> file), same schema, different data.  Each represents a snapshot of 
> something at a particular time.  During a session, user wants switch 
> among these databases.  I am trying to add a method to my Model 
> (Catalyst::Model::DBIC::Schema) that takes a new connection string, 
> and switches to that database ... all things otherwise remaining the 
> same.  Seems relatively simple, but I just ain't getting it.  Lots of 
> hints on the list, but nothing I can find that's helping.
>
> I've tried a lot of variants on something like this:
>
> package SMART::Model::SMARTDB;
>
> use strict;
> use base 'Catalyst::Model::DBIC::Schema';
>
> my $dsn = $ENV{MY_DSN} ||= 
> 'dbi:SQLite:/auto/project/tools/metadot/html/cgi/SMART/smart.db';
>
> __PACKAGE__->config(
>    schema_class => 'SMARTDB',
>    connect_info => [
>        $dsn,
>        '',
>        '',
>        {AutoCommit => 1},
>          ],
> );
>
> sub switch {
>    my $self = shift;
>    my $dsn  = shift;
>
>    my $connect_info = [ @{$self->{connect_info}} ];
>    $$connect_info[0] = $dsn;
>
>    my $schema = $self->clone();
>    $schema->connection(@$connect_info);
>    $self->schema($schema);
> }
>
> 1;
>
>
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: 
> http://www.mail-archive.com/catalyst@lists.rawmode.org/
> Dev site: http://dev.catalyst.perl.org/
Well ... in fact the code above works like a charm.  I had a problem in 
another part of my environment that was making this look like it didn't 
work, but it does.  Be nice to add this piece of code to one of the 
Cookbooks somewhere, maybe.
a




More information about the Catalyst mailing list