Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to wrap DBI $sth object?

Reply
Thread Tools

How to wrap DBI $sth object?

 
 
Vito Corleone
Guest
Posts: n/a
 
      03-11-2005
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
 
Reply With Quote
 
 
 
 
Fabian Pilkowski
Guest
Posts: n/a
 
      03-11-2005
* Vito Corleone wrote:
>
> 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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-11-2005
Vito Corleone <(E-Mail Removed)> wrote in
news:20050311124630.147f7c68.vito_corleone@godfath er.com:

> 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 ClassBI. Search CPAN for it.

Sinan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
To wrap or not to wrap? Aaron Fude Java 12 05-10-2008 06:33 PM
Wrap computer components in bubble wrap? Ickshka Computer Support 7 05-05-2006 05:54 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 1 09-25-2003 06:15 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 0 09-22-2003 02:36 PM



Advertisments