Description : Performs a switch of the backing DBConnection if the currently
connected database is not the same as the database this proxy
wants to connect to. It currently supports MySQL, Oracle and
Postges switches is untested with all bar MySQL. If it
cannot do a live DB/schema switch then it will disconnect
the connection and then wait for the next process to
connect therefore switching the DB.
Exceptions : None but will warn if you attempt to switch a DB with
active kids attached to the proxied database handle.
=cut
sub switch_database{
my($self)=@_;
my$proxy=$self->__proxy();
my$backing_dbname=$proxy->dbname();
my$dbname=$self->dbname();
my$switch=0;
if(defined$dbname){
if(defined$backing_dbname){
$switch=($dbnamene$backing_dbname)?1:0;
}
else{
$switch=1;
}
}
else{
$switch=1ifdefined$backing_dbname;
}
if($switch){
$proxy->dbname($dbname);
if($proxy->connected()){
my$kids=$proxy->db_handle()->{Kids};
my$driver=lc($proxy->driver());
#Edit to add other DB switching strategies on a per driver basis
if($drivereq'mysql'){
$proxy->do('use '.$dbname);
}
elsif($drivereq'oracle'){
$proxy->do('ALTER SESSION SET CURRENT_SCHEMA = '.$dbname);
}
elsif($drivereq'pg'){
$proxy->do('set search_path to '.$dbname);
}
else{
if($kids>0){
warning"Attempting a database switch from '$backing_dbname' to '$dbname' with $kids active handle(s). Check your logic or do not use a ProxyDBConnection";