Re: DBI object in modules

From: James Willmore (jwillmore_at_remove.adelphia.net)
Date: 02/26/04


Date: Thu, 26 Feb 2004 09:50:24 -0500

On Tue, 24 Feb 2004 17:34:58 +0200, Roman Khutkyy wrote:

> Can anybody help? I built my script using module structure as i read: "...
> and use the subroutines from modules as you are using its directly in main
> program." So suppose i have the script main.pl and with 'use' i linked some
> modules "Module1.pm", "Module1.pm","ModuleN.pm". The subroutines in each of
> these modules use the DBI module to connect to database, make transaction,
> query etc. Is there a method to create DBI object once in main.pl and use it
> in any subroutine from any module. Now i have to create database connection
> in each module, and there is no problem to do it because there is just 6
> modules, and i can't see now how this technique slows server down, but when
> the number of modules will be much more, or inet traffic will be like on
> Microsoft servers :) what to do then?

The connection to the database - is it the same connection for all the
packages you refer to or different databases?

If it's the same database connection, but you're performing different
queries, you can just define different scalar values for each query.

For example:

my $dbh = DBI->connect(...);

my $sth_select_dogs = $dbh->prepare(<<SQL);
select * from db where animal='dogs'
SQL

my $sth_select_cats = $dbh->prepare(<<SQL);
select * from db where animal='cats'
SQL

Now you can execute and fetch based upon the scalar for each query. And
you connected *only once* to the database :-)

If you *have* to use packages, then you could pass the database handle as
a reference to your packages. I say reference because if you just pass it
as a scalar, you're basically duplicating the scalar holding the DBI
object ($dbh) and taking up space :-) It's also untested, so your milage
may vary :-)

For example:

--main--
my $dbh = DBI->connect( ... );

use PackageA;

my $pkg_a = PackageA::do_query(\$dbh);

--PackageA--
sub do_query{
    my $dbh = shift;
    my $sth = $dbh->prepare(<<SQL);
select * from db where animal='dogs';
SQL
    ... do stuff ...
}

In the above example, it's not, IMHO, practical to even use a package if
it's the same database connection. It's practical if you use different
databases in the same main script. You may want to file that away
somewhere for future reference :-)

Just my $0.02 ....

HTH

-- 
Jim
Copyright notice: all code written by the author in this post is
 released under the GPL. http://www.gnu.org/licenses/gpl.txt 
for more information.
a fortune quote ...
If a President doesn't do it to his wife, he'll do it to his
country. 


Relevant Pages

  • Re: table structure questions
    ... I can not afford any of the more robust packages right now ... How am I going to go about entering multiple schedules where each step must ... and an interface built by people who know your business. ... Often people hear that database software is a good tool, ...
    (microsoft.public.access.tablesdbdesign)
  • Re: Help ! -> Distributed BL and Database Access
    ... I have one server located in a remote part of the country ... method the data within the object will be comitted to the database. ... reference to the database connection and then send it to the client (by ... value - to move the work off onto the client). ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Is FreeBSD just a sandbox for hackers?
    ... No one is forcing any policy on newcomers. ... The ports collection is not part of the FreeBSD ... Pre-built packages are another way ... the whole collection is best thought of as a single "database of ...
    (comp.unix.bsd.freebsd.misc)
  • Re: New FreeBSD package system (a.k.a. Daemon Package System (dps))
    ... this be replaced by a single SQLite database. ... so they should not have to use sql for managing packages. ... XML structure. ... Stanislav Sedov ...
    (freebsd-hackers)
  • Re: choices regarding where to place code - in the database or middletier
    ... > written for any other database product: ... > of how you could use packages and sequences in Oracle and meet your ... Solutions that are completely dependent on the database's feature set: ... Build it with mod_plsql and Oracle HTTP Server ...
    (comp.lang.java.databases)