You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Ilya Soldatkin <ar...@bob.techcen.zgrad.su> on 2000/11/21 11:47:20 UTC

moidperl, OOP & DBI

I  am creating an object that make queries to database. But how shall I
explain to him what database handler he shall use?
Under mod_perl it is too dangerous to make $dbh global variable.
Well, I can create data field in this object that will store current $dbh,
but it seems too strange too me to have such data field in object:

$person={
name=>'Jack',
age=10,
...,
$dbh=>...
}

$dbh have no relation to information about Jack.
Is there any better solution?

There is some piece of code
-------------------
mainscript.pl
....

my $dsn = "DBI:$config{'driver'}:database=$config{'dbname'};";
my $dbh = DBI->connect($dsn, $config{'user'}, $config{'pass'});
....
some SQL queries
......

$guest=new Person($dbh,$id);
print <<END
Guest name:$guest->name;
Guest age:$guest->age;
END
;
......

------------------
Person.pm

Package Person;
use strict;

sub new
 {
my ($that, $dbh, $id) = @_;
my $class =ref($that)||$that;
my $table ='SITE';

my $sth = $dbh->prepare( qq{SELECT * FROM $table where id=?});
if (!$sth) { die "Error:" . $dbh->errstr . "\n"; }
if (!$sth->execute($id)) { die "Error:" . $sth->errstr . "\n";}

 if (my $ref = $sth->fetchrow_hashref)
  {
    my $person->{'name'}=$ref->{'name'};
    my $person->{'age'}=$ref->{'age'};
    .......
  }

bless $person, $that;
return $person;

}
.... others methods



[OT] Re:moidperl, OOP & DBI

Posted by Mike Miller <mm...@crusoe.net>.
Hi Ilya,

IS> I  am creating an object that make queries to database. But how shall I
IS> explain to him what database handler he shall use?
IS> Under mod_perl it is too dangerous to make $dbh global variable.
IS> Well, I can create data field in this object that will store current $dbh,
IS> but it seems too strange too me to have such data field in object:

I'd take a look at Damian Conway's book Object Oriented Perl.  Has
some good examples and pointers on this stuff.  (ISBN 1-884777-79-1.)

FWIW, I think its okay to have database connectors (or pointers to)
within the objects.  If you look at an object as being
self-sufficient, how else can it connect to the database .... That's
just my opinion, though.

This is really not a true mod_perl issue, however.  Sorry for veering
off-topic.

Best Regards,

Mike Miller
mmiller@crusoe.net



Re: moidperl, OOP & DBI

Posted by Matt Sergeant <ma...@sergeant.org>.
On Tue, 21 Nov 2000, Ilya Soldatkin wrote:

> I  am creating an object that make queries to database. But how shall I
> explain to him what database handler he shall use?
> Under mod_perl it is too dangerous to make $dbh global variable.
> Well, I can create data field in this object that will store current $dbh,
> but it seems too strange too me to have such data field in object:
> 
> $person={
> name=>'Jack',
> age=10,
> ...,
> $dbh=>...
> }
> 
> $dbh have no relation to information about Jack.
> Is there any better solution?

This really has little to do with mod_perl, more about object design. You
should get yourself a book about OO design. Or you can use a Singleton
(see Class::Singleton on CPAN, or write your own Singleton module - its
only about 10 lines of code).

-- 
<Matt/>

    /||    ** Director and CTO **
   //||    **  AxKit.com Ltd   **  ** XML Application Serving **
  // ||    ** http://axkit.org **  ** XSLT, XPathScript, XSP  **
 // \\| // **     Personal Web Site: http://sergeant.org/     **
     \\//
     //\\
    //  \\


Re: moidperl, OOP & DBI

Posted by "Sean C. Brady" <se...@valueclick.com>.
Hi Ilya.  Even though this isn't a mod_perl issue....  you should be able
to open your database connection within your constructor.  You also could
maybe call an internal method from within your constructor that opens the
db connection and checks for the existence of 'id'.

- Sean

On Tue, 21 Nov 2000, Ilya Soldatkin wrote:

> I  am creating an object that make queries to database. But how shall I
> explain to him what database handler he shall use?
> Under mod_perl it is too dangerous to make $dbh global variable.
> Well, I can create data field in this object that will store current $dbh,
> but it seems too strange too me to have such data field in object:
> 
> $person={
> name=>'Jack',
> age=10,
> ...,
> $dbh=>...
> }
> 
> $dbh have no relation to information about Jack.
> Is there any better solution?
> 
> There is some piece of code
> -------------------
> mainscript.pl
> ....
> 
> my $dsn = "DBI:$config{'driver'}:database=$config{'dbname'};";
> my $dbh = DBI->connect($dsn, $config{'user'}, $config{'pass'});
> ....
> some SQL queries
> ......
> 
> $guest=new Person($dbh,$id);
> print <<END
> Guest name:$guest->name;
> Guest age:$guest->age;
> END
> ;
> ......
> 
> ------------------
> Person.pm
> 
> Package Person;
> use strict;
> 
> sub new
>  {
> my ($that, $dbh, $id) = @_;
> my $class =ref($that)||$that;
> my $table ='SITE';
> 
> my $sth = $dbh->prepare( qq{SELECT * FROM $table where id=?});
> if (!$sth) { die "Error:" . $dbh->errstr . "\n"; }
> if (!$sth->execute($id)) { die "Error:" . $sth->errstr . "\n";}
> 
>  if (my $ref = $sth->fetchrow_hashref)
>   {
>     my $person->{'name'}=$ref->{'name'};
>     my $person->{'age'}=$ref->{'age'};
>     .......
>   }
> 
> bless $person, $that;
> return $person;
> 
> }
> .... others methods
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: modperl-unsubscribe@apache.org
> For additional commands, e-mail: modperl-help@apache.org
>