Sunday, 15 January 2012

Perl class::dbi - multiple connections -


I have a class library which I have developed in the last few years which uses Pearl Class :: DBI Wrap up a relational database (DB scheme for prestashop, this is not important)

Is anyone aware of any of the Perl scripts to create several "instances" of this class, different Pointing to the database? Like now I do something like this:

  Use MyClassLib; MyClassLib- & gt; Connection ('dbi: mysql: mydatabase', 'username', 'password'); MyClassLib- & gt; Some_method ()  

It all works well.

What I am trying to do is essentially the nickname MyClassLib being able to use another "example" to point to another database. Class :: DBi stores its database connection as a static state.

Use MyClassLib in some way such a code

 ; Use MyClassLibAlias ​​as MyClassLib; MyClassLib- & gt; Connection ('dbi: mysql: mydatabase', 'username', 'password'); MyClassLibAlias- & gt; Connection ('dbi: mysql: mynewdatabase', 'username', 'password'); MyClassLib- & gt; Some_method ()  

and then code access MyClassLib and MyClassLibAlias ​​. I know that the solution using class :: dbi legacy and dBiX :: class would also be appreciated if class :: dbi Does not exist

thanks

class :: dbi You can provide your own db_Main () method instead of using Connection () . I believe it can handle a standard DBI, but class :: DBI ima :: DBI uses internally. You can use a single square for this, but to mirror your proxy code:

  package MyClassLibAlias; Use base qw (MyClassLib); Sub db_Main {my $ self = shift; My ($ DSN, $ username, $ password) = ...; Return IMA :: DBi- & gt; Connected ($ DSN, $ username, $ password); }  

You will refer to DSN, username and password using class attributes.


No comments:

Post a Comment