[Catalyst] Switching between different databases during a session

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


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;




More information about the Catalyst mailing list