On 5/18/06, Jonathan Vanasco <[EMAIL PROTECTED]> wrote:
so my question is simply - is there a way for me to explicitly set
the db handle in rosedb objects midstream?
Sure, there are several ways. The usual way is to register different
data sources in your Rose::DB sublcass:
package My::DB;
use base 'Rose::DB';
__PACKAGE__->use_private_registry;
# Register read-only data source
__PACKAGE__->register_db(
type => 'reader',
driver => 'pg',
database => 'readonly_db',
host => 'somehost',
username => 'myusername',
password => 'mysecret',
);
# Register read/write data source
__PACKAGE__->register_db(
type => 'writer',
driver => 'pg',
...
);
# Etc.
Then you can explicitly set the db() attribute of your Rose::DB::Objects:
$o = My::Whatever->new(...);
$o->db(My::DB->new('reader'));
$o->load;
...
$o->db(My::DB->new('writer'));
$o->save;
Of course, that's ripe for automation in your Rose::DB::Object base class:
package My::DB::Object;
use base 'Rose::DB::Object';
...
# Override load() and force the use of the read-only data source
sub load
{
my $self = $_[0]; # Copy, not shift (see docs)
$self->db(My::DB->new('reader')) unless($self->db->type eq 'reader');
shift->SUPER::load(@_); # Call superclass
}
# Override save() and force the user of the read/write data source
sub save
{
my $self = shift; # shift okay here; load() is the only special case
$self->db(My::DB->new('writer')) unless($self->db->type eq 'writer');
shift->SUPER::save(@_); # Call superclass
}
If you would rather work at the DBI $dbh level instead of swapping
whole Rose::DB-derived data source objects, you can do that too. Any
Rose::DB object can have its $dbh "transplanted" at any time,
*provided* that the driver is the same. IOW, you can swap one MySQL
$dbh for another, but you can't swap a MySQL $dbh with a Postgres
$dbh. (This restriction does NOT apply to swapping whole Rose::DB
data source objects, however.)
Swapping DBI $dbh handles would look like this if done "manually"
$o = My::Whatever->new(...);
$dbh = get_readonly_dbh_somehow(...);
$o->db->dbh($dbh);
$o->load();
$dbh = get_readwrite_dbh_somehow(...);
$o->db->dbh($dbh);
$o->save;
The "automated" versions should be pretty obvious given the earlier examples.
Finally, there's the case of the default db object. To set that,
override init_db() in your Rose::DB::Object base class:
package My::DB::Object;
use base 'Rose::DB::Object';
...
# Use read-only data source by default
sub init_db { My::DB->new('reader') }
You can also "inject" a $dbh as before:
sub init_db
{
my $dbh = get_readonly_dbh_somehow(...);
my $db = My::DB->new('reader');
$db->dbh($dbh);
return $db;
}
I think that covers most of the "simple" permutations. Anything I missed?
-John
-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0709&bid&3057&dat1642
_______________________________________________
Rose-db-object mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object