Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Dynamic DSN w/ Class::DBI Example

by jZed (Prior)
on Apr 24, 2004 at 02:39 UTC ( #347810=note: print w/replies, xml ) Need Help??

in reply to Dynamic DSN w/ Class::DBI Example

I know this isn't exactly what you're asking, but here's an example of using two different data sources on a single schema in the same script. The example uses SQLite and AnyData but any DBI source should work.
#!perl -w use strict; sub run { for my $source( qw(sqlite anydata)){ Schema->db_connect( $source ); Schema->db_drop_table; Schema->db_make_table; Schema->create({c1=>9,c2=>'Hi From'}); printf "%s %s!\n", Schema->retrieve(9)->c2 , Schema->db_Main->{Driver}->{Name}; } } package My::DBI; use base 'Class::DBI'; my $source_name; Ima::DBI->set_db ( 'anydata','DBI:AnyData(AutoCommit=1):','','' ); Ima::DBI->set_db ( 'sqlite' ,'DBI:SQLite:dbname=cdbi' ,'','' ); sub db_Main { my $db = 'db_'.$source_name; __PACKAGE__->$db } sub db_connect { $source_name = lc $_[1] } sub db_drop_table{ eval{ $_[0]->db_Main->do('DROP TABLE '.$_[0]->table +)} } sub db_make_table{ $_[0]->db_drop_table; $_[0]->sql_create->execute } package Schema; use base 'My::DBI'; __PACKAGE__->table ( 't1' ); __PACKAGE__->columns( All=>qw(c1 c2) ); __PACKAGE__->columns( Primary => qw(c1) ); __PACKAGE__->set_sql( 'create' => "CREATE TABLE t1 (c1 INT, c2 CHAR(9) +)"); package main; run(); __END__

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://347810]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2021-02-27 04:33 GMT
Find Nodes?
    Voting Booth?

    No recent polls found