How to wrap DBI $sth object?

V

Vito Corleone

Hi,

I want to make a module that execute the query and return the $sth in
wrapped form.

If you don't use the wrapper it would be like this:
## no_wrapper.pl
my $sth = $dbh->prepare("SELECT member_id, item_name FROM item");
$sth->execute();
while (my $row = $sth->fetchrow_arrayref()) {
print $row->[0];
pirnt $row->[1];
}

I want to wrap that, so I can write it like below:
## with_wrapper.pl
use Item;
my $it = Item::->new();
my $obj = $it->get_items(); ## This return $sth in wrapped form
while (my $item = $obj->each()) {
print $item->member_id();
print $item->item_name();
## etc
}

How should I write Item.pm, so it can be used as the wrapper above?
Could you please give some simple example, as I don't know where to
start. And thank you in advance.


Regards,
Vito
 
F

Fabian Pilkowski

* Vito Corleone said:
I want to make a module that execute the query and return the $sth in
wrapped form.

If you don't use the wrapper it would be like this:
## no_wrapper.pl
my $sth = $dbh->prepare("SELECT member_id, item_name FROM item");
$sth->execute();
while (my $row = $sth->fetchrow_arrayref()) {
print $row->[0];
pirnt $row->[1];
}

I want to wrap that, so I can write it like below:
## with_wrapper.pl
use Item;
my $it = Item::->new();
my $obj = $it->get_items(); ## This return $sth in wrapped form
while (my $item = $obj->each()) {
print $item->member_id();
print $item->item_name();
## etc
}

Eh, what should be your "item": the object which Item->new() returned or
each element *within* this object. Certainly you can find better names
for your variables -- or bundle all these functions in only one wrapper
object (in your example there are three: $it, $obj and $item).
How should I write Item.pm, so it can be used as the wrapper above?
Could you please give some simple example, as I don't know where to
start. And thank you in advance.

The following code was simply hacked into my newsreader (at the moment I
have neither a database nor DBI installed). Therefore it is untested.


#!/usr/bin/perl -w
use strict;

# -- Item.pm --

package Item;

sub new {
my $class = shift;
my $dbh = shift;
bless { dbh => $dbh }, $class;
}

sub get_items {
my $self = shift;
my $sql = 'SELECT member_id, item_name FROM item';
$self->{sth} = $self->{dbh}->prepare( $sql );
$self->{sth}->execute;
1;
}

sub each {
my $self = shift;
my $row = $self->{sth}->fetchrow_arrayref;
if ( $row ) {
$self->{ member_id } = $row->[0];
$self->{ item_name } = $row->[1];
return 1;
}
else {
$self->{ member_id } = undef;
$self->{ item_name } = undef;
return undef;
}
}

sub member_id {
my $self = shift;
return $self->{ member_id };
}

sub item_name {
my $self = shift;
return $self->{ item_name };
}

1;
__END__


With that, you can fetch your data without cryptic calls now. However,
this should be a little different from these in your predefined example.


#!/usr/bin/perl -w
use strict;
use DBI;
use Item;

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

my $it = Item->new( $dbh );
$it->get_items();
while ( $it->each() ) {
print $it->member_id();
print $it->item_name();
# ...
}
__END__


regards,
fabian
 
A

A. Sinan Unur

I want to wrap that, so I can write it like below:
## with_wrapper.pl
use Item;
my $it = Item::->new();
my $obj = $it->get_items(); ## This return $sth in wrapped form
while (my $item = $obj->each()) {
print $item->member_id();
print $item->item_name();
## etc
}

How should I write Item.pm, so it can be used as the wrapper above?

I think you are looking for Class:DBI. Search CPAN for it.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top